Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp

Issue 2745243002: Implement a Mojo service for Background Fetch (Closed)
Patch Set: Implement a Mojo service for Background Fetch Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 HeapVector<RequestOrUSVString> requests, 27 HeapVector<RequestOrUSVString> 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 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG:
79 resolver->resolve(registration);
harkness 2017/03/14 18:19:35 Now that getRegistration shouldn't return DUPLICAT
Peter Beverloo 2017/03/16 16:26:55 Done.
80 return;
81 }
82
83 NOTREACHED();
84 }
85
70 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) { 86 ScriptPromise BackgroundFetchManager::getTags(ScriptState* scriptState) {
71 if (!m_registration->active()) { 87 if (!m_registration->active()) {
72 return ScriptPromise::reject( 88 return ScriptPromise::reject(
73 scriptState, 89 scriptState,
74 V8ThrowException::createTypeError(scriptState->isolate(), 90 V8ThrowException::createTypeError(scriptState->isolate(),
75 "No active registration available on " 91 "No active registration available on "
76 "the ServiceWorkerRegistration.")); 92 "the ServiceWorkerRegistration."));
77 } 93 }
78 94
79 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 95 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
80 ScriptPromise promise = resolver->promise(); 96 ScriptPromise promise = resolver->promise();
81 97
82 // TODO(peter): Get a list of tags from the browser process. 98 m_bridge->getTags(WTF::bind(&BackgroundFetchManager::didGetTags,
83 resolver->resolve(Vector<String>()); 99 wrapPersistent(this), wrapPersistent(resolver)));
84 100
85 return promise; 101 return promise;
86 } 102 }
87 103
104 void BackgroundFetchManager::didGetTags(
105 ScriptPromiseResolver* resolver,
106 mojom::blink::BackgroundFetchError error,
107 const Vector<String>& tags) {
108 switch (error) {
109 case mojom::blink::BackgroundFetchError::NONE:
110 resolver->resolve(tags);
111 return;
112 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG:
113 // Not applicable for this callback.
114 break;
115 }
116
117 NOTREACHED();
118 }
119
88 DEFINE_TRACE(BackgroundFetchManager) { 120 DEFINE_TRACE(BackgroundFetchManager) {
89 visitor->trace(m_registration); 121 visitor->trace(m_registration);
122 visitor->trace(m_bridge);
90 } 123 }
91 124
92 } // namespace blink 125 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698