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/containers/scoped_ptr_map.h" | 10 #include "base/containers/scoped_ptr_map.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } | 192 } |
193 }; | 193 }; |
194 | 194 |
195 class ServiceWorkerScriptContextSet { | 195 class ServiceWorkerScriptContextSet { |
196 public: | 196 public: |
197 ServiceWorkerScriptContextSet() {} | 197 ServiceWorkerScriptContextSet() {} |
198 ~ServiceWorkerScriptContextSet() {} | 198 ~ServiceWorkerScriptContextSet() {} |
199 | 199 |
200 void Insert(const GURL& url, scoped_ptr<ScriptContext> context) { | 200 void Insert(const GURL& url, scoped_ptr<ScriptContext> context) { |
201 base::AutoLock lock(lock_); | 201 base::AutoLock lock(lock_); |
202 CHECK(script_contexts_.find(url) == script_contexts_.end()); | 202 scoped_ptr<ScriptContext> existing = script_contexts_.take_and_erase(url); |
| 203 // This should be CHECK(!existing), but can't until these ScriptContexts |
| 204 // are keyed on v8::Contexts rather than URLs. See crbug.com/525965. |
| 205 if (existing) |
| 206 existing->Invalidate(); |
203 script_contexts_.set(url, context.Pass()); | 207 script_contexts_.set(url, context.Pass()); |
204 } | 208 } |
205 | 209 |
206 void Remove(const GURL& url) { | 210 void Remove(const GURL& url) { |
207 base::AutoLock lock(lock_); | 211 base::AutoLock lock(lock_); |
208 scoped_ptr<ScriptContext> context = script_contexts_.take_and_erase(url); | 212 scoped_ptr<ScriptContext> context = script_contexts_.take_and_erase(url); |
209 CHECK(context); | 213 CHECK(context); |
210 context->Invalidate(); | 214 context->Invalidate(); |
211 } | 215 } |
212 | 216 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 355 |
352 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); | 356 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); |
353 } | 357 } |
354 | 358 |
355 // static | 359 // static |
356 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread( | 360 void Dispatcher::DidInitializeServiceWorkerContextOnWorkerThread( |
357 v8::Local<v8::Context> v8_context, | 361 v8::Local<v8::Context> v8_context, |
358 const GURL& url) { | 362 const GURL& url) { |
359 const base::TimeTicks start_time = base::TimeTicks::Now(); | 363 const base::TimeTicks start_time = base::TimeTicks::Now(); |
360 | 364 |
| 365 if (!url.SchemeIs(kExtensionScheme) && |
| 366 !url.SchemeIs(kExtensionResourceScheme)) { |
| 367 // Early-out if this isn't a chrome-extension:// or resource scheme, |
| 368 // because looking up the extension registry is unnecessary if it's not. |
| 369 // Checking this will also skip over hosted apps, which is the desired |
| 370 // behavior - hosted app service workers are not our concern. |
| 371 return; |
| 372 } |
| 373 |
361 const Extension* extension = | 374 const Extension* extension = |
362 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(url); | 375 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(url); |
363 | 376 |
364 if (!extension) | 377 if (!extension) |
365 return; | 378 return; |
366 | 379 |
367 ScriptContext* context = new ScriptContext( | 380 ScriptContext* context = new ScriptContext( |
368 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT, | 381 v8_context, nullptr, extension, Feature::SERVICE_WORKER_CONTEXT, |
369 extension, Feature::SERVICE_WORKER_CONTEXT); | 382 extension, Feature::SERVICE_WORKER_CONTEXT); |
370 | 383 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 // In fact |request_sender_| should really be owned by ScriptContext. | 423 // In fact |request_sender_| should really be owned by ScriptContext. |
411 request_sender_->InvalidateSource(context); | 424 request_sender_->InvalidateSource(context); |
412 | 425 |
413 script_context_set_->Remove(context); | 426 script_context_set_->Remove(context); |
414 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); | 427 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); |
415 } | 428 } |
416 | 429 |
417 // static | 430 // static |
418 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread( | 431 void Dispatcher::WillDestroyServiceWorkerContextOnWorkerThread( |
419 const GURL& url) { | 432 const GURL& url) { |
420 if (RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(url)) | 433 if (url.SchemeIs(kExtensionScheme) || |
| 434 url.SchemeIs(kExtensionResourceScheme)) { |
| 435 // See comment in DidInitializeServiceWorkerContextOnWorkerThread. |
421 g_service_worker_script_context_set.Get().Remove(url); | 436 g_service_worker_script_context_set.Get().Remove(url); |
| 437 } |
422 } | 438 } |
423 | 439 |
424 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { | 440 void Dispatcher::DidCreateDocumentElement(blink::WebLocalFrame* frame) { |
425 // Note: use GetEffectiveDocumentURL not just frame->document()->url() | 441 // Note: use GetEffectiveDocumentURL not just frame->document()->url() |
426 // so that this also injects the stylesheet on about:blank frames that | 442 // so that this also injects the stylesheet on about:blank frames that |
427 // are hosted in the extension process. | 443 // are hosted in the extension process. |
428 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 444 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
429 frame, frame->document().url(), true /* match_about_blank */); | 445 frame, frame->document().url(), true /* match_about_blank */); |
430 | 446 |
431 const Extension* extension = | 447 const Extension* extension = |
(...skipping 1137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1569 void Dispatcher::AddChannelSpecificFeatures() { | 1585 void Dispatcher::AddChannelSpecificFeatures() { |
1570 // chrome-extension: resources should be allowed to register a Service Worker. | 1586 // chrome-extension: resources should be allowed to register a Service Worker. |
1571 if (FeatureProvider::GetBehaviorFeature(BehaviorFeature::kServiceWorker) | 1587 if (FeatureProvider::GetBehaviorFeature(BehaviorFeature::kServiceWorker) |
1572 ->IsAvailableToEnvironment() | 1588 ->IsAvailableToEnvironment() |
1573 .is_available()) | 1589 .is_available()) |
1574 WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers( | 1590 WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers( |
1575 WebString::fromUTF8(kExtensionScheme)); | 1591 WebString::fromUTF8(kExtensionScheme)); |
1576 } | 1592 } |
1577 | 1593 |
1578 } // namespace extensions | 1594 } // namespace extensions |
OLD | NEW |