Chromium Code Reviews| 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/policy/device_status_collector.h" | 5 #include "chrome/browser/policy/device_status_collector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/chromeos/system/statistics_provider.h" | 10 #include "chrome/browser/chromeos/system/statistics_provider.h" |
| 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 14 #include "chrome/common/pref_names.h" | |
|
Joao da Silva
2012/01/26 09:48:58
Nit: alphabetic order
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 14 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 15 | 16 |
| 16 using base::Time; | 17 using base::Time; |
| 17 using base::TimeDelta; | 18 using base::TimeDelta; |
| 18 using chromeos::VersionLoader; | 19 using chromeos::VersionLoader; |
| 19 | 20 |
| 20 namespace em = enterprise_management; | 21 namespace em = enterprise_management; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 // How many seconds of inactivity triggers the idle state. | 24 // How many seconds of inactivity triggers the idle state. |
| 24 const unsigned int kIdleStateThresholdSeconds = 300; | 25 const unsigned int kIdleStateThresholdSeconds = 300; |
| 25 | 26 |
| 26 // The maximum number of time periods stored in the local state. | 27 // The maximum number of time periods stored in the local state. |
| 27 const unsigned int kMaxStoredActivePeriods = 500; | 28 const unsigned int kMaxStoredActivePeriods = 500; |
| 28 | 29 |
| 29 // Stores the baseline timestamp, to which the active periods are relative. | |
| 30 const char* const kPrefBaselineTime = "device_status.baseline_timestamp"; | |
| 31 | |
| 32 // Stores a list of timestamps representing device active periods. | 30 // Stores a list of timestamps representing device active periods. |
| 33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; | 31 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; |
| 34 | 32 |
| 35 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { | 33 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { |
| 36 std::string string_value; | 34 std::string string_value; |
| 37 if (list->GetString(index, &string_value)) | 35 if (list->GetString(index, &string_value)) |
| 38 return base::StringToInt64(string_value, out_value); | 36 return base::StringToInt64(string_value, out_value); |
| 39 return false; | 37 return false; |
| 40 } | 38 } |
| 41 | 39 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 64 version_loader_.GetFirmware(&consumer_, | 62 version_loader_.GetFirmware(&consumer_, |
| 65 base::Bind(&DeviceStatusCollector::OnOSFirmware, | 63 base::Bind(&DeviceStatusCollector::OnOSFirmware, |
| 66 base::Unretained(this))); | 64 base::Unretained(this))); |
| 67 } | 65 } |
| 68 | 66 |
| 69 DeviceStatusCollector::~DeviceStatusCollector() { | 67 DeviceStatusCollector::~DeviceStatusCollector() { |
| 70 } | 68 } |
| 71 | 69 |
| 72 // static | 70 // static |
| 73 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { | 71 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { |
| 74 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT()); | |
| 75 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); | 72 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); |
| 73 local_state->RegisterBooleanPref(prefs::kReportDeviceVersionInfo, false); | |
|
pastarmovj
2012/01/26 09:52:11
You won't need this once you regard my next commen
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 74 local_state->RegisterBooleanPref(prefs::kReportDeviceActivityTimes, false); | |
| 75 local_state->RegisterBooleanPref(prefs::kReportDeviceBootMode, false); | |
| 76 } | 76 } |
| 77 | 77 |
| 78 void DeviceStatusCollector::CheckIdleState() { | 78 void DeviceStatusCollector::CheckIdleState() { |
| 79 CalculateIdleState(kIdleStateThresholdSeconds, | 79 CalculateIdleState(kIdleStateThresholdSeconds, |
| 80 base::Bind(&DeviceStatusCollector::IdleStateCallback, | 80 base::Bind(&DeviceStatusCollector::IdleStateCallback, |
| 81 base::Unretained(this))); | 81 base::Unretained(this))); |
| 82 } | 82 } |
| 83 | 83 |
| 84 Time DeviceStatusCollector::GetCurrentTime() { | 84 Time DeviceStatusCollector::GetCurrentTime() { |
| 85 return Time::Now(); | 85 return Time::Now(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 // Add a new period to the list. | 114 // Add a new period to the list. |
| 115 active_periods->Append( | 115 active_periods->Append( |
| 116 new StringValue(base::Int64ToString(start_timestamp))); | 116 new StringValue(base::Int64ToString(start_timestamp))); |
| 117 active_periods->Append(end_value); | 117 active_periods->Append(end_value); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void DeviceStatusCollector::IdleStateCallback(IdleState state) { | 120 void DeviceStatusCollector::IdleStateCallback(IdleState state) { |
| 121 // Do nothing if device activity reporting is disabled. | |
| 122 if (!local_state_->GetBoolean(prefs::kReportDeviceActivityTimes)) | |
|
pastarmovj
2012/01/26 09:52:11
This is not the right way to read device policy. I
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 123 return; | |
| 124 | |
| 121 Time now = GetCurrentTime(); | 125 Time now = GetCurrentTime(); |
| 122 | 126 |
| 123 if (state == IDLE_STATE_ACTIVE) { | 127 if (state == IDLE_STATE_ACTIVE) { |
| 124 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; | 128 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; |
| 125 | 129 |
| 126 // If it's been too long since the last report, assume that the system was | 130 // If it's been too long since the last report, assume that the system was |
| 127 // in standby, and only count a single interval of activity. | 131 // in standby, and only count a single interval of activity. |
| 128 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) | 132 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) |
| 129 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); | 133 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); |
| 130 else | 134 else |
| 131 AddActivePeriod(last_idle_check_, now); | 135 AddActivePeriod(last_idle_check_, now); |
| 132 } | 136 } |
| 133 last_idle_check_ = now; | 137 last_idle_check_ = now; |
| 134 last_idle_state_ = state; | 138 last_idle_state_ = state; |
| 135 } | 139 } |
| 136 | 140 |
| 137 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | 141 void DeviceStatusCollector::GetActivityTimes( |
| 138 // Report device active periods. | 142 em::DeviceStatusReportRequest* request) { |
| 139 const ListValue* active_periods = | 143 const ListValue* active_periods = |
| 140 local_state_->GetList(kPrefDeviceActivePeriods); | 144 local_state_->GetList(kPrefDeviceActivePeriods); |
| 141 em::TimePeriod* time_period; | 145 em::TimePeriod* time_period; |
| 142 | 146 |
| 143 DCHECK(active_periods->GetSize() % 2 == 0); | 147 DCHECK(active_periods->GetSize() % 2 == 0); |
| 144 | 148 |
| 145 int period_count = active_periods->GetSize() / 2; | 149 int period_count = active_periods->GetSize() / 2; |
| 146 for (int i = 0; i < period_count; i++) { | 150 for (int i = 0; i < period_count; i++) { |
| 147 int64 start, end; | 151 int64 start, end; |
| 148 | 152 |
| 149 if (!GetTimestamp(active_periods, 2 * i, &start) || | 153 if (!GetTimestamp(active_periods, 2 * i, &start) || |
| 150 !GetTimestamp(active_periods, 2 * i + 1, &end) || | 154 !GetTimestamp(active_periods, 2 * i + 1, &end) || |
| 151 end < start) { | 155 end < start) { |
| 152 // Something is amiss -- bail out. | 156 // Something is amiss -- bail out. |
| 153 NOTREACHED(); | 157 NOTREACHED(); |
| 154 break; | 158 break; |
| 155 } | 159 } |
| 156 time_period = request->add_active_time(); | 160 time_period = request->add_active_time(); |
| 157 time_period->set_start_timestamp(start); | 161 time_period->set_start_timestamp(start); |
| 158 time_period->set_end_timestamp(end); | 162 time_period->set_end_timestamp(end); |
| 159 } | 163 } |
| 160 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); | 164 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); |
| 161 update.Get()->Clear(); | 165 update.Get()->Clear(); |
| 166 } | |
| 162 | 167 |
| 168 void DeviceStatusCollector::GetVersionInfo( | |
| 169 em::DeviceStatusReportRequest* request) { | |
| 163 chrome::VersionInfo version_info; | 170 chrome::VersionInfo version_info; |
| 164 request->set_browser_version(version_info.Version()); | 171 request->set_browser_version(version_info.Version()); |
| 165 request->set_os_version(os_version_); | 172 request->set_os_version(os_version_); |
| 166 request->set_firmware_version(firmware_version_); | 173 request->set_firmware_version(firmware_version_); |
| 174 } | |
| 167 | 175 |
| 168 // Report the state of the dev switch at boot. | 176 void DeviceStatusCollector::GetBootMode( |
| 177 em::DeviceStatusReportRequest* request) { | |
| 169 std::string dev_switch_mode; | 178 std::string dev_switch_mode; |
| 170 if (statistics_provider_->GetMachineStatistic( | 179 if (statistics_provider_->GetMachineStatistic( |
| 171 "devsw_boot", &dev_switch_mode)) { | 180 "devsw_boot", &dev_switch_mode)) { |
| 172 if (dev_switch_mode == "1") | 181 if (dev_switch_mode == "1") |
| 173 request->set_boot_mode("Dev"); | 182 request->set_boot_mode("Dev"); |
| 174 else if (dev_switch_mode == "0") | 183 else if (dev_switch_mode == "0") |
| 175 request->set_boot_mode("Verified"); | 184 request->set_boot_mode("Verified"); |
| 176 } | 185 } |
| 177 } | 186 } |
| 178 | 187 |
| 188 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { | |
| 189 if (local_state_->GetBoolean(prefs::kReportDeviceActivityTimes)) | |
|
pastarmovj
2012/01/26 09:52:11
Ditto.
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 190 GetActivityTimes(request); | |
| 191 | |
| 192 if (local_state_->GetBoolean(prefs::kReportDeviceVersionInfo)) | |
|
pastarmovj
2012/01/26 09:52:11
Ditto.
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 193 GetVersionInfo(request); | |
| 194 | |
| 195 if (local_state_->GetBoolean(prefs::kReportDeviceBootMode)) | |
|
pastarmovj
2012/01/26 09:52:11
Ditto.
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
| 196 GetBootMode(request); | |
| 197 } | |
| 198 | |
| 179 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, | 199 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, |
| 180 std::string version) { | 200 std::string version) { |
| 181 os_version_ = version; | 201 os_version_ = version; |
| 182 } | 202 } |
| 183 | 203 |
| 184 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, | 204 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, |
| 185 std::string version) { | 205 std::string version) { |
| 186 firmware_version_ = version; | 206 firmware_version_ = version; |
| 187 } | 207 } |
| 188 | 208 |
| 189 } // namespace policy | 209 } // namespace policy |
| OLD | NEW |