device_status_collector.h revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// found in the LICENSE file.
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <string>
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/basictypes.h"
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/callback_forward.h"
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/compiler_specific.h"
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/memory/ref_counted.h"
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/memory/weak_ptr.h"
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/time.h"
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "base/timer.h"
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "chrome/browser/chromeos/version_loader.h"
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "chrome/browser/idle.h"
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "chrome/browser/policy/cloud/cloud_policy_client.h"
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "chrome/common/cancelable_task_tracker.h"
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "content/public/browser/geolocation_provider.h"
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "content/public/common/geoposition.h"
2402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace chromeos {
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class CrosSettings;
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)namespace system {
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class StatisticsProvider;
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
31d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace content {
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class NotificationDetails;
34c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)class NotificationSource;
355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)namespace enterprise_management {
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class DeviceStatusReportRequest;
39c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class PrefService;
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class PrefRegistrySimple;
4309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
447242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccinamespace policy {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)// Collects and summarizes the status of an enterprised-managed ChromeOS device.
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class DeviceStatusCollector : public CloudPolicyClient::StatusProvider,
4807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch                              public content::NotificationObserver {
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) public:
50a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch  // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  // way to mock geolocation exists.
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  typedef base::Callback<void(
5307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch      const content::GeolocationProvider::LocationUpdateCallback& callback)>
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)          LocationUpdateRequester;
55a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  DeviceStatusCollector(
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      PrefService* local_state,
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)      chromeos::system::StatisticsProvider* provider,
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      LocationUpdateRequester* location_update_requester);
60c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)  virtual ~DeviceStatusCollector();
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  void GetStatus(enterprise_management::DeviceStatusReportRequest* request);
63d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  // CloudPolicyClient::StatusProvider:
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  virtual bool GetDeviceStatus(
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)      enterprise_management::DeviceStatusReportRequest* status) OVERRIDE;
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)  virtual bool GetSessionStatus(
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)      enterprise_management::SessionStatusReportRequest* status) OVERRIDE;
69c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)  virtual void OnSubmittedSuccessfully() OVERRIDE;
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  static void RegisterPrefs(PrefRegistrySimple* registry);
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
73c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)  // How often, in seconds, to poll to see if the user is idle.
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)  static const unsigned int kIdlePollIntervalSeconds = 30;
757242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
76 protected:
77  // Check whether the user has been idle for a certain period of time.
78  virtual void CheckIdleState();
79
80  // Used instead of base::Time::Now(), to make testing possible.
81  virtual base::Time GetCurrentTime();
82
83  // Callback which receives the results of the idle state check.
84  void IdleStateCallback(IdleState state);
85
86  // The number of days in the past to store device activity.
87  // This is kept in case device status uploads fail for a number of days.
88  unsigned int max_stored_past_activity_days_;
89
90  // The number of days in the future to store device activity.
91  // When changing the system time and/or timezones, it's possible to record
92  // activity time that is slightly in the future.
93  unsigned int max_stored_future_activity_days_;
94
95 private:
96  // A helper class to manage receiving geolocation notifications on the IO
97  // thread.
98  class Context : public base::RefCountedThreadSafe<Context> {
99   public:
100    Context();
101
102    void GetLocationUpdate(
103        const content::GeolocationProvider::LocationUpdateCallback& callback);
104
105   private:
106    friend class base::RefCountedThreadSafe<Context>;
107
108    ~Context();
109
110    void GetLocationUpdateInternal();
111    void OnLocationUpdate(const content::Geoposition& geoposition);
112    void CallCollector(const content::Geoposition& geoposition);
113
114    // The callback which this class registers with
115    // content::GeolocationProvider.
116    content::GeolocationProvider::LocationUpdateCallback our_callback_;
117
118    // The callback passed in to GetLocationUpdate.
119    content::GeolocationProvider::LocationUpdateCallback owner_callback_;
120  };
121
122  // Prevents the local store of activity periods from growing too large by
123  // removing entries that are outside the reporting window.
124  void PruneStoredActivityPeriods(base::Time base_time);
125
126  // Trims the store activity periods to only retain data within the
127  // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
128  // adjusted by subtracting |min_day_trim_duration|.
129  void TrimStoredActivityPeriods(int64 min_day_key,
130                                 int min_day_trim_duration,
131                                 int64 max_day_key);
132
133  void AddActivePeriod(base::Time start, base::Time end);
134
135  // Callbacks from chromeos::VersionLoader.
136  void OnOSVersion(const std::string& version);
137  void OnOSFirmware(const std::string& version);
138
139  // Helpers for the various portions of the status.
140  void GetActivityTimes(
141      enterprise_management::DeviceStatusReportRequest* request);
142  void GetVersionInfo(
143      enterprise_management::DeviceStatusReportRequest* request);
144  void GetBootMode(
145      enterprise_management::DeviceStatusReportRequest* request);
146  void GetLocation(
147      enterprise_management::DeviceStatusReportRequest* request);
148
149  // Update the cached values of the reporting settings.
150  void UpdateReportingSettings();
151
152  // content::NotificationObserver interface.
153  virtual void Observe(
154      int type,
155      const content::NotificationSource& source,
156      const content::NotificationDetails& details) OVERRIDE;
157
158  void ScheduleGeolocationUpdateRequest();
159
160  // content::GeolocationUpdateCallback implementation.
161  void ReceiveGeolocationUpdate(const content::Geoposition&);
162
163  // How often to poll to see if the user is idle.
164  int poll_interval_seconds_;
165
166  PrefService* local_state_;
167
168  // The last time an idle state check was performed.
169  base::Time last_idle_check_;
170
171  // The maximum key that went into the last report generated by
172  // GetDeviceStatus(), and the duration for it. This is used to trim the
173  // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
174  // unsuccessful uploads don't result in dropped data.
175  int64 last_reported_day_;
176  int duration_for_last_reported_day_;
177
178  // Whether a geolocation update is currently in progress.
179  bool geolocation_update_in_progress_;
180
181  base::RepeatingTimer<DeviceStatusCollector> idle_poll_timer_;
182  base::OneShotTimer<DeviceStatusCollector> geolocation_update_timer_;
183
184  chromeos::VersionLoader version_loader_;
185  CancelableTaskTracker tracker_;
186
187  std::string os_version_;
188  std::string firmware_version_;
189
190  content::Geoposition position_;
191
192  chromeos::system::StatisticsProvider* statistics_provider_;
193
194  chromeos::CrosSettings* cros_settings_;
195
196  base::WeakPtrFactory<DeviceStatusCollector> weak_factory_;
197
198  // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
199  // way to mock geolocation exists.
200  LocationUpdateRequester location_update_requester_;
201
202  // Cached values of the reporting settings from the device policy.
203  bool report_version_info_;
204  bool report_activity_times_;
205  bool report_boot_mode_;
206  bool report_location_;
207
208  scoped_refptr<Context> context_;
209
210  DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector);
211};
212
213}  // namespace policy
214
215#endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
216