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.
6 #include "modules/geofencing/Geofencing.h"
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 "modules/serviceworkers/ServiceWorkerRegistration.h"
17 #include "public/platform/Platform.h"
18 #include "public/platform/WebCircularGeofencingRegion.h"
19 #include "public/platform/WebGeofencingProvider.h"
20 #include "public/platform/WebGeofencingRegistration.h"
26 // For CallbackPromiseAdapter to convert a WebVector of regions to a HeapVector.
29 typedef blink::WebVector<blink::WebGeofencingRegistration> WebType;
30 static HeapVector<Member<GeofencingRegion> > take(ScriptPromiseResolver* resolver, WebType* regionsRaw)
32 OwnPtr<WebType> webRegions = adoptPtr(regionsRaw);
33 HeapVector<Member<GeofencingRegion> > regions;
34 for (size_t i = 0; i < webRegions->size(); ++i)
35 regions.append(CircularGeofencingRegion::create((*webRegions)[i].id, (*webRegions)[i].region));
39 static void dispose(WebType* regionsRaw)
50 Geofencing::Geofencing(ServiceWorkerRegistration* registration)
51 : m_registration(registration)
55 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingRegion* region)
57 WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
59 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
61 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
62 ScriptPromise promise = resolver->promise();
63 WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, GeofencingError>(resolver);
64 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
66 serviceWorkerRegistration = m_registration->webRegistration();
67 provider->registerRegion(region->id(), toCircularGeofencingRegion(region)->webRegion(), serviceWorkerRegistration, callbacks);
71 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const String& regionId)
73 WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
75 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
77 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
78 ScriptPromise promise = resolver->promise();
79 WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, GeofencingError>(resolver);
80 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
82 serviceWorkerRegistration = m_registration->webRegistration();
83 provider->unregisterRegion(regionId, serviceWorkerRegistration, callbacks);
87 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const
89 WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
91 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
93 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
94 ScriptPromise promise = resolver->promise();
95 WebGeofencingRegionsCallbacks* callbacks = new CallbackPromiseAdapter<RegionArray, GeofencingError>(resolver);
96 WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
98 serviceWorkerRegistration = m_registration->webRegistration();
99 provider->getRegisteredRegions(serviceWorkerRegistration, callbacks);
103 void Geofencing::trace(Visitor* visitor)
105 visitor->trace(m_registration);