Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: content/browser/geofencing/geofencing_manager.cc

Issue 1549113002: Switch to standard integer types in content/browser/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/browser/geofencing/geofencing_manager.h" 5 #include "content/browser/geofencing/geofencing_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "content/browser/geofencing/geofencing_service.h" 10 #include "content/browser/geofencing/geofencing_service.h"
11 #include "content/browser/geofencing/mock_geofencing_service.h" 11 #include "content/browser/geofencing/mock_geofencing_service.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h" 12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/public/browser/browser_context.h" 13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/storage_partition.h" 15 #include "content/public/browser/storage_partition.h"
16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" 16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
17 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" 17 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
18 #include "url/gurl.h" 18 #include "url/gurl.h"
19 19
20 namespace content { 20 namespace content {
21 21
22 struct GeofencingManager::Registration { 22 struct GeofencingManager::Registration {
23 Registration(int64 service_worker_registration_id, 23 Registration(int64_t service_worker_registration_id,
24 const GURL& service_worker_origin, 24 const GURL& service_worker_origin,
25 const std::string& region_id, 25 const std::string& region_id,
26 const blink::WebCircularGeofencingRegion& region, 26 const blink::WebCircularGeofencingRegion& region,
27 const StatusCallback& callback, 27 const StatusCallback& callback,
28 int64 geofencing_registration_id); 28 int64_t geofencing_registration_id);
29 int64 service_worker_registration_id; 29 int64_t service_worker_registration_id;
30 GURL service_worker_origin; 30 GURL service_worker_origin;
31 std::string region_id; 31 std::string region_id;
32 blink::WebCircularGeofencingRegion region; 32 blink::WebCircularGeofencingRegion region;
33 33
34 // Registration ID as returned by the |GeofencingService|. 34 // Registration ID as returned by the |GeofencingService|.
35 int64 geofencing_registration_id; 35 int64_t geofencing_registration_id;
36 36
37 // Callback to call when registration is completed. This field is reset when 37 // Callback to call when registration is completed. This field is reset when
38 // registration is complete. 38 // registration is complete.
39 StatusCallback registration_callback; 39 StatusCallback registration_callback;
40 40
41 // Returns true if registration has been completed, and thus should be 41 // Returns true if registration has been completed, and thus should be
42 // included in calls to GetRegisteredRegions. 42 // included in calls to GetRegisteredRegions.
43 bool is_active() const { return registration_callback.is_null(); } 43 bool is_active() const { return registration_callback.is_null(); }
44 }; 44 };
45 45
46 GeofencingManager::Registration::Registration( 46 GeofencingManager::Registration::Registration(
47 int64 service_worker_registration_id, 47 int64_t service_worker_registration_id,
48 const GURL& service_worker_origin, 48 const GURL& service_worker_origin,
49 const std::string& region_id, 49 const std::string& region_id,
50 const blink::WebCircularGeofencingRegion& region, 50 const blink::WebCircularGeofencingRegion& region,
51 const GeofencingManager::StatusCallback& callback, 51 const GeofencingManager::StatusCallback& callback,
52 int64 geofencing_registration_id) 52 int64_t geofencing_registration_id)
53 : service_worker_registration_id(service_worker_registration_id), 53 : service_worker_registration_id(service_worker_registration_id),
54 service_worker_origin(service_worker_origin), 54 service_worker_origin(service_worker_origin),
55 region_id(region_id), 55 region_id(region_id),
56 region(region), 56 region(region),
57 geofencing_registration_id(geofencing_registration_id), 57 geofencing_registration_id(geofencing_registration_id),
58 registration_callback(callback) { 58 registration_callback(callback) {}
59 }
60 59
61 GeofencingManager::GeofencingManager( 60 GeofencingManager::GeofencingManager(
62 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) 61 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
63 : service_(nullptr), service_worker_context_(service_worker_context) { 62 : service_(nullptr), service_worker_context_(service_worker_context) {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); 63 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 } 64 }
66 65
67 GeofencingManager::~GeofencingManager() { 66 GeofencingManager::~GeofencingManager() {
68 } 67 }
69 68
(...skipping 23 matching lines...) Expand all
93 service_worker_context_->RemoveObserver(this); 92 service_worker_context_->RemoveObserver(this);
94 // Clean up all registrations with the |GeofencingService|. 93 // Clean up all registrations with the |GeofencingService|.
95 // TODO(mek): This will need to change to support geofence registrations that 94 // TODO(mek): This will need to change to support geofence registrations that
96 // outlive the browser, although removing the references to this 95 // outlive the browser, although removing the references to this
97 // |GeofencingManager| from the |GeofencingService| will still be needed. 96 // |GeofencingManager| from the |GeofencingService| will still be needed.
98 for (const auto& registration : registrations_by_id_) 97 for (const auto& registration : registrations_by_id_)
99 service_->UnregisterRegion(registration.first); 98 service_->UnregisterRegion(registration.first);
100 } 99 }
101 100
102 void GeofencingManager::RegisterRegion( 101 void GeofencingManager::RegisterRegion(
103 int64 service_worker_registration_id, 102 int64_t service_worker_registration_id,
104 const std::string& region_id, 103 const std::string& region_id,
105 const blink::WebCircularGeofencingRegion& region, 104 const blink::WebCircularGeofencingRegion& region,
106 const StatusCallback& callback) { 105 const StatusCallback& callback) {
107 DCHECK_CURRENTLY_ON(BrowserThread::IO); 106 DCHECK_CURRENTLY_ON(BrowserThread::IO);
108 107
109 // TODO(mek): Validate region_id and region. 108 // TODO(mek): Validate region_id and region.
110 109
111 // Look up service worker. 110 // Look up service worker.
112 ServiceWorkerRegistration* service_worker_registration = 111 ServiceWorkerRegistration* service_worker_registration =
113 service_worker_context_->GetLiveRegistration( 112 service_worker_context_->GetLiveRegistration(
(...skipping 16 matching lines...) Expand all
130 // TODO(mek): Use a more specific error code. 129 // TODO(mek): Use a more specific error code.
131 callback.Run(GEOFENCING_STATUS_ERROR); 130 callback.Run(GEOFENCING_STATUS_ERROR);
132 return; 131 return;
133 } 132 }
134 133
135 AddRegistration(service_worker_registration_id, service_worker_origin, 134 AddRegistration(service_worker_registration_id, service_worker_origin,
136 region_id, region, callback, 135 region_id, region, callback,
137 service_->RegisterRegion(region, this)); 136 service_->RegisterRegion(region, this));
138 } 137 }
139 138
140 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, 139 void GeofencingManager::UnregisterRegion(int64_t service_worker_registration_id,
141 const std::string& region_id, 140 const std::string& region_id,
142 const StatusCallback& callback) { 141 const StatusCallback& callback) {
143 DCHECK_CURRENTLY_ON(BrowserThread::IO); 142 DCHECK_CURRENTLY_ON(BrowserThread::IO);
144 143
145 // TODO(mek): Validate region_id. 144 // TODO(mek): Validate region_id.
146 145
147 // Look up service worker. 146 // Look up service worker.
148 ServiceWorkerRegistration* service_worker_registration = 147 ServiceWorkerRegistration* service_worker_registration =
149 service_worker_context_->GetLiveRegistration( 148 service_worker_context_->GetLiveRegistration(
150 service_worker_registration_id); 149 service_worker_registration_id);
(...skipping 20 matching lines...) Expand all
171 callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED); 170 callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED);
172 return; 171 return;
173 } 172 }
174 173
175 service_->UnregisterRegion(registration->geofencing_registration_id); 174 service_->UnregisterRegion(registration->geofencing_registration_id);
176 ClearRegistration(registration); 175 ClearRegistration(registration);
177 callback.Run(GEOFENCING_STATUS_OK); 176 callback.Run(GEOFENCING_STATUS_OK);
178 } 177 }
179 178
180 GeofencingStatus GeofencingManager::GetRegisteredRegions( 179 GeofencingStatus GeofencingManager::GetRegisteredRegions(
181 int64 service_worker_registration_id, 180 int64_t service_worker_registration_id,
182 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { 181 std::map<std::string, blink::WebCircularGeofencingRegion>* result) {
183 DCHECK_CURRENTLY_ON(BrowserThread::IO); 182 DCHECK_CURRENTLY_ON(BrowserThread::IO);
184 CHECK(result); 183 CHECK(result);
185 184
186 // Look up service worker. 185 // Look up service worker.
187 ServiceWorkerRegistration* service_worker_registration = 186 ServiceWorkerRegistration* service_worker_registration =
188 service_worker_context_->GetLiveRegistration( 187 service_worker_context_->GetLiveRegistration(
189 service_worker_registration_id); 188 service_worker_registration_id);
190 if (!service_worker_registration) { 189 if (!service_worker_registration) {
191 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER; 190 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 } 232 }
234 233
235 void GeofencingManager::SetMockPosition(double latitude, double longitude) { 234 void GeofencingManager::SetMockPosition(double latitude, double longitude) {
236 DCHECK_CURRENTLY_ON(BrowserThread::IO); 235 DCHECK_CURRENTLY_ON(BrowserThread::IO);
237 if (!mock_service_) 236 if (!mock_service_)
238 return; 237 return;
239 mock_service_->SetMockPosition(latitude, longitude); 238 mock_service_->SetMockPosition(latitude, longitude);
240 } 239 }
241 240
242 void GeofencingManager::OnRegistrationDeleted( 241 void GeofencingManager::OnRegistrationDeleted(
243 int64 service_worker_registration_id, 242 int64_t service_worker_registration_id,
244 const GURL& pattern) { 243 const GURL& pattern) {
245 DCHECK_CURRENTLY_ON(BrowserThread::IO); 244 DCHECK_CURRENTLY_ON(BrowserThread::IO);
246 CleanUpForServiceWorker(service_worker_registration_id); 245 CleanUpForServiceWorker(service_worker_registration_id);
247 } 246 }
248 247
249 void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id, 248 void GeofencingManager::RegistrationFinished(int64_t geofencing_registration_id,
250 GeofencingStatus status) { 249 GeofencingStatus status) {
251 DCHECK_CURRENTLY_ON(BrowserThread::IO); 250 DCHECK_CURRENTLY_ON(BrowserThread::IO);
252 Registration* registration = FindRegistrationById(geofencing_registration_id); 251 Registration* registration = FindRegistrationById(geofencing_registration_id);
253 DCHECK(registration); 252 DCHECK(registration);
254 DCHECK(!registration->is_active()); 253 DCHECK(!registration->is_active());
255 registration->registration_callback.Run(status); 254 registration->registration_callback.Run(status);
256 registration->registration_callback.Reset(); 255 registration->registration_callback.Reset();
257 256
258 // If the registration wasn't succesful, remove it from our storage. 257 // If the registration wasn't succesful, remove it from our storage.
259 if (status != GEOFENCING_STATUS_OK) 258 if (status != GEOFENCING_STATUS_OK)
260 ClearRegistration(registration); 259 ClearRegistration(registration);
261 } 260 }
262 261
263 void GeofencingManager::RegionEntered(int64 geofencing_registration_id) { 262 void GeofencingManager::RegionEntered(int64_t geofencing_registration_id) {
264 DispatchGeofencingEvent(blink::WebGeofencingEventTypeEnter, 263 DispatchGeofencingEvent(blink::WebGeofencingEventTypeEnter,
265 geofencing_registration_id); 264 geofencing_registration_id);
266 } 265 }
267 266
268 void GeofencingManager::RegionExited(int64 geofencing_registration_id) { 267 void GeofencingManager::RegionExited(int64_t geofencing_registration_id) {
269 DispatchGeofencingEvent(blink::WebGeofencingEventTypeLeave, 268 DispatchGeofencingEvent(blink::WebGeofencingEventTypeLeave,
270 geofencing_registration_id); 269 geofencing_registration_id);
271 } 270 }
272 271
273 GeofencingManager::Registration* GeofencingManager::FindRegistration( 272 GeofencingManager::Registration* GeofencingManager::FindRegistration(
274 int64 service_worker_registration_id, 273 int64_t service_worker_registration_id,
275 const std::string& region_id) { 274 const std::string& region_id) {
276 DCHECK_CURRENTLY_ON(BrowserThread::IO); 275 DCHECK_CURRENTLY_ON(BrowserThread::IO);
277 ServiceWorkerRegistrationsMap::iterator registrations_iterator = 276 ServiceWorkerRegistrationsMap::iterator registrations_iterator =
278 registrations_.find(service_worker_registration_id); 277 registrations_.find(service_worker_registration_id);
279 if (registrations_iterator == registrations_.end()) 278 if (registrations_iterator == registrations_.end())
280 return nullptr; 279 return nullptr;
281 RegionIdRegistrationMap::iterator registration = 280 RegionIdRegistrationMap::iterator registration =
282 registrations_iterator->second.find(region_id); 281 registrations_iterator->second.find(region_id);
283 if (registration == registrations_iterator->second.end()) 282 if (registration == registrations_iterator->second.end())
284 return nullptr; 283 return nullptr;
285 return &registration->second; 284 return &registration->second;
286 } 285 }
287 286
288 GeofencingManager::Registration* GeofencingManager::FindRegistrationById( 287 GeofencingManager::Registration* GeofencingManager::FindRegistrationById(
289 int64 geofencing_registration_id) { 288 int64_t geofencing_registration_id) {
290 DCHECK_CURRENTLY_ON(BrowserThread::IO); 289 DCHECK_CURRENTLY_ON(BrowserThread::IO);
291 RegistrationIdRegistrationMap::iterator registration_iterator = 290 RegistrationIdRegistrationMap::iterator registration_iterator =
292 registrations_by_id_.find(geofencing_registration_id); 291 registrations_by_id_.find(geofencing_registration_id);
293 if (registration_iterator == registrations_by_id_.end()) 292 if (registration_iterator == registrations_by_id_.end())
294 return nullptr; 293 return nullptr;
295 return &registration_iterator->second->second; 294 return &registration_iterator->second->second;
296 } 295 }
297 296
298 GeofencingManager::Registration& GeofencingManager::AddRegistration( 297 GeofencingManager::Registration& GeofencingManager::AddRegistration(
299 int64 service_worker_registration_id, 298 int64_t service_worker_registration_id,
300 const GURL& service_worker_origin, 299 const GURL& service_worker_origin,
301 const std::string& region_id, 300 const std::string& region_id,
302 const blink::WebCircularGeofencingRegion& region, 301 const blink::WebCircularGeofencingRegion& region,
303 const StatusCallback& callback, 302 const StatusCallback& callback,
304 int64 geofencing_registration_id) { 303 int64_t geofencing_registration_id) {
305 DCHECK_CURRENTLY_ON(BrowserThread::IO); 304 DCHECK_CURRENTLY_ON(BrowserThread::IO);
306 DCHECK(!FindRegistration(service_worker_registration_id, region_id)); 305 DCHECK(!FindRegistration(service_worker_registration_id, region_id));
307 RegionIdRegistrationMap::iterator registration = 306 RegionIdRegistrationMap::iterator registration =
308 registrations_[service_worker_registration_id] 307 registrations_[service_worker_registration_id]
309 .insert(std::make_pair(region_id, 308 .insert(std::make_pair(region_id,
310 Registration(service_worker_registration_id, 309 Registration(service_worker_registration_id,
311 service_worker_origin, 310 service_worker_origin,
312 region_id, 311 region_id,
313 region, 312 region,
314 callback, 313 callback,
315 geofencing_registration_id))) 314 geofencing_registration_id)))
316 .first; 315 .first;
317 registrations_by_id_[geofencing_registration_id] = registration; 316 registrations_by_id_[geofencing_registration_id] = registration;
318 return registration->second; 317 return registration->second;
319 } 318 }
320 319
321 void GeofencingManager::ClearRegistration(Registration* registration) { 320 void GeofencingManager::ClearRegistration(Registration* registration) {
322 DCHECK_CURRENTLY_ON(BrowserThread::IO); 321 DCHECK_CURRENTLY_ON(BrowserThread::IO);
323 registrations_by_id_.erase(registration->geofencing_registration_id); 322 registrations_by_id_.erase(registration->geofencing_registration_id);
324 ServiceWorkerRegistrationsMap::iterator registrations_iterator = 323 ServiceWorkerRegistrationsMap::iterator registrations_iterator =
325 registrations_.find(registration->service_worker_registration_id); 324 registrations_.find(registration->service_worker_registration_id);
326 DCHECK(registrations_iterator != registrations_.end()); 325 DCHECK(registrations_iterator != registrations_.end());
327 registrations_iterator->second.erase(registration->region_id); 326 registrations_iterator->second.erase(registration->region_id);
328 if (registrations_iterator->second.empty()) 327 if (registrations_iterator->second.empty())
329 registrations_.erase(registrations_iterator); 328 registrations_.erase(registrations_iterator);
330 } 329 }
331 330
332 void GeofencingManager::CleanUpForServiceWorker( 331 void GeofencingManager::CleanUpForServiceWorker(
333 int64 service_worker_registration_id) { 332 int64_t service_worker_registration_id) {
334 DCHECK_CURRENTLY_ON(BrowserThread::IO); 333 DCHECK_CURRENTLY_ON(BrowserThread::IO);
335 ServiceWorkerRegistrationsMap::iterator registrations_iterator = 334 ServiceWorkerRegistrationsMap::iterator registrations_iterator =
336 registrations_.find(service_worker_registration_id); 335 registrations_.find(service_worker_registration_id);
337 if (registrations_iterator == registrations_.end()) 336 if (registrations_iterator == registrations_.end())
338 return; 337 return;
339 338
340 for (const auto& registration : registrations_iterator->second) { 339 for (const auto& registration : registrations_iterator->second) {
341 int geofencing_registration_id = 340 int geofencing_registration_id =
342 registration.second.geofencing_registration_id; 341 registration.second.geofencing_registration_id;
343 service_->UnregisterRegion(geofencing_registration_id); 342 service_->UnregisterRegion(geofencing_registration_id);
344 registrations_by_id_.erase(geofencing_registration_id); 343 registrations_by_id_.erase(geofencing_registration_id);
345 } 344 }
346 registrations_.erase(service_worker_registration_id); 345 registrations_.erase(service_worker_registration_id);
347 } 346 }
348 347
349 void GeofencingManager::DispatchGeofencingEvent( 348 void GeofencingManager::DispatchGeofencingEvent(
350 blink::WebGeofencingEventType event_type, 349 blink::WebGeofencingEventType event_type,
351 int64 geofencing_registration_id) { 350 int64_t geofencing_registration_id) {
352 DCHECK_CURRENTLY_ON(BrowserThread::IO); 351 DCHECK_CURRENTLY_ON(BrowserThread::IO);
353 Registration* registration = FindRegistrationById(geofencing_registration_id); 352 Registration* registration = FindRegistrationById(geofencing_registration_id);
354 if (!registration || 353 if (!registration ||
355 registration->service_worker_registration_id == 354 registration->service_worker_registration_id ==
356 kInvalidServiceWorkerRegistrationId) { 355 kInvalidServiceWorkerRegistrationId) {
357 // TODO(mek): Log/track these failures. 356 // TODO(mek): Log/track these failures.
358 return; 357 return;
359 } 358 }
360 359
361 service_worker_context_->FindReadyRegistrationForId( 360 service_worker_context_->FindReadyRegistrationForId(
362 registration->service_worker_registration_id, 361 registration->service_worker_registration_id,
363 registration->service_worker_origin, 362 registration->service_worker_origin,
364 base::Bind(&GeofencingManager::DeliverGeofencingEvent, 363 base::Bind(&GeofencingManager::DeliverGeofencingEvent,
365 this, 364 this,
366 event_type, 365 event_type,
367 geofencing_registration_id)); 366 geofencing_registration_id));
368 } 367 }
369 368
370 void GeofencingManager::DeliverGeofencingEvent( 369 void GeofencingManager::DeliverGeofencingEvent(
371 blink::WebGeofencingEventType event_type, 370 blink::WebGeofencingEventType event_type,
372 int64 geofencing_registration_id, 371 int64_t geofencing_registration_id,
373 ServiceWorkerStatusCode service_worker_status, 372 ServiceWorkerStatusCode service_worker_status,
374 const scoped_refptr<ServiceWorkerRegistration>& 373 const scoped_refptr<ServiceWorkerRegistration>&
375 service_worker_registration) { 374 service_worker_registration) {
376 DCHECK_CURRENTLY_ON(BrowserThread::IO); 375 DCHECK_CURRENTLY_ON(BrowserThread::IO);
377 Registration* registration = FindRegistrationById(geofencing_registration_id); 376 Registration* registration = FindRegistrationById(geofencing_registration_id);
378 if (!registration) { 377 if (!registration) {
379 // Geofence got unregistered in the meantime, no longer need to deliver 378 // Geofence got unregistered in the meantime, no longer need to deliver
380 // event. 379 // event.
381 return; 380 return;
382 } 381 }
(...skipping 21 matching lines...) Expand all
404 } 403 }
405 404
406 void GeofencingManager::DeliverGeofencingEventEnd( 405 void GeofencingManager::DeliverGeofencingEventEnd(
407 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 406 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
408 ServiceWorkerStatusCode service_worker_status) { 407 ServiceWorkerStatusCode service_worker_status) {
409 DCHECK_CURRENTLY_ON(BrowserThread::IO); 408 DCHECK_CURRENTLY_ON(BrowserThread::IO);
410 // TODO(mek): log/check result. 409 // TODO(mek): log/check result.
411 } 410 }
412 411
413 } // namespace content 412 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geofencing/geofencing_manager.h ('k') | content/browser/geofencing/geofencing_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698