Chromium Code Reviews| 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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 10 #include "extensions/common/user_script.h" | 11 #include "extensions/common/user_script.h" |
| 11 #include "extensions/renderer/injection_host.h" | 12 #include "extensions/renderer/injection_host.h" |
| 12 #include "extensions/renderer/script_injector.h" | 13 #include "extensions/renderer/script_injector.h" |
| 13 | 14 |
| 14 struct HostID; | 15 struct HostID; |
| 15 | 16 |
| 16 namespace blink { | 17 namespace blink { |
| 17 class WebLocalFrame; | 18 class WebLocalFrame; |
| 19 template<typename T> class WebVector; | |
| 20 } | |
| 21 | |
| 22 namespace v8 { | |
| 23 class Value; | |
| 24 template <class T> class Local; | |
| 18 } | 25 } |
| 19 | 26 |
| 20 namespace extensions { | 27 namespace extensions { |
| 28 class ScriptInjectionManager; | |
| 21 struct ScriptsRunInfo; | 29 struct ScriptsRunInfo; |
| 22 | 30 |
| 23 // A script wrapper which is aware of whether or not it is allowed to execute, | 31 // A script wrapper which is aware of whether or not it is allowed to execute, |
| 24 // and contains the implementation to do so. | 32 // and contains the implementation to do so. |
| 25 class ScriptInjection { | 33 class ScriptInjection { |
| 26 public: | 34 public: |
| 35 enum InjectionResult { | |
| 36 INJECTION_FINISHED, | |
| 37 INJECTION_BLOCKED, | |
| 38 INJECTION_WAITING | |
| 39 }; | |
| 40 | |
| 27 // Return the id of the injection host associated with the given world. | 41 // Return the id of the injection host associated with the given world. |
| 28 static std::string GetHostIdForIsolatedWorld(int world_id); | 42 static std::string GetHostIdForIsolatedWorld(int world_id); |
| 29 | 43 |
| 30 // Remove the isolated world associated with the given injection host. | 44 // Remove the isolated world associated with the given injection host. |
| 31 static void RemoveIsolatedWorld(const std::string& host_id); | 45 static void RemoveIsolatedWorld(const std::string& host_id); |
| 32 | 46 |
| 33 ScriptInjection(scoped_ptr<ScriptInjector> injector, | 47 ScriptInjection(scoped_ptr<ScriptInjector> injector, |
| 34 blink::WebLocalFrame* web_frame, | 48 blink::WebLocalFrame* web_frame, |
| 35 scoped_ptr<const InjectionHost> injection_host, | 49 scoped_ptr<const InjectionHost> injection_host, |
| 36 UserScript::RunLocation run_location, | 50 UserScript::RunLocation run_location, |
| 37 int tab_id); | 51 int tab_id); |
| 38 ~ScriptInjection(); | 52 ~ScriptInjection(); |
| 39 | 53 |
| 40 // Try to inject the script at the |current_location|. This returns true if | 54 // Try to inject the script at the |current_location|. This returns |
| 41 // the script has either injected or will never inject (i.e., if the object | 55 // INJECTION_FINISHED if injection has injected or will never inject, returns |
| 42 // is done), and false if injection is delayed (either for permission purposes | 56 // INJECTION_BLOCKED if injection is running asynchronously and has not |
| 43 // or because |current_location| is not the designated |run_location_|). | 57 // finished yet, returns INJECTION_WAITING if injections is delayed (either |
| 44 bool TryToInject(UserScript::RunLocation current_location, | 58 // for permission purposes or because |current_location| is not the designated |
| 45 ScriptsRunInfo* scripts_run_info); | 59 // |run_location_|). |
| 60 // NOTE: |injection_host| may be NULL, if the injection_host is removed! | |
|
Devlin
2015/03/05 16:32:13
Remove |injection_host| comment.
kozy
2015/03/05 21:41:37
Done.
| |
| 61 InjectionResult TryToInject(UserScript::RunLocation current_location, | |
| 62 ScriptsRunInfo* scripts_run_info, | |
| 63 ScriptInjectionManager* manager); | |
| 46 | 64 |
| 47 // Called when permission for the given injection has been granted. | 65 // Called when permission for the given injection has been granted. |
| 48 // Returns true if the injection ran. | 66 // Returns INJECTION_FINISHED if injection has injected or will never inject, |
| 49 bool OnPermissionGranted(ScriptsRunInfo* scripts_run_info); | 67 // returns INJECTION_BLOCKED if injection is runned asynchronously. |
|
Devlin
2015/03/05 16:32:12
nit: "if the injection is ran asynchronously" (not
kozy
2015/03/05 21:41:37
Done.
| |
| 68 InjectionResult OnPermissionGranted(ScriptsRunInfo* scripts_run_info); | |
| 50 | 69 |
| 51 // Resets the pointer of the injection host when the host is gone. | 70 // Resets the pointer of the injection host when the host is gone. |
| 52 void OnHostRemoved(); | 71 void OnHostRemoved(); |
| 53 | 72 |
| 73 // Called when JS injection for the given frame has been completed. | |
| 74 void OnJsInjectionCompleted(blink::WebLocalFrame* frame, | |
| 75 const blink::WebVector<v8::Local<v8::Value> >&); | |
|
Devlin
2015/03/05 16:32:13
nit: assign a name to the vector (e.g., result)
kozy
2015/03/05 21:41:36
Done.
| |
| 76 | |
| 54 // Accessors. | 77 // Accessors. |
| 55 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 78 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
| 56 const HostID& host_id() const { return injection_host_->id(); } | 79 const HostID& host_id() const { return injection_host_->id(); } |
| 57 int64 request_id() const { return request_id_; } | 80 int64 request_id() const { return request_id_; } |
| 58 | 81 |
| 59 private: | 82 private: |
| 60 // Sends a message to the browser, either that the script injection would | 83 // 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. | 84 // like to inject, or to notify the browser that it is currently injecting. |
| 62 void SendInjectionMessage(bool request_permission); | 85 void SendInjectionMessage(bool request_permission); |
| 63 | 86 |
| 64 // Injects the script, optionally populating |scripts_run_info|. | 87 // Injects the script. Returns INJECTION_FINISHED if injection has finished, |
| 65 void Inject(ScriptsRunInfo* scripts_run_info); | 88 // otherwise INJECTION_BLOCKED. |
| 89 InjectionResult Inject(ScriptsRunInfo* scripts_run_info); | |
| 66 | 90 |
| 67 // Inject any JS scripts into the |frame|, optionally populating | 91 // Inject any JS scripts into the |frame|. |
| 68 // |execution_results|. | 92 void InjectJs(blink::WebLocalFrame* frame); |
| 69 void InjectJs(blink::WebLocalFrame* frame, | 93 |
| 70 base::ListValue* execution_results); | 94 // Checks if all scripts have been injected and finished. |
| 95 void TryToFinish(); | |
| 71 | 96 |
| 72 // Inject any CSS source into the |frame|. | 97 // Inject any CSS source into the |frame|. |
| 73 void InjectCss(blink::WebLocalFrame* frame); | 98 void InjectCss(blink::WebLocalFrame* frame); |
| 74 | 99 |
| 75 // Notify that we will not inject, and mark it as acknowledged. | 100 // Notify that we will not inject, and mark it as acknowledged. |
| 76 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 101 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
| 77 | 102 |
| 78 // The injector for this injection. | 103 // The injector for this injection. |
| 79 scoped_ptr<ScriptInjector> injector_; | 104 scoped_ptr<ScriptInjector> injector_; |
| 80 | 105 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 91 int tab_id_; | 116 int tab_id_; |
| 92 | 117 |
| 93 // This injection's request id. This will be -1 unless the injection is | 118 // This injection's request id. This will be -1 unless the injection is |
| 94 // currently waiting on permission. | 119 // currently waiting on permission. |
| 95 int64 request_id_; | 120 int64 request_id_; |
| 96 | 121 |
| 97 // Whether or not the injection is complete, either via injecting the script | 122 // Whether or not the injection is complete, either via injecting the script |
| 98 // or because it will never complete. | 123 // or because it will never complete. |
| 99 bool complete_; | 124 bool complete_; |
| 100 | 125 |
| 126 // Number of frames in which the injection is running. | |
| 127 int running_frames_; | |
| 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_injections_started_; | |
| 134 | |
| 135 // ScriptInjectionManager::OnInjectionFinished will be called after injection | |
| 136 // finished. | |
| 137 ScriptInjectionManager* script_injection_manager_; | |
| 138 | |
| 101 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 139 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
| 102 }; | 140 }; |
| 103 | 141 |
| 104 } // namespace extensions | 142 } // namespace extensions |
| 105 | 143 |
| 106 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 144 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| OLD | NEW |