Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / geofencing / Geofencing.cpp
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 "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"
21
22 namespace blink {
23
24 namespace {
25
26 // For CallbackPromiseAdapter to convert a WebVector of regions to a HeapVector.
27 class RegionArray {
28 public:
29     typedef blink::WebVector<blink::WebGeofencingRegistration> WebType;
30     static HeapVector<Member<GeofencingRegion> > take(ScriptPromiseResolver* resolver, WebType* regionsRaw)
31     {
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));
36         return regions;
37     }
38
39     static void dispose(WebType* regionsRaw)
40     {
41         delete regionsRaw;
42     }
43
44 private:
45     RegionArray();
46 };
47
48 } // namespace
49
50 Geofencing::Geofencing(ServiceWorkerRegistration* registration)
51     : m_registration(registration)
52 {
53 }
54
55 ScriptPromise Geofencing::registerRegion(ScriptState* scriptState, GeofencingRegion* region)
56 {
57     WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
58     if (!provider)
59         return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
60
61     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
62     ScriptPromise promise = resolver->promise();
63     WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, GeofencingError>(resolver);
64     WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
65     if (m_registration)
66         serviceWorkerRegistration = m_registration->webRegistration();
67     provider->registerRegion(region->id(), toCircularGeofencingRegion(region)->webRegion(), serviceWorkerRegistration, callbacks);
68     return promise;
69 }
70
71 ScriptPromise Geofencing::unregisterRegion(ScriptState* scriptState, const String& regionId)
72 {
73     WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
74     if (!provider)
75         return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
76
77     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
78     ScriptPromise promise = resolver->promise();
79     WebGeofencingCallbacks* callbacks = new CallbackPromiseAdapter<void, GeofencingError>(resolver);
80     WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
81     if (m_registration)
82         serviceWorkerRegistration = m_registration->webRegistration();
83     provider->unregisterRegion(regionId, serviceWorkerRegistration, callbacks);
84     return promise;
85 }
86
87 ScriptPromise Geofencing::getRegisteredRegions(ScriptState* scriptState) const
88 {
89     WebGeofencingProvider* provider = Platform::current()->geofencingProvider();
90     if (!provider)
91         return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError));
92
93     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
94     ScriptPromise promise = resolver->promise();
95     WebGeofencingRegionsCallbacks* callbacks = new CallbackPromiseAdapter<RegionArray, GeofencingError>(resolver);
96     WebServiceWorkerRegistration* serviceWorkerRegistration = nullptr;
97     if (m_registration)
98         serviceWorkerRegistration = m_registration->webRegistration();
99     provider->getRegisteredRegions(serviceWorkerRegistration, callbacks);
100     return promise;
101 }
102
103 void Geofencing::trace(Visitor* visitor)
104 {
105     visitor->trace(m_registration);
106 }
107
108 } // namespace blink