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

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: 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 bool is_inside;
65 };
66
67 MockGeofencingService::MockGeofencingService(bool service_available)
68 : available_(service_available),
69 next_id_(0),
70 has_position_(false),
71 last_latitude_(0),
72 last_longitude_(0) {
73 }
74
75 MockGeofencingService::~MockGeofencingService() {
76 }
77
78 void MockGeofencingService::SetMockPosition(double latitude, double longitude) {
79 has_position_ = true;
80 last_latitude_ = latitude;
81 last_longitude_ = longitude;
82 for (auto& registration : registrations_) {
83 bool is_inside =
84 PositionInRegion(latitude, longitude, registration.second.region);
85 if (is_inside != registration.second.is_inside) {
86 if (is_inside)
87 registration.second.delegate->RegionEntered(registration.first);
88 else
89 registration.second.delegate->RegionExited(registration.first);
90 }
91 registration.second.is_inside = is_inside;
92 }
93 }
94
95 bool MockGeofencingService::IsServiceAvailable() {
96 return available_;
97 }
98
99 int64 MockGeofencingService::RegisterRegion(
100 const blink::WebCircularGeofencingRegion& region,
101 GeofencingRegistrationDelegate* delegate) {
102 int64 id = next_id_++;
103 Registration& registration = registrations_[id];
104 registration.region = region;
105 registration.delegate = delegate;
106 registration.is_inside =
107 has_position_ &&
108 PositionInRegion(last_latitude_, last_longitude_, region);
109 RegisterRegionResult(delegate, id, GEOFENCING_STATUS_OK);
110 if (registration.is_inside) {
111 base::MessageLoop::current()->PostTask(
112 FROM_HERE, base::Bind(&GeofencingRegistrationDelegate::RegionEntered,
113 base::Unretained(delegate), id));
114 }
115 return id;
116 }
117
118 void MockGeofencingService::UnregisterRegion(int64 geofencing_registration_id) {
119 registrations_.erase(geofencing_registration_id);
120 }
121
122 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698