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 "config.h" | 5 #include "config.h" |
6 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 6 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
7 | 7 |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 Navigator& navigator = *document.frame()->domWindow()->navigator(); | 29 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
30 return &from(navigator); | 30 return &from(navigator); |
31 } | 31 } |
32 | 32 |
33 NavigatorServiceWorker& NavigatorServiceWorker::from(Navigator& navigator) | 33 NavigatorServiceWorker& NavigatorServiceWorker::from(Navigator& navigator) |
34 { | 34 { |
35 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); | 35 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); |
36 if (!supplement) { | 36 if (!supplement) { |
37 supplement = new NavigatorServiceWorker(navigator); | 37 supplement = new NavigatorServiceWorker(navigator); |
38 provideTo(navigator, supplementName(), supplement); | 38 provideTo(navigator, supplementName(), supplement); |
39 // Initialize ServiceWorkerContainer too. | 39 if (navigator.frame() && navigator.frame()->securityContext()->securityO
rigin()->canAccessServiceWorkers()) { |
40 supplement->serviceWorker(); | 40 // Initialize ServiceWorkerContainer too. |
| 41 supplement->serviceWorker(ASSERT_NO_EXCEPTION); |
| 42 } |
41 } | 43 } |
42 return *supplement; | 44 return *supplement; |
43 } | 45 } |
44 | 46 |
45 NavigatorServiceWorker* NavigatorServiceWorker::toNavigatorServiceWorker(Navigat
or& navigator) | 47 NavigatorServiceWorker* NavigatorServiceWorker::toNavigatorServiceWorker(Navigat
or& navigator) |
46 { | 48 { |
47 return static_cast<NavigatorServiceWorker*>(HeapSupplement<Navigator>::from(
navigator, supplementName())); | 49 return static_cast<NavigatorServiceWorker*>(HeapSupplement<Navigator>::from(
navigator, supplementName())); |
48 } | 50 } |
49 | 51 |
50 const char* NavigatorServiceWorker::supplementName() | 52 const char* NavigatorServiceWorker::supplementName() |
51 { | 53 { |
52 return "NavigatorServiceWorker"; | 54 return "NavigatorServiceWorker"; |
53 } | 55 } |
54 | 56 |
55 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(Navigator& navigat
or) | 57 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(Navigator& navigat
or, ExceptionState& exceptionState) |
56 { | 58 { |
57 return NavigatorServiceWorker::from(navigator).serviceWorker(); | 59 return NavigatorServiceWorker::from(navigator).serviceWorker(exceptionState)
; |
58 } | 60 } |
59 | 61 |
60 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker() | 62 ServiceWorkerContainer* NavigatorServiceWorker::serviceWorker(ExceptionState& ex
ceptionState) |
61 { | 63 { |
| 64 if (frame() && !frame()->securityContext()->securityOrigin()->canAccessServi
ceWorkers()) { |
| 65 if (frame()->securityContext()->isSandboxed(SandboxOrigin)) |
| 66 exceptionState.throwSecurityError("Service worker is disabled becaus
e the context is sandboxed and lacks the 'allow-same-origin' flag."); |
| 67 else |
| 68 exceptionState.throwSecurityError("Access to service workers is deni
ed in this document origin."); |
| 69 return nullptr; |
| 70 } |
62 if (!m_serviceWorker && frame()) { | 71 if (!m_serviceWorker && frame()) { |
63 ASSERT(frame()->domWindow()); | 72 ASSERT(frame()->domWindow()); |
64 m_serviceWorker = ServiceWorkerContainer::create(frame()->domWindow()->e
xecutionContext()); | 73 m_serviceWorker = ServiceWorkerContainer::create(frame()->domWindow()->e
xecutionContext()); |
65 } | 74 } |
66 return m_serviceWorker.get(); | 75 return m_serviceWorker.get(); |
67 } | 76 } |
68 | 77 |
69 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame() | 78 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame() |
70 { | 79 { |
71 if (m_serviceWorker) { | 80 if (m_serviceWorker) { |
72 m_serviceWorker->willBeDetachedFromFrame(); | 81 m_serviceWorker->willBeDetachedFromFrame(); |
73 m_serviceWorker = nullptr; | 82 m_serviceWorker = nullptr; |
74 } | 83 } |
75 } | 84 } |
76 | 85 |
77 DEFINE_TRACE(NavigatorServiceWorker) | 86 DEFINE_TRACE(NavigatorServiceWorker) |
78 { | 87 { |
79 visitor->trace(m_serviceWorker); | 88 visitor->trace(m_serviceWorker); |
80 HeapSupplement<Navigator>::trace(visitor); | 89 HeapSupplement<Navigator>::trace(visitor); |
81 DOMWindowProperty::trace(visitor); | 90 DOMWindowProperty::trace(visitor); |
82 } | 91 } |
83 | 92 |
84 } // namespace blink | 93 } // namespace blink |
OLD | NEW |