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/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
;
26 namespace extensions
{
30 // The ScriptInjectionManager manages extensions injecting scripts into frames
31 // via both content/user scripts and tabs.executeScript(). It is responsible for
32 // maintaining any pending injections awaiting permission or the appropriate
33 // load point, and injecting them when ready.
34 class ScriptInjectionManager
: public UserScriptSetManager::Observer
{
36 ScriptInjectionManager(const ExtensionSet
* extensions
,
37 UserScriptSetManager
* user_script_set_manager
);
38 virtual ~ScriptInjectionManager();
40 // Notifies that a new render view has been created.
41 void OnRenderFrameCreated(content::RenderFrame
* render_frame
);
43 // Removes pending injections of the unloaded extension.
44 void OnExtensionUnloaded(const std::string
& extension_id
);
47 // A RenderFrameObserver implementation which watches the various render
48 // frames in order to notify the ScriptInjectionManager of different
49 // document load states and IPCs.
52 using FrameStatusMap
=
53 std::map
<content::RenderFrame
*, UserScript::RunLocation
>;
55 // Notifies that an injection has been finished.
56 void OnInjectionFinished(ScriptInjection
* injection
);
58 // UserScriptSetManager::Observer implementation.
59 void OnUserScriptsUpdated(const std::set
<HostID
>& changed_hosts
,
60 const std::vector
<UserScript
*>& scripts
) override
;
62 // Notifies that an RFOHelper should be removed.
63 void RemoveObserver(RFOHelper
* helper
);
65 // Invalidate any pending tasks associated with |frame|.
66 void InvalidateForFrame(content::RenderFrame
* frame
);
68 // Starts the process to inject appropriate scripts into |frame|.
69 void StartInjectScripts(content::RenderFrame
* frame
,
70 UserScript::RunLocation run_location
);
72 // Actually injects the scripts into |frame|.
73 void InjectScripts(content::RenderFrame
* frame
,
74 UserScript::RunLocation run_location
);
76 // Try to inject and store injection if it has not finished.
77 void TryToInject(scoped_ptr
<ScriptInjection
> injection
,
78 UserScript::RunLocation run_location
,
79 ScriptsRunInfo
* scripts_run_info
);
81 // Handle the ExecuteCode extension message.
82 void HandleExecuteCode(const ExtensionMsg_ExecuteCode_Params
& params
,
83 content::RenderFrame
* render_frame
);
85 // Handle the ExecuteDeclarativeScript extension message.
86 void HandleExecuteDeclarativeScript(content::RenderFrame
* web_frame
,
88 const ExtensionId
& extension_id
,
92 // Handle the GrantInjectionPermission extension message.
93 void HandlePermitScriptInjection(int64 request_id
);
95 // Extensions metadata, owned by Dispatcher (which owns this object).
96 const ExtensionSet
* extensions_
;
98 // The map of active web frames to their corresponding statuses. The
99 // RunLocation of the frame corresponds to the last location that has ran.
100 FrameStatusMap frame_statuses_
;
102 // The collection of RFOHelpers.
103 ScopedVector
<RFOHelper
> rfo_helpers_
;
105 // The set of UserScripts associated with extensions. Owned by the Dispatcher.
106 UserScriptSetManager
* user_script_set_manager_
;
108 // Pending injections which are waiting for either the proper run location or
110 ScopedVector
<ScriptInjection
> pending_injections_
;
112 // Running injections which are waiting for async callbacks from blink.
113 ScopedVector
<ScriptInjection
> running_injections_
;
115 ScopedObserver
<UserScriptSetManager
, UserScriptSetManager::Observer
>
116 user_script_set_manager_observer_
;
118 DISALLOW_COPY_AND_ASSIGN(ScriptInjectionManager
);
121 } // namespace extensions
123 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_MANAGER_H_