OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/geofencing/Geofencing.h" | |
6 | |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | |
8 #include "bindings/core/v8/ScriptPromise.h" | |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
10 #include "core/dom/DOMException.h" | |
11 #include "core/dom/ExceptionCode.h" | |
12 #include "modules/geofencing/CircularGeofencingRegion.h" | |
13 #include "modules/geofencing/GeofencingError.h" | |
14 #include "modules/geofencing/GeofencingRegion.h" | |
15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | |
16 #include "public/platform/Platform.h" | |
17 #include "public/platform/WebCircularGeofencingRegion.h" | |
18 #include "public/platform/WebGeofencingProvider.h" | |
19 #include "public/platform/WebGeofencingRegistration.h" | |
20 #include "wtf/OwnPtr.h" | |
21 #include "wtf/PassOwnPtr.h" | |
22 | |
23 namespace blink { | |
24 | |
25 namespace { | |
26 | |
27 // For CallbackPromiseAdapter to convert a WebVector of regions to a HeapVector. | |
28 class RegionArray { | |
29 public: | |
30 using WebType = const WebVector<WebGeofencingRegistration>&; | |
31 static HeapVector<Member<GeofencingRegion>> take(ScriptPromiseResolver* reso
lver, const WebVector<WebGeofencingRegistration>& webRegions) | |
32 { | |
33 HeapVector<Member<GeofencingRegion>> regions; | |
34 for (size_t i = 0; i < webRegions.size(); ++i) | |
35 regions.append(CircularGeofencingRegion::create(webRegions[i].id, we
bRegions[i].region)); | |
36 return regions; | |
37 } | |
38 | |
39 private: | |
40 RegionArray(); | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 Geofencing::Geofencing(ServiceWorkerRegistration* registration) | |
46 : m_registration(registration) | |
47 { | |
48 } | |
49 | |
50 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingReg
ion* region) | |
51 { | |
52 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); | |
53 if (!provider) | |
54 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | |
55 | |
56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | |
57 ScriptPromise promise = resolver->promise(); | |
58 WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, Geofenc
ingError>(resolver); | |
59 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr; | |
60 if (m_registration) | |
61 serviceWorkerRegistration = m_registration->webRegistration(); | |
62 provider->registerRegion(region->id(), toCircularGeofencingRegion(region)->w
ebRegion(), serviceWorkerRegistration, callbacks); | |
63 return promise; | |
64 } | |
65 | |
66 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const Strin
g& regionId) | |
67 { | |
68 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); | |
69 if (!provider) | |
70 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | |
71 | |
72 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | |
73 ScriptPromise promise = resolver->promise(); | |
74 WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, Geofenc
ingError>(resolver); | |
75 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr; | |
76 if (m_registration) | |
77 serviceWorkerRegistration = m_registration->webRegistration(); | |
78 provider->unregisterRegion(regionId, serviceWorkerRegistration, callbacks); | |
79 return promise; | |
80 } | |
81 | |
82 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const | |
83 { | |
84 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); | |
85 if (!provider) | |
86 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | |
87 | |
88 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | |
89 ScriptPromise promise = resolver->promise(); | |
90 WebGeofencingRegionsCallbacks* callbacks = new CallbackPromiseAdapter<Region
Array, GeofencingError>(resolver); | |
91 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr; | |
92 if (m_registration) | |
93 serviceWorkerRegistration = m_registration->webRegistration(); | |
94 provider->getRegisteredRegions(serviceWorkerRegistration, callbacks); | |
95 return promise; | |
96 } | |
97 | |
98 DEFINE_TRACE(Geofencing) | |
99 { | |
100 visitor->trace(m_registration); | |
101 } | |
102 | |
103 } // namespace blink | |
OLD | NEW |