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! | 46 // NOTE: |injection_host| may be NULL, if the injection_host is removed! |
47 bool TryToInject(UserScript::RunLocation current_location, | 47 bool TryToInject(UserScript::RunLocation current_location, |
48 const InjectionHost* injection_host, | 48 const InjectionHost* injection_host, |
49 ScriptsRunInfo* scripts_run_info); | 49 ScriptsRunInfo* scripts_run_info); |
50 | 50 |
51 // Called when permission for the given injection has been granted. | 51 // Called when permission for the given injection has been granted. |
52 // Returns true if the injection ran. | 52 // Returns true if the injection ran. |
53 bool OnPermissionGranted(const InjectionHost* injection_host, | 53 bool OnPermissionGranted(const InjectionHost* injection_host, |
54 ScriptsRunInfo* scripts_run_info); | 54 ScriptsRunInfo* scripts_run_info); |
55 | 55 |
| 56 // Gets the associated injection host. If the host is an extension, we check |
| 57 // whether the extension exists first; and if the extension is gone, will |
| 58 // return a nullptr. |
| 59 const InjectionHost* GetInjectionHost(); |
| 60 |
56 // Accessors. | 61 // Accessors. |
57 blink::WebLocalFrame* web_frame() const { return web_frame_; } | 62 blink::WebLocalFrame* web_frame() const { return web_frame_; } |
58 const HostID& host_id() const { return host_id_; } | 63 const HostID& host_id() const { return injection_host_->id(); } |
59 int64 request_id() const { return request_id_; } | 64 int64 request_id() const { return request_id_; } |
60 | 65 |
61 private: | 66 private: |
62 // Send a message to the browser requesting permission to execute. | 67 // Send a message to the browser requesting permission to execute. |
63 void RequestPermission(); | 68 void RequestPermission(); |
64 | 69 |
65 // Injects the script, optionally populating |scripts_run_info|. | 70 // Injects the script, optionally populating |scripts_run_info|. |
66 void Inject(const InjectionHost* injection_host, | 71 void Inject(const InjectionHost* injection_host, |
67 ScriptsRunInfo* scripts_run_info); | 72 ScriptsRunInfo* scripts_run_info); |
68 | 73 |
69 // Inject any JS scripts into the |frame|, optionally populating | 74 // Inject any JS scripts into the |frame|, optionally populating |
70 // |execution_results|. | 75 // |execution_results|. |
71 void InjectJs(const InjectionHost* injection_host, | 76 void InjectJs(const InjectionHost* injection_host, |
72 blink::WebLocalFrame* frame, | 77 blink::WebLocalFrame* frame, |
73 base::ListValue* execution_results); | 78 base::ListValue* execution_results); |
74 | 79 |
75 // Inject any CSS source into the |frame|. | 80 // Inject any CSS source into the |frame|. |
76 void InjectCss(blink::WebLocalFrame* frame); | 81 void InjectCss(blink::WebLocalFrame* frame); |
77 | 82 |
78 // Notify that we will not inject, and mark it as acknowledged. | 83 // Notify that we will not inject, and mark it as acknowledged. |
79 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); | 84 void NotifyWillNotInject(ScriptInjector::InjectFailureReason reason); |
80 | 85 |
81 // The injector for this injection. | 86 // The injector for this injection. |
82 scoped_ptr<ScriptInjector> injector_; | 87 scoped_ptr<ScriptInjector> injector_; |
83 | 88 |
84 // The (main) WebFrame into which this should inject the script. | 89 // The (main) WebFrame into which this should inject the script. |
85 blink::WebLocalFrame* web_frame_; | 90 blink::WebLocalFrame* web_frame_; |
86 | 91 |
87 // The id of the associated injection_host. | 92 // The associated injection host. |
88 HostID host_id_; | 93 scoped_ptr<const InjectionHost> injection_host_; |
89 | 94 |
90 // The type of the instance on which the host will inject the script. | 95 // The type of the instance on which the host will inject the script. |
91 UserScript::ConsumerInstanceType consumer_instance_type_; | 96 UserScript::ConsumerInstanceType consumer_instance_type_; |
92 | 97 |
93 // The location in the document load at which we inject the script. | 98 // The location in the document load at which we inject the script. |
94 UserScript::RunLocation run_location_; | 99 UserScript::RunLocation run_location_; |
95 | 100 |
96 // The tab id associated with the frame. | 101 // The tab id associated with the frame. |
97 int tab_id_; | 102 int tab_id_; |
98 | 103 |
99 // This injection's request id. This will be -1 unless the injection is | 104 // This injection's request id. This will be -1 unless the injection is |
100 // currently waiting on permission. | 105 // currently waiting on permission. |
101 int64 request_id_; | 106 int64 request_id_; |
102 | 107 |
103 // Whether or not the injection is complete, either via injecting the script | 108 // Whether or not the injection is complete, either via injecting the script |
104 // or because it will never complete. | 109 // or because it will never complete. |
105 bool complete_; | 110 bool complete_; |
106 | 111 |
107 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 112 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
108 }; | 113 }; |
109 | 114 |
110 } // namespace extensions | 115 } // namespace extensions |
111 | 116 |
112 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 117 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |