OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 #include "config.h" | 30 #include "config.h" |
31 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 31 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
32 | 32 |
33 #include "RuntimeEnabledFeatures.h" | 33 #include "RuntimeEnabledFeatures.h" |
34 #include "V8ServiceWorker.h" | 34 #include "V8ServiceWorker.h" |
35 #include "bindings/v8/ScriptPromiseResolver.h" | 35 #include "bindings/v8/ScriptPromiseResolver.h" |
36 #include "core/dom/Document.h" | 36 #include "core/dom/Document.h" |
| 37 #include "core/dom/ExceptionCode.h" |
37 #include "core/loader/DocumentLoader.h" | 38 #include "core/loader/DocumentLoader.h" |
38 #include "core/loader/FrameLoaderClient.h" | 39 #include "core/loader/FrameLoaderClient.h" |
39 #include "core/frame/Frame.h" | 40 #include "core/frame/Frame.h" |
40 #include "core/frame/Navigator.h" | 41 #include "core/frame/Navigator.h" |
41 #include "core/workers/SharedWorker.h" | 42 #include "core/workers/SharedWorker.h" |
42 #include "modules/serviceworkers/CallbackPromiseAdapter.h" | 43 #include "modules/serviceworkers/CallbackPromiseAdapter.h" |
43 #include "modules/serviceworkers/ServiceWorker.h" | 44 #include "modules/serviceworkers/ServiceWorker.h" |
44 #include "public/platform/WebServiceWorkerRegistry.h" | 45 #include "public/platform/WebServiceWorkerProvider.h" |
| 46 #include "public/platform/WebServiceWorkerProviderClient.h" |
45 #include "public/platform/WebString.h" | 47 #include "public/platform/WebString.h" |
46 #include "public/platform/WebURL.h" | 48 #include "public/platform/WebURL.h" |
47 | 49 |
| 50 using WebKit::WebServiceWorkerProvider; |
| 51 using WebKit::WebString; |
| 52 |
48 namespace WebCore { | 53 namespace WebCore { |
49 | 54 |
50 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator) | 55 NavigatorServiceWorker::NavigatorServiceWorker(Navigator* navigator) |
51 : m_navigator(navigator) | 56 : DOMWindowProperty(navigator->frame()) |
| 57 , m_navigator(navigator) |
52 { | 58 { |
53 } | 59 } |
54 | 60 |
55 NavigatorServiceWorker::~NavigatorServiceWorker() | 61 NavigatorServiceWorker::~NavigatorServiceWorker() |
56 { | 62 { |
57 } | 63 } |
58 | 64 |
59 const char* NavigatorServiceWorker::supplementName() | 65 const char* NavigatorServiceWorker::supplementName() |
60 { | 66 { |
61 return "NavigatorServiceWorker"; | 67 return "NavigatorServiceWorker"; |
62 } | 68 } |
63 | 69 |
| 70 WebServiceWorkerProvider* NavigatorServiceWorker::ensureProvider() |
| 71 { |
| 72 ASSERT(m_navigator->frame()); |
| 73 if (!m_provider) { |
| 74 Frame* frame = m_navigator->frame(); |
| 75 |
| 76 FrameLoaderClient* client = frame->loader()->client(); |
| 77 // FIXME: This is temporarily hooked up here until we hook up to the loa
ding process. |
| 78 m_provider = client->createServiceWorkerProvider(nullptr); |
| 79 } |
| 80 return m_provider.get(); |
| 81 } |
| 82 |
64 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator) | 83 NavigatorServiceWorker* NavigatorServiceWorker::from(Navigator* navigator) |
65 { | 84 { |
66 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); | 85 NavigatorServiceWorker* supplement = toNavigatorServiceWorker(navigator); |
67 if (!supplement) { | 86 if (!supplement) { |
68 supplement = new NavigatorServiceWorker(navigator); | 87 supplement = new NavigatorServiceWorker(navigator); |
69 provideTo(navigator, supplementName(), adoptPtr(supplement)); | 88 provideTo(navigator, supplementName(), adoptPtr(supplement)); |
70 } | 89 } |
71 return supplement; | 90 return supplement; |
72 } | 91 } |
73 | 92 |
74 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* co
ntext, Navigator* navigator, const String& pattern, const String& url, Exception
State& es) | 93 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* co
ntext, Navigator* navigator, const String& pattern, const String& url, Exception
State& es) |
75 { | 94 { |
76 return from(navigator)->registerServiceWorker(context, pattern, url, es); | 95 return from(navigator)->registerServiceWorker(context, pattern, url, es); |
77 } | 96 } |
78 | 97 |
79 | |
80 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* ex
ecutionContext, const String& pattern, const String& scriptSrc, ExceptionState&
es) | 98 ScriptPromise NavigatorServiceWorker::registerServiceWorker(ExecutionContext* ex
ecutionContext, const String& pattern, const String& scriptSrc, ExceptionState&
es) |
81 { | 99 { |
82 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); | 100 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
83 FrameLoaderClient* client = m_navigator->frame()->loader()->client(); | 101 Frame* frame = m_navigator->frame(); |
84 // WTF? Surely there's a better way to resolve a url? | 102 |
85 KURL scriptUrl = m_navigator->frame()->document()->completeURL(scriptSrc); | 103 if (!frame) { |
86 WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); | 104 es.throwDOMException(InvalidStateError, "No document available."); |
| 105 return ScriptPromise(); |
| 106 } |
| 107 |
| 108 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin(); |
| 109 |
| 110 KURL patternURL = executionContext->completeURL(pattern); |
| 111 if (documentOrigin->canRequest(patternURL)) { |
| 112 es.throwSecurityError("Can only register for patterns in the document's
origin."); |
| 113 return ScriptPromise(); |
| 114 } |
| 115 |
| 116 KURL scriptURL = executionContext->completeURL(scriptSrc); |
| 117 if (documentOrigin->canRequest(scriptURL)) { |
| 118 es.throwSecurityError("Script must be in document's origin."); |
| 119 return ScriptPromise(); |
| 120 } |
| 121 |
87 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(execu
tionContext); | 122 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(execu
tionContext); |
88 | 123 ensureProvider()->registerServiceWorker(patternURL, scriptURL, new CallbackP
romiseAdapter(resolver, executionContext)); |
89 if (peer) | |
90 peer->registerServiceWorker(pattern, scriptUrl, new CallbackPromiseAdapt
er(resolver, executionContext)); | |
91 else | |
92 resolver->reject(PassRefPtr<ServiceWorker>(0)); | |
93 // call here? | |
94 return resolver->promise(); | 124 return resolver->promise(); |
95 } | 125 } |
96 | 126 |
97 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext*
context, Navigator* navigator, const String& pattern, ExceptionState& es) | 127 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext*
context, Navigator* navigator, const String& pattern, ExceptionState& es) |
98 { | 128 { |
99 return from(navigator)->unregisterServiceWorker(context, pattern, es); | 129 return from(navigator)->unregisterServiceWorker(context, pattern, es); |
100 } | 130 } |
101 | 131 |
102 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext*
executionContext, const String& pattern, ExceptionState& es) | 132 ScriptPromise NavigatorServiceWorker::unregisterServiceWorker(ExecutionContext*
executionContext, const String& pattern, ExceptionState& es) |
103 { | 133 { |
104 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); | 134 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
105 FrameLoaderClient* client = m_navigator->frame()->loader()->client(); | 135 Frame* frame = m_navigator->frame(); |
106 WebKit::WebServiceWorkerRegistry* peer = client->serviceWorkerRegistry(); | 136 if (!frame) { |
| 137 es.throwDOMException(InvalidStateError, "No document available."); |
| 138 return ScriptPromise(); |
| 139 } |
| 140 |
| 141 RefPtr<SecurityOrigin> documentOrigin = frame->document()->securityOrigin(); |
| 142 |
| 143 KURL patternURL = executionContext->completeURL(pattern); |
| 144 if (documentOrigin->canRequest(patternURL)) { |
| 145 es.throwSecurityError("Can only unregister for patterns in the document'
s origin."); |
| 146 return ScriptPromise(); |
| 147 } |
| 148 |
107 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(execu
tionContext); | 149 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(execu
tionContext); |
108 if (peer) | 150 ensureProvider()->unregisterServiceWorker(patternURL, new CallbackPromiseAda
pter(resolver, executionContext)); |
109 peer->unregisterServiceWorker(pattern, new CallbackPromiseAdapter(resolv
er, executionContext)); | |
110 else | |
111 resolver->reject(PassRefPtr<ServiceWorker>(0)); | |
112 return resolver->promise(); | 151 return resolver->promise(); |
113 } | 152 } |
114 | 153 |
| 154 void NavigatorServiceWorker::willDetachGlobalObjectFromFrame() |
| 155 { |
| 156 m_provider = nullptr; |
| 157 DOMWindowProperty::willDetachGlobalObjectFromFrame(); |
| 158 } |
| 159 |
| 160 |
115 } // namespace WebCore | 161 } // namespace WebCore |
OLD | NEW |