OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "config.h" | 5 #include "config.h" |
6 #include "modules/geofencing/Geofencing.h" | 6 #include "modules/geofencing/Geofencing.h" |
7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
10 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "modules/geofencing/CircularGeofencingRegion.h" |
| 14 #include "modules/geofencing/GeofencingError.h" |
| 15 #include "modules/geofencing/GeofencingRegion.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" |
11 | 20 |
12 namespace blink { | 21 namespace blink { |
13 | 22 |
| 23 namespace { |
| 24 |
| 25 // For CallbackPromiseAdapter to convert a WebVector of regions to a HeapVector. |
| 26 class RegionArray { |
| 27 public: |
| 28 typedef blink::WebVector<blink::WebGeofencingRegistration> WebType; |
| 29 static HeapVector<Member<GeofencingRegion> > take(ScriptPromiseResolver* res
olver, WebType* regionsRaw) |
| 30 { |
| 31 OwnPtr<WebType> webRegions = adoptPtr(regionsRaw); |
| 32 HeapVector<Member<GeofencingRegion> > regions; |
| 33 for (size_t i = 0; i < webRegions->size(); ++i) |
| 34 regions.append(CircularGeofencingRegion::create((*webRegions)[i].id,
(*webRegions)[i].region)); |
| 35 return regions; |
| 36 } |
| 37 |
| 38 static void dispose(WebType* regionsRaw) |
| 39 { |
| 40 delete regionsRaw; |
| 41 } |
| 42 |
| 43 private: |
| 44 RegionArray(); |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
14 Geofencing::Geofencing() | 49 Geofencing::Geofencing() |
15 { | 50 { |
16 ScriptWrappable::init(this); | 51 ScriptWrappable::init(this); |
17 } | 52 } |
18 | 53 |
19 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingReg
ion* region) | 54 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingReg
ion* region) |
20 { | 55 { |
21 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 56 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 57 if (!provider) |
| 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 59 |
| 60 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 61 ScriptPromise promise = resolver->promise(); |
| 62 // FIXME: somehow pass a reference to the current serviceworker to the provi
der. |
| 63 provider->registerRegion(region->id(), toCircularGeofencingRegion(region)->w
ebRegion(), new CallbackPromiseAdapter<void, GeofencingError>(resolver)); |
| 64 return promise; |
22 } | 65 } |
23 | 66 |
24 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const Strin
g& regionId) | 67 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const Strin
g& regionId) |
25 { | 68 { |
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 69 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 70 if (!provider) |
| 71 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 72 |
| 73 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 74 ScriptPromise promise = resolver->promise(); |
| 75 // FIXME: somehow pass a reference to the current serviceworker to the provi
der. |
| 76 provider->unregisterRegion(regionId, new CallbackPromiseAdapter<void, Geofen
cingError>(resolver)); |
| 77 return promise; |
27 } | 78 } |
28 | 79 |
29 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const | 80 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const |
30 { | 81 { |
31 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 82 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 83 if (!provider) |
| 84 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 85 |
| 86 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 87 ScriptPromise promise = resolver->promise(); |
| 88 // FIXME: somehow pass a reference to the current serviceworker to the provi
der. |
| 89 provider->getRegisteredRegions(new CallbackPromiseAdapter<RegionArray, Geofe
ncingError>(resolver)); |
| 90 return promise; |
32 } | 91 } |
33 | 92 |
34 } // namespace blink | 93 } // namespace blink |
OLD | NEW |