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

Side by Side Diff: chrome/browser/chromeos/policy/device_status_collector.cc

Issue 18348016: If requested, report network interfaces to management server. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback. Created 7 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_registry_simple.h" 14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/values.h" 17 #include "base/values.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h" 18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 19 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
20 #include "chrome/browser/chromeos/system/statistics_provider.h" 20 #include "chrome/browser/chromeos/system/statistics_provider.h"
21 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 21 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
22 #include "chrome/browser/prefs/scoped_user_pref_update.h" 22 #include "chrome/browser/prefs/scoped_user_pref_update.h"
23 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/chrome_version_info.h" 24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/pref_names.h" 25 #include "chrome/common/pref_names.h"
26 #include "chromeos/network/device_state.h"
27 #include "chromeos/network/network_handler.h"
28 #include "chromeos/network/network_state_handler.h"
26 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_details.h" 30 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.h" 31 #include "content/public/browser/notification_source.h"
32 #include "third_party/cros_system_api/dbus/service_constants.h"
29 33
30 using base::Time; 34 using base::Time;
31 using base::TimeDelta; 35 using base::TimeDelta;
32 using chromeos::VersionLoader; 36 using chromeos::VersionLoader;
33 37
34 namespace em = enterprise_management; 38 namespace em = enterprise_management;
35 39
36 namespace { 40 namespace {
37 // How many seconds of inactivity triggers the idle state. 41 // How many seconds of inactivity triggers the idle state.
38 const int kIdleStateThresholdSeconds = 300; 42 const int kIdleStateThresholdSeconds = 300;
39 43
40 // How many days in the past to store active periods for. 44 // How many days in the past to store active periods for.
41 const unsigned int kMaxStoredPastActivityDays = 30; 45 const unsigned int kMaxStoredPastActivityDays = 30;
42 46
43 // How many days in the future to store active periods for. 47 // How many days in the future to store active periods for.
44 const unsigned int kMaxStoredFutureActivityDays = 2; 48 const unsigned int kMaxStoredFutureActivityDays = 2;
45 49
46 // How often, in seconds, to update the device location. 50 // How often, in seconds, to update the device location.
47 const unsigned int kGeolocationPollIntervalSeconds = 30 * 60; 51 const unsigned int kGeolocationPollIntervalSeconds = 30 * 60;
48 52
49 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; 53 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000;
50 54
55 // Keys for the geolocation status dictionary in local state.
51 const char kLatitude[] = "latitude"; 56 const char kLatitude[] = "latitude";
52
53 const char kLongitude[] = "longitude"; 57 const char kLongitude[] = "longitude";
54
55 const char kAltitude[] = "altitude"; 58 const char kAltitude[] = "altitude";
56
57 const char kAccuracy[] = "accuracy"; 59 const char kAccuracy[] = "accuracy";
58
59 const char kAltitudeAccuracy[] = "altitude_accuracy"; 60 const char kAltitudeAccuracy[] = "altitude_accuracy";
60
61 const char kHeading[] = "heading"; 61 const char kHeading[] = "heading";
62
63 const char kSpeed[] = "speed"; 62 const char kSpeed[] = "speed";
64
65 const char kTimestamp[] = "timestamp"; 63 const char kTimestamp[] = "timestamp";
66 64
67 // Determine the day key (milliseconds since epoch for corresponding day in UTC) 65 // Determine the day key (milliseconds since epoch for corresponding day in UTC)
68 // for a given |timestamp|. 66 // for a given |timestamp|.
69 int64 TimestampToDayKey(Time timestamp) { 67 int64 TimestampToDayKey(Time timestamp) {
70 Time::Exploded exploded; 68 Time::Exploded exploded;
71 timestamp.LocalMidnight().LocalExplode(&exploded); 69 timestamp.LocalMidnight().LocalExplode(&exploded);
72 return (Time::FromUTCExploded(exploded) - Time::UnixEpoch()).InMilliseconds(); 70 return (Time::FromUTCExploded(exploded) - Time::UnixEpoch()).InMilliseconds();
73 } 71 }
74 72
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 last_idle_check_(Time()), 125 last_idle_check_(Time()),
128 last_reported_day_(0), 126 last_reported_day_(0),
129 duration_for_last_reported_day_(0), 127 duration_for_last_reported_day_(0),
130 geolocation_update_in_progress_(false), 128 geolocation_update_in_progress_(false),
131 statistics_provider_(provider), 129 statistics_provider_(provider),
132 weak_factory_(this), 130 weak_factory_(this),
133 report_version_info_(false), 131 report_version_info_(false),
134 report_activity_times_(false), 132 report_activity_times_(false),
135 report_boot_mode_(false), 133 report_boot_mode_(false),
136 report_location_(false), 134 report_location_(false),
135 report_network_interfaces_(false),
137 context_(new Context()) { 136 context_(new Context()) {
138 if (location_update_requester) { 137 if (location_update_requester) {
139 location_update_requester_ = *location_update_requester; 138 location_update_requester_ = *location_update_requester;
140 } else { 139 } else {
141 location_update_requester_ = 140 location_update_requester_ =
142 base::Bind(&Context::GetLocationUpdate, context_.get()); 141 base::Bind(&Context::GetLocationUpdate, context_.get());
143 } 142 }
144 idle_poll_timer_.Start(FROM_HERE, 143 idle_poll_timer_.Start(FROM_HERE,
145 TimeDelta::FromSeconds(kIdlePollIntervalSeconds), 144 TimeDelta::FromSeconds(kIdlePollIntervalSeconds),
146 this, &DeviceStatusCollector::CheckIdleState); 145 this, &DeviceStatusCollector::CheckIdleState);
147 146
148 cros_settings_ = chromeos::CrosSettings::Get(); 147 cros_settings_ = chromeos::CrosSettings::Get();
149 148
150 // Watch for changes to the individual policies that control what the status 149 // Watch for changes to the individual policies that control what the status
151 // reports contain. 150 // reports contain.
152 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this); 151 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this);
153 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes, 152 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes,
154 this); 153 this);
155 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this); 154 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this);
156 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceLocation, this); 155 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceLocation, this);
156 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceNetworkInterfaces,
157 this);
157 158
158 // The last known location is persisted in local state. This makes location 159 // The last known location is persisted in local state. This makes location
159 // information available immediately upon startup and avoids the need to 160 // information available immediately upon startup and avoids the need to
160 // reacquire the location on every user session change or browser crash. 161 // reacquire the location on every user session change or browser crash.
161 content::Geoposition position; 162 content::Geoposition position;
162 std::string timestamp_str; 163 std::string timestamp_str;
163 int64 timestamp; 164 int64 timestamp;
164 const base::DictionaryValue* location = 165 const base::DictionaryValue* location =
165 local_state_->GetDictionary(prefs::kDeviceLocation); 166 local_state_->GetDictionary(prefs::kDeviceLocation);
166 if (location->GetDouble(kLatitude, &position.latitude) && 167 if (location->GetDouble(kLatitude, &position.latitude) &&
(...skipping 22 matching lines...) Expand all
189 &tracker_); 190 &tracker_);
190 } 191 }
191 192
192 DeviceStatusCollector::~DeviceStatusCollector() { 193 DeviceStatusCollector::~DeviceStatusCollector() {
193 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo, 194 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo,
194 this); 195 this);
195 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes, 196 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes,
196 this); 197 this);
197 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this); 198 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this);
198 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceLocation, this); 199 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceLocation, this);
200 cros_settings_->RemoveSettingsObserver(
201 chromeos::kReportDeviceNetworkInterfaces, this);
199 } 202 }
200 203
201 // static 204 // static
202 void DeviceStatusCollector::RegisterPrefs(PrefRegistrySimple* registry) { 205 void DeviceStatusCollector::RegisterPrefs(PrefRegistrySimple* registry) {
203 registry->RegisterDictionaryPref(prefs::kDeviceActivityTimes, 206 registry->RegisterDictionaryPref(prefs::kDeviceActivityTimes,
204 new base::DictionaryValue); 207 new base::DictionaryValue);
205 registry->RegisterDictionaryPref(prefs::kDeviceLocation, 208 registry->RegisterDictionaryPref(prefs::kDeviceLocation,
206 new base::DictionaryValue); 209 new base::DictionaryValue);
207 } 210 }
208 211
(...skipping 14 matching lines...) Expand all
223 return; 226 return;
224 } 227 }
225 cros_settings_->GetBoolean( 228 cros_settings_->GetBoolean(
226 chromeos::kReportDeviceVersionInfo, &report_version_info_); 229 chromeos::kReportDeviceVersionInfo, &report_version_info_);
227 cros_settings_->GetBoolean( 230 cros_settings_->GetBoolean(
228 chromeos::kReportDeviceActivityTimes, &report_activity_times_); 231 chromeos::kReportDeviceActivityTimes, &report_activity_times_);
229 cros_settings_->GetBoolean( 232 cros_settings_->GetBoolean(
230 chromeos::kReportDeviceBootMode, &report_boot_mode_); 233 chromeos::kReportDeviceBootMode, &report_boot_mode_);
231 cros_settings_->GetBoolean( 234 cros_settings_->GetBoolean(
232 chromeos::kReportDeviceLocation, &report_location_); 235 chromeos::kReportDeviceLocation, &report_location_);
236 cros_settings_->GetBoolean(
237 chromeos::kReportDeviceNetworkInterfaces, &report_network_interfaces_);
233 238
234 if (report_location_) { 239 if (report_location_) {
235 ScheduleGeolocationUpdateRequest(); 240 ScheduleGeolocationUpdateRequest();
236 } else { 241 } else {
237 geolocation_update_timer_.Stop(); 242 geolocation_update_timer_.Stop();
238 position_ = content::Geoposition(); 243 position_ = content::Geoposition();
239 local_state_->ClearPref(prefs::kDeviceLocation); 244 local_state_->ClearPref(prefs::kDeviceLocation);
240 } 245 }
241 } 246 }
242 247
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 if (position_.altitude_accuracy >= 0.) 403 if (position_.altitude_accuracy >= 0.)
399 location->set_altitude_accuracy(position_.altitude_accuracy); 404 location->set_altitude_accuracy(position_.altitude_accuracy);
400 if (position_.heading >= 0. && position_.heading <= 360) 405 if (position_.heading >= 0. && position_.heading <= 360)
401 location->set_heading(position_.heading); 406 location->set_heading(position_.heading);
402 if (position_.speed >= 0.) 407 if (position_.speed >= 0.)
403 location->set_speed(position_.speed); 408 location->set_speed(position_.speed);
404 location->set_error_code(em::DeviceLocation::ERROR_CODE_NONE); 409 location->set_error_code(em::DeviceLocation::ERROR_CODE_NONE);
405 } 410 }
406 } 411 }
407 412
413 void DeviceStatusCollector::GetNetworkInterfaces(
414 em::DeviceStatusReportRequest* request) {
415 // Maps flimflam device type strings to proto enum constants.
pneubeck (no reviews) 2013/07/11 18:49:32 nit: although the namespace is still 'flimflam', w
416 static struct {
417 const char* type_string;
418 em::NetworkInterface::NetworkDeviceType type_constant;
419 } kDeviceTypeMap[] = {
pneubeck (no reviews) 2013/07/11 18:49:32 nit: could be an array of _const_ entries.
pneubeck (no reviews) 2013/07/12 07:57:54 would be in accordance with this thread (they disc
Mattias Nissler (ping if slow) 2013/07/12 08:11:55 Valid point, done.
420 { flimflam::kTypeEthernet, em::NetworkInterface::TYPE_ETHERNET, },
421 { flimflam::kTypeWifi, em::NetworkInterface::TYPE_WIFI, },
422 { flimflam::kTypeWimax, em::NetworkInterface::TYPE_WIMAX, },
423 { flimflam::kTypeBluetooth, em::NetworkInterface::TYPE_BLUETOOTH, },
424 { flimflam::kTypeCellular, em::NetworkInterface::TYPE_CELLULAR, },
425 };
426
427 chromeos::NetworkStateHandler::DeviceStateList device_list;
428 chromeos::NetworkHandler::Get()->network_state_handler()->GetDeviceList(
429 &device_list);
430
431 chromeos::NetworkStateHandler::DeviceStateList::const_iterator device;
432 for (device = device_list.begin(); device != device_list.end(); ++device) {
433 // Determine the type enum constant for |device|.
434 size_t type_idx = 0;
435 for (; type_idx < ARRAYSIZE_UNSAFE(kDeviceTypeMap); ++type_idx) {
436 if ((*device)->type() == kDeviceTypeMap[type_idx].type_string)
437 break;
438 }
439
440 // If the type isn't in |kDeviceTypeMap|, the interface is not relevant for
441 // reporting. This filters out VPN devices.
442 if (type_idx >= ARRAYSIZE_UNSAFE(kDeviceTypeMap))
443 continue;
444
445 em::NetworkInterface* interface = request->add_network_interface();
446 interface->set_type(kDeviceTypeMap[type_idx].type_constant);
447 if (!(*device)->mac_address().empty())
448 interface->set_mac_address((*device)->mac_address());
449 if (!(*device)->meid().empty())
450 interface->set_meid((*device)->meid());
451 if (!(*device)->imei().empty())
452 interface->set_imei((*device)->imei());
453 }
454 }
455
408 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { 456 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
409 // TODO(mnissler): Remove once the old cloud policy stack is retired. The old 457 // TODO(mnissler): Remove once the old cloud policy stack is retired. The old
410 // stack doesn't support reporting successful submissions back to here, so 458 // stack doesn't support reporting successful submissions back to here, so
411 // just assume whatever ends up in |request| gets submitted successfully. 459 // just assume whatever ends up in |request| gets submitted successfully.
412 GetDeviceStatus(request); 460 GetDeviceStatus(request);
413 OnSubmittedSuccessfully(); 461 OnSubmittedSuccessfully();
414 } 462 }
415 463
416 bool DeviceStatusCollector::GetDeviceStatus( 464 bool DeviceStatusCollector::GetDeviceStatus(
417 em::DeviceStatusReportRequest* status) { 465 em::DeviceStatusReportRequest* status) {
418 if (report_activity_times_) 466 if (report_activity_times_)
419 GetActivityTimes(status); 467 GetActivityTimes(status);
420 468
421 if (report_version_info_) 469 if (report_version_info_)
422 GetVersionInfo(status); 470 GetVersionInfo(status);
423 471
424 if (report_boot_mode_) 472 if (report_boot_mode_)
425 GetBootMode(status); 473 GetBootMode(status);
426 474
427 if (report_location_) 475 if (report_location_)
428 GetLocation(status); 476 GetLocation(status);
429 477
478 if (report_network_interfaces_)
479 GetNetworkInterfaces(status);
480
430 return true; 481 return true;
431 } 482 }
432 483
433 bool DeviceStatusCollector::GetSessionStatus( 484 bool DeviceStatusCollector::GetSessionStatus(
434 em::SessionStatusReportRequest* status) { 485 em::SessionStatusReportRequest* status) {
435 return false; 486 return false;
436 } 487 }
437 488
438 void DeviceStatusCollector::OnSubmittedSuccessfully() { 489 void DeviceStatusCollector::OnSubmittedSuccessfully() {
439 TrimStoredActivityPeriods(last_reported_day_, duration_for_last_reported_day_, 490 TrimStoredActivityPeriods(last_reported_day_, duration_for_last_reported_day_,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 location.SetDouble(kSpeed, position.speed); 557 location.SetDouble(kSpeed, position.speed);
507 location.SetString(kTimestamp, 558 location.SetString(kTimestamp,
508 base::Int64ToString(position.timestamp.ToInternalValue())); 559 base::Int64ToString(position.timestamp.ToInternalValue()));
509 local_state_->Set(prefs::kDeviceLocation, location); 560 local_state_->Set(prefs::kDeviceLocation, location);
510 } 561 }
511 562
512 ScheduleGeolocationUpdateRequest(); 563 ScheduleGeolocationUpdateRequest();
513 } 564 }
514 565
515 } // namespace policy 566 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698