| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/extensions/api/location/location_manager.h" | 5 #include "chrome/browser/extensions/api/location/location_manager.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // A policy that controls sending an update below a distance threshold. | 52 // A policy that controls sending an update below a distance threshold. |
| 53 class DistanceBasedUpdatePolicy : public UpdatePolicy { | 53 class DistanceBasedUpdatePolicy : public UpdatePolicy { |
| 54 public: | 54 public: |
| 55 explicit DistanceBasedUpdatePolicy(double distance_update_threshold_meters) : | 55 explicit DistanceBasedUpdatePolicy(double distance_update_threshold_meters) : |
| 56 distance_update_threshold_meters_(distance_update_threshold_meters) | 56 distance_update_threshold_meters_(distance_update_threshold_meters) |
| 57 {} | 57 {} |
| 58 | 58 |
| 59 // UpdatePolicy Implementation | 59 // UpdatePolicy Implementation |
| 60 virtual bool ShouldSendUpdate(const content::Geoposition& position) const | 60 bool ShouldSendUpdate(const content::Geoposition& position) const override { |
| 61 override { | |
| 62 return !last_updated_position_.Validate() || | 61 return !last_updated_position_.Validate() || |
| 63 Distance(position.latitude, | 62 Distance(position.latitude, |
| 64 position.longitude, | 63 position.longitude, |
| 65 last_updated_position_.latitude, | 64 last_updated_position_.latitude, |
| 66 last_updated_position_.longitude) > | 65 last_updated_position_.longitude) > |
| 67 distance_update_threshold_meters_; | 66 distance_update_threshold_meters_; |
| 68 } | 67 } |
| 69 | 68 |
| 70 virtual void OnPositionReported(const content::Geoposition& position) | 69 void OnPositionReported(const content::Geoposition& position) override { |
| 71 override { | |
| 72 last_updated_position_ = position; | 70 last_updated_position_ = position; |
| 73 } | 71 } |
| 74 | 72 |
| 75 private: | 73 private: |
| 76 virtual ~DistanceBasedUpdatePolicy() {} | 74 ~DistanceBasedUpdatePolicy() override {} |
| 77 | 75 |
| 78 // Calculates the distance between two latitude and longitude points. | 76 // Calculates the distance between two latitude and longitude points. |
| 79 static double Distance(const double latitude1, | 77 static double Distance(const double latitude1, |
| 80 const double longitude1, | 78 const double longitude1, |
| 81 const double latitude2, | 79 const double latitude2, |
| 82 const double longitude2) { | 80 const double longitude2) { |
| 83 // The earth has a radius of about 6371 km. | 81 // The earth has a radius of about 6371 km. |
| 84 const double kRadius = 6371000; | 82 const double kRadius = 6371000; |
| 85 const double kPi = 3.14159265358979323846; | 83 const double kPi = 3.14159265358979323846; |
| 86 const double kDegreesToRadians = kPi / 180.0; | 84 const double kDegreesToRadians = kPi / 180.0; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 110 }; | 108 }; |
| 111 | 109 |
| 112 // A policy that controls sending an update above a time threshold. | 110 // A policy that controls sending an update above a time threshold. |
| 113 class TimeBasedUpdatePolicy : public UpdatePolicy { | 111 class TimeBasedUpdatePolicy : public UpdatePolicy { |
| 114 public: | 112 public: |
| 115 explicit TimeBasedUpdatePolicy(double time_between_updates_ms) : | 113 explicit TimeBasedUpdatePolicy(double time_between_updates_ms) : |
| 116 time_between_updates_ms_(time_between_updates_ms) | 114 time_between_updates_ms_(time_between_updates_ms) |
| 117 {} | 115 {} |
| 118 | 116 |
| 119 // UpdatePolicy Implementation | 117 // UpdatePolicy Implementation |
| 120 virtual bool ShouldSendUpdate(const content::Geoposition&) const override { | 118 bool ShouldSendUpdate(const content::Geoposition&) const override { |
| 121 return (base::Time::Now() - last_update_time_).InMilliseconds() > | 119 return (base::Time::Now() - last_update_time_).InMilliseconds() > |
| 122 time_between_updates_ms_; | 120 time_between_updates_ms_; |
| 123 } | 121 } |
| 124 | 122 |
| 125 virtual void OnPositionReported(const content::Geoposition&) override { | 123 void OnPositionReported(const content::Geoposition&) override { |
| 126 last_update_time_ = base::Time::Now(); | 124 last_update_time_ = base::Time::Now(); |
| 127 } | 125 } |
| 128 | 126 |
| 129 private: | 127 private: |
| 130 virtual ~TimeBasedUpdatePolicy() {} | 128 ~TimeBasedUpdatePolicy() override {} |
| 131 | 129 |
| 132 base::Time last_update_time_; | 130 base::Time last_update_time_; |
| 133 const double time_between_updates_ms_; | 131 const double time_between_updates_ms_; |
| 134 | 132 |
| 135 DISALLOW_COPY_AND_ASSIGN(TimeBasedUpdatePolicy); | 133 DISALLOW_COPY_AND_ASSIGN(TimeBasedUpdatePolicy); |
| 136 }; | 134 }; |
| 137 | 135 |
| 138 } // namespace updatepolicy | 136 } // namespace updatepolicy |
| 139 | 137 |
| 140 // Request created by chrome.location.watchLocation() call. | 138 // Request created by chrome.location.watchLocation() call. |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 LocationManager::GetFactoryInstance() { | 366 LocationManager::GetFactoryInstance() { |
| 369 return g_factory.Pointer(); | 367 return g_factory.Pointer(); |
| 370 } | 368 } |
| 371 | 369 |
| 372 // static | 370 // static |
| 373 LocationManager* LocationManager::Get(content::BrowserContext* context) { | 371 LocationManager* LocationManager::Get(content::BrowserContext* context) { |
| 374 return BrowserContextKeyedAPIFactory<LocationManager>::Get(context); | 372 return BrowserContextKeyedAPIFactory<LocationManager>::Get(context); |
| 375 } | 373 } |
| 376 | 374 |
| 377 } // namespace extensions | 375 } // namespace extensions |
| OLD | NEW |