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

Unified Diff: chrome/browser/policy/device_status_collector.cc

Issue 10103029: Add device location reporting (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: New implementation on top of a new content geolocation API. Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/device_status_collector.cc
diff --git a/chrome/browser/policy/device_status_collector.cc b/chrome/browser/policy/device_status_collector.cc
index 5a1be4a47e71c915d8cacb835f309389be5cc942..88cd361eabf6ecd46f4f6c72d7fe036104d6dab1 100644
--- a/chrome/browser/policy/device_status_collector.cc
+++ b/chrome/browser/policy/device_status_collector.cc
@@ -5,8 +5,12 @@
#include "chrome/browser/policy/device_status_collector.h"
#include "base/bind.h"
-#include "base/callback.h"
+#include "base/bind_helpers.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
#include "base/string_number_conversions.h"
+#include "base/values.h"
#include "chrome/browser/chromeos/cros_settings.h"
#include "chrome/browser/chromeos/cros_settings_names.h"
#include "chrome/browser/chromeos/system/statistics_provider.h"
@@ -16,6 +20,8 @@
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_source.h"
using base::Time;
using base::TimeDelta;
@@ -33,6 +39,9 @@ const unsigned int kMaxStoredPastActivityDays = 30;
// How many days in the future to store active periods for.
const unsigned int kMaxStoredFutureActivityDays = 2;
+// How often, in seconds, to update the device location.
+const unsigned int kGeolocationPollIntervalSeconds = 30 * 60;
+
const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000;
// Record device activity for the specified day into the given dictionary.
@@ -55,18 +64,25 @@ namespace policy {
DeviceStatusCollector::DeviceStatusCollector(
PrefService* local_state,
- chromeos::system::StatisticsProvider* provider)
+ chromeos::system::StatisticsProvider* provider,
+ LocationUpdateRequester location_update_requester)
: max_stored_past_activity_days_(kMaxStoredPastActivityDays),
max_stored_future_activity_days_(kMaxStoredFutureActivityDays),
local_state_(local_state),
last_idle_check_(Time()),
+ geolocation_update_in_progress_(false),
statistics_provider_(provider),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)),
+ location_update_requester_(location_update_requester),
report_version_info_(false),
report_activity_times_(false),
- report_boot_mode_(false) {
- timer_.Start(FROM_HERE,
- TimeDelta::FromSeconds(kPollIntervalSeconds),
- this, &DeviceStatusCollector::CheckIdleState);
+ report_boot_mode_(false),
+ report_location_(false) {
+ if (!location_update_requester_)
+ location_update_requester_ = &content::RequestLocationUpdate;
+ idle_poll_timer_.Start(FROM_HERE,
+ TimeDelta::FromSeconds(kIdlePollIntervalSeconds),
+ this, &DeviceStatusCollector::CheckIdleState);
cros_settings_ = chromeos::CrosSettings::Get();
@@ -76,6 +92,29 @@ DeviceStatusCollector::DeviceStatusCollector(
cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes,
this);
cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this);
+ cros_settings_->AddSettingsObserver(chromeos::kReportDeviceLocation, this);
+
+ // The last known location is persisted in local state. This makes location
+ // information available immediately upon startup and avoids the need to
+ // reacquire the location on every user session change or browser crash.
+ content::Geoposition position;
+ std::string timestamp_str;
+ int64 timestamp;
+ const base::DictionaryValue* location =
+ local_state_->GetDictionary(prefs::kDeviceLocation);
+ if (location->GetDouble("latitude", &position.latitude) &&
Joao da Silva 2012/05/03 19:04:08 It'd be nicer to use constants for these strings:
bartfab (slow) 2012/05/04 09:14:57 Done.
+ location->GetDouble("longitude", &position.longitude) &&
+ location->GetDouble("altitude", &position.altitude) &&
+ location->GetDouble("accuracy", &position.accuracy) &&
+ location->GetDouble("altitude_accuracy",
+ &position.altitude_accuracy) &&
+ location->GetDouble("heading", &position.heading) &&
+ location->GetDouble("speed", &position.speed) &&
+ location->GetString("timestamp", &timestamp_str) &&
+ base::StringToInt64(timestamp_str, &timestamp)) {
+ position.timestamp = base::Time::FromInternalValue(timestamp);
Joao da Silva 2012/05/03 19:04:08 nit: drop the base::
bartfab (slow) 2012/05/04 09:14:57 Done.
+ position_ = position;
+ }
// Fetch the current values of the policies.
UpdateReportingSettings();
@@ -96,12 +135,15 @@ DeviceStatusCollector::~DeviceStatusCollector() {
cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes,
this);
cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this);
+ cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceLocation, this);
}
// static
void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
local_state->RegisterDictionaryPref(prefs::kDeviceActivityTimes,
new DictionaryValue);
+ local_state->RegisterDictionaryPref(prefs::kDeviceLocation,
+ new DictionaryValue);
}
void DeviceStatusCollector::CheckIdleState() {
@@ -125,6 +167,16 @@ void DeviceStatusCollector::UpdateReportingSettings() {
chromeos::kReportDeviceActivityTimes, &report_activity_times_);
cros_settings_->GetBoolean(
chromeos::kReportDeviceBootMode, &report_boot_mode_);
+ cros_settings_->GetBoolean(
+ chromeos::kReportDeviceLocation, &report_location_);
+
+ if (report_location_) {
+ ScheduleGeolocationUpdateRequest();
+ } else {
+ geolocation_update_timer_.Stop();
+ position_ = content::Geoposition();
+ local_state_->ClearPref(prefs::kDeviceLocation);
+ }
}
Time DeviceStatusCollector::GetCurrentTime() {
@@ -201,8 +253,9 @@ void DeviceStatusCollector::IdleStateCallback(IdleState state) {
// interval of activity.
int active_seconds = (now - last_idle_check_).InSeconds();
if (active_seconds < 0 ||
- active_seconds >= static_cast<int>((2 * kPollIntervalSeconds)))
- AddActivePeriod(now - TimeDelta::FromSeconds(kPollIntervalSeconds), now);
+ active_seconds >= static_cast<int>((2 * kIdlePollIntervalSeconds)))
+ AddActivePeriod(now - TimeDelta::FromSeconds(kIdlePollIntervalSeconds),
+ now);
else
AddActivePeriod(last_idle_check_, now);
@@ -258,6 +311,32 @@ void DeviceStatusCollector::GetBootMode(
}
}
+void DeviceStatusCollector::GetLocation(
+ em::DeviceStatusReportRequest* request) {
+ em::DeviceLocation* location = request->mutable_device_location();
+ if (!position_.Validate()) {
+ location->set_error_code(
+ em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE);
+ location->set_error_message(position_.error_message);
+ } else {
+ location->set_latitude(position_.latitude);
+ location->set_longitude(position_.longitude);
+ location->set_accuracy(position_.accuracy);
+ location->set_timestamp(
+ (position_.timestamp - Time::UnixEpoch()).InMilliseconds());
+ // Lowest point on land is at approximately -400 meters.
+ if (position_.altitude > -10000.)
+ location->set_altitude(position_.altitude);
+ if (position_.altitude_accuracy >= 0.)
+ location->set_altitude_accuracy(position_.altitude_accuracy);
+ if (position_.heading >= 0. && position_.heading <= 360)
+ location->set_heading(position_.heading);
+ if (position_.speed >= 0.)
+ location->set_speed(position_.speed);
+ location->set_error_code(em::DeviceLocation::ERROR_CODE_NONE);
+ }
+}
+
void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
if (report_activity_times_)
GetActivityTimes(request);
@@ -267,6 +346,9 @@ void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
if (report_boot_mode_)
GetBootMode(request);
+
+ if (report_location_)
+ GetLocation(request);
}
void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle,
@@ -289,4 +371,59 @@ void DeviceStatusCollector::Observe(
NOTREACHED();
}
+void DeviceStatusCollector::ScheduleGeolocationUpdateRequest() {
+ if (geolocation_update_timer_.IsRunning() || geolocation_update_in_progress_)
+ return;
+
+ if (position_.Validate()) {
+ base::TimeDelta elapsed = base::Time::Now() - position_.timestamp;
+ base::TimeDelta interval =
+ base::TimeDelta::FromSeconds(kGeolocationPollIntervalSeconds);
Joao da Silva 2012/05/03 19:04:08 nit: drop the base:: in these 3 lines
bartfab (slow) 2012/05/04 09:14:57 Done.
+ if (elapsed > interval) {
+ geolocation_update_in_progress_ = true;
+ location_update_requester_(base::Bind(
+ &DeviceStatusCollector::ReceiveGeolocationUpdate,
+ weak_ptr_factory_.GetWeakPtr()));
+ } else {
+ geolocation_update_timer_.Start(
+ FROM_HERE,
+ interval - elapsed,
+ this,
+ &DeviceStatusCollector::ScheduleGeolocationUpdateRequest);
+ }
+ } else {
+ geolocation_update_in_progress_ = true;
+ location_update_requester_(base::Bind(
+ &DeviceStatusCollector::ReceiveGeolocationUpdate,
+ weak_ptr_factory_.GetWeakPtr()));
+
+ }
+}
+
+void DeviceStatusCollector::ReceiveGeolocationUpdate(
+ const content::Geoposition& position) {
+ geolocation_update_in_progress_ = false;
+
+ // Ignore update if device location reporting has since been disabled.
+ if (!report_location_)
+ return;
+
+ if (position.Validate()) {
+ position_ = position;
+ DictionaryValue location;
Joao da Silva 2012/05/03 19:04:08 nit: base::*Value (here and elsewhere)
bartfab (slow) 2012/05/04 09:14:57 Done.
+ location.SetDouble("latitude", position.latitude);
+ location.SetDouble("longitude", position.longitude);
+ location.SetDouble("altitude", position.altitude);
+ location.SetDouble("accuracy", position.accuracy);
+ location.SetDouble("altitude_accuracy", position.altitude_accuracy);
+ location.SetDouble("heading", position.heading);
+ location.SetDouble("speed", position.speed);
+ location.SetString("timestamp",
+ base::Int64ToString(position.timestamp.ToInternalValue()));
+ local_state_->Set(prefs::kDeviceLocation, location);
+ }
+
+ ScheduleGeolocationUpdateRequest();
+}
+
} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698