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 #include "extensions/renderer/dispatcher.h" | 5 #include "extensions/renderer/dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 212 } |
213 | 213 |
214 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const { | 214 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const { |
215 bool is_active = | 215 bool is_active = |
216 active_extension_ids_.find(extension_id) != active_extension_ids_.end(); | 216 active_extension_ids_.find(extension_id) != active_extension_ids_.end(); |
217 if (is_active) | 217 if (is_active) |
218 CHECK(extensions_.Contains(extension_id)); | 218 CHECK(extensions_.Contains(extension_id)); |
219 return is_active; | 219 return is_active; |
220 } | 220 } |
221 | 221 |
222 std::string Dispatcher::GetExtensionID(const WebFrame* frame, int world_id) { | 222 const Extension* Dispatcher::GetExtensionFromFrameAndWorld( |
| 223 const WebFrame* frame, |
| 224 int world_id, |
| 225 bool use_effective_url) { |
| 226 std::string extension_id; |
223 if (world_id != 0) { | 227 if (world_id != 0) { |
224 // Isolated worlds (content script). | 228 // Isolated worlds (content script). |
225 return ScriptInjection::GetExtensionIdForIsolatedWorld(world_id); | 229 extension_id = ScriptInjection::GetExtensionIdForIsolatedWorld(world_id); |
| 230 } else if (!frame->document().securityOrigin().isUnique()) { |
| 231 // TODO(kalman): Delete the above check. |
| 232 |
| 233 // Extension pages (chrome-extension:// URLs). |
| 234 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); |
| 235 frame_url = ScriptContext::GetEffectiveDocumentURL( |
| 236 frame, frame_url, use_effective_url); |
| 237 extension_id = extensions_.GetExtensionOrAppIDByURL(frame_url); |
226 } | 238 } |
227 | 239 |
228 // TODO(kalman): Delete this check. | |
229 if (frame->document().securityOrigin().isUnique()) | |
230 return std::string(); | |
231 | |
232 // Extension pages (chrome-extension:// URLs). | |
233 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); | |
234 return extensions_.GetExtensionOrAppIDByURL(frame_url); | |
235 } | |
236 | |
237 void Dispatcher::DidCreateScriptContext( | |
238 WebFrame* frame, | |
239 const v8::Handle<v8::Context>& v8_context, | |
240 int extension_group, | |
241 int world_id) { | |
242 #if !defined(ENABLE_EXTENSIONS) | |
243 return; | |
244 #endif | |
245 | |
246 std::string extension_id = GetExtensionID(frame, world_id); | |
247 | |
248 const Extension* extension = extensions_.GetByID(extension_id); | 240 const Extension* extension = extensions_.GetByID(extension_id); |
249 if (!extension && !extension_id.empty()) { | 241 if (!extension && !extension_id.empty()) { |
250 // There are conditions where despite a context being associated with an | 242 // There are conditions where despite a context being associated with an |
251 // extension, no extension actually gets found. Ignore "invalid" because | 243 // extension, no extension actually gets found. Ignore "invalid" because |
252 // CSP blocks extension page loading by switching the extension ID to | 244 // CSP blocks extension page loading by switching the extension ID to |
253 // "invalid". This isn't interesting. | 245 // "invalid". This isn't interesting. |
254 if (extension_id != "invalid") { | 246 if (extension_id != "invalid") { |
255 LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; | 247 LOG(ERROR) << "Extension \"" << extension_id << "\" not found"; |
256 RenderThread::Get()->RecordAction( | 248 RenderThread::Get()->RecordAction( |
257 UserMetricsAction("ExtensionNotFound_ED")); | 249 UserMetricsAction("ExtensionNotFound_ED")); |
258 } | 250 } |
| 251 } |
| 252 return extension; |
| 253 } |
259 | 254 |
260 extension_id = ""; | 255 void Dispatcher::DidCreateScriptContext( |
261 } | 256 WebFrame* frame, |
| 257 const v8::Handle<v8::Context>& v8_context, |
| 258 int extension_group, |
| 259 int world_id) { |
| 260 #if !defined(ENABLE_EXTENSIONS) |
| 261 return; |
| 262 #endif |
262 | 263 |
| 264 const Extension* extension = |
| 265 GetExtensionFromFrameAndWorld(frame, world_id, false); |
| 266 const Extension* effective_extension = |
| 267 GetExtensionFromFrameAndWorld(frame, world_id, true); |
| 268 |
| 269 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); |
263 Feature::Context context_type = | 270 Feature::Context context_type = |
264 ClassifyJavaScriptContext(extension, | 271 ClassifyJavaScriptContext(extension, |
265 extension_group, | 272 extension_group, |
266 ScriptContext::GetDataSourceURLForFrame(frame), | 273 frame_url, |
267 frame->document().securityOrigin()); | 274 frame->document().securityOrigin()); |
| 275 Feature::Context effective_context_type = ClassifyJavaScriptContext( |
| 276 effective_extension, |
| 277 extension_group, |
| 278 ScriptContext::GetEffectiveDocumentURL(frame, frame_url, true), |
| 279 frame->document().securityOrigin()); |
268 | 280 |
269 ScriptContext* context = | 281 ScriptContext* context = |
270 delegate_->CreateScriptContext(v8_context, frame, extension, context_type) | 282 delegate_->CreateScriptContext(v8_context, |
271 .release(); | 283 frame, |
| 284 extension, |
| 285 context_type, |
| 286 effective_extension, |
| 287 effective_context_type).release(); |
272 script_context_set_.Add(context); | 288 script_context_set_.Add(context); |
273 | 289 |
274 // Initialize origin permissions for content scripts, which can't be | 290 // Initialize origin permissions for content scripts, which can't be |
275 // initialized in |OnActivateExtension|. | 291 // initialized in |OnActivateExtension|. |
276 if (context_type == Feature::CONTENT_SCRIPT_CONTEXT) | 292 if (context_type == Feature::CONTENT_SCRIPT_CONTEXT) |
277 InitOriginPermissions(extension); | 293 InitOriginPermissions(extension); |
278 | 294 |
279 { | 295 { |
280 scoped_ptr<ModuleSystem> module_system( | 296 scoped_ptr<ModuleSystem> module_system( |
281 new ModuleSystem(context, &source_map_)); | 297 new ModuleSystem(context, &source_map_)); |
(...skipping 1018 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1300 return v8::Handle<v8::Object>(); | 1316 return v8::Handle<v8::Object>(); |
1301 | 1317 |
1302 if (bind_name) | 1318 if (bind_name) |
1303 *bind_name = split.back(); | 1319 *bind_name = split.back(); |
1304 | 1320 |
1305 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context)) | 1321 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context)) |
1306 : bind_object; | 1322 : bind_object; |
1307 } | 1323 } |
1308 | 1324 |
1309 } // namespace extensions | 1325 } // namespace extensions |
OLD | NEW |