Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / renderer / script_injection_manager.h
blob5eda418bd1a5057b525672a169b89174cd3502e3
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 content {
23 class RenderFrame;
26 namespace extensions {
27 class Extension;
29 // The ScriptInjectionManager manages extensions injecting scripts into frames
30 // via both content/user scripts and tabs.executeScript(). It is responsible for
31 // maintaining any pending injections awaiting permission or the appropriate
32 // load point, and injecting them when ready.
33 class ScriptInjectionManager : public UserScriptSetManager::Observer {
34 public:
35 explicit ScriptInjectionManager(
36 UserScriptSetManager* user_script_set_manager);
37 virtual ~ScriptInjectionManager();
39 // Notifies that a new render view has been created.
40 void OnRenderFrameCreated(content::RenderFrame* render_frame);
42 // Removes pending injections of the unloaded extension.
43 void OnExtensionUnloaded(const std::string& extension_id);
45 private:
46 // A RenderFrameObserver implementation which watches the various render
47 // frames in order to notify the ScriptInjectionManager of different
48 // document load states and IPCs.
49 class RFOHelper;
51 using FrameStatusMap =
52 std::map<content::RenderFrame*, UserScript::RunLocation>;
54 // Notifies that an injection has been finished.
55 void OnInjectionFinished(ScriptInjection* injection);
57 // UserScriptSetManager::Observer implementation.
58 void OnUserScriptsUpdated(const std::set<HostID>& changed_hosts,
59 const std::vector<UserScript*>& scripts) override;
61 // Notifies that an RFOHelper should be removed.
62 void RemoveObserver(RFOHelper* helper);
64 // Invalidate any pending tasks associated with |frame|.
65 void InvalidateForFrame(content::RenderFrame* frame);
67 // Starts the process to inject appropriate scripts into |frame|.
68 void StartInjectScripts(content::RenderFrame* frame,
69 UserScript::RunLocation run_location);
71 // Actually injects the scripts into |frame|.
72 void InjectScripts(content::RenderFrame* frame,
73 UserScript::RunLocation run_location);
75 // Try to inject and store injection if it has not finished.
76 void TryToInject(scoped_ptr<ScriptInjection> injection,
77 UserScript::RunLocation run_location,
78 ScriptsRunInfo* scripts_run_info);
80 // Handle the ExecuteCode extension message.
81 void HandleExecuteCode(const ExtensionMsg_ExecuteCode_Params& params,
82 content::RenderFrame* render_frame);
84 // Handle the ExecuteDeclarativeScript extension message.
85 void HandleExecuteDeclarativeScript(content::RenderFrame* web_frame,
86 int tab_id,
87 const ExtensionId& extension_id,
88 int script_id,
89 const GURL& url);
91 // Handle the GrantInjectionPermission extension message.
92 void HandlePermitScriptInjection(int64 request_id);
94 // The map of active web frames to their corresponding statuses. The
95 // RunLocation of the frame corresponds to the last location that has ran.
96 FrameStatusMap frame_statuses_;
98 // The frames currently being injected into, so long as that frame is valid.
99 std::set<content::RenderFrame*> active_injection_frames_;
101 // The collection of RFOHelpers.
102 ScopedVector<RFOHelper> rfo_helpers_;
104 // The set of UserScripts associated with extensions. Owned by the Dispatcher.
105 UserScriptSetManager* user_script_set_manager_;
107 // Pending injections which are waiting for either the proper run location or
108 // user consent.
109 ScopedVector<ScriptInjection> pending_injections_;
111 // Running injections which are waiting for async callbacks from blink.
112 ScopedVector<ScriptInjection> running_injections_;
114 ScopedObserver<UserScriptSetManager, UserScriptSetManager::Observer>
115 user_script_set_manager_observer_;
117 DISALLOW_COPY_AND_ASSIGN(ScriptInjectionManager);
120 } // namespace extensions
122 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_