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 "config.h" 6 #include "modules/geofencing/Geofencing.h" 7 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" 9 #include "bindings/core/v8/ScriptPromise.h" 10 #include "bindings/core/v8/ScriptPromiseResolver.h" 11 #include "core/dom/DOMException.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" 20 21 namespace blink { 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* resolver, 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 49 Geofencing::Geofencing() 50 { 51 } 52 53 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingRegion* region) 54 { 55 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); 56 if (!provider) 57 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError)); 58 59 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); 60 ScriptPromise promise = resolver->promise(); 61 // FIXME: somehow pass a reference to the current serviceworker to the provider. 62 provider->registerRegion(region->id(), toCircularGeofencingRegion(region)->webRegion(), new CallbackPromiseAdapter<void, GeofencingError>(resolver)); 63 return promise; 64 } 65 66 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const String& regionId) 67 { 68 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); 69 if (!provider) 70 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError)); 71 72 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); 73 ScriptPromise promise = resolver->promise(); 74 // FIXME: somehow pass a reference to the current serviceworker to the provider. 75 provider->unregisterRegion(regionId, new CallbackPromiseAdapter<void, GeofencingError>(resolver)); 76 return promise; 77 } 78 79 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const 80 { 81 WebGeofencingProvider* provider = Platform::current()->geofencingProvider(); 82 if (!provider) 83 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError)); 84 85 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); 86 ScriptPromise promise = resolver->promise(); 87 // FIXME: somehow pass a reference to the current serviceworker to the provider. 88 provider->getRegisteredRegions(new CallbackPromiseAdapter<RegionArray, GeofencingError>(resolver)); 89 return promise; 90 } 91 92 } // namespace blink 93