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_H_
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_
8 #include "base/basictypes.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "extensions/common/user_script.h"
12 #include "extensions/renderer/injection_host.h"
13 #include "extensions/renderer/script_injector.h"
19 template<typename T
> class WebVector
;
24 template <class T
> class Local
;
27 namespace extensions
{
28 class ScriptInjectionManager
;
29 struct ScriptsRunInfo
;
31 // A script wrapper which is aware of whether or not it is allowed to execute,
32 // and contains the implementation to do so.
33 class ScriptInjection
{
35 enum InjectionResult
{
41 // Return the id of the injection host associated with the given world.
42 static std::string
GetHostIdForIsolatedWorld(int world_id
);
44 // Remove the isolated world associated with the given injection host.
45 static void RemoveIsolatedWorld(const std::string
& host_id
);
47 ScriptInjection(scoped_ptr
<ScriptInjector
> injector
,
48 blink::WebLocalFrame
* web_frame
,
49 scoped_ptr
<const InjectionHost
> injection_host
,
50 UserScript::RunLocation run_location
,
54 // Try to inject the script at the |current_location|. This returns
55 // INJECTION_FINISHED if injection has injected or will never inject, returns
56 // INJECTION_BLOCKED if injection is running asynchronously and has not
57 // finished yet, returns INJECTION_WAITING if injections is delayed (either
58 // for permission purposes or because |current_location| is not the designated
60 InjectionResult
TryToInject(UserScript::RunLocation current_location
,
61 ScriptsRunInfo
* scripts_run_info
,
62 ScriptInjectionManager
* manager
);
64 // Called when permission for the given injection has been granted.
65 // Returns INJECTION_FINISHED if injection has injected or will never inject,
66 // returns INJECTION_BLOCKED if injection is ran asynchronously.
67 InjectionResult
OnPermissionGranted(ScriptsRunInfo
* scripts_run_info
);
69 // Resets the pointer of the injection host when the host is gone.
72 // Called when JS injection for the given frame has been completed.
73 void OnJsInjectionCompleted(
74 blink::WebLocalFrame
* frame
,
75 const blink::WebVector
<v8::Local
<v8::Value
> >& results
);
78 blink::WebLocalFrame
* web_frame() const { return web_frame_
; }
79 const HostID
& host_id() const { return injection_host_
->id(); }
80 int64
request_id() const { return request_id_
; }
83 // Sends a message to the browser, either that the script injection would
84 // like to inject, or to notify the browser that it is currently injecting.
85 void SendInjectionMessage(bool request_permission
);
87 // Injects the script. Returns INJECTION_FINISHED if injection has finished,
88 // otherwise INJECTION_BLOCKED.
89 InjectionResult
Inject(ScriptsRunInfo
* scripts_run_info
);
91 // Inject any JS scripts into the |frame|.
92 void InjectJs(blink::WebLocalFrame
* frame
);
94 // Checks if all scripts have been injected and finished.
97 // Inject any CSS source into the |frame|.
98 void InjectCss(blink::WebLocalFrame
* frame
);
100 // Notify that we will not inject, and mark it as acknowledged.
101 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason
);
103 // The injector for this injection.
104 scoped_ptr
<ScriptInjector
> injector_
;
106 // The (main) WebFrame into which this should inject the script.
107 blink::WebLocalFrame
* web_frame_
;
109 // The associated injection host.
110 scoped_ptr
<const InjectionHost
> injection_host_
;
112 // The location in the document load at which we inject the script.
113 UserScript::RunLocation run_location_
;
115 // The tab id associated with the frame.
118 // This injection's request id. This will be -1 unless the injection is
119 // currently waiting on permission.
122 // Whether or not the injection is complete, either via injecting the script
123 // or because it will never complete.
126 // Number of frames in which the injection is running.
130 scoped_ptr
<base::ListValue
> execution_results_
;
132 // Flag is true when injections for each frame started.
133 bool all_injections_started_
;
135 // ScriptInjectionManager::OnInjectionFinished will be called after injection
137 ScriptInjectionManager
* script_injection_manager_
;
139 DISALLOW_COPY_AND_ASSIGN(ScriptInjection
);
142 } // namespace extensions
144 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_