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 // Copy of same class in ServiceWorkerContainer.cpp |
| 23 class UndefinedValue { |
| 24 public: |
| 25 typedef blink::WebGeofencingError WebType; |
| 26 static V8UndefinedType take(ScriptPromiseResolver* resolver, WebType* value) |
| 27 { |
| 28 ASSERT(!value); // Anything passed here will be leaked. |
| 29 return V8UndefinedType(); |
| 30 } |
| 31 |
| 32 static void dispose(WebType* value) |
| 33 { |
| 34 ASSERT(!value); // Anything passed here will be leaked. |
| 35 } |
| 36 |
| 37 private: |
| 38 UndefinedValue(); |
| 39 }; |
| 40 |
| 41 class RegionArray { |
| 42 public: |
| 43 typedef blink::WebVector<blink::WebGeofencingRegion> WebType; |
| 44 static HeapVector<Member<GeofencingRegion> > take(ScriptPromiseResolver* res
olver, WebType* regionsRaw) |
| 45 { |
| 46 OwnPtr<WebType> webRegions = adoptPtr(regionsRaw); |
| 47 HeapVector<Member<GeofencingRegion> > regions; |
| 48 for (size_t i = 0; i < webRegions->size(); ++i) { |
| 49 regions.append(GeofencingRegion::createFromWebRegion((*webRegions)[i
])); |
| 50 } |
| 51 return regions; |
| 52 } |
| 53 |
| 54 static void dispose(WebType* regionsRaw) |
| 55 { |
| 56 delete regionsRaw; |
| 57 } |
| 58 |
| 59 private: |
| 60 RegionArray(); |
| 61 }; |
| 62 |
| 63 } // namespace |
| 64 |
14 Geofencing::Geofencing() | 65 Geofencing::Geofencing() |
15 { | 66 { |
16 ScriptWrappable::init(this); | 67 ScriptWrappable::init(this); |
17 } | 68 } |
18 | 69 |
19 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingReg
ion* region) | 70 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingReg
ion* region) |
20 { | 71 { |
21 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 72 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 73 if (!provider) { |
| 74 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 75 } |
| 76 |
| 77 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 78 ScriptPromise promise = resolver->promise(); |
| 79 provider->registerRegion(region->webRegion(), new CallbackPromiseAdapter<Und
efinedValue, GeofencingError>(resolver)); |
| 80 return promise; |
22 } | 81 } |
23 | 82 |
24 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const Strin
g& regionId) | 83 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const Strin
g& regionId) |
25 { | 84 { |
26 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 85 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 86 if (!provider) { |
| 87 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 88 } |
| 89 |
| 90 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 91 ScriptPromise promise = resolver->promise(); |
| 92 provider->unregisterRegion(regionId, new CallbackPromiseAdapter<UndefinedVal
ue, GeofencingError>(resolver)); |
| 93 return promise; |
27 } | 94 } |
28 | 95 |
29 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const | 96 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const |
30 { | 97 { |
31 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError)); | 98 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); |
| 99 if (!provider) { |
| 100 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 101 } |
| 102 |
| 103 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 104 ScriptPromise promise = resolver->promise(); |
| 105 provider->getRegisteredRegions(new CallbackPromiseAdapter<RegionArray, Geofe
ncingError>(resolver)); |
| 106 return promise; |
32 } | 107 } |
33 | 108 |
34 } // namespace blink | 109 } // namespace blink |
OLD | NEW |