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 <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/basictypes.h" | |
13 #include "base/macros.h" | 12 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/timer/elapsed_timer.h" | 14 #include "base/timer/elapsed_timer.h" |
17 #include "extensions/common/user_script.h" | 15 #include "extensions/common/user_script.h" |
18 | 16 |
19 class GURL; | 17 class GURL; |
20 | 18 |
21 namespace blink { | 19 namespace blink { |
22 class WebFrame; | 20 class WebFrame; |
23 } | 21 } |
24 | 22 |
25 namespace content { | |
26 class RenderView; | |
27 } | |
28 | |
29 namespace extensions { | 23 namespace extensions { |
30 class UserScriptSlave; | 24 class UserScriptSlave; |
31 | 25 |
32 // This class is a wrapper around a UserScript that knows how to inject itself | 26 // This class is a wrapper around a UserScript that knows how to inject itself |
33 // into a frame. | 27 // into a frame. |
34 class ScriptInjection { | 28 class ScriptInjection { |
35 public: | 29 public: |
36 // Map of extensions IDs to the executing script paths. | 30 // Map of extensions IDs to the executing script paths. |
37 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; | 31 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; |
38 | 32 |
(...skipping 16 matching lines...) Expand all Loading... |
55 }; | 49 }; |
56 | 50 |
57 // Return the URL to use as the document url when checking permissions for | 51 // Return the URL to use as the document url when checking permissions for |
58 // script injection. | 52 // script injection. |
59 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); | 53 static GURL GetDocumentUrlForFrame(blink::WebFrame* frame); |
60 | 54 |
61 ScriptInjection(scoped_ptr<UserScript> script, | 55 ScriptInjection(scoped_ptr<UserScript> script, |
62 UserScriptSlave* user_script_slave); | 56 UserScriptSlave* user_script_slave); |
63 ~ScriptInjection(); | 57 ~ScriptInjection(); |
64 | 58 |
65 // Inject the script into the given |frame| if the script should run on the | |
66 // frame and has permission to do so. If the script requires user consent, | |
67 // this will register a pending request to inject at a later time. | |
68 // If the script is run immediately, |scripts_run_info| is updated with | |
69 // information about the run. | |
70 void InjectIfAllowed(blink::WebFrame* frame, | |
71 UserScript::RunLocation location, | |
72 const GURL& document_url, | |
73 ScriptsRunInfo* scripts_run_info); | |
74 | |
75 // If a request with the given |request_id| exists, runs that request and | |
76 // modifies |scripts_run_info| with information about the run. Otherwise, does | |
77 // nothing. | |
78 // If |frame_out| is non-NULL and a script was run, |frame_out| will be | |
79 // populated with the frame in which the script was run. | |
80 // Returns true if the request was found *and* the script was run. | |
81 bool NotifyScriptPermitted(int64 request_id, | |
82 content::RenderView* render_view, | |
83 ScriptsRunInfo* scripts_run_info, | |
84 blink::WebFrame** frame_out); | |
85 | |
86 // Notififies the Injection that the frame has been detached (i.e. is about | |
87 // to be destroyed). | |
88 void FrameDetached(blink::WebFrame* frame); | |
89 | |
90 const std::string& extension_id() { return extension_id_; } | |
91 | |
92 private: | |
93 struct PendingInjection; | |
94 | |
95 // Returns true if this ScriptInjection wants to run on the given |frame| at | 59 // Returns true if this ScriptInjection wants to run on the given |frame| at |
96 // the given |run_location| (i.e., if this script would inject either JS or | 60 // the given |run_location| (i.e., if this script would inject either JS or |
97 // CSS). | 61 // CSS). |
98 bool WantsToRun(blink::WebFrame* frame, | 62 bool WantsToRun(blink::WebFrame* frame, |
99 UserScript::RunLocation run_location, | 63 UserScript::RunLocation run_location, |
100 const GURL& document_url) const; | 64 const GURL& document_url) const; |
101 | 65 |
102 // Returns true if the script will inject [css|js] at the given | |
103 // |run_location|. | |
104 bool ShouldInjectJS(UserScript::RunLocation run_location) const; | |
105 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; | |
106 | |
107 // Injects the script into the given |frame|, and updates |scripts_run_info| | 66 // Injects the script into the given |frame|, and updates |scripts_run_info| |
108 // information about the run. | 67 // information about the run. |
109 void Inject(blink::WebFrame* frame, | 68 void Inject(blink::WebFrame* frame, |
110 UserScript::RunLocation run_location, | 69 UserScript::RunLocation run_location, |
111 ScriptsRunInfo* scripts_run_info) const; | 70 ScriptsRunInfo* scripts_run_info) const; |
112 | 71 |
| 72 const std::string& extension_id() { return extension_id_; } |
| 73 |
| 74 private: |
| 75 // Returns true if the script will inject [css|js] at the given |
| 76 // |run_location|. |
| 77 bool ShouldInjectJS(UserScript::RunLocation run_location) const; |
| 78 bool ShouldInjectCSS(UserScript::RunLocation run_location) const; |
| 79 |
113 // Injects the [css|js] scripts into the frame, and stores the results of | 80 // Injects the [css|js] scripts into the frame, and stores the results of |
114 // the run in |scripts_run_info|. | 81 // the run in |scripts_run_info|. |
115 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; | 82 void InjectJS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) const; |
116 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) | 83 void InjectCSS(blink::WebFrame* frame, ScriptsRunInfo* scripts_run_info) |
117 const; | 84 const; |
118 | 85 |
119 // The UserScript this is injecting. | 86 // The UserScript this is injecting. |
120 scoped_ptr<UserScript> script_; | 87 scoped_ptr<UserScript> script_; |
121 | 88 |
122 // The associated extension's id. This is a safe const&, since it is owned by | 89 // The associated extension's id. This is a safe const&, since it is owned by |
123 // the |user_script_|. | 90 // the |user_script_|. |
124 const std::string& extension_id_; | 91 const std::string& extension_id_; |
125 | 92 |
126 // The associated UserScriptSlave. | 93 // The associated UserScriptSlave. |
127 // It's unfortunate that this is needed, but we use it to get the isolated | 94 // It's unfortunate that this is needed, but we use it to get the isolated |
128 // world ids and the associated extensions. | 95 // world ids and the associated extensions. |
129 // TODO(rdevlin.cronin): It would be nice to clean this up more. | 96 // TODO(rdevlin.cronin): It would be nice to clean this up more. |
130 UserScriptSlave* user_script_slave_; | 97 UserScriptSlave* user_script_slave_; |
131 | 98 |
132 // True if the script is a standalone script or emulates greasemonkey. | 99 // True if the script is a standalone script or emulates greasemonkey. |
133 bool is_standalone_or_emulate_greasemonkey_; | 100 bool is_standalone_or_emulate_greasemonkey_; |
134 | 101 |
135 ScopedVector<PendingInjection> pending_injections_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); | 102 DISALLOW_COPY_AND_ASSIGN(ScriptInjection); |
138 }; | 103 }; |
139 | 104 |
140 } // namespace extensions | 105 } // namespace extensions |
141 | 106 |
142 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ | 107 #endif // EXTENSIONS_RENDERER_SCRIPT_INJECTION_H_ |
OLD | NEW |