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/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
8 #include "core/dom/DOMException.h" | 9 #include "bindings/core/v8/V8ThrowException.h" |
| 10 #include "modules/background_fetch/BackgroundFetchOptions.h" |
| 11 #include "modules/background_fetch/BackgroundFetchRegistration.h" |
9 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 12 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
10 | 13 |
11 namespace blink { | 14 namespace blink { |
12 | 15 |
13 BackgroundFetchManager::BackgroundFetchManager( | 16 BackgroundFetchManager::BackgroundFetchManager( |
14 ServiceWorkerRegistration* registration) | 17 ServiceWorkerRegistration* registration) |
15 : m_registration(registration) { | 18 : m_registration(registration) { |
16 DCHECK(registration); | 19 DCHECK(registration); |
17 } | 20 } |
18 | 21 |
| 22 ScriptPromise BackgroundFetchManager::fetch( |
| 23 ScriptState* scriptState, |
| 24 String tag, |
| 25 HeapVector<RequestOrUSVString> requests, |
| 26 const BackgroundFetchOptions& options) { |
| 27 if (!m_registration->active()) { |
| 28 return ScriptPromise::reject( |
| 29 scriptState, |
| 30 V8ThrowException::createTypeError(scriptState->isolate(), |
| 31 "No active registration available on " |
| 32 "the ServiceWorkerRegistration.")); |
| 33 } |
| 34 |
| 35 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 36 ScriptPromise promise = resolver->promise(); |
| 37 |
| 38 // TODO(peter): Register the fetch() with the browser process. The reject |
| 39 // cases there are storage errors and duplicated registrations for the `tag` |
| 40 // given the `m_registration`. |
| 41 BackgroundFetchRegistration* registration = new BackgroundFetchRegistration( |
| 42 m_registration.get(), tag, options.icons(), options.totalDownloadSize(), |
| 43 options.title()); |
| 44 |
| 45 resolver->resolve(registration); |
| 46 |
| 47 return promise; |
| 48 } |
| 49 |
| 50 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, |
| 51 String tag) { |
| 52 if (!m_registration->active()) { |
| 53 return ScriptPromise::reject( |
| 54 scriptState, |
| 55 V8ThrowException::createTypeError(scriptState->isolate(), |
| 56 "No active registration available on " |
| 57 "the ServiceWorkerRegistration.")); |
| 58 } |
| 59 |
| 60 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 61 ScriptPromise promise = resolver->promise(); |
| 62 |
| 63 // TODO(peter): Get the background fetch registration for `tag` from the |
| 64 // browser process. There may not be one. |
| 65 resolver->resolve(v8::Null(scriptState->isolate())); |
| 66 |
| 67 return promise; |
| 68 } |
| 69 |
19 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) { | 70 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) { |
20 return ScriptPromise::rejectWithDOMException( | 71 if (!m_registration->active()) { |
21 scriptState, | 72 return ScriptPromise::reject( |
22 DOMException::create(NotSupportedError, "Not implemented yet.")); | 73 scriptState, |
| 74 V8ThrowException::createTypeError(scriptState->isolate(), |
| 75 "No active registration available on " |
| 76 "the ServiceWorkerRegistration.")); |
| 77 } |
| 78 |
| 79 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 80 ScriptPromise promise = resolver->promise(); |
| 81 |
| 82 // TODO(peter): Get a list of tags from the browser process. |
| 83 resolver->resolve(Vector<String>()); |
| 84 |
| 85 return promise; |
23 } | 86 } |
24 | 87 |
25 DEFINE_TRACE(BackgroundFetchManager) { | 88 DEFINE_TRACE(BackgroundFetchManager) { |
26 visitor->trace(m_registration); | 89 visitor->trace(m_registration); |
27 } | 90 } |
28 | 91 |
29 } // namespace blink | 92 } // namespace blink |
OLD | NEW |