Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / renderer / script_injection.h
blob14e34ae407235b77df585f3ffb6b66d65e993931
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"
17 struct HostID;
19 namespace blink {
20 template<typename T> class WebVector;
23 namespace content {
24 class RenderFrame;
27 namespace v8 {
28 class Value;
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 {
38 public:
39 enum InjectionResult {
40 INJECTION_FINISHED,
41 INJECTION_BLOCKED,
42 INJECTION_WAITING
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);
57 ~ScriptInjection();
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
64 // |run_location_|).
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.
78 void OnHostRemoved();
80 // Accessors.
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_; }
85 private:
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.
95 void InjectJs();
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.
102 void InjectCss();
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.
121 int64 request_id_;
123 // Whether or not the injection is complete, either via injecting the script
124 // or because it will never complete.
125 bool complete_;
127 // Whether or not the injection successfully injected JS.
128 bool did_inject_js_;
130 // Results storage.
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_