OLD | NEW |
| (Empty) |
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_POLICY_CLOUD_DEVICE_MANAGEMENT_SERVICE_H_ | |
6 #define CHROME_BROWSER_POLICY_CLOUD_DEVICE_MANAGEMENT_SERVICE_H_ | |
7 | |
8 #include <deque> | |
9 #include <map> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/callback.h" | |
15 #include "base/compiler_specific.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | |
18 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | |
19 #include "net/url_request/url_fetcher_delegate.h" | |
20 | |
21 namespace net { | |
22 class URLRequestContextGetter; | |
23 } | |
24 | |
25 namespace policy { | |
26 | |
27 class DeviceManagementRequestJobImpl; | |
28 class DeviceManagementService; | |
29 | |
30 // DeviceManagementRequestJob describes a request to send to the device | |
31 // management service. Jobs are created by DeviceManagementService. They can be | |
32 // canceled by deleting the object. | |
33 class DeviceManagementRequestJob { | |
34 public: | |
35 // Describes the job type. | |
36 enum JobType { | |
37 TYPE_AUTO_ENROLLMENT, | |
38 TYPE_REGISTRATION, | |
39 TYPE_API_AUTH_CODE_FETCH, | |
40 TYPE_POLICY_FETCH, | |
41 TYPE_UNREGISTRATION, | |
42 TYPE_UPLOAD_CERTIFICATE, | |
43 }; | |
44 | |
45 typedef base::Callback< | |
46 void(DeviceManagementStatus, int, | |
47 const enterprise_management::DeviceManagementResponse&)> Callback; | |
48 | |
49 typedef base::Callback<void(DeviceManagementRequestJob*)> RetryCallback; | |
50 | |
51 virtual ~DeviceManagementRequestJob(); | |
52 | |
53 // Functions for configuring the job. These should only be called before | |
54 // Start()ing the job, but never afterwards. | |
55 void SetGaiaToken(const std::string& gaia_token); | |
56 void SetOAuthToken(const std::string& oauth_token); | |
57 void SetUserAffiliation(UserAffiliation user_affiliation); | |
58 void SetDMToken(const std::string& dm_token); | |
59 void SetClientID(const std::string& client_id); | |
60 enterprise_management::DeviceManagementRequest* GetRequest(); | |
61 | |
62 // A job may automatically retry if it fails due to a temporary condition, or | |
63 // due to proxy misconfigurations. If a |retry_callback| is set then it will | |
64 // be invoked with the DeviceManagementRequestJob as an argument when that | |
65 // happens, so that the job's owner can customize the retry request before | |
66 // it's sent. | |
67 void SetRetryCallback(const RetryCallback& retry_callback); | |
68 | |
69 // Starts the job. |callback| will be invoked on completion. | |
70 void Start(const Callback& callback); | |
71 | |
72 protected: | |
73 typedef std::vector<std::pair<std::string, std::string> > ParameterMap; | |
74 | |
75 DeviceManagementRequestJob(JobType type, | |
76 const std::string& agent_parameter, | |
77 const std::string& platform_parameter); | |
78 | |
79 // Appends a parameter to |query_params|. | |
80 void AddParameter(const std::string& name, const std::string& value); | |
81 | |
82 // Fires the job, to be filled in by implementations. | |
83 virtual void Run() = 0; | |
84 | |
85 ParameterMap query_params_; | |
86 std::string gaia_token_; | |
87 std::string dm_token_; | |
88 enterprise_management::DeviceManagementRequest request_; | |
89 RetryCallback retry_callback_; | |
90 | |
91 Callback callback_; | |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(DeviceManagementRequestJob); | |
95 }; | |
96 | |
97 // The device management service is responsible for everything related to | |
98 // communication with the device management server. It creates the backends | |
99 // objects that the device management policy provider and friends use to issue | |
100 // requests. | |
101 class DeviceManagementService : public net::URLFetcherDelegate { | |
102 public: | |
103 // Obtains the parameters used to contact the server. | |
104 // This allows creating the DeviceManagementService early and getting these | |
105 // parameters later. Passing the parameters directly in the ctor isn't | |
106 // possible because some aren't ready during startup. http://crbug.com/302798 | |
107 class Configuration { | |
108 public: | |
109 virtual ~Configuration() {} | |
110 | |
111 // Server at which to contact the service. | |
112 virtual std::string GetServerUrl() = 0; | |
113 | |
114 // Agent reported in the "agent" query parameter. | |
115 virtual std::string GetAgentParameter() = 0; | |
116 | |
117 // The platform reported in the "platform" query parameter. | |
118 virtual std::string GetPlatformParameter() = 0; | |
119 }; | |
120 | |
121 explicit DeviceManagementService(scoped_ptr<Configuration> configuration); | |
122 virtual ~DeviceManagementService(); | |
123 | |
124 // The ID of URLFetchers created by the DeviceManagementService. This can be | |
125 // used by tests that use a TestURLFetcherFactory to get the pending fetchers | |
126 // created by the DeviceManagementService. | |
127 static const int kURLFetcherID; | |
128 | |
129 // Creates a new device management request job. Ownership is transferred to | |
130 // the caller. | |
131 virtual DeviceManagementRequestJob* CreateJob( | |
132 DeviceManagementRequestJob::JobType type, | |
133 net::URLRequestContextGetter* request_context); | |
134 | |
135 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed. | |
136 void ScheduleInitialization(int64 delay_milliseconds); | |
137 | |
138 // Makes the service stop all requests. | |
139 void Shutdown(); | |
140 | |
141 // Gets the URL that the DMServer requests are sent to. | |
142 std::string GetServerURL(); | |
143 | |
144 private: | |
145 typedef std::map<const net::URLFetcher*, | |
146 DeviceManagementRequestJobImpl*> JobFetcherMap; | |
147 typedef std::deque<DeviceManagementRequestJobImpl*> JobQueue; | |
148 | |
149 friend class DeviceManagementRequestJobImpl; | |
150 | |
151 // net::URLFetcherDelegate override. | |
152 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
153 | |
154 // Starts processing any queued jobs. | |
155 void Initialize(); | |
156 | |
157 // Starts a job. | |
158 void StartJob(DeviceManagementRequestJobImpl* job); | |
159 | |
160 // Adds a job. Caller must make sure the job pointer stays valid until the job | |
161 // completes or gets canceled via RemoveJob(). | |
162 void AddJob(DeviceManagementRequestJobImpl* job); | |
163 | |
164 // Removes a job. The job will be removed and won't receive a completion | |
165 // callback. | |
166 void RemoveJob(DeviceManagementRequestJobImpl* job); | |
167 | |
168 // A Configuration implementation that is used to obtain various parameters | |
169 // used to talk to the device management server. | |
170 scoped_ptr<Configuration> configuration_; | |
171 | |
172 // The jobs we currently have in flight. | |
173 JobFetcherMap pending_jobs_; | |
174 | |
175 // Jobs that are registered, but not started yet. | |
176 JobQueue queued_jobs_; | |
177 | |
178 // If this service is initialized, incoming requests get fired instantly. | |
179 // If it is not initialized, incoming requests are queued. | |
180 bool initialized_; | |
181 | |
182 // Used to create tasks to run |Initialize| delayed on the UI thread. | |
183 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_; | |
184 | |
185 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService); | |
186 }; | |
187 | |
188 } // namespace policy | |
189 | |
190 #endif // CHROME_BROWSER_POLICY_CLOUD_DEVICE_MANAGEMENT_SERVICE_H_ | |
OLD | NEW |