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" |
| 14 #include "base/memory/scoped_vector.h" |
10 #include "extensions/common/user_script.h" | 15 #include "extensions/common/user_script.h" |
11 #include "extensions/renderer/script_injector.h" | 16 #include "extensions/renderer/script_injector.h" |
| 17 #include "extensions/renderer/scripts_run_info.h" |
| 18 #include "v8/include/v8.h" |
12 | 19 |
13 namespace blink { | 20 namespace blink { |
14 class WebLocalFrame; | 21 class WebLocalFrame; |
| 22 class WebScriptExecutionCallback; |
| 23 template<typename T> class WebVector; |
| 24 } |
| 25 |
| 26 namespace v8 { |
| 27 class Value; |
| 28 template <class T> class Local; |
15 } | 29 } |
16 | 30 |
17 namespace extensions { | 31 namespace extensions { |
18 class Extension; | 32 class Extension; |
| 33 class ScriptInjectionManager; |
19 struct ScriptsRunInfo; | 34 struct ScriptsRunInfo; |
20 | 35 |
21 // A script wrapper which is aware of whether or not it is allowed to execute, | 36 // A script wrapper which is aware of whether or not it is allowed to execute, |
22 // and contains the implementation to do so. | 37 // and contains the implementation to do so. |
23 class ScriptInjection { | 38 class ScriptInjection { |
24 public: | 39 public: |
25 // Return the id of the extension associated with the given world. | 40 // Return the id of the extension associated with the given world. |
26 static std::string GetExtensionIdForIsolatedWorld(int world_id); | 41 static std::string GetExtensionIdForIsolatedWorld(int world_id); |
27 | 42 |
28 // Remove the isolated world associated with the given extension. | 43 // Remove the isolated world associated with the given extension. |
29 static void RemoveIsolatedWorld(const std::string& extension_id); | 44 static void RemoveIsolatedWorld(const std::string& extension_id); |
30 | 45 |
31 ScriptInjection(scoped_ptr<ScriptInjector> injector, | 46 ScriptInjection(scoped_ptr<ScriptInjector> injector, |
32 blink::WebLocalFrame* web_frame, | 47 blink::WebLocalFrame* web_frame, |
33 const std::string& extension_id, | 48 const std::string& extension_id, |
34 UserScript::RunLocation run_location, | 49 UserScript::RunLocation run_location, |
35 int tab_id); | 50 int tab_id); |
36 ~ScriptInjection(); | 51 ~ScriptInjection(); |
37 | 52 |
38 // Try to inject the script at the |current_location|. This returns true if | 53 // Try to inject the script at the |current_location|. This returns true if |
39 // the script has either injected or will never inject (i.e., if the object | 54 // the script has either injected or will never inject (i.e., if the object |
40 // is done), and false if injection is delayed (either for permission purposes | 55 // is done), and false if injection is delayed (either for permission purposes |
41 // or because |current_location| is not the designated |run_location_|). | 56 // or because |current_location| is not the designated |run_location_|). |
42 // NOTE: |extension| may be NULL, if the extension is removed! | 57 // NOTE: |extension| may be NULL, if the extension is removed! |
43 bool TryToInject(UserScript::RunLocation current_location, | 58 bool TryToInject(UserScript::RunLocation current_location, |
44 const Extension* extension, | 59 const Extension* extension); |
45 ScriptsRunInfo* scripts_run_info); | |
46 | 60 |
47 // Called when permission for the given injection has been granted. | 61 // Called when permission for the given injection has been granted. |
48 // Returns true if the injection ran. | 62 // Returns true if the injection ran. |
49 bool OnPermissionGranted(const Extension* extension, | 63 bool OnPermissionGranted(const Extension* extension); |
50 ScriptsRunInfo* scripts_run_info); | 64 |
| 65 // Called when JS injection for the given frame has been completed. |
| 66 void OnJSInjectionCompleted(blink::WebLocalFrame* frame, |
| 67 const blink::WebVector<v8::Local<v8::Value> >&); |
51 | 68 |
52 // Accessors. | 69 // Accessors. |
53 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 70 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
54 const std::string& extension_id() const { return extension_id_; } | 71 const std::string& extension_id() const { return extension_id_; } |
55 int64 request_id() const { return request_id_; } | 72 int64 request_id() const { return request_id_; } |
| 73 bool is_complete() const { return complete_; } |
| 74 UserScript::RunLocation run_location() const { return run_location_; } |
| 75 |
| 76 void SetScriptInjectionManager(ScriptInjectionManager* manager); |
56 | 77 |
57 private: | 78 private: |
58 // Send a message to the browser requesting permission to execute. | 79 // Send a message to the browser requesting permission to execute. |
59 void RequestPermission(); | 80 void RequestPermission(); |
60 | 81 |
61 // Injects the script, optionally populating |scripts_run_info|. | 82 // Injects the script. |
62 void Inject(const Extension* extension, ScriptsRunInfo* scripts_run_info); | 83 void Inject(const Extension* extension); |
63 | 84 |
64 // Inject any JS scripts into the |frame|, optionally populating | 85 // Inject any JS scripts into the |frame|, optionally populating |
65 // |execution_results|. | 86 // |execution_results|. |
66 void InjectJs(const Extension* extension, | 87 void InjectJs(const Extension* extension, |
67 blink::WebLocalFrame* frame, | 88 blink::WebLocalFrame* frame); |
68 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(); |
69 | 93 |
70 // Inject any CSS source into the |frame|. | 94 // Inject any CSS source into the |frame|. |
71 void InjectCss(blink::WebLocalFrame* frame); | 95 void InjectCss(blink::WebLocalFrame* frame); |
72 | 96 |
73 // Notify that we will not inject, and mark it as acknowledged. | 97 // Notify that we will not inject, and mark it as acknowledged. |
74 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 98 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
75 | 99 |
76 // The injector for this injection. | 100 // The injector for this injection. |
77 scoped_ptr<ScriptInjector> injector_; | 101 scoped_ptr<ScriptInjector> injector_; |
78 | 102 |
(...skipping 10 matching lines...) Expand all Loading... |
89 int tab_id_; | 113 int tab_id_; |
90 | 114 |
91 // 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 |
92 // currently waiting on permission. | 116 // currently waiting on permission. |
93 int64 request_id_; | 117 int64 request_id_; |
94 | 118 |
95 // 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 |
96 // or because it will never complete. | 120 // or because it will never complete. |
97 bool complete_; | 121 bool complete_; |
98 | 122 |
| 123 // A mapping of WebLocalFrame* to an index of result in results array |
| 124 std::map<blink::WebLocalFrame*, size_t> frame_result_index_; |
| 125 |
| 126 // Results storage |
| 127 scoped_ptr<base::ListValue> execution_results_; |
| 128 |
| 129 // Flag is true when all injections for each frame started |
| 130 bool all_injection_started_; |
| 131 |
| 132 // Pending execution callbacks |
| 133 ScopedVector<blink::WebScriptExecutionCallback> callbacks_; |
| 134 |
| 135 // Injection run info |
| 136 scoped_ptr<ScriptsRunInfo> scripts_run_info_; |
| 137 |
| 138 // If not null ScriptInjectionManager::OnInjectionFinished will be called |
| 139 // after injection finished. |
| 140 ScriptInjectionManager* script_injection_manager_; |
| 141 |
99 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 142 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
100 }; | 143 }; |
101 | 144 |
102 } // namespace extensions | 145 } // namespace extensions |
103 | 146 |
104 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 147 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |