Miscellaneous fixes for virtual/override/final specifiers to match style guide.
[chromium-blink-merge.git] / content / child / geofencing / geofencing_dispatcher.cc
blobef8ae9b0131f5cabe82ee7af7e09eed042d9053f
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.
5 #include "content/child/geofencing/geofencing_dispatcher.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/child/service_worker/web_service_worker_registration_impl.h"
12 #include "content/child/thread_safe_sender.h"
13 #include "content/child/worker_thread_task_runner.h"
14 #include "content/common/geofencing_messages.h"
15 #include "content/common/service_worker/service_worker_types.h"
16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
17 #include "third_party/WebKit/public/platform/WebGeofencingError.h"
18 #include "third_party/WebKit/public/platform/WebGeofencingRegistration.h"
20 using blink::WebGeofencingError;
22 namespace content {
24 namespace {
26 base::LazyInstance<base::ThreadLocalPointer<GeofencingDispatcher>>::Leaky
27 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
29 GeofencingDispatcher* const kHasBeenDeleted =
30 reinterpret_cast<GeofencingDispatcher*>(0x1);
32 int CurrentWorkerId() {
33 return WorkerTaskRunner::Instance()->CurrentWorkerId();
36 } // namespace
38 GeofencingDispatcher::GeofencingDispatcher(ThreadSafeSender* sender)
39 : thread_safe_sender_(sender) {
40 g_dispatcher_tls.Pointer()->Set(this);
43 GeofencingDispatcher::~GeofencingDispatcher() {
44 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
47 bool GeofencingDispatcher::Send(IPC::Message* msg) {
48 return thread_safe_sender_->Send(msg);
51 void GeofencingDispatcher::OnMessageReceived(const IPC::Message& msg) {
52 bool handled = true;
53 IPC_BEGIN_MESSAGE_MAP(GeofencingDispatcher, msg)
54 IPC_MESSAGE_HANDLER(GeofencingMsg_RegisterRegionComplete,
55 OnRegisterRegionComplete)
56 IPC_MESSAGE_HANDLER(GeofencingMsg_UnregisterRegionComplete,
57 OnUnregisterRegionComplete)
58 IPC_MESSAGE_HANDLER(GeofencingMsg_GetRegisteredRegionsComplete,
59 OnGetRegisteredRegionsComplete)
60 IPC_MESSAGE_UNHANDLED(handled = false)
61 IPC_END_MESSAGE_MAP()
62 DCHECK(handled) << "Unhandled message:" << msg.type();
65 void GeofencingDispatcher::RegisterRegion(
66 const blink::WebString& region_id,
67 const blink::WebCircularGeofencingRegion& region,
68 blink::WebServiceWorkerRegistration* service_worker_registration,
69 blink::WebGeofencingCallbacks* callbacks) {
70 DCHECK(callbacks);
71 int request_id = region_registration_requests_.Add(callbacks);
72 // TODO(mek): Immediately reject requests lacking a service worker
73 // registration, without bouncing through browser process.
74 int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
75 if (service_worker_registration) {
76 serviceworker_registration_id =
77 static_cast<WebServiceWorkerRegistrationImpl*>(
78 service_worker_registration)->registration_id();
80 Send(new GeofencingHostMsg_RegisterRegion(CurrentWorkerId(),
81 request_id,
82 region_id.utf8(),
83 region,
84 serviceworker_registration_id));
87 void GeofencingDispatcher::UnregisterRegion(
88 const blink::WebString& region_id,
89 blink::WebServiceWorkerRegistration* service_worker_registration,
90 blink::WebGeofencingCallbacks* callbacks) {
91 DCHECK(callbacks);
92 int request_id = region_unregistration_requests_.Add(callbacks);
93 // TODO(mek): Immediately reject requests lacking a service worker
94 // registration, without bouncing through browser process.
95 int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
96 if (service_worker_registration) {
97 serviceworker_registration_id =
98 static_cast<WebServiceWorkerRegistrationImpl*>(
99 service_worker_registration)->registration_id();
101 Send(new GeofencingHostMsg_UnregisterRegion(CurrentWorkerId(),
102 request_id,
103 region_id.utf8(),
104 serviceworker_registration_id));
107 void GeofencingDispatcher::GetRegisteredRegions(
108 blink::WebServiceWorkerRegistration* service_worker_registration,
109 blink::WebGeofencingRegionsCallbacks* callbacks) {
110 DCHECK(callbacks);
111 int request_id = get_registered_regions_requests_.Add(callbacks);
112 // TODO(mek): Immediately reject requests lacking a service worker
113 // registration, without bouncing through browser process.
114 int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
115 if (service_worker_registration) {
116 serviceworker_registration_id =
117 static_cast<WebServiceWorkerRegistrationImpl*>(
118 service_worker_registration)->registration_id();
120 Send(new GeofencingHostMsg_GetRegisteredRegions(
121 CurrentWorkerId(), request_id, serviceworker_registration_id));
124 void GeofencingDispatcher::SetMockProvider(bool service_available) {
125 Send(new GeofencingHostMsg_SetMockProvider(
126 service_available ? GeofencingMockState::SERVICE_AVAILABLE
127 : GeofencingMockState::SERVICE_UNAVAILABLE));
130 void GeofencingDispatcher::ClearMockProvider() {
131 Send(new GeofencingHostMsg_SetMockProvider(GeofencingMockState::NONE));
134 void GeofencingDispatcher::SetMockPosition(double latitude, double longitude) {
135 Send(new GeofencingHostMsg_SetMockPosition(latitude, longitude));
138 GeofencingDispatcher* GeofencingDispatcher::GetOrCreateThreadSpecificInstance(
139 ThreadSafeSender* thread_safe_sender) {
140 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
141 NOTREACHED() << "Re-instantiating TLS GeofencingDispatcher.";
142 g_dispatcher_tls.Pointer()->Set(NULL);
144 if (g_dispatcher_tls.Pointer()->Get())
145 return g_dispatcher_tls.Pointer()->Get();
147 GeofencingDispatcher* dispatcher =
148 new GeofencingDispatcher(thread_safe_sender);
149 if (WorkerTaskRunner::Instance()->CurrentWorkerId())
150 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher);
151 return dispatcher;
154 GeofencingDispatcher* GeofencingDispatcher::GetThreadSpecificInstance() {
155 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted)
156 return NULL;
157 return g_dispatcher_tls.Pointer()->Get();
160 void GeofencingDispatcher::OnRegisterRegionComplete(int thread_id,
161 int request_id,
162 GeofencingStatus status) {
163 blink::WebGeofencingCallbacks* callbacks =
164 region_registration_requests_.Lookup(request_id);
165 DCHECK(callbacks);
167 if (status == GEOFENCING_STATUS_OK) {
168 callbacks->onSuccess();
169 } else {
170 callbacks->onError(new WebGeofencingError(
171 WebGeofencingError::ErrorTypeAbort,
172 blink::WebString::fromUTF8(GeofencingStatusToString(status))));
174 region_registration_requests_.Remove(request_id);
177 void GeofencingDispatcher::OnUnregisterRegionComplete(int thread_id,
178 int request_id,
179 GeofencingStatus status) {
180 blink::WebGeofencingCallbacks* callbacks =
181 region_unregistration_requests_.Lookup(request_id);
182 DCHECK(callbacks);
184 if (status == GEOFENCING_STATUS_OK) {
185 callbacks->onSuccess();
186 } else {
187 callbacks->onError(new WebGeofencingError(
188 WebGeofencingError::ErrorTypeAbort,
189 blink::WebString::fromUTF8(GeofencingStatusToString(status))));
191 region_unregistration_requests_.Remove(request_id);
194 void GeofencingDispatcher::OnGetRegisteredRegionsComplete(
195 int thread_id,
196 int request_id,
197 GeofencingStatus status,
198 const GeofencingRegistrations& regions) {
199 blink::WebGeofencingRegionsCallbacks* callbacks =
200 get_registered_regions_requests_.Lookup(request_id);
201 DCHECK(callbacks);
203 if (status == GEOFENCING_STATUS_OK) {
204 scoped_ptr<blink::WebVector<blink::WebGeofencingRegistration>> result(
205 new blink::WebVector<blink::WebGeofencingRegistration>(regions.size()));
206 size_t index = 0;
207 for (GeofencingRegistrations::const_iterator it = regions.begin();
208 it != regions.end();
209 ++it, ++index) {
210 (*result)[index].id = blink::WebString::fromUTF8(it->first);
211 (*result)[index].region = it->second;
213 callbacks->onSuccess(result.release());
214 } else {
215 callbacks->onError(new WebGeofencingError(
216 WebGeofencingError::ErrorTypeAbort,
217 blink::WebString::fromUTF8(GeofencingStatusToString(status))));
219 get_registered_regions_requests_.Remove(request_id);
222 void GeofencingDispatcher::OnWorkerRunLoopStopped() {
223 delete this;
226 } // namespace content