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