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_
12 #include "base/callback.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/scoped_observer.h"
15 #include "extensions/common/user_script.h"
16 #include "extensions/renderer/script_injection.h"
17 #include "extensions/renderer/user_script_set_manager.h"
19 struct ExtensionMsg_ExecuteCode_Params
;
25 namespace extensions
{
28 // The ScriptInjectionManager manages extensions injecting scripts into frames
29 // via both content/user scripts and tabs.executeScript(). It is responsible for
30 // maintaining any pending injections awaiting permission or the appropriate
31 // load point, and injecting them when ready.
32 class ScriptInjectionManager
: public UserScriptSetManager::Observer
{
34 explicit ScriptInjectionManager(
35 UserScriptSetManager
* user_script_set_manager
);
36 virtual ~ScriptInjectionManager();
38 // Notifies that a new render view has been created.
39 void OnRenderFrameCreated(content::RenderFrame
* render_frame
);
41 // Removes pending injections of the unloaded extension.
42 void OnExtensionUnloaded(const std::string
& extension_id
);
45 // A RenderFrameObserver implementation which watches the various render
46 // frames in order to notify the ScriptInjectionManager of different
47 // document load states and IPCs.
50 using FrameStatusMap
=
51 std::map
<content::RenderFrame
*, UserScript::RunLocation
>;
53 // Notifies that an injection has been finished.
54 void OnInjectionFinished(ScriptInjection
* injection
);
56 // UserScriptSetManager::Observer implementation.
57 void OnUserScriptsUpdated(const std::set
<HostID
>& changed_hosts
,
58 const std::vector
<UserScript
*>& scripts
) override
;
60 // Notifies that an RFOHelper should be removed.
61 void RemoveObserver(RFOHelper
* helper
);
63 // Invalidate any pending tasks associated with |frame|.
64 void InvalidateForFrame(content::RenderFrame
* frame
);
66 // Starts the process to inject appropriate scripts into |frame|.
67 void StartInjectScripts(content::RenderFrame
* frame
,
68 UserScript::RunLocation run_location
);
70 // Actually injects the scripts into |frame|.
71 void InjectScripts(content::RenderFrame
* frame
,
72 UserScript::RunLocation run_location
);
74 // Try to inject and store injection if it has not finished.
75 void TryToInject(scoped_ptr
<ScriptInjection
> injection
,
76 UserScript::RunLocation run_location
,
77 ScriptsRunInfo
* scripts_run_info
);
79 // Handle the ExecuteCode extension message.
80 void HandleExecuteCode(const ExtensionMsg_ExecuteCode_Params
& params
,
81 content::RenderFrame
* render_frame
);
83 // Handle the ExecuteDeclarativeScript extension message.
84 void HandleExecuteDeclarativeScript(content::RenderFrame
* web_frame
,
86 const ExtensionId
& extension_id
,
90 // Handle the GrantInjectionPermission extension message.
91 void HandlePermitScriptInjection(int64 request_id
);
93 // The map of active web frames to their corresponding statuses. The
94 // RunLocation of the frame corresponds to the last location that has ran.
95 FrameStatusMap frame_statuses_
;
97 // The frames currently being injected into, so long as that frame is valid.
98 std::set
<content::RenderFrame
*> active_injection_frames_
;
100 // The collection of RFOHelpers.
101 ScopedVector
<RFOHelper
> rfo_helpers_
;
103 // The set of UserScripts associated with extensions. Owned by the Dispatcher.
104 UserScriptSetManager
* user_script_set_manager_
;
106 // Pending injections which are waiting for either the proper run location or
108 ScopedVector
<ScriptInjection
> pending_injections_
;
110 // Running injections which are waiting for async callbacks from blink.
111 ScopedVector
<ScriptInjection
> running_injections_
;
113 ScopedObserver
<UserScriptSetManager
, UserScriptSetManager::Observer
>
114 user_script_set_manager_observer_
;
116 DISALLOW_COPY_AND_ASSIGN(ScriptInjectionManager
);
119 } // namespace extensions
121 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_