OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 5 #ifndef EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 6 #define EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
7 | 7 |
8 #include <map> | |
9 | |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | |
10 #include "extensions/common/user_script.h" | 14 #include "extensions/common/user_script.h" |
11 #include "extensions/renderer/script_injector.h" | 15 #include "extensions/renderer/script_injector.h" |
12 | 16 |
13 class InjectionHost; | 17 class InjectionHost; |
14 struct HostID; | 18 struct HostID; |
15 | 19 |
16 namespace blink { | 20 namespace blink { |
17 class WebLocalFrame; | 21 class WebLocalFrame; |
22 template<typename T> class WebVector; | |
23 } | |
24 | |
25 namespace v8 { | |
26 class Value; | |
27 template <class T> class Local; | |
18 } | 28 } |
19 | 29 |
20 namespace extensions { | 30 namespace extensions { |
31 class ScriptInjectionManager; | |
21 struct ScriptsRunInfo; | 32 struct ScriptsRunInfo; |
22 | 33 |
23 // A script wrapper which is aware of whether or not it is allowed to execute, | 34 // A script wrapper which is aware of whether or not it is allowed to execute, |
24 // and contains the implementation to do so. | 35 // and contains the implementation to do so. |
25 class ScriptInjection { | 36 class ScriptInjection { |
26 public: | 37 public: |
27 // Return the id of the injection host associated with the given world. | 38 // Return the id of the injection host associated with the given world. |
28 static std::string GetHostIdForIsolatedWorld(int world_id); | 39 static std::string GetHostIdForIsolatedWorld(int world_id); |
29 | 40 |
30 // Remove the isolated world associated with the given injection host. | 41 // Remove the isolated world associated with the given injection host. |
31 static void RemoveIsolatedWorld(const std::string& host_id); | 42 static void RemoveIsolatedWorld(const std::string& host_id); |
32 | 43 |
33 ScriptInjection(scoped_ptr<ScriptInjector> injector, | 44 ScriptInjection(scoped_ptr<ScriptInjector> injector, |
34 blink::WebLocalFrame* web_frame, | 45 blink::WebLocalFrame* web_frame, |
35 const HostID& host_id, | 46 const HostID& host_id, |
36 UserScript::RunLocation run_location, | 47 UserScript::RunLocation run_location, |
37 int tab_id); | 48 int tab_id); |
38 ~ScriptInjection(); | 49 ~ScriptInjection(); |
39 | 50 |
40 // Try to inject the script at the |current_location|. This returns true if | 51 // Try to inject the script at the |current_location|. This returns true if |
41 // the script has either injected or will never inject (i.e., if the object | 52 // the script has either injected or will never inject (i.e., if the object |
42 // is done), and false if injection is delayed (either for permission purposes | 53 // is done), and false if injection is delayed (either for permission purposes |
43 // or because |current_location| is not the designated |run_location_|). | 54 // or because |current_location| is not the designated |run_location_|). |
44 // NOTE: |injection_host| may be NULL, if the injection_host is removed! | 55 // NOTE: |injection_host| may be NULL, if the injection_host is removed! |
45 bool TryToInject(UserScript::RunLocation current_location, | 56 bool TryToInject(UserScript::RunLocation current_location, |
Devlin
2015/02/25 18:02:15
As a result of some of these changes, I think it b
kozy
2015/02/27 17:32:28
Done.
| |
46 const InjectionHost* injection_host, | 57 const InjectionHost* injection_host, |
47 ScriptsRunInfo* scripts_run_info); | 58 scoped_refptr<ScriptsRunInfo> scripts_run_info); |
48 | 59 |
49 // Called when permission for the given injection has been granted. | 60 // Called when permission for the given injection has been granted. |
50 // Returns true if the injection ran. | 61 // Returns true if the injection ran. |
51 bool OnPermissionGranted(const InjectionHost* injection_host, | 62 bool OnPermissionGranted(const InjectionHost* injection_host); |
52 ScriptsRunInfo* scripts_run_info); | 63 |
64 // Called when JS injection for the given frame has been completed. | |
65 void OnJSInjectionCompleted(blink::WebLocalFrame* frame, | |
66 const blink::WebVector<v8::Local<v8::Value> >&); | |
53 | 67 |
54 // Accessors. | 68 // Accessors. |
55 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 69 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
56 const HostID& host_id() const { return host_id_; } | 70 const HostID& host_id() const { return host_id_; } |
71 UserScript::RunLocation run_location() const { return run_location_; } | |
57 int64 request_id() const { return request_id_; } | 72 int64 request_id() const { return request_id_; } |
73 bool is_complete() const { return complete_; } | |
74 | |
75 void SetScriptInjectionManager(ScriptInjectionManager* manager); | |
58 | 76 |
59 private: | 77 private: |
60 // Sends a message to the browser, either that the script injection would | 78 // Sends a message to the browser, either that the script injection would |
61 // like to inject, or to notify the browser that it is currently injecting. | 79 // like to inject, or to notify the browser that it is currently injecting. |
62 void SendInjectionMessage(bool request_permission); | 80 void SendInjectionMessage(bool request_permission); |
63 | 81 |
64 // Injects the script, optionally populating |scripts_run_info|. | 82 // Injects the script, optionally populating |scripts_run_info|. |
65 void Inject(const InjectionHost* injection_host, | 83 void Inject(const InjectionHost* injection_host); |
66 ScriptsRunInfo* scripts_run_info); | |
67 | 84 |
68 // Inject any JS scripts into the |frame|, optionally populating | 85 // Inject any JS scripts into the |frame|, optionally populating |
69 // |execution_results|. | 86 // |execution_results|. |
70 void InjectJs(const InjectionHost* injection_host, | 87 void InjectJs(const InjectionHost* injection_host, |
71 blink::WebLocalFrame* frame, | 88 blink::WebLocalFrame* frame); |
72 base::ListValue* execution_results); | 89 |
90 // If all injection are sended to frame and results received, | |
91 // OnInjectionComplete will be called on injector | |
92 void TryToFinish(); | |
73 | 93 |
74 // Inject any CSS source into the |frame|. | 94 // Inject any CSS source into the |frame|. |
75 void InjectCss(blink::WebLocalFrame* frame); | 95 void InjectCss(blink::WebLocalFrame* frame); |
76 | 96 |
77 // Notify that we will not inject, and mark it as acknowledged. | 97 // Notify that we will not inject, and mark it as acknowledged. |
78 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 98 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
79 | 99 |
80 // The injector for this injection. | 100 // The injector for this injection. |
81 scoped_ptr<ScriptInjector> injector_; | 101 scoped_ptr<ScriptInjector> injector_; |
82 | 102 |
(...skipping 10 matching lines...) Expand all Loading... | |
93 int tab_id_; | 113 int tab_id_; |
94 | 114 |
95 // This injection's request id. This will be -1 unless the injection is | 115 // This injection's request id. This will be -1 unless the injection is |
96 // currently waiting on permission. | 116 // currently waiting on permission. |
97 int64 request_id_; | 117 int64 request_id_; |
98 | 118 |
99 // Whether or not the injection is complete, either via injecting the script | 119 // Whether or not the injection is complete, either via injecting the script |
100 // or because it will never complete. | 120 // or because it will never complete. |
101 bool complete_; | 121 bool complete_; |
102 | 122 |
123 // The scripts run info passed from injection user | |
124 scoped_refptr<ScriptsRunInfo> scripts_run_info_; | |
125 | |
126 // A mapping of WebLocalFrame* to an index of result in results array | |
127 std::map<blink::WebLocalFrame*, size_t> frame_result_index_; | |
128 | |
129 // Results storage | |
130 scoped_ptr<base::ListValue> execution_results_; | |
131 | |
132 // Flag is true when injections for each frame started | |
133 bool all_injection_started_; | |
134 | |
135 // If not null ScriptInjectionManager::OnInjectionFinished will be called | |
136 // after injection finished. | |
137 ScriptInjectionManager* script_injection_manager_; | |
138 | |
103 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 139 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
104 }; | 140 }; |
105 | 141 |
106 } // namespace extensions | 142 } // namespace extensions |
107 | 143 |
108 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 144 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |