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

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

Issue 9289017: Apply individual policies for the various parts of device status reports. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Julian's comments. Created 8 years, 11 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/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/cros_settings.h"
11 #include "chrome/browser/chromeos/cros_settings_names.h"
10 #include "chrome/browser/chromeos/system/statistics_provider.h" 12 #include "chrome/browser/chromeos/system/statistics_provider.h"
11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
12 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/prefs/scoped_user_pref_update.h"
16 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/chrome_version_info.h" 17 #include "chrome/common/chrome_version_info.h"
15 18
16 using base::Time; 19 using base::Time;
17 using base::TimeDelta; 20 using base::TimeDelta;
18 using chromeos::VersionLoader; 21 using chromeos::VersionLoader;
19 22
20 namespace em = enterprise_management; 23 namespace em = enterprise_management;
21 24
22 namespace { 25 namespace {
23 // How many seconds of inactivity triggers the idle state. 26 // How many seconds of inactivity triggers the idle state.
24 const unsigned int kIdleStateThresholdSeconds = 300; 27 const unsigned int kIdleStateThresholdSeconds = 300;
25 28
26 // The maximum number of time periods stored in the local state. 29 // The maximum number of time periods stored in the local state.
27 const unsigned int kMaxStoredActivePeriods = 500; 30 const unsigned int kMaxStoredActivePeriods = 500;
28 31
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. 32 // Stores a list of timestamps representing device active periods.
33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods"; 33 const char* const kPrefDeviceActivePeriods = "device_status.active_periods";
34 34
35 bool GetTimestamp(const ListValue* list, int index, int64* out_value) { 35 bool GetTimestamp(const ListValue* list, int index, int64* out_value) {
36 std::string string_value; 36 std::string string_value;
37 if (list->GetString(index, &string_value)) 37 if (list->GetString(index, &string_value))
38 return base::StringToInt64(string_value, out_value); 38 return base::StringToInt64(string_value, out_value);
39 return false; 39 return false;
40 } 40 }
41 41
42 } // namespace 42 } // namespace
43 43
44 namespace policy { 44 namespace policy {
45 45
46 DeviceStatusCollector::DeviceStatusCollector( 46 DeviceStatusCollector::DeviceStatusCollector(
47 PrefService* local_state, 47 PrefService* local_state,
48 chromeos::system::StatisticsProvider* provider) 48 chromeos::system::StatisticsProvider* provider)
49 : max_stored_active_periods_(kMaxStoredActivePeriods), 49 : max_stored_active_periods_(kMaxStoredActivePeriods),
50 local_state_(local_state), 50 local_state_(local_state),
51 last_idle_check_(Time()), 51 last_idle_check_(Time()),
52 last_idle_state_(IDLE_STATE_UNKNOWN), 52 last_idle_state_(IDLE_STATE_UNKNOWN),
53 statistics_provider_(provider) { 53 statistics_provider_(provider),
54 report_version_info_(false),
55 report_activity_times_(false),
56 report_boot_mode_(false) {
54 timer_.Start(FROM_HERE, 57 timer_.Start(FROM_HERE,
55 TimeDelta::FromSeconds( 58 TimeDelta::FromSeconds(
56 DeviceStatusCollector::kPollIntervalSeconds), 59 DeviceStatusCollector::kPollIntervalSeconds),
57 this, &DeviceStatusCollector::CheckIdleState); 60 this, &DeviceStatusCollector::CheckIdleState);
58 61
62 cros_settings_ = chromeos::CrosSettings::Get();
63
64 // Watch for changes to the individual policies that control what the status
65 // reports contain.
66 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceVersionInfo, this);
67 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceActivityTimes,
68 this);
69 cros_settings_->AddSettingsObserver(chromeos::kReportDeviceBootMode, this);
70
71 // Fetch the current values of the policies.
72 UpdateReportingSettings();
73
59 // Get the the OS and firmware version info. 74 // Get the the OS and firmware version info.
60 version_loader_.GetVersion(&consumer_, 75 version_loader_.GetVersion(&consumer_,
61 base::Bind(&DeviceStatusCollector::OnOSVersion, 76 base::Bind(&DeviceStatusCollector::OnOSVersion,
62 base::Unretained(this)), 77 base::Unretained(this)),
63 VersionLoader::VERSION_FULL); 78 VersionLoader::VERSION_FULL);
64 version_loader_.GetFirmware(&consumer_, 79 version_loader_.GetFirmware(&consumer_,
65 base::Bind(&DeviceStatusCollector::OnOSFirmware, 80 base::Bind(&DeviceStatusCollector::OnOSFirmware,
66 base::Unretained(this))); 81 base::Unretained(this)));
67 } 82 }
68 83
69 DeviceStatusCollector::~DeviceStatusCollector() { 84 DeviceStatusCollector::~DeviceStatusCollector() {
85 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceVersionInfo,
86 this);
87 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceActivityTimes,
88 this);
89 cros_settings_->RemoveSettingsObserver(chromeos::kReportDeviceBootMode, this);
70 } 90 }
71 91
72 // static 92 // static
73 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) { 93 void DeviceStatusCollector::RegisterPrefs(PrefService* local_state) {
74 local_state->RegisterInt64Pref(kPrefBaselineTime, Time::Now().ToTimeT());
75 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue); 94 local_state->RegisterListPref(kPrefDeviceActivePeriods, new ListValue);
76 } 95 }
77 96
78 void DeviceStatusCollector::CheckIdleState() { 97 void DeviceStatusCollector::CheckIdleState() {
79 CalculateIdleState(kIdleStateThresholdSeconds, 98 CalculateIdleState(kIdleStateThresholdSeconds,
80 base::Bind(&DeviceStatusCollector::IdleStateCallback, 99 base::Bind(&DeviceStatusCollector::IdleStateCallback,
81 base::Unretained(this))); 100 base::Unretained(this)));
82 } 101 }
83 102
103 void DeviceStatusCollector::UpdateReportingSettings() {
104 // Attempt to fetch the current value of the reporting settings.
105 // If trusted values are not available, register this function to be called
106 // back when they are available.
107 bool is_trusted = cros_settings_->GetTrusted(
108 chromeos::kReportDeviceVersionInfo,
109 base::Bind(&DeviceStatusCollector::UpdateReportingSettings,
110 base::Unretained(this)));
111 if (is_trusted) {
112 cros_settings_->GetBoolean(
113 chromeos::kReportDeviceVersionInfo, &report_version_info_);
114 cros_settings_->GetBoolean(
115 chromeos::kReportDeviceActivityTimes, &report_activity_times_);
116 cros_settings_->GetBoolean(
117 chromeos::kReportDeviceBootMode, &report_boot_mode_);
118 }
119 }
120
84 Time DeviceStatusCollector::GetCurrentTime() { 121 Time DeviceStatusCollector::GetCurrentTime() {
85 return Time::Now(); 122 return Time::Now();
86 } 123 }
87 124
88 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) { 125 void DeviceStatusCollector::AddActivePeriod(Time start, Time end) {
89 // Maintain the list of active periods in a local_state pref. 126 // Maintain the list of active periods in a local_state pref.
90 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); 127 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
91 ListValue* active_periods = update.Get(); 128 ListValue* active_periods = update.Get();
92 129
93 // Cap the number of active periods that we store. 130 // Cap the number of active periods that we store.
(...skipping 17 matching lines...) Expand all
111 return; 148 return;
112 } 149 }
113 } 150 }
114 // Add a new period to the list. 151 // Add a new period to the list.
115 active_periods->Append( 152 active_periods->Append(
116 new StringValue(base::Int64ToString(start_timestamp))); 153 new StringValue(base::Int64ToString(start_timestamp)));
117 active_periods->Append(end_value); 154 active_periods->Append(end_value);
118 } 155 }
119 156
120 void DeviceStatusCollector::IdleStateCallback(IdleState state) { 157 void DeviceStatusCollector::IdleStateCallback(IdleState state) {
158 // Do nothing if device activity reporting is disabled.
159 if (!report_activity_times_)
160 return;
161
121 Time now = GetCurrentTime(); 162 Time now = GetCurrentTime();
122 163
123 if (state == IDLE_STATE_ACTIVE) { 164 if (state == IDLE_STATE_ACTIVE) {
124 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds; 165 unsigned int poll_interval = DeviceStatusCollector::kPollIntervalSeconds;
125 166
126 // If it's been too long since the last report, assume that the system was 167 // 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. 168 // in standby, and only count a single interval of activity.
128 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval)) 169 if ((now - last_idle_check_).InSeconds() >= (2 * poll_interval))
129 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now); 170 AddActivePeriod(now - TimeDelta::FromSeconds(poll_interval), now);
130 else 171 else
131 AddActivePeriod(last_idle_check_, now); 172 AddActivePeriod(last_idle_check_, now);
132 } 173 }
133 last_idle_check_ = now; 174 last_idle_check_ = now;
134 last_idle_state_ = state; 175 last_idle_state_ = state;
135 } 176 }
136 177
137 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) { 178 void DeviceStatusCollector::GetActivityTimes(
138 // Report device active periods. 179 em::DeviceStatusReportRequest* request) {
139 const ListValue* active_periods = 180 const ListValue* active_periods =
140 local_state_->GetList(kPrefDeviceActivePeriods); 181 local_state_->GetList(kPrefDeviceActivePeriods);
141 em::TimePeriod* time_period; 182 em::TimePeriod* time_period;
142 183
143 DCHECK(active_periods->GetSize() % 2 == 0); 184 DCHECK(active_periods->GetSize() % 2 == 0);
144 185
145 int period_count = active_periods->GetSize() / 2; 186 int period_count = active_periods->GetSize() / 2;
146 for (int i = 0; i < period_count; i++) { 187 for (int i = 0; i < period_count; i++) {
147 int64 start, end; 188 int64 start, end;
148 189
149 if (!GetTimestamp(active_periods, 2 * i, &start) || 190 if (!GetTimestamp(active_periods, 2 * i, &start) ||
150 !GetTimestamp(active_periods, 2 * i + 1, &end) || 191 !GetTimestamp(active_periods, 2 * i + 1, &end) ||
151 end < start) { 192 end < start) {
152 // Something is amiss -- bail out. 193 // Something is amiss -- bail out.
153 NOTREACHED(); 194 NOTREACHED();
154 break; 195 break;
155 } 196 }
156 time_period = request->add_active_time(); 197 time_period = request->add_active_time();
157 time_period->set_start_timestamp(start); 198 time_period->set_start_timestamp(start);
158 time_period->set_end_timestamp(end); 199 time_period->set_end_timestamp(end);
159 } 200 }
160 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods); 201 ListPrefUpdate update(local_state_, kPrefDeviceActivePeriods);
161 update.Get()->Clear(); 202 update.Get()->Clear();
203 }
162 204
205 void DeviceStatusCollector::GetVersionInfo(
206 em::DeviceStatusReportRequest* request) {
163 chrome::VersionInfo version_info; 207 chrome::VersionInfo version_info;
164 request->set_browser_version(version_info.Version()); 208 request->set_browser_version(version_info.Version());
165 request->set_os_version(os_version_); 209 request->set_os_version(os_version_);
166 request->set_firmware_version(firmware_version_); 210 request->set_firmware_version(firmware_version_);
211 }
167 212
168 // Report the state of the dev switch at boot. 213 void DeviceStatusCollector::GetBootMode(
214 em::DeviceStatusReportRequest* request) {
169 std::string dev_switch_mode; 215 std::string dev_switch_mode;
170 if (statistics_provider_->GetMachineStatistic( 216 if (statistics_provider_->GetMachineStatistic(
171 "devsw_boot", &dev_switch_mode)) { 217 "devsw_boot", &dev_switch_mode)) {
172 if (dev_switch_mode == "1") 218 if (dev_switch_mode == "1")
173 request->set_boot_mode("Dev"); 219 request->set_boot_mode("Dev");
174 else if (dev_switch_mode == "0") 220 else if (dev_switch_mode == "0")
175 request->set_boot_mode("Verified"); 221 request->set_boot_mode("Verified");
176 } 222 }
177 } 223 }
178 224
225 void DeviceStatusCollector::GetStatus(em::DeviceStatusReportRequest* request) {
226 if (report_activity_times_)
227 GetActivityTimes(request);
228
229 if (report_version_info_)
230 GetVersionInfo(request);
231
232 if (report_boot_mode_)
233 GetBootMode(request);
234 }
235
179 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle, 236 void DeviceStatusCollector::OnOSVersion(VersionLoader::Handle handle,
180 std::string version) { 237 std::string version) {
181 os_version_ = version; 238 os_version_ = version;
182 } 239 }
183 240
184 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle, 241 void DeviceStatusCollector::OnOSFirmware(VersionLoader::Handle handle,
185 std::string version) { 242 std::string version) {
186 firmware_version_ = version; 243 firmware_version_ = version;
187 } 244 }
188 245
246 void DeviceStatusCollector::Observe(
247 int type,
248 const content::NotificationSource& source,
249 const content::NotificationDetails& details) {
250 if (type == chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED)
251 UpdateReportingSettings();
252 else
253 NOTREACHED();
254 }
255
189 } // namespace policy 256 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_status_collector.h ('k') | chrome/browser/policy/device_status_collector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698