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/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "extensions/common/user_script.h"
14 #include "extensions/renderer/injection_host.h"
15 #include "extensions/renderer/script_injector.h"
20 template<typename T
> class WebVector
;
29 template <class T
> class Local
;
32 namespace extensions
{
33 struct ScriptsRunInfo
;
35 // A script wrapper which is aware of whether or not it is allowed to execute,
36 // and contains the implementation to do so.
37 class ScriptInjection
{
39 enum InjectionResult
{
45 using CompletionCallback
= base::Callback
<void(ScriptInjection
*)>;
47 // Return the id of the injection host associated with the given world.
48 static std::string
GetHostIdForIsolatedWorld(int world_id
);
50 // Remove the isolated world associated with the given injection host.
51 static void RemoveIsolatedWorld(const std::string
& host_id
);
53 ScriptInjection(scoped_ptr
<ScriptInjector
> injector
,
54 content::RenderFrame
* render_frame
,
55 scoped_ptr
<const InjectionHost
> injection_host
,
56 UserScript::RunLocation run_location
,
60 // Try to inject the script at the |current_location|. This returns
61 // INJECTION_FINISHED if injection has injected or will never inject, returns
62 // INJECTION_BLOCKED if injection is running asynchronously and has not
63 // finished yet, returns INJECTION_WAITING if injections is delayed (either
64 // for permission purposes or because |current_location| is not the designated
66 // If INJECTION_BLOCKED is returned, |async_completion_callback| will be
67 // called upon completion.
68 InjectionResult
TryToInject(
69 UserScript::RunLocation current_location
,
70 ScriptsRunInfo
* scripts_run_info
,
71 const CompletionCallback
& async_completion_callback
);
73 // Called when permission for the given injection has been granted.
74 // Returns INJECTION_FINISHED if injection has injected or will never inject,
75 // returns INJECTION_BLOCKED if injection is ran asynchronously.
76 InjectionResult
OnPermissionGranted(ScriptsRunInfo
* scripts_run_info
);
78 // Resets the pointer of the injection host when the host is gone.
82 content::RenderFrame
* render_frame() const { return render_frame_
; }
83 const HostID
& host_id() const { return injection_host_
->id(); }
84 int64
request_id() const { return request_id_
; }
87 // Sends a message to the browser, either that the script injection would
88 // like to inject, or to notify the browser that it is currently injecting.
89 void SendInjectionMessage(bool request_permission
);
91 // Injects the script. Returns INJECTION_FINISHED if injection has finished,
92 // otherwise INJECTION_BLOCKED.
93 InjectionResult
Inject(ScriptsRunInfo
* scripts_run_info
);
95 // Inject any JS scripts into the frame for the injection.
98 // Called when JS injection for the given frame has been completed.
99 void OnJsInjectionCompleted(
100 const blink::WebVector
<v8::Local
<v8::Value
> >& results
);
102 // Inject any CSS source into the frame for the injection.
105 // Notify that we will not inject, and mark it as acknowledged.
106 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason
);
108 // The injector for this injection.
109 scoped_ptr
<ScriptInjector
> injector_
;
111 // The RenderFrame into which this should inject the script.
112 content::RenderFrame
* render_frame_
;
114 // The associated injection host.
115 scoped_ptr
<const InjectionHost
> injection_host_
;
117 // The location in the document load at which we inject the script.
118 UserScript::RunLocation run_location_
;
120 // The tab id associated with the frame.
123 // This injection's request id. This will be -1 unless the injection is
124 // currently waiting on permission.
127 // Whether or not the injection is complete, either via injecting the script
128 // or because it will never complete.
131 // Whether or not the injection successfully injected JS.
135 scoped_ptr
<base::Value
> execution_result_
;
137 // The callback to run upon completing asynchronously.
138 CompletionCallback async_completion_callback_
;
140 base::WeakPtrFactory
<ScriptInjection
> weak_ptr_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(ScriptInjection
);
145 } // namespace extensions
147 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_