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