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

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

Issue 701953007: Expose mock geofencing service via testRunner. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mention unifying bug in a comment Created 6 years 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
(Empty)
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 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
7
8 #include "content/browser/geofencing/mock_geofencing_service.h"
9
10 #include <cmath>
11
12 #include "base/bind.h"
13 #include "base/message_loop/message_loop.h"
14 #include "content/browser/geofencing/geofencing_registration_delegate.h"
15 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
16
17 namespace content {
18
19 namespace {
20
21 void RegisterRegionResult(GeofencingRegistrationDelegate* delegate,
22 int64 geofencing_registration_id,
23 GeofencingStatus status) {
24 base::MessageLoop::current()->PostTask(
25 FROM_HERE,
26 base::Bind(&GeofencingRegistrationDelegate::RegistrationFinished,
27 base::Unretained(delegate), geofencing_registration_id,
28 status));
29 }
30
31 double DegreesToRadians(float degrees) {
32 return (M_PI * degrees) / 180.f;
33 }
34
35 double Haversin(double theta) {
36 double temp = sin(theta / 2);
37 return temp * temp;
38 }
39
40 // Calculates the distance in meters between two points with coordinates in
41 // degrees.
42 double Distance(double lat1, double long1, double lat2, double long2) {
43 double R = 6371000; // radius of earth in meters
44 double phi1 = DegreesToRadians(lat1);
45 double phi2 = DegreesToRadians(lat2);
46 double dphi = DegreesToRadians(lat2 - lat1);
47 double dlambda = DegreesToRadians(long2 - long1);
48 double haversine = Haversin(dphi) + cos(phi1) * cos(phi2) * Haversin(dlambda);
49 return 2 * R * asin(sqrt(haversine));
50 }
51
52 // Returns true iff the provided coordinate is inside the region.
53 bool PositionInRegion(double latitude,
54 double longitude,
55 const blink::WebCircularGeofencingRegion& region) {
56 return Distance(latitude, longitude, region.latitude, region.longitude) <=
57 region.radius;
58 }
59 }
60
61 struct MockGeofencingService::Registration {
62 blink::WebCircularGeofencingRegion region;
63 GeofencingRegistrationDelegate* delegate;
64 // True iff the last event emitted for this region was a RegionEntered event.
65 bool is_inside;
66 };
67
68 MockGeofencingService::MockGeofencingService(bool service_available)
69 : available_(service_available),
70 next_id_(0),
71 has_position_(false),
72 last_latitude_(0),
73 last_longitude_(0) {
74 }
75
76 MockGeofencingService::~MockGeofencingService() {
77 }
78
79 void MockGeofencingService::SetMockPosition(double latitude, double longitude) {
80 has_position_ = true;
81 last_latitude_ = latitude;
82 last_longitude_ = longitude;
83 for (auto& registration : registrations_) {
84 bool is_inside =
85 PositionInRegion(latitude, longitude, registration.second.region);
86 if (is_inside != registration.second.is_inside) {
87 if (is_inside)
88 registration.second.delegate->RegionEntered(registration.first);
89 else
90 registration.second.delegate->RegionExited(registration.first);
91 }
92 registration.second.is_inside = is_inside;
93 }
94 }
95
96 bool MockGeofencingService::IsServiceAvailable() {
97 return available_;
98 }
99
100 int64 MockGeofencingService::RegisterRegion(
101 const blink::WebCircularGeofencingRegion& region,
102 GeofencingRegistrationDelegate* delegate) {
103 int64 id = next_id_++;
104 Registration& registration = registrations_[id];
105 registration.region = region;
106 registration.delegate = delegate;
107 registration.is_inside =
108 has_position_ &&
109 PositionInRegion(last_latitude_, last_longitude_, region);
110 RegisterRegionResult(delegate, id, GEOFENCING_STATUS_OK);
111 if (registration.is_inside) {
112 base::MessageLoop::current()->PostTask(
113 FROM_HERE, base::Bind(&GeofencingRegistrationDelegate::RegionEntered,
114 base::Unretained(delegate), id));
115 }
116 return id;
117 }
118
119 void MockGeofencingService::UnregisterRegion(int64 geofencing_registration_id) {
120 registrations_.erase(geofencing_registration_id);
121 }
122
123 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geofencing/mock_geofencing_service.h ('k') | content/child/geofencing/geofencing_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698