OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/policy/device_status_collector.h" | 5 #include "chrome/browser/chromeos/policy/device_status_collector.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return (Time::FromUTCExploded(exploded) - Time::UnixEpoch()).InMilliseconds(); | 74 return (Time::FromUTCExploded(exploded) - Time::UnixEpoch()).InMilliseconds(); |
75 } | 75 } |
76 | 76 |
77 // Maximum number of users to report. | 77 // Maximum number of users to report. |
78 const int kMaxUserCount = 5; | 78 const int kMaxUserCount = 5; |
79 | 79 |
80 } // namespace | 80 } // namespace |
81 | 81 |
82 namespace policy { | 82 namespace policy { |
83 | 83 |
84 DeviceStatusCollector::Context::Context() { | |
85 } | |
86 | |
87 DeviceStatusCollector::Context::~Context() { | |
88 } | |
89 | |
90 void DeviceStatusCollector::Context::GetLocationUpdate( | |
91 const content::GeolocationProvider::LocationUpdateCallback& callback) { | |
92 owner_callback_ = callback; | |
93 content::BrowserThread::PostTask( | |
94 content::BrowserThread::IO, | |
95 FROM_HERE, | |
96 base::Bind(&DeviceStatusCollector::Context::GetLocationUpdateInternal, | |
97 this)); | |
98 } | |
99 | |
100 void DeviceStatusCollector::Context::GetLocationUpdateInternal() { | |
101 our_callback_ = base::Bind( | |
102 &DeviceStatusCollector::Context::OnLocationUpdate, this); | |
103 content::GeolocationProvider::GetInstance()->AddLocationUpdateCallback( | |
104 our_callback_, true); | |
105 } | |
106 | |
107 void DeviceStatusCollector::Context::OnLocationUpdate( | |
108 const content::Geoposition& geoposition) { | |
109 content::GeolocationProvider::GetInstance()->RemoveLocationUpdateCallback( | |
110 our_callback_); | |
111 our_callback_.Reset(); | |
112 content::BrowserThread::PostTask( | |
113 content::BrowserThread::UI, | |
114 FROM_HERE, | |
115 base::Bind(&DeviceStatusCollector::Context::CallCollector, | |
116 this, geoposition)); | |
117 } | |
118 | |
119 void DeviceStatusCollector::Context::CallCollector( | |
120 const content::Geoposition& geoposition) { | |
121 owner_callback_.Run(geoposition); | |
122 owner_callback_.Reset(); | |
123 } | |
124 | |
125 DeviceStatusCollector::DeviceStatusCollector( | 84 DeviceStatusCollector::DeviceStatusCollector( |
126 PrefService* local_state, | 85 PrefService* local_state, |
127 chromeos::system::StatisticsProvider* provider, | 86 chromeos::system::StatisticsProvider* provider, |
128 LocationUpdateRequester* location_update_requester) | 87 LocationUpdateRequester* location_update_requester) |
129 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), | 88 : max_stored_past_activity_days_(kMaxStoredPastActivityDays), |
130 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), | 89 max_stored_future_activity_days_(kMaxStoredFutureActivityDays), |
131 local_state_(local_state), | 90 local_state_(local_state), |
132 last_idle_check_(Time()), | 91 last_idle_check_(Time()), |
133 last_reported_day_(0), | 92 last_reported_day_(0), |
134 duration_for_last_reported_day_(0), | 93 duration_for_last_reported_day_(0), |
135 geolocation_update_in_progress_(false), | 94 geolocation_update_in_progress_(false), |
136 statistics_provider_(provider), | 95 statistics_provider_(provider), |
137 weak_factory_(this), | 96 weak_factory_(this), |
138 report_version_info_(false), | 97 report_version_info_(false), |
139 report_activity_times_(false), | 98 report_activity_times_(false), |
140 report_boot_mode_(false), | 99 report_boot_mode_(false), |
141 report_location_(false), | 100 report_location_(false), |
142 report_network_interfaces_(false), | 101 report_network_interfaces_(false), |
143 report_users_(false), | 102 report_users_(false) { |
144 context_(new Context()) { | 103 if (location_update_requester) |
145 if (location_update_requester) { | |
146 location_update_requester_ = *location_update_requester; | 104 location_update_requester_ = *location_update_requester; |
147 } else { | |
148 location_update_requester_ = | |
149 base::Bind(&Context::GetLocationUpdate, context_.get()); | |
150 } | |
151 idle_poll_timer_.Start(FROM_HERE, | 105 idle_poll_timer_.Start(FROM_HERE, |
152 TimeDelta::FromSeconds(kIdlePollIntervalSeconds), | 106 TimeDelta::FromSeconds(kIdlePollIntervalSeconds), |
153 this, &DeviceStatusCollector::CheckIdleState); | 107 this, &DeviceStatusCollector::CheckIdleState); |
154 | 108 |
155 cros_settings_ = chromeos::CrosSettings::Get(); | 109 cros_settings_ = chromeos::CrosSettings::Get(); |
156 | 110 |
157 // Watch for changes to the individual policies that control what the status | 111 // Watch for changes to the individual policies that control what the status |
158 // reports contain. | 112 // reports contain. |
159 base::Closure callback = | 113 base::Closure callback = |
160 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, | 114 base::Bind(&DeviceStatusCollector::UpdateReportingSettings, |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 } | 499 } |
546 | 500 |
547 void DeviceStatusCollector::ScheduleGeolocationUpdateRequest() { | 501 void DeviceStatusCollector::ScheduleGeolocationUpdateRequest() { |
548 if (geolocation_update_timer_.IsRunning() || geolocation_update_in_progress_) | 502 if (geolocation_update_timer_.IsRunning() || geolocation_update_in_progress_) |
549 return; | 503 return; |
550 | 504 |
551 if (position_.Validate()) { | 505 if (position_.Validate()) { |
552 TimeDelta elapsed = GetCurrentTime() - position_.timestamp; | 506 TimeDelta elapsed = GetCurrentTime() - position_.timestamp; |
553 TimeDelta interval = | 507 TimeDelta interval = |
554 TimeDelta::FromSeconds(kGeolocationPollIntervalSeconds); | 508 TimeDelta::FromSeconds(kGeolocationPollIntervalSeconds); |
555 if (elapsed > interval) { | 509 if (elapsed <= interval) { |
556 geolocation_update_in_progress_ = true; | |
557 location_update_requester_.Run(base::Bind( | |
558 &DeviceStatusCollector::ReceiveGeolocationUpdate, | |
559 weak_factory_.GetWeakPtr())); | |
560 } else { | |
561 geolocation_update_timer_.Start( | 510 geolocation_update_timer_.Start( |
562 FROM_HERE, | 511 FROM_HERE, |
563 interval - elapsed, | 512 interval - elapsed, |
564 this, | 513 this, |
565 &DeviceStatusCollector::ScheduleGeolocationUpdateRequest); | 514 &DeviceStatusCollector::ScheduleGeolocationUpdateRequest); |
| 515 return; |
566 } | 516 } |
| 517 } |
| 518 |
| 519 geolocation_update_in_progress_ = true; |
| 520 if (location_update_requester_.is_null()) { |
| 521 geolocation_subscription_ = content::GeolocationProvider::GetInstance()-> |
| 522 AddLocationUpdateCallback( |
| 523 base::Bind(&DeviceStatusCollector::ReceiveGeolocationUpdate, |
| 524 weak_factory_.GetWeakPtr()), |
| 525 true); |
567 } else { | 526 } else { |
568 geolocation_update_in_progress_ = true; | |
569 location_update_requester_.Run(base::Bind( | 527 location_update_requester_.Run(base::Bind( |
570 &DeviceStatusCollector::ReceiveGeolocationUpdate, | 528 &DeviceStatusCollector::ReceiveGeolocationUpdate, |
571 weak_factory_.GetWeakPtr())); | 529 weak_factory_.GetWeakPtr())); |
572 } | 530 } |
573 } | 531 } |
574 | 532 |
575 void DeviceStatusCollector::ReceiveGeolocationUpdate( | 533 void DeviceStatusCollector::ReceiveGeolocationUpdate( |
576 const content::Geoposition& position) { | 534 const content::Geoposition& position) { |
577 geolocation_update_in_progress_ = false; | 535 geolocation_update_in_progress_ = false; |
578 | 536 |
(...skipping 13 matching lines...) Expand all Loading... |
592 location.SetDouble(kSpeed, position.speed); | 550 location.SetDouble(kSpeed, position.speed); |
593 location.SetString(kTimestamp, | 551 location.SetString(kTimestamp, |
594 base::Int64ToString(position.timestamp.ToInternalValue())); | 552 base::Int64ToString(position.timestamp.ToInternalValue())); |
595 local_state_->Set(prefs::kDeviceLocation, location); | 553 local_state_->Set(prefs::kDeviceLocation, location); |
596 } | 554 } |
597 | 555 |
598 ScheduleGeolocationUpdateRequest(); | 556 ScheduleGeolocationUpdateRequest(); |
599 } | 557 } |
600 | 558 |
601 } // namespace policy | 559 } // namespace policy |
OLD | NEW |