Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / extensions / renderer / script_injection.h
blobbaffec485705d91bb5c824c1cfd87185a3df3da9
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"
15 struct HostID;
17 namespace blink {
18 class WebLocalFrame;
19 template<typename T> class WebVector;
22 namespace v8 {
23 class Value;
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 {
34 public:
35 enum InjectionResult {
36 INJECTION_FINISHED,
37 INJECTION_BLOCKED,
38 INJECTION_WAITING
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,
51 int tab_id);
52 ~ScriptInjection();
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
59 // |run_location_|).
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.
70 void OnHostRemoved();
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);
77 // Accessors.
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_; }
82 private:
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.
95 void TryToFinish();
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.
116 int tab_id_;
118 // This injection's request id. This will be -1 unless the injection is
119 // currently waiting on permission.
120 int64 request_id_;
122 // Whether or not the injection is complete, either via injecting the script
123 // or because it will never complete.
124 bool complete_;
126 // Number of frames in which the injection is running.
127 int running_frames_;
129 // Results storage.
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
136 // finished.
137 ScriptInjectionManager* script_injection_manager_;
139 DISALLOW_COPY_AND_ASSIGN(ScriptInjection);
142 } // namespace extensions
144 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_