| 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 "modules/background_fetch/BackgroundFetchBridge.h" |
| 10 #include "modules/background_fetch/BackgroundFetchOptions.h" | 11 #include "modules/background_fetch/BackgroundFetchOptions.h" |
| 11 #include "modules/background_fetch/BackgroundFetchRegistration.h" | 12 #include "modules/background_fetch/BackgroundFetchRegistration.h" |
| 12 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 13 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 BackgroundFetchManager::BackgroundFetchManager( | 17 BackgroundFetchManager::BackgroundFetchManager( |
| 17 ServiceWorkerRegistration* registration) | 18 ServiceWorkerRegistration* registration) |
| 18 : m_registration(registration) { | 19 : m_registration(registration) { |
| 19 DCHECK(registration); | 20 DCHECK(registration); |
| 21 m_bridge = BackgroundFetchBridge::from(m_registration); |
| 20 } | 22 } |
| 21 | 23 |
| 22 ScriptPromise BackgroundFetchManager::fetch( | 24 ScriptPromise BackgroundFetchManager::fetch( |
| 23 ScriptState* scriptState, | 25 ScriptState* scriptState, |
| 24 String tag, | 26 const String& tag, |
| 25 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, | 27 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, |
| 26 const BackgroundFetchOptions& options) { | 28 const BackgroundFetchOptions& options) { |
| 27 if (!m_registration->active()) { | 29 if (!m_registration->active()) { |
| 28 return ScriptPromise::reject( | 30 return ScriptPromise::reject( |
| 29 scriptState, | 31 scriptState, |
| 30 V8ThrowException::createTypeError(scriptState->isolate(), | 32 V8ThrowException::createTypeError(scriptState->isolate(), |
| 31 "No active registration available on " | 33 "No active registration available on " |
| 32 "the ServiceWorkerRegistration.")); | 34 "the ServiceWorkerRegistration.")); |
| 33 } | 35 } |
| 34 | 36 |
| 35 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 36 ScriptPromise promise = resolver->promise(); | 38 ScriptPromise promise = resolver->promise(); |
| 37 | 39 |
| 38 // TODO(peter): Register the fetch() with the browser process. The reject | 40 // TODO(peter): Register the fetch() with the browser process. The reject |
| 39 // cases there are storage errors and duplicated registrations for the `tag` | 41 // cases there are storage errors and duplicated registrations for the `tag` |
| 40 // given the `m_registration`. | 42 // given the `m_registration`. |
| 41 BackgroundFetchRegistration* registration = new BackgroundFetchRegistration( | 43 BackgroundFetchRegistration* registration = new BackgroundFetchRegistration( |
| 42 m_registration.get(), tag, options.icons(), options.totalDownloadSize(), | 44 m_registration.get(), tag, options.icons(), options.totalDownloadSize(), |
| 43 options.title()); | 45 options.title()); |
| 44 | 46 |
| 45 resolver->resolve(registration); | 47 resolver->resolve(registration); |
| 46 | 48 |
| 47 return promise; | 49 return promise; |
| 48 } | 50 } |
| 49 | 51 |
| 50 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, | 52 ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, |
| 51 String tag) { | 53 const String& tag) { |
| 52 if (!m_registration->active()) { | 54 if (!m_registration->active()) { |
| 53 return ScriptPromise::reject( | 55 return ScriptPromise::reject( |
| 54 scriptState, | 56 scriptState, |
| 55 V8ThrowException::createTypeError(scriptState->isolate(), | 57 V8ThrowException::createTypeError(scriptState->isolate(), |
| 56 "No active registration available on " | 58 "No active registration available on " |
| 57 "the ServiceWorkerRegistration.")); | 59 "the ServiceWorkerRegistration.")); |
| 58 } | 60 } |
| 59 | 61 |
| 60 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 62 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 61 ScriptPromise promise = resolver->promise(); | 63 ScriptPromise promise = resolver->promise(); |
| 62 | 64 |
| 63 // TODO(peter): Get the background fetch registration for `tag` from the | 65 m_bridge->getRegistration( |
| 64 // browser process. There may not be one. | 66 tag, WTF::bind(&BackgroundFetchManager::didGetRegistration, |
| 65 resolver->resolve(v8::Null(scriptState->isolate())); | 67 wrapPersistent(this), wrapPersistent(resolver))); |
| 66 | 68 |
| 67 return promise; | 69 return promise; |
| 68 } | 70 } |
| 69 | 71 |
| 72 void BackgroundFetchManager::didGetRegistration( |
| 73 ScriptPromiseResolver* resolver, |
| 74 mojom::blink::BackgroundFetchError error, |
| 75 BackgroundFetchRegistration* registration) { |
| 76 switch (error) { |
| 77 case mojom::blink::BackgroundFetchError::NONE: |
| 78 resolver->resolve(registration); |
| 79 return; |
| 80 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
| 81 // Not applicable for this callback. |
| 82 break; |
| 83 } |
| 84 |
| 85 NOTREACHED(); |
| 86 } |
| 87 |
| 70 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) { | 88 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) { |
| 71 if (!m_registration->active()) { | 89 if (!m_registration->active()) { |
| 72 return ScriptPromise::reject( | 90 return ScriptPromise::reject( |
| 73 scriptState, | 91 scriptState, |
| 74 V8ThrowException::createTypeError(scriptState->isolate(), | 92 V8ThrowException::createTypeError(scriptState->isolate(), |
| 75 "No active registration available on " | 93 "No active registration available on " |
| 76 "the ServiceWorkerRegistration.")); | 94 "the ServiceWorkerRegistration.")); |
| 77 } | 95 } |
| 78 | 96 |
| 79 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 97 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 80 ScriptPromise promise = resolver->promise(); | 98 ScriptPromise promise = resolver->promise(); |
| 81 | 99 |
| 82 // TODO(peter): Get a list of tags from the browser process. | 100 m_bridge->getTags(WTF::bind(&BackgroundFetchManager::didGetTags, |
| 83 resolver->resolve(Vector<String>()); | 101 wrapPersistent(this), wrapPersistent(resolver))); |
| 84 | 102 |
| 85 return promise; | 103 return promise; |
| 86 } | 104 } |
| 87 | 105 |
| 106 void BackgroundFetchManager::didGetTags( |
| 107 ScriptPromiseResolver* resolver, |
| 108 mojom::blink::BackgroundFetchError error, |
| 109 const Vector<String>& tags) { |
| 110 switch (error) { |
| 111 case mojom::blink::BackgroundFetchError::NONE: |
| 112 resolver->resolve(tags); |
| 113 return; |
| 114 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
| 115 // Not applicable for this callback. |
| 116 break; |
| 117 } |
| 118 |
| 119 NOTREACHED(); |
| 120 } |
| 121 |
| 88 DEFINE_TRACE(BackgroundFetchManager) { | 122 DEFINE_TRACE(BackgroundFetchManager) { |
| 89 visitor->trace(m_registration); | 123 visitor->trace(m_registration); |
| 124 visitor->trace(m_bridge); |
| 90 } | 125 } |
| 91 | 126 |
| 92 } // namespace blink | 127 } // namespace blink |
| OLD | NEW |