| 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/script_injector.h" | 12 #include "extensions/renderer/script_injector.h" |
| 12 | 13 |
| 13 class InjectionHost; | 14 class InjectionHost; |
| 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 const HostID& host_id, | 49 const HostID& host_id, |
| 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 |
| 58 // for permission purposes or because |current_location| is not the designated |
| 59 // |run_location_|). |
| 44 // NOTE: |injection_host| may be NULL, if the injection_host is removed! | 60 // NOTE: |injection_host| may be NULL, if the injection_host is removed! |
| 45 bool TryToInject(UserScript::RunLocation current_location, | 61 InjectionResult TryToInject(UserScript::RunLocation current_location, |
| 46 const InjectionHost* injection_host, | 62 const InjectionHost* injection_host, |
| 47 ScriptsRunInfo* scripts_run_info); | 63 ScriptsRunInfo* scripts_run_info, |
| 64 ScriptInjectionManager* manager); |
| 48 | 65 |
| 49 // Called when permission for the given injection has been granted. | 66 // Called when permission for the given injection has been granted. |
| 50 // Returns true if the injection ran. | 67 // Returns INJECTION_FINISHED if injection has injected or will never inject, |
| 51 bool OnPermissionGranted(const InjectionHost* injection_host, | 68 // returns INJECTION_BLOCKED if injection is runned asynchronously. |
| 52 ScriptsRunInfo* scripts_run_info); | 69 InjectionResult OnPermissionGranted(const InjectionHost* injection_host, |
| 70 ScriptsRunInfo* scripts_run_info); |
| 71 |
| 72 // Called when JS injection for the given frame has been completed. |
| 73 void OnJsInjectionCompleted(blink::WebLocalFrame* frame, |
| 74 const blink::WebVector<v8::Local<v8::Value> >&); |
| 53 | 75 |
| 54 // Accessors. | 76 // Accessors. |
| 55 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 77 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
| 56 const HostID& host_id() const { return host_id_; } | 78 const HostID& host_id() const { return host_id_; } |
| 57 int64 request_id() const { return request_id_; } | 79 int64 request_id() const { return request_id_; } |
| 58 | 80 |
| 59 private: | 81 private: |
| 60 // Sends a message to the browser, either that the script injection would | 82 // 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. | 83 // like to inject, or to notify the browser that it is currently injecting. |
| 62 void SendInjectionMessage(bool request_permission); | 84 void SendInjectionMessage(bool request_permission); |
| 63 | 85 |
| 64 // Injects the script, optionally populating |scripts_run_info|. | 86 // Injects the script. Returns INJECTION_FINISHED if injection has finished, |
| 65 void Inject(const InjectionHost* injection_host, | 87 // otherwise INJECTION_BLOCKED. |
| 66 ScriptsRunInfo* scripts_run_info); | 88 InjectionResult Inject(const InjectionHost* injection_host, |
| 89 ScriptsRunInfo* scripts_run_info); |
| 67 | 90 |
| 68 // Inject any JS scripts into the |frame|, optionally populating | 91 // Inject any JS scripts into the |frame|, optionally populating |
| 69 // |execution_results|. | 92 // |execution_results|. |
| 70 void InjectJs(const InjectionHost* injection_host, | 93 void InjectJs(const InjectionHost* injection_host, |
| 71 blink::WebLocalFrame* frame, | 94 blink::WebLocalFrame* frame); |
| 72 base::ListValue* execution_results); | 95 |
| 96 // Checks if all scripts have been injected and finished. |
| 97 void TryToFinish(); |
| 73 | 98 |
| 74 // Inject any CSS source into the |frame|. | 99 // Inject any CSS source into the |frame|. |
| 75 void InjectCss(blink::WebLocalFrame* frame); | 100 void InjectCss(blink::WebLocalFrame* frame); |
| 76 | 101 |
| 77 // Notify that we will not inject, and mark it as acknowledged. | 102 // Notify that we will not inject, and mark it as acknowledged. |
| 78 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 103 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
| 79 | 104 |
| 80 // The injector for this injection. | 105 // The injector for this injection. |
| 81 scoped_ptr<ScriptInjector> injector_; | 106 scoped_ptr<ScriptInjector> injector_; |
| 82 | 107 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 int tab_id_; | 118 int tab_id_; |
| 94 | 119 |
| 95 // This injection's request id. This will be -1 unless the injection is | 120 // This injection's request id. This will be -1 unless the injection is |
| 96 // currently waiting on permission. | 121 // currently waiting on permission. |
| 97 int64 request_id_; | 122 int64 request_id_; |
| 98 | 123 |
| 99 // Whether or not the injection is complete, either via injecting the script | 124 // Whether or not the injection is complete, either via injecting the script |
| 100 // or because it will never complete. | 125 // or because it will never complete. |
| 101 bool complete_; | 126 bool complete_; |
| 102 | 127 |
| 128 // Number of frames in which the injection is running. |
| 129 int running_frames_; |
| 130 |
| 131 // Results storage. |
| 132 scoped_ptr<base::ListValue> execution_results_; |
| 133 |
| 134 // Flag is true when injections for each frame started. |
| 135 bool all_injections_started_; |
| 136 |
| 137 // ScriptInjectionManager::OnInjectionFinished will be called after injection |
| 138 // finished. |
| 139 ScriptInjectionManager* script_injection_manager_; |
| 140 |
| 103 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 141 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
| 104 }; | 142 }; |
| 105 | 143 |
| 106 } // namespace extensions | 144 } // namespace extensions |
| 107 | 145 |
| 108 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 146 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| OLD | NEW |