Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / device_status_collector.h
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
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback_list.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/chromeos/version_loader.h"
21 #include "chrome/browser/idle.h"
22 #include "components/policy/core/common/cloud/cloud_policy_client.h"
23 #include "content/public/browser/geolocation_provider.h"
24 #include "content/public/common/geoposition.h"
25
26 namespace chromeos {
27 class CrosSettings;
28 namespace system {
29 class StatisticsProvider;
30 }
31 }
32
33 namespace content {
34 class NotificationDetails;
35 class NotificationSource;
36 }
37
38 namespace enterprise_management {
39 class DeviceStatusReportRequest;
40 }
41
42 class PrefRegistrySimple;
43 class PrefService;
44
45 namespace policy {
46
47 // Collects and summarizes the status of an enterprised-managed ChromeOS device.
48 class DeviceStatusCollector : public CloudPolicyClient::StatusProvider {
49  public:
50   // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
51   // way to mock geolocation exists.
52   typedef base::Callback<void(
53       const content::GeolocationProvider::LocationUpdateCallback& callback)>
54           LocationUpdateRequester;
55
56   DeviceStatusCollector(
57       PrefService* local_state,
58       chromeos::system::StatisticsProvider* provider,
59       LocationUpdateRequester* location_update_requester);
60   virtual ~DeviceStatusCollector();
61
62   void GetStatus(enterprise_management::DeviceStatusReportRequest* request);
63
64   // CloudPolicyClient::StatusProvider:
65   virtual bool GetDeviceStatus(
66       enterprise_management::DeviceStatusReportRequest* status) OVERRIDE;
67   virtual bool GetSessionStatus(
68       enterprise_management::SessionStatusReportRequest* status) OVERRIDE;
69   virtual void OnSubmittedSuccessfully() OVERRIDE;
70
71   static void RegisterPrefs(PrefRegistrySimple* registry);
72
73   // How often, in seconds, to poll to see if the user is idle.
74   static const unsigned int kIdlePollIntervalSeconds = 30;
75
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   // Prevents the local store of activity periods from growing too large by
97   // removing entries that are outside the reporting window.
98   void PruneStoredActivityPeriods(base::Time base_time);
99
100   // Trims the store activity periods to only retain data within the
101   // [|min_day_key|, |max_day_key|). The record for |min_day_key| will be
102   // adjusted by subtracting |min_day_trim_duration|.
103   void TrimStoredActivityPeriods(int64 min_day_key,
104                                  int min_day_trim_duration,
105                                  int64 max_day_key);
106
107   void AddActivePeriod(base::Time start, base::Time end);
108
109   // Callbacks from chromeos::VersionLoader.
110   void OnOSVersion(const std::string& version);
111   void OnOSFirmware(const std::string& version);
112
113   // Helpers for the various portions of the status.
114   void GetActivityTimes(
115       enterprise_management::DeviceStatusReportRequest* request);
116   void GetVersionInfo(
117       enterprise_management::DeviceStatusReportRequest* request);
118   void GetBootMode(
119       enterprise_management::DeviceStatusReportRequest* request);
120   void GetLocation(
121       enterprise_management::DeviceStatusReportRequest* request);
122   void GetNetworkInterfaces(
123       enterprise_management::DeviceStatusReportRequest* request);
124   void GetUsers(
125       enterprise_management::DeviceStatusReportRequest* request);
126
127   // Update the cached values of the reporting settings.
128   void UpdateReportingSettings();
129
130   void ScheduleGeolocationUpdateRequest();
131
132   // content::GeolocationUpdateCallback implementation.
133   void ReceiveGeolocationUpdate(const content::Geoposition&);
134
135   // How often to poll to see if the user is idle.
136   int poll_interval_seconds_;
137
138   PrefService* local_state_;
139
140   // The last time an idle state check was performed.
141   base::Time last_idle_check_;
142
143   // The maximum key that went into the last report generated by
144   // GetDeviceStatus(), and the duration for it. This is used to trim the
145   // stored data in OnSubmittedSuccessfully(). Trimming is delayed so
146   // unsuccessful uploads don't result in dropped data.
147   int64 last_reported_day_;
148   int duration_for_last_reported_day_;
149
150   // Whether a geolocation update is currently in progress.
151   bool geolocation_update_in_progress_;
152
153   base::RepeatingTimer<DeviceStatusCollector> idle_poll_timer_;
154   base::OneShotTimer<DeviceStatusCollector> geolocation_update_timer_;
155
156   chromeos::VersionLoader version_loader_;
157   base::CancelableTaskTracker tracker_;
158
159   std::string os_version_;
160   std::string firmware_version_;
161
162   content::Geoposition position_;
163
164   chromeos::system::StatisticsProvider* statistics_provider_;
165
166   chromeos::CrosSettings* cros_settings_;
167
168   // TODO(bartfab): Remove this once crbug.com/125931 is addressed and a proper
169   // way to mock geolocation exists.
170   LocationUpdateRequester location_update_requester_;
171
172   scoped_ptr<content::GeolocationProvider::Subscription>
173       geolocation_subscription_;
174
175   // Cached values of the reporting settings from the device policy.
176   bool report_version_info_;
177   bool report_activity_times_;
178   bool report_boot_mode_;
179   bool report_location_;
180   bool report_network_interfaces_;
181   bool report_users_;
182
183   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
184       version_info_subscription_;
185   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
186       activity_times_subscription_;
187   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
188       boot_mode_subscription_;
189   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
190       location_subscription_;
191   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
192       network_interfaces_subscription_;
193   scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
194       users_subscription_;
195
196   base::WeakPtrFactory<DeviceStatusCollector> weak_factory_;
197
198   DISALLOW_COPY_AND_ASSIGN(DeviceStatusCollector);
199 };
200
201 }  // namespace policy
202
203 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_STATUS_COLLECTOR_H_