OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_POLICY_DEVICE_MANAGEMENT_POLICY_PROVIDER_H_ | |
6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_POLICY_PROVIDER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/file_path.h" | |
12 #include "base/observer_list.h" | |
13 #include "base/scoped_ptr.h" | |
14 #include "base/time.h" | |
15 #include "chrome/browser/policy/configuration_policy_provider.h" | |
16 #include "chrome/browser/policy/device_management_backend.h" | |
17 #include "chrome/browser/policy/device_token_fetcher.h" | |
18 | |
19 class Profile; | |
20 class TokenService; | |
21 | |
22 namespace policy { | |
23 | |
24 class DeviceManagementBackend; | |
25 class DeviceManagementPolicyCache; | |
26 | |
27 // Provides policy fetched from the device management server. With the exception | |
28 // of the Provide method, which can be called on the FILE thread, all public | |
29 // methods must be called on the UI thread. | |
30 class DeviceManagementPolicyProvider | |
31 : public ConfigurationPolicyProvider, | |
32 public DeviceManagementBackend::DevicePolicyResponseDelegate, | |
33 public DeviceTokenFetcher::Observer { | |
34 public: | |
35 DeviceManagementPolicyProvider(const PolicyDefinitionList* policy_list, | |
36 DeviceManagementBackend* backend, | |
37 Profile* profile); | |
38 | |
39 virtual ~DeviceManagementPolicyProvider(); | |
40 | |
41 // ConfigurationPolicyProvider implementation: | |
42 virtual bool Provide(ConfigurationPolicyStoreInterface* store); | |
43 virtual bool IsInitializationComplete() const; | |
44 | |
45 // DevicePolicyResponseDelegate implementation: | |
46 virtual void HandlePolicyResponse( | |
47 const em::DevicePolicyResponse& response); | |
48 virtual void OnError(DeviceManagementBackend::ErrorCode code); | |
49 | |
50 // DeviceTokenFetcher::Observer implementation: | |
51 virtual void OnTokenSuccess(); | |
52 virtual void OnTokenError(); | |
53 virtual void OnNotManaged(); | |
54 | |
55 // Sets the refresh rate at which to re-fetch policy information. | |
56 void SetRefreshRate(int64 refresh_rate_milliseconds); | |
57 | |
58 private: | |
59 // Indicates the current state the provider is in. | |
60 enum ProviderState { | |
61 // The provider is initializing, policy information not yet available. | |
62 STATE_INITIALIZING, | |
63 // This device is not managed through policy. | |
64 STATE_UNMANAGED, | |
65 // The token is valid, but policy is yet to be fetched. | |
66 STATE_TOKEN_VALID, | |
67 // Policy information is available and valid. | |
68 STATE_POLICY_VALID, | |
69 // The token was found to be invalid and needs to be obtained again. | |
70 STATE_TOKEN_RESET, | |
71 // There has been an error fetching the token, retry later. | |
72 STATE_TOKEN_ERROR, | |
73 // The service returned an error when requesting policy, ask again later. | |
74 STATE_POLICY_ERROR, | |
75 }; | |
76 | |
77 class RefreshTask; | |
78 | |
79 friend class DeviceManagementPolicyProviderTest; | |
80 | |
81 // More configurable constructor for use by test cases. | |
82 DeviceManagementPolicyProvider(const PolicyDefinitionList* policy_list, | |
83 DeviceManagementBackend* backend, | |
84 Profile* profile, | |
85 int64 policy_refresh_rate_ms, | |
86 int policy_refresh_deviation_factor_percent, | |
87 int64 policy_refresh_deviation_max_ms, | |
88 int64 policy_refresh_error_delay_ms, | |
89 int64 token_fetch_error_delay_ms, | |
90 int64 unmanaged_device_refresh_rate_ms); | |
91 | |
92 // Called by constructors to perform shared initialization. Initialization | |
93 // requiring the IOThread must not be performed directly in this method, | |
94 // rather must be deferred until the IOThread is fully initialized. This is | |
95 // the case in InitializeAfterIOThreadExists. | |
96 void Initialize(DeviceManagementBackend* backend, | |
97 Profile* profile, | |
98 int64 policy_refresh_rate_ms, | |
99 int policy_refresh_deviation_factor_percent, | |
100 int64 policy_refresh_deviation_max_ms, | |
101 int64 policy_refresh_error_delay_ms, | |
102 int64 token_fetch_error_delay_ms, | |
103 int64 unmanaged_device_refresh_rate_ms); | |
104 | |
105 // ConfigurationPolicyProvider overrides: | |
106 virtual void AddObserver(ConfigurationPolicyProvider::Observer* observer); | |
107 virtual void RemoveObserver(ConfigurationPolicyProvider::Observer* observer); | |
108 | |
109 // Sends a request to the device manager backend to fetch policy if one isn't | |
110 // already outstanding. | |
111 void SendPolicyRequest(); | |
112 | |
113 // Triggers policy refresh, re-requesting device token and policy information | |
114 // as necessary. | |
115 void RefreshTaskExecute(); | |
116 | |
117 // Cancels the refresh task. | |
118 void CancelRefreshTask(); | |
119 | |
120 // Notify observers about a policy update. | |
121 void NotifyCloudPolicyUpdate(); | |
122 | |
123 // The path of the device token file. | |
124 FilePath GetTokenPath(); | |
125 | |
126 // Used only by tests. | |
127 void SetDeviceTokenFetcher(DeviceTokenFetcher* token_fetcher); | |
128 | |
129 // Switches to a new state and triggers any appropriate actions. | |
130 void SetState(ProviderState new_state); | |
131 | |
132 // Check whether the current state is one in which the token is available. | |
133 bool TokenAvailable() const; | |
134 | |
135 // Computes the refresh delay to use. | |
136 int64 GetRefreshDelay(); | |
137 | |
138 // Provides the URL at which requests are sent to from the device management | |
139 // backend. | |
140 static std::string GetDeviceManagementURL(); | |
141 | |
142 // Returns the path to the sub-directory in the user data directory | |
143 // in which device management persistent state is stored. | |
144 static FilePath GetOrCreateDeviceManagementDir( | |
145 const FilePath& user_data_dir); | |
146 | |
147 scoped_ptr<DeviceManagementBackend> backend_; | |
148 Profile* profile_; // weak | |
149 scoped_ptr<DeviceManagementPolicyCache> cache_; | |
150 scoped_refptr<DeviceTokenFetcher> token_fetcher_; | |
151 DeviceTokenFetcher::ObserverRegistrar registrar_; | |
152 ObserverList<ConfigurationPolicyProvider::Observer, true> observer_list_; | |
153 FilePath storage_dir_; | |
154 ProviderState state_; | |
155 bool initial_fetch_done_; | |
156 RefreshTask* refresh_task_; | |
157 int64 policy_refresh_rate_ms_; | |
158 int policy_refresh_deviation_factor_percent_; | |
159 int64 policy_refresh_deviation_max_ms_; | |
160 int64 policy_refresh_error_delay_ms_; | |
161 int64 effective_policy_refresh_error_delay_ms_; | |
162 int64 token_fetch_error_delay_ms_; | |
163 int64 effective_token_fetch_error_delay_ms_; | |
164 int64 unmanaged_device_refresh_rate_ms_; | |
165 | |
166 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProvider); | |
167 }; | |
168 | |
169 } // namespace policy | |
170 | |
171 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_POLICY_PROVIDER_H_ | |
OLD | NEW |