OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/background_fetch/BackgroundFetchManager.h" | 5 #include "modules/background_fetch/BackgroundFetchManager.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
9 #include "bindings/core/v8/V8ThrowException.h" | 9 #include "bindings/core/v8/V8ThrowException.h" |
| 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" |
10 #include "modules/background_fetch/BackgroundFetchBridge.h" | 12 #include "modules/background_fetch/BackgroundFetchBridge.h" |
11 #include "modules/background_fetch/BackgroundFetchOptions.h" | 13 #include "modules/background_fetch/BackgroundFetchOptions.h" |
12 #include "modules/background_fetch/BackgroundFetchRegistration.h" | 14 #include "modules/background_fetch/BackgroundFetchRegistration.h" |
13 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
14 | 16 |
15 namespace blink { | 17 namespace blink { |
16 | 18 |
17 BackgroundFetchManager::BackgroundFetchManager( | 19 BackgroundFetchManager::BackgroundFetchManager( |
18 ServiceWorkerRegistration* registration) | 20 ServiceWorkerRegistration* registration) |
19 : m_registration(registration) { | 21 : m_registration(registration) { |
(...skipping 10 matching lines...) Expand all Loading... |
30 return ScriptPromise::reject( | 32 return ScriptPromise::reject( |
31 scriptState, | 33 scriptState, |
32 V8ThrowException::createTypeError(scriptState->isolate(), | 34 V8ThrowException::createTypeError(scriptState->isolate(), |
33 "No active registration available on " | 35 "No active registration available on " |
34 "the ServiceWorkerRegistration.")); | 36 "the ServiceWorkerRegistration.")); |
35 } | 37 } |
36 | 38 |
37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
38 ScriptPromise promise = resolver->promise(); | 40 ScriptPromise promise = resolver->promise(); |
39 | 41 |
40 // TODO(peter): Register the fetch() with the browser process. The reject | 42 // TODO(peter): Include the |requests| in the Mojo call. |
41 // cases there are storage errors and duplicated registrations for the `tag` | |
42 // given the `m_registration`. | |
43 BackgroundFetchRegistration* registration = new BackgroundFetchRegistration( | |
44 m_registration.get(), tag, options.icons(), options.totalDownloadSize(), | |
45 options.title()); | |
46 | 43 |
47 resolver->resolve(registration); | 44 m_bridge->fetch(tag, options, |
| 45 WTF::bind(&BackgroundFetchManager::didFetch, |
| 46 wrapPersistent(this), wrapPersistent(resolver))); |
48 | 47 |
49 return promise; | 48 return promise; |
50 } | 49 } |
51 | 50 |
| 51 void BackgroundFetchManager::didFetch( |
| 52 ScriptPromiseResolver* resolver, |
| 53 mojom::blink::BackgroundFetchError error, |
| 54 BackgroundFetchRegistration* registration) { |
| 55 switch (error) { |
| 56 case mojom::blink::BackgroundFetchError::NONE: |
| 57 DCHECK(registration); |
| 58 resolver->resolve(registration); |
| 59 return; |
| 60 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
| 61 DCHECK(!registration); |
| 62 resolver->reject(DOMException::create( |
| 63 InvalidStateError, |
| 64 "There already is a registration for the given tag.")); |
| 65 return; |
| 66 } |
| 67 |
| 68 NOTREACHED(); |
| 69 } |
| 70 |
52 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, | 71 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, |
53 const String& tag) { | 72 const String& tag) { |
54 if (!m_registration->active()) { | 73 if (!m_registration->active()) { |
55 return ScriptPromise::reject( | 74 return ScriptPromise::reject( |
56 scriptState, | 75 scriptState, |
57 V8ThrowException::createTypeError(scriptState->isolate(), | 76 V8ThrowException::createTypeError(scriptState->isolate(), |
58 "No active registration available on " | 77 "No active registration available on " |
59 "the ServiceWorkerRegistration.")); | 78 "the ServiceWorkerRegistration.")); |
60 } | 79 } |
61 | 80 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 137 |
119 NOTREACHED(); | 138 NOTREACHED(); |
120 } | 139 } |
121 | 140 |
122 DEFINE_TRACE(BackgroundFetchManager) { | 141 DEFINE_TRACE(BackgroundFetchManager) { |
123 visitor->trace(m_registration); | 142 visitor->trace(m_registration); |
124 visitor->trace(m_bridge); | 143 visitor->trace(m_bridge); |
125 } | 144 } |
126 | 145 |
127 } // namespace blink | 146 } // namespace blink |
OLD | NEW |