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

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

Issue 307933008: Revert 273866 "Block content scripts from executing until user g..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: 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
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"
13 #include "extensions/common/extension.h" 12 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_messages.h" 13 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/feature_switch.h"
16 #include "extensions/common/permissions/permissions_data.h" 14 #include "extensions/common/permissions/permissions_data.h"
17 #include "extensions/renderer/dom_activity_logger.h" 15 #include "extensions/renderer/dom_activity_logger.h"
18 #include "extensions/renderer/extension_groups.h" 16 #include "extensions/renderer/extension_groups.h"
19 #include "extensions/renderer/extension_helper.h"
20 #include "extensions/renderer/script_context.h" 17 #include "extensions/renderer/script_context.h"
21 #include "extensions/renderer/user_script_slave.h" 18 #include "extensions/renderer/user_script_slave.h"
22 #include "grit/renderer_resources.h" 19 #include "grit/renderer_resources.h"
23 #include "third_party/WebKit/public/web/WebDocument.h" 20 #include "third_party/WebKit/public/web/WebDocument.h"
24 #include "third_party/WebKit/public/web/WebFrame.h" 21 #include "third_party/WebKit/public/web/WebFrame.h"
25 #include "third_party/WebKit/public/web/WebScriptSource.h" 22 #include "third_party/WebKit/public/web/WebScriptSource.h"
26 #include "third_party/WebKit/public/web/WebView.h"
27 #include "ui/base/resource/resource_bundle.h" 23 #include "ui/base/resource/resource_bundle.h"
28 #include "url/gurl.h" 24 #include "url/gurl.h"
29 25
30 namespace extensions { 26 namespace extensions {
31 27
32 namespace { 28 namespace {
33 29
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
41 // These two strings are injected before and after the Greasemonkey API and 30 // These two strings are injected before and after the Greasemonkey API and
42 // user script to wrap it in an anonymous scope. 31 // user script to wrap it in an anonymous scope.
43 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 32 const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
44 const char kUserScriptTail[] = "\n})(window);"; 33 const char kUserScriptTail[] = "\n})(window);";
45 34
46 // Greasemonkey API source that is injected with the scripts. 35 // Greasemonkey API source that is injected with the scripts.
47 struct GreasemonkeyApiJsString { 36 struct GreasemonkeyApiJsString {
48 GreasemonkeyApiJsString(); 37 GreasemonkeyApiJsString();
49 blink::WebScriptSource source; 38 blink::WebScriptSource source;
50 }; 39 };
(...skipping 10 matching lines...) Expand all
61 LAZY_INSTANCE_INITIALIZER; 50 LAZY_INSTANCE_INITIALIZER;
62 51
63 } // namespace 52 } // namespace
64 53
65 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) { 54 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) {
66 } 55 }
67 56
68 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() { 57 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() {
69 } 58 }
70 59
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
107 // static 60 // static
108 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { 61 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) {
109 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); 62 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
110 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { 63 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
111 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + 64 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
112 data_source_url.spec()); 65 data_source_url.spec());
113 } 66 }
114 67
115 return data_source_url; 68 return data_source_url;
116 } 69 }
117 70
118 ScriptInjection::ScriptInjection( 71 ScriptInjection::ScriptInjection(
119 scoped_ptr<UserScript> script, 72 scoped_ptr<UserScript> script,
120 UserScriptSlave* user_script_slave) 73 UserScriptSlave* user_script_slave)
121 : script_(script.Pass()), 74 : script_(script.Pass()),
122 extension_id_(script_->extension_id()), 75 extension_id_(script_->extension_id()),
123 user_script_slave_(user_script_slave), 76 user_script_slave_(user_script_slave),
124 is_standalone_or_emulate_greasemonkey_( 77 is_standalone_or_emulate_greasemonkey_(
125 script_->is_standalone() || script_->emulate_greasemonkey()) { 78 script_->is_standalone() || script_->emulate_greasemonkey()) {
126 } 79 }
127 80
128 ScriptInjection::~ScriptInjection() { 81 ScriptInjection::~ScriptInjection() {
129 } 82 }
130 83
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
235 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, 84 bool ScriptInjection::WantsToRun(blink::WebFrame* frame,
236 UserScript::RunLocation run_location, 85 UserScript::RunLocation run_location,
237 const GURL& document_url) const { 86 const GURL& document_url) const {
238 if (frame->parent() && !script_->match_all_frames()) 87 if (frame->parent() && !script_->match_all_frames())
239 return false; // Only match subframes if the script declared it wanted to. 88 return false; // Only match subframes if the script declared it wanted to.
240 89
241 const Extension* extension = user_script_slave_->GetExtension(extension_id_); 90 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
242 // Since extension info is sent separately from user script info, they can 91 // Since extension info is sent separately from user script info, they can
243 // be out of sync. We just ignore this situation. 92 // be out of sync. We just ignore this situation.
244 if (!extension) 93 if (!extension)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 scripts_run_info->num_css += css_scripts.size(); 193 scripts_run_info->num_css += css_scripts.size();
345 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 194 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
346 iter != css_scripts.end(); 195 iter != css_scripts.end();
347 ++iter) { 196 ++iter) {
348 frame->document().insertStyleSheet( 197 frame->document().insertStyleSheet(
349 blink::WebString::fromUTF8(iter->GetContent().as_string())); 198 blink::WebString::fromUTF8(iter->GetContent().as_string()));
350 } 199 }
351 } 200 }
352 201
353 } // namespace extensions 202 } // namespace extensions
OLDNEW
« no previous file with comments | « trunk/src/extensions/renderer/script_injection.h ('k') | trunk/src/extensions/renderer/user_script_slave.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698