Add UMA histograms and logging for bad IPC message handling
[chromium-blink-merge.git] / extensions / renderer / script_injection_manager.h
blob29ab11ee38f2de2470fe5e67e60fc6d6e59961bc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/callback.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/scoped_observer.h"
16 #include "extensions/common/user_script.h"
17 #include "extensions/renderer/script_injection.h"
18 #include "extensions/renderer/user_script_set_manager.h"
20 struct ExtensionMsg_ExecuteCode_Params;
22 namespace blink {
23 class WebFrame;
24 class WebLocalFrame;
27 namespace content {
28 class RenderView;
31 namespace extensions {
32 class Extension;
33 class ExtensionSet;
35 // The ScriptInjectionManager manages extensions injecting scripts into frames
36 // via both content/user scripts and tabs.executeScript(). It is responsible for
37 // maintaining any pending injections awaiting permission or the appropriate
38 // load point, and injecting them when ready.
39 class ScriptInjectionManager : public UserScriptSetManager::Observer {
40 public:
41 ScriptInjectionManager(const ExtensionSet* extensions,
42 UserScriptSetManager* user_script_set_manager);
43 virtual ~ScriptInjectionManager();
45 // Notifies that a new render view has been created.
46 void OnRenderViewCreated(content::RenderView* render_view);
48 // Removes pending injections of the unloaded extension.
49 void OnExtensionUnloaded(const std::string& extension_id);
51 // Notifies that an injection has been finished.
52 void OnInjectionFinished(ScriptInjection* injection);
54 private:
55 // A RenderViewObserver implementation which watches the various render views
56 // in order to notify the ScriptInjectionManager of different document load
57 // states.
58 class RVOHelper;
60 typedef std::map<blink::WebFrame*, UserScript::RunLocation> FrameStatusMap;
62 // UserScriptSetManager::Observer implementation.
63 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
64 const std::vector<UserScript*>& scripts) override;
66 // Notifies that an RVOHelper should be removed.
67 void RemoveObserver(RVOHelper* helper);
69 // Invalidate any pending tasks associated with |frame|.
70 void InvalidateForFrame(blink::WebFrame* frame);
72 // Starts the process to inject appropriate scripts into |frame|.
73 void StartInjectScripts(blink::WebFrame* frame,
74 UserScript::RunLocation run_location);
76 // Actually injects the scripts into |frame|.
77 void InjectScripts(blink::WebFrame* frame,
78 UserScript::RunLocation run_location);
80 // Try to inject and store injection if it has not finished.
81 void TryToInject(scoped_ptr<ScriptInjection> injection,
82 UserScript::RunLocation run_location,
83 ScriptsRunInfo* scripts_run_info);
85 // Handle the ExecuteCode extension message.
86 void HandleExecuteCode(const ExtensionMsg_ExecuteCode_Params& params,
87 content::RenderView* render_view);
89 // Handle the ExecuteDeclarativeScript extension message.
90 void HandleExecuteDeclarativeScript(blink::WebFrame* web_frame,
91 int tab_id,
92 const ExtensionId& extension_id,
93 int script_id,
94 const GURL& url);
96 // Handle the GrantInjectionPermission extension message.
97 void HandlePermitScriptInjection(int64 request_id);
99 // Extensions metadata, owned by Dispatcher (which owns this object).
100 const ExtensionSet* extensions_;
102 // The map of active web frames to their corresponding statuses. The
103 // RunLocation of the frame corresponds to the last location that has ran.
104 FrameStatusMap frame_statuses_;
106 // The collection of RVOHelpers.
107 ScopedVector<RVOHelper> rvo_helpers_;
109 // The set of UserScripts associated with extensions. Owned by the Dispatcher.
110 UserScriptSetManager* user_script_set_manager_;
112 // Pending injections which are waiting for either the proper run location or
113 // user consent.
114 ScopedVector<ScriptInjection> pending_injections_;
116 // Running injections which are waiting for async callbacks from blink.
117 ScopedVector<ScriptInjection> running_injections_;
119 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
120 user_script_set_manager_observer_;
122 DISALLOW_COPY_AND_ASSIGN(ScriptInjectionManager);
125 } // namespace extensions
127 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_