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
);
59 // Try to inject the script at the |current_location|. This returns
60 // INJECTION_FINISHED if injection has injected or will never inject, returns
61 // INJECTION_BLOCKED if injection is running asynchronously and has not
62 // finished yet, returns INJECTION_WAITING if injections is delayed (either
63 // for permission purposes or because |current_location| is not the designated
65 // If INJECTION_BLOCKED is returned, |async_completion_callback| will be
66 // called upon completion.
67 InjectionResult
TryToInject(
68 UserScript::RunLocation current_location
,
69 ScriptsRunInfo
* scripts_run_info
,
70 const CompletionCallback
& async_completion_callback
);
72 // Called when permission for the given injection has been granted.
73 // Returns INJECTION_FINISHED if injection has injected or will never inject,
74 // returns INJECTION_BLOCKED if injection is ran asynchronously.
75 InjectionResult
OnPermissionGranted(ScriptsRunInfo
* scripts_run_info
);
77 // Resets the pointer of the injection host when the host is gone.
81 content::RenderFrame
* render_frame() const { return render_frame_
; }
82 const HostID
& host_id() const { return injection_host_
->id(); }
83 int64
request_id() const { return request_id_
; }
86 // Sends a message to the browser, either that the script injection would
87 // like to inject, or to notify the browser that it is currently injecting.
88 void SendInjectionMessage(bool request_permission
);
90 // Injects the script. Returns INJECTION_FINISHED if injection has finished,
91 // otherwise INJECTION_BLOCKED.
92 InjectionResult
Inject(ScriptsRunInfo
* scripts_run_info
);
94 // Inject any JS scripts into the frame for the injection.
97 // Called when JS injection for the given frame has been completed.
98 void OnJsInjectionCompleted(
99 const blink::WebVector
<v8::Local
<v8::Value
> >& results
);
101 // Inject any CSS source into the frame for the injection.
104 // Notify that we will not inject, and mark it as acknowledged.
105 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason
);
107 // The injector for this injection.
108 scoped_ptr
<ScriptInjector
> injector_
;
110 // The RenderFrame into which this should inject the script.
111 content::RenderFrame
* render_frame_
;
113 // The associated injection host.
114 scoped_ptr
<const InjectionHost
> injection_host_
;
116 // The location in the document load at which we inject the script.
117 UserScript::RunLocation run_location_
;
119 // This injection's request id. This will be -1 unless the injection is
120 // currently waiting on permission.
123 // Whether or not the injection is complete, either via injecting the script
124 // or because it will never complete.
127 // Whether or not the injection successfully injected JS.
131 scoped_ptr
<base::Value
> execution_result_
;
133 // The callback to run upon completing asynchronously.
134 CompletionCallback async_completion_callback_
;
136 base::WeakPtrFactory
<ScriptInjection
> weak_ptr_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(ScriptInjection
);
141 } // namespace extensions
143 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_