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 "modules/serviceworkers/NavigatorServiceWorker.h" | 5 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/LocalDOMWindow.h" | 8 #include "core/frame/LocalDOMWindow.h" |
9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
10 #include "core/frame/Navigator.h" | 10 #include "core/frame/Navigator.h" |
11 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 11 #include "modules/serviceworkers/ServiceWorkerContainer.h" |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 NavigatorServiceWorker::NavigatorServiceWorker(Navigator& navigator) | 15 NavigatorServiceWorker::NavigatorServiceWorker(Navigator& navigator) |
16 : DOMWindowProperty(navigator.frame()) | 16 : ContextLifecycleObserver(navigator.frame()->document()) |
17 { | 17 { |
18 } | 18 } |
19 | 19 |
20 NavigatorServiceWorker* NavigatorServiceWorker::from(Document& document) | 20 NavigatorServiceWorker* NavigatorServiceWorker::from(Document& document) |
21 { | 21 { |
22 if (!document.frame() || !document.frame()->domWindow()) | 22 if (!document.frame() || !document.frame()->domWindow()) |
23 return nullptr; | 23 return nullptr; |
24 Navigator& navigator = *document.frame()->domWindow()->navigator(); | 24 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
25 return &from(navigator); | 25 return &from(navigator); |
26 } | 26 } |
27 | 27 |
28 NavigatorServiceWorker& NavigatorServiceWorker::from(Navigator& navigator) | 28 NavigatorServiceWorker& NavigatorServiceWorker::from(Navigator& navigator) |
29 { | 29 { |
30 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); | 30 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); |
31 if (!supplement) { | 31 if (!supplement) { |
32 supplement = new NavigatorServiceWorker(navigator); | 32 supplement = new NavigatorServiceWorker(navigator); |
33 provideTo(navigator, supplementName(), supplement); | 33 provideTo(navigator, supplementName(), supplement); |
34 if (navigator.frame() && navigator.frame()->securityContext()->getSecuri
tyOrigin()->canAccessServiceWorkers()) { | 34 if (navigator.frame() && navigator.frame()->securityContext()->getSecuri
tyOrigin()->canAccessServiceWorkers()) { |
35 // Initialize ServiceWorkerContainer too. | 35 // Initialize ServiceWorkerContainer too. |
36 supplement->serviceWorker(ASSERT_NO_EXCEPTION); | 36 supplement->serviceWorker(navigator.frame(), ASSERT_NO_EXCEPTION); |
37 } | 37 } |
38 } | 38 } |
39 return *supplement; | 39 return *supplement; |
40 } | 40 } |
41 | 41 |
42 NavigatorServiceWorker* NavigatorServiceWorker::toNavigatorServiceWorker(Navigat
or& navigator) | 42 NavigatorServiceWorker* NavigatorServiceWorker::toNavigatorServiceWorker(Navigat
or& navigator) |
43 { | 43 { |
44 return static_cast<NavigatorServiceWorker*>(Supplement<Navigator>::from(navi
gator, supplementName())); | 44 return static_cast<NavigatorServiceWorker*>(Supplement<Navigator>::from(navi
gator, supplementName())); |
45 } | 45 } |
46 | 46 |
47 const char* NavigatorServiceWorker::supplementName() | 47 const char* NavigatorServiceWorker::supplementName() |
48 { | 48 { |
49 return "NavigatorServiceWorker"; | 49 return "NavigatorServiceWorker"; |
50 } | 50 } |
51 | 51 |
52 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(ExecutionContext*
executionContext, Navigator& navigator, ExceptionState& exceptionState) | 52 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(ExecutionContext*
executionContext, Navigator& navigator, ExceptionState& exceptionState) |
53 { | 53 { |
54 ASSERT(!navigator.frame() || executionContext->getSecurityOrigin()->canAcces
sCheckSuborigins(navigator.frame()->securityContext()->getSecurityOrigin())); | 54 DCHECK(!navigator.frame() || executionContext->getSecurityOrigin()->canAcces
sCheckSuborigins(navigator.frame()->securityContext()->getSecurityOrigin())); |
55 return NavigatorServiceWorker::from(navigator).serviceWorker(exceptionState)
; | 55 return NavigatorServiceWorker::from(navigator).serviceWorker(navigator.frame
(), exceptionState); |
56 } | 56 } |
57 | 57 |
58 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(ExceptionState& ex
ceptionState) | 58 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(LocalFrame* frame,
ExceptionState& exceptionState) |
59 { | 59 { |
60 if (frame() && !frame()->securityContext()->getSecurityOrigin()->canAccessSe
rviceWorkers()) { | 60 if (frame && !frame->securityContext()->getSecurityOrigin()->canAccessServic
eWorkers()) { |
61 if (frame()->securityContext()->isSandboxed(SandboxOrigin)) | 61 if (frame->securityContext()->isSandboxed(SandboxOrigin)) |
62 exceptionState.throwSecurityError("Service worker is disabled becaus
e the context is sandboxed and lacks the 'allow-same-origin' flag."); | 62 exceptionState.throwSecurityError("Service worker is disabled becaus
e the context is sandboxed and lacks the 'allow-same-origin' flag."); |
63 else | 63 else |
64 exceptionState.throwSecurityError("Access to service workers is deni
ed in this document origin."); | 64 exceptionState.throwSecurityError("Access to service workers is deni
ed in this document origin."); |
65 return nullptr; | 65 return nullptr; |
66 } | 66 } |
67 if (!m_serviceWorker && frame()) { | 67 if (!m_serviceWorker && frame) { |
68 ASSERT(frame()->domWindow()); | 68 DCHECK(frame->domWindow()); |
69 m_serviceWorker = ServiceWorkerContainer::create(frame()->domWindow()->g
etExecutionContext()); | 69 m_serviceWorker = ServiceWorkerContainer::create(frame->domWindow()->get
ExecutionContext()); |
70 } | 70 } |
71 return m_serviceWorker.get(); | 71 return m_serviceWorker.get(); |
72 } | 72 } |
73 | 73 |
74 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame() | 74 void NavigatorServiceWorker::contextDestroyed() |
75 { | 75 { |
76 if (m_serviceWorker) { | 76 if (m_serviceWorker) { |
77 m_serviceWorker->willBeDetachedFromFrame(); | 77 m_serviceWorker->willBeDetachedFromFrame(); |
78 m_serviceWorker = nullptr; | 78 m_serviceWorker = nullptr; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 DEFINE_TRACE(NavigatorServiceWorker) | 82 DEFINE_TRACE(NavigatorServiceWorker) |
83 { | 83 { |
84 visitor->trace(m_serviceWorker); | 84 visitor->trace(m_serviceWorker); |
85 Supplement<Navigator>::trace(visitor); | 85 Supplement<Navigator>::trace(visitor); |
86 DOMWindowProperty::trace(visitor); | 86 ContextLifecycleObserver::trace(visitor); |
87 } | 87 } |
88 | 88 |
89 } // namespace blink | 89 } // namespace blink |
OLD | NEW |