| 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 case mojom::blink::BackgroundFetchError::INVALID_TAG: |
| 67 // Not applicable for this callback. |
| 68 break; |
| 69 } |
| 70 |
| 71 NOTREACHED(); |
| 72 } |
| 73 |
| 52 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, | 74 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, |
| 53 const String& tag) { | 75 const String& tag) { |
| 54 if (!m_registration->active()) { | 76 if (!m_registration->active()) { |
| 55 return ScriptPromise::reject( | 77 return ScriptPromise::reject( |
| 56 scriptState, | 78 scriptState, |
| 57 V8ThrowException::createTypeError(scriptState->isolate(), | 79 V8ThrowException::createTypeError(scriptState->isolate(), |
| 58 "No active registration available on " | 80 "No active registration available on " |
| 59 "the ServiceWorkerRegistration.")); | 81 "the ServiceWorkerRegistration.")); |
| 60 } | 82 } |
| 61 | 83 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 142 |
| 121 NOTREACHED(); | 143 NOTREACHED(); |
| 122 } | 144 } |
| 123 | 145 |
| 124 DEFINE_TRACE(BackgroundFetchManager) { | 146 DEFINE_TRACE(BackgroundFetchManager) { |
| 125 visitor->trace(m_registration); | 147 visitor->trace(m_registration); |
| 126 visitor->trace(m_bridge); | 148 visitor->trace(m_bridge); |
| 127 } | 149 } |
| 128 | 150 |
| 129 } // namespace blink | 151 } // namespace blink |
| OLD | NEW |