Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: extensions/renderer/script_injection.cc

Issue 288053002: Block content scripts from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CQ Time! Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « extensions/renderer/script_injection.h ('k') | extensions/renderer/user_script_slave.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "extensions/renderer/script_injection.h" 5 #include "extensions/renderer/script_injection.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "content/public/common/url_constants.h" 11 #include "content/public/common/url_constants.h"
12 #include "content/public/renderer/render_view.h"
12 #include "extensions/common/extension.h" 13 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_messages.h" 14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/feature_switch.h"
14 #include "extensions/common/permissions/permissions_data.h" 16 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/renderer/dom_activity_logger.h" 17 #include "extensions/renderer/dom_activity_logger.h"
16 #include "extensions/renderer/extension_groups.h" 18 #include "extensions/renderer/extension_groups.h"
19 #include "extensions/renderer/extension_helper.h"
17 #include "extensions/renderer/script_context.h" 20 #include "extensions/renderer/script_context.h"
18 #include "extensions/renderer/user_script_slave.h" 21 #include "extensions/renderer/user_script_slave.h"
19 #include "grit/renderer_resources.h" 22 #include "grit/renderer_resources.h"
20 #include "third_party/WebKit/public/web/WebDocument.h" 23 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebFrame.h" 24 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebScriptSource.h" 25 #include "third_party/WebKit/public/web/WebScriptSource.h"
26 #include "third_party/WebKit/public/web/WebView.h"
23 #include "ui/base/resource/resource_bundle.h" 27 #include "ui/base/resource/resource_bundle.h"
24 #include "url/gurl.h" 28 #include "url/gurl.h"
25 29
26 namespace extensions { 30 namespace extensions {
27 31
28 namespace { 32 namespace {
29 33
34 // The id of the next pending injection.
35 int64 g_next_pending_id = 0;
36
37 // The number of an invalid request, which is used if the feature to delay
38 // script injection is not enabled.
39 const int64 kInvalidRequestId = -1;
40
30 // These two strings are injected before and after the Greasemonkey API and 41 // These two strings are injected before and after the Greasemonkey API and
31 // user script to wrap it in an anonymous scope. 42 // user script to wrap it in an anonymous scope.
32 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 43 const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
33 const char kUserScriptTail[] = "\n})(window);"; 44 const char kUserScriptTail[] = "\n})(window);";
34 45
35 // Greasemonkey API source that is injected with the scripts. 46 // Greasemonkey API source that is injected with the scripts.
36 struct GreasemonkeyApiJsString { 47 struct GreasemonkeyApiJsString {
37 GreasemonkeyApiJsString(); 48 GreasemonkeyApiJsString();
38 blink::WebScriptSource source; 49 blink::WebScriptSource source;
39 }; 50 };
(...skipping 10 matching lines...) Expand all
50 LAZY_INSTANCE_INITIALIZER; 61 LAZY_INSTANCE_INITIALIZER;
51 62
52 } // namespace 63 } // namespace
53 64
54 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) { 65 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) {
55 } 66 }
56 67
57 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() { 68 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() {
58 } 69 }
59 70
71 struct ScriptInjection::PendingInjection {
72 PendingInjection(blink::WebFrame* web_frame,
73 UserScript::RunLocation run_location,
74 int page_id);
75 ~PendingInjection();
76
77 // The globally-unique id of this request.
78 int64 id;
79
80 // The pointer to the web frame into which the script should be injected.
81 // This is weak, but safe because we remove pending requests when a frame is
82 // terminated.
83 blink::WebFrame* web_frame;
84
85 // The run location to inject at.
86 // Note: This could be a lie - we might inject well after this run location
87 // has come and gone. But we need to know it to know which scripts to inject.
88 UserScript::RunLocation run_location;
89
90 // The corresponding page id, to protect against races.
91 int page_id;
92 };
93
94 ScriptInjection::PendingInjection::PendingInjection(
95 blink::WebFrame* web_frame,
96 UserScript::RunLocation run_location,
97 int page_id)
98 : id(g_next_pending_id++),
99 web_frame(web_frame),
100 run_location(run_location),
101 page_id(page_id) {
102 }
103
104 ScriptInjection::PendingInjection::~PendingInjection() {
105 }
106
60 // static 107 // static
61 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { 108 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) {
62 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); 109 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
63 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { 110 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
64 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + 111 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
65 data_source_url.spec()); 112 data_source_url.spec());
66 } 113 }
67 114
68 return data_source_url; 115 return data_source_url;
69 } 116 }
70 117
71 ScriptInjection::ScriptInjection( 118 ScriptInjection::ScriptInjection(
72 scoped_ptr<UserScript> script, 119 scoped_ptr<UserScript> script,
73 UserScriptSlave* user_script_slave) 120 UserScriptSlave* user_script_slave)
74 : script_(script.Pass()), 121 : script_(script.Pass()),
75 extension_id_(script_->extension_id()), 122 extension_id_(script_->extension_id()),
76 user_script_slave_(user_script_slave), 123 user_script_slave_(user_script_slave),
77 is_standalone_or_emulate_greasemonkey_( 124 is_standalone_or_emulate_greasemonkey_(
78 script_->is_standalone() || script_->emulate_greasemonkey()) { 125 script_->is_standalone() || script_->emulate_greasemonkey()) {
79 } 126 }
80 127
81 ScriptInjection::~ScriptInjection() { 128 ScriptInjection::~ScriptInjection() {
82 } 129 }
83 130
131 void ScriptInjection::InjectIfAllowed(blink::WebFrame* frame,
132 UserScript::RunLocation run_location,
133 const GURL& document_url,
134 ScriptsRunInfo* scripts_run_info) {
135 if (!WantsToRun(frame, run_location, document_url))
136 return;
137
138 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
139 DCHECK(extension); // WantsToRun() should be false if there's no extension.
140
141 // We use the top render view here (instead of the render view for the
142 // frame), because script injection on any frame requires permission for
143 // the top frame. Additionally, if we have to show any UI for permissions,
144 // it should only be done on the top frame.
145 content::RenderView* top_render_view =
146 content::RenderView::FromWebView(frame->top()->view());
147
148 int tab_id = ExtensionHelper::Get(top_render_view)->tab_id();
149
150 // By default, we allow injection.
151 bool should_inject = true;
152
153 // Check if the extension requires user consent for injection *and* we have a
154 // valid tab id (if we don't have a tab id, we have no UI surface to ask for
155 // user consent).
156 if (tab_id != -1 &&
157 PermissionsData::RequiresActionForScriptExecution(
158 extension,
159 tab_id,
160 frame->top()->document().url())) {
161 int64 request_id = kInvalidRequestId;
162 int page_id = top_render_view->GetPageId();
163
164 // We only delay the injection if the feature is enabled.
165 // Otherwise, we simply treat this as a notification by passing an invalid
166 // id.
167 if (FeatureSwitch::scripts_require_action()->IsEnabled()) {
168 should_inject = false;
169 ScopedVector<PendingInjection>::iterator pending_injection =
170 pending_injections_.insert(
171 pending_injections_.end(),
172 new PendingInjection(frame, run_location, page_id));
173 request_id = (*pending_injection)->id;
174 }
175
176 top_render_view->Send(
177 new ExtensionHostMsg_RequestContentScriptPermission(
178 top_render_view->GetRoutingID(),
179 extension->id(),
180 page_id,
181 request_id));
182 }
183
184 if (should_inject)
185 Inject(frame, run_location, scripts_run_info);
186 }
187
188 bool ScriptInjection::NotifyScriptPermitted(
189 int64 request_id,
190 content::RenderView* render_view,
191 ScriptsRunInfo* scripts_run_info,
192 blink::WebFrame** frame_out) {
193 ScopedVector<PendingInjection>::iterator iter = pending_injections_.begin();
194 while (iter != pending_injections_.end() && (*iter)->id != request_id)
195 ++iter;
196
197 // No matching request.
198 if (iter == pending_injections_.end())
199 return false;
200
201 // We found the request, so pull it out of the pending list.
202 scoped_ptr<PendingInjection> pending_injection(*iter);
203 pending_injections_.weak_erase(iter);
204
205 // Ensure the Page ID and Extension are still valid. Otherwise, don't inject.
206 if (render_view->GetPageId() != pending_injection->page_id)
207 return false;
208
209 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
210 if (!extension)
211 return false;
212
213 // Everything matches! Inject the script.
214 if (frame_out)
215 *frame_out = pending_injection->web_frame;
216 Inject(pending_injection->web_frame,
217 pending_injection->run_location,
218 scripts_run_info);
219 return true;
220 }
221
222 void ScriptInjection::FrameDetached(blink::WebFrame* frame) {
223 // Any pending injections associated with the given frame will never run.
224 // Remove them.
225 for (ScopedVector<PendingInjection>::iterator iter =
226 pending_injections_.begin();
227 iter != pending_injections_.end();) {
228 if ((*iter)->web_frame == frame)
229 pending_injections_.erase(iter);
230 else
231 ++iter;
232 }
233 }
234
84 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, 235 bool ScriptInjection::WantsToRun(blink::WebFrame* frame,
85 UserScript::RunLocation run_location, 236 UserScript::RunLocation run_location,
86 const GURL& document_url) const { 237 const GURL& document_url) const {
87 if (frame->parent() && !script_->match_all_frames()) 238 if (frame->parent() && !script_->match_all_frames())
88 return false; // Only match subframes if the script declared it wanted to. 239 return false; // Only match subframes if the script declared it wanted to.
89 240
90 const Extension* extension = user_script_slave_->GetExtension(extension_id_); 241 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
91 // Since extension info is sent separately from user script info, they can 242 // Since extension info is sent separately from user script info, they can
92 // be out of sync. We just ignore this situation. 243 // be out of sync. We just ignore this situation.
93 if (!extension) 244 if (!extension)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 scripts_run_info->num_css += css_scripts.size(); 344 scripts_run_info->num_css += css_scripts.size();
194 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 345 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
195 iter != css_scripts.end(); 346 iter != css_scripts.end();
196 ++iter) { 347 ++iter) {
197 frame->document().insertStyleSheet( 348 frame->document().insertStyleSheet(
198 blink::WebString::fromUTF8(iter->GetContent().as_string())); 349 blink::WebString::fromUTF8(iter->GetContent().as_string()));
199 } 350 }
200 } 351 }
201 352
202 } // namespace extensions 353 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/script_injection.h ('k') | extensions/renderer/user_script_slave.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698