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 "extensions/common/user_script.h" | 10 #include "extensions/common/user_script.h" |
| 11 #include "extensions/renderer/injection_host.h" | |
| 11 #include "extensions/renderer/script_injector.h" | 12 #include "extensions/renderer/script_injector.h" |
| 12 | 13 |
| 13 class InjectionHost; | |
| 14 struct HostID; | 14 struct HostID; |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 class WebLocalFrame; | 17 class WebLocalFrame; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 struct ScriptsRunInfo; | 21 struct ScriptsRunInfo; |
| 22 | 22 |
| 23 // A script wrapper which is aware of whether or not it is allowed to execute, | 23 // A script wrapper which is aware of whether or not it is allowed to execute, |
| 24 // and contains the implementation to do so. | 24 // and contains the implementation to do so. |
| 25 class ScriptInjection { | 25 class ScriptInjection { |
| 26 public: | 26 public: |
| 27 // Return the id of the injection host associated with the given world. | 27 // Return the id of the injection host associated with the given world. |
| 28 static std::string GetHostIdForIsolatedWorld(int world_id); | 28 static std::string GetHostIdForIsolatedWorld(int world_id); |
| 29 | 29 |
| 30 // Remove the isolated world associated with the given injection host. | 30 // Remove the isolated world associated with the given injection host. |
| 31 static void RemoveIsolatedWorld(const std::string& host_id); | 31 static void RemoveIsolatedWorld(const std::string& host_id); |
| 32 | 32 |
| 33 ScriptInjection( | 33 ScriptInjection( |
| 34 scoped_ptr<ScriptInjector> injector, | 34 scoped_ptr<ScriptInjector> injector, |
| 35 blink::WebLocalFrame* web_frame, | 35 blink::WebLocalFrame* web_frame, |
| 36 const HostID& host_id, | 36 scoped_ptr<const InjectionHost> injection_host, |
| 37 const UserScript::ConsumerInstanceType& consumer_instance_type, | 37 const UserScript::ConsumerInstanceType& consumer_instance_type, |
| 38 UserScript::RunLocation run_location, | 38 UserScript::RunLocation run_location, |
| 39 int tab_id); | 39 int tab_id); |
| 40 ~ScriptInjection(); | 40 ~ScriptInjection(); |
| 41 | 41 |
| 42 // Try to inject the script at the |current_location|. This returns true if | 42 // Try to inject the script at the |current_location|. This returns true if |
| 43 // the script has either injected or will never inject (i.e., if the object | 43 // the script has either injected or will never inject (i.e., if the object |
| 44 // is done), and false if injection is delayed (either for permission purposes | 44 // is done), and false if injection is delayed (either for permission purposes |
| 45 // or because |current_location| is not the designated |run_location_|). | 45 // or because |current_location| is not the designated |run_location_|). |
| 46 // NOTE: |injection_host| may be NULL, if the injection_host is removed! | |
| 47 bool TryToInject(UserScript::RunLocation current_location, | 46 bool TryToInject(UserScript::RunLocation current_location, |
| 48 const InjectionHost* injection_host, | |
| 49 ScriptsRunInfo* scripts_run_info); | 47 ScriptsRunInfo* scripts_run_info); |
| 50 | 48 |
| 51 // Called when permission for the given injection has been granted. | 49 // Called when permission for the given injection has been granted. |
| 52 // Returns true if the injection ran. | 50 // Returns true if the injection ran. |
| 53 bool OnPermissionGranted(const InjectionHost* injection_host, | 51 bool OnPermissionGranted(ScriptsRunInfo* scripts_run_info); |
| 54 ScriptsRunInfo* scripts_run_info); | |
| 55 | 52 |
| 56 // Accessors. | 53 // Accessors. |
| 57 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 54 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
| 58 const HostID& host_id() const { return host_id_; } | 55 const HostID& host_id() const { return injection_host_->id(); } |
| 59 int64 request_id() const { return request_id_; } | 56 int64 request_id() const { return request_id_; } |
| 57 void set_injection_host(InjectionHost* host) { | |
|
Devlin
2015/02/20 22:39:13
two things:
- if this would actually reset the hos
Xi Han
2015/02/23 16:14:59
You are right, it makes more sense to have "OnHost
| |
| 58 injection_host_.reset(host); | |
| 59 } | |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 // Sends a message to the browser, either that the script injection would | 62 // Sends a message to the browser, either that the script injection would |
| 63 // like to inject, or to notify the browser that it is currently injecting. | 63 // like to inject, or to notify the browser that it is currently injecting. |
| 64 void SendInjectionMessage(bool request_permission); | 64 void SendInjectionMessage(bool request_permission); |
| 65 | 65 |
| 66 // Injects the script, optionally populating |scripts_run_info|. | 66 // Injects the script, optionally populating |scripts_run_info|. |
| 67 void Inject(const InjectionHost* injection_host, | 67 void Inject(ScriptsRunInfo* scripts_run_info); |
| 68 ScriptsRunInfo* scripts_run_info); | |
| 69 | 68 |
| 70 // Inject any JS scripts into the |frame|, optionally populating | 69 // Inject any JS scripts into the |frame|, optionally populating |
| 71 // |execution_results|. | 70 // |execution_results|. |
| 72 void InjectJs(const InjectionHost* injection_host, | 71 void InjectJs(blink::WebLocalFrame* frame, |
| 73 blink::WebLocalFrame* frame, | |
| 74 base::ListValue* execution_results); | 72 base::ListValue* execution_results); |
| 75 | 73 |
| 76 // Inject any CSS source into the |frame|. | 74 // Inject any CSS source into the |frame|. |
| 77 void InjectCss(blink::WebLocalFrame* frame); | 75 void InjectCss(blink::WebLocalFrame* frame); |
| 78 | 76 |
| 79 // Notify that we will not inject, and mark it as acknowledged. | 77 // Notify that we will not inject, and mark it as acknowledged. |
| 80 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 78 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
| 81 | 79 |
| 82 // The injector for this injection. | 80 // The injector for this injection. |
| 83 scoped_ptr<ScriptInjector> injector_; | 81 scoped_ptr<ScriptInjector> injector_; |
| 84 | 82 |
| 85 // The (main) WebFrame into which this should inject the script. | 83 // The (main) WebFrame into which this should inject the script. |
| 86 blink::WebLocalFrame* web_frame_; | 84 blink::WebLocalFrame* web_frame_; |
| 87 | 85 |
| 88 // The id of the associated injection_host. | 86 // The associated injection host. |
| 89 HostID host_id_; | 87 scoped_ptr<const InjectionHost> injection_host_; |
| 90 | 88 |
| 91 // The type of the instance on which the host will inject the script. | 89 // The type of the instance on which the host will inject the script. |
| 92 UserScript::ConsumerInstanceType consumer_instance_type_; | 90 UserScript::ConsumerInstanceType consumer_instance_type_; |
| 93 | 91 |
| 94 // The location in the document load at which we inject the script. | 92 // The location in the document load at which we inject the script. |
| 95 UserScript::RunLocation run_location_; | 93 UserScript::RunLocation run_location_; |
| 96 | 94 |
| 97 // The tab id associated with the frame. | 95 // The tab id associated with the frame. |
| 98 int tab_id_; | 96 int tab_id_; |
| 99 | 97 |
| 100 // This injection's request id. This will be -1 unless the injection is | 98 // This injection's request id. This will be -1 unless the injection is |
| 101 // currently waiting on permission. | 99 // currently waiting on permission. |
| 102 int64 request_id_; | 100 int64 request_id_; |
| 103 | 101 |
| 104 // Whether or not the injection is complete, either via injecting the script | 102 // Whether or not the injection is complete, either via injecting the script |
| 105 // or because it will never complete. | 103 // or because it will never complete. |
| 106 bool complete_; | 104 bool complete_; |
| 107 | 105 |
| 108 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 106 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
| 109 }; | 107 }; |
| 110 | 108 |
| 111 } // namespace extensions | 109 } // namespace extensions |
| 112 | 110 |
| 113 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 111 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
| OLD | NEW |