OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_management_backend_impl.h" | 5 #include "chrome/browser/policy/device_management_backend_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/stl_util-inl.h" | |
11 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
12 #include "chrome/browser/browser_thread.h" | |
13 #include "chrome/common/net/url_request_context_getter.h" | |
14 #include "net/base/cookie_monster.h" | |
15 #include "net/base/escape.h" | 11 #include "net/base/escape.h" |
16 #include "net/base/host_resolver.h" | |
17 #include "net/base/ssl_config_service_defaults.h" | |
18 #include "net/http/http_auth_handler_factory.h" | |
19 #include "net/http/http_network_layer.h" | |
20 #include "net/proxy/proxy_service.h" | |
21 #include "net/url_request/url_request_context.h" | |
22 #include "net/url_request/url_request_status.h" | 12 #include "net/url_request/url_request_status.h" |
23 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/policy/device_management_service.h" |
24 #include "chrome/browser/io_thread.h" | |
25 #include "chrome/browser/net/chrome_net_log.h" | |
26 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
27 | 15 |
28 namespace policy { | 16 namespace policy { |
29 | 17 |
30 namespace { | 18 namespace { |
31 | 19 |
32 // Name constants for URL query parameters. | 20 // Name constants for URL query parameters. |
33 const char kServiceParamRequest[] = "request"; | 21 const char kServiceParamRequest[] = "request"; |
34 const char kServiceParamDeviceType[] = "devicetype"; | 22 const char kServiceParamDeviceType[] = "devicetype"; |
| 23 const char kServiceParamAppType[] = "apptype"; |
35 const char kServiceParamDeviceID[] = "deviceid"; | 24 const char kServiceParamDeviceID[] = "deviceid"; |
36 const char kServiceParamAgent[] = "agent"; | 25 const char kServiceParamAgent[] = "agent"; |
37 | 26 |
38 // String constants for the device type and agent we report to the service. | 27 // String constants for the device and app type we report to the server. |
39 const char kServiceValueDeviceType[] = "Chrome"; | 28 const char kServiceValueDeviceType[] = "Chrome OS"; |
| 29 const char kServiceValueAppType[] = "Chrome"; |
| 30 |
40 const char kServiceValueAgent[] = | 31 const char kServiceValueAgent[] = |
41 "%s enterprise management client version %s (%s)"; | 32 "%s enterprise management client version %s (%s)"; |
42 | 33 |
43 const char kServiceTokenAuthHeader[] = "Authorization: GoogleLogin auth="; | 34 const char kServiceTokenAuthHeader[] = "Authorization: GoogleLogin auth="; |
44 const char kDMTokenAuthHeader[] = "Authorization: GoogleDMToken token="; | 35 const char kDMTokenAuthHeader[] = "Authorization: GoogleDMToken token="; |
45 | 36 |
46 } // namespace | |
47 | |
48 // Custom request context implementation that allows to override the user agent, | |
49 // amongst others. Using the default request context is not an option since this | |
50 // service may be constructed before the default request context is created | |
51 // (i.e. before the profile has been loaded). | |
52 class DeviceManagementBackendRequestContext : public URLRequestContext { | |
53 public: | |
54 explicit DeviceManagementBackendRequestContext(IOThread::Globals* io_globals); | |
55 virtual ~DeviceManagementBackendRequestContext(); | |
56 | |
57 private: | |
58 virtual const std::string& GetUserAgent(const GURL& url) const; | |
59 | |
60 std::string user_agent_; | |
61 }; | |
62 | |
63 DeviceManagementBackendRequestContext::DeviceManagementBackendRequestContext( | |
64 IOThread::Globals* io_globals) { | |
65 net_log_ = io_globals->net_log.get(); | |
66 host_resolver_ = io_globals->host_resolver.get(); | |
67 proxy_service_ = net::ProxyService::CreateDirect(); | |
68 ssl_config_service_ = net::SSLConfigService::CreateSystemSSLConfigService(); | |
69 http_auth_handler_factory_ = | |
70 net::HttpAuthHandlerFactory::CreateDefault(host_resolver_); | |
71 http_transaction_factory_ = | |
72 net::HttpNetworkLayer::CreateFactory(host_resolver_, | |
73 io_globals->dnsrr_resolver.get(), | |
74 NULL /* ssl_host_info_factory */, | |
75 proxy_service_, | |
76 ssl_config_service_, | |
77 http_auth_handler_factory_, | |
78 NULL /* network_delegate */, | |
79 net_log_); | |
80 cookie_store_ = new net::CookieMonster(NULL, NULL); | |
81 user_agent_ = DeviceManagementBackendImpl::GetAgentString(); | |
82 accept_language_ = "*"; | |
83 accept_charset_ = "*"; | |
84 } | |
85 | |
86 DeviceManagementBackendRequestContext | |
87 ::~DeviceManagementBackendRequestContext() { | |
88 delete http_transaction_factory_; | |
89 delete http_auth_handler_factory_; | |
90 } | |
91 | |
92 const std::string& | |
93 DeviceManagementBackendRequestContext::GetUserAgent(const GURL& url) const { | |
94 return user_agent_; | |
95 } | |
96 | |
97 // Request context holder. | |
98 class DeviceManagementBackendRequestContextGetter | |
99 : public URLRequestContextGetter { | |
100 public: | |
101 DeviceManagementBackendRequestContextGetter() | |
102 : io_thread_(g_browser_process->io_thread()) {} | |
103 | |
104 // URLRequestContextGetter overrides. | |
105 virtual URLRequestContext* GetURLRequestContext(); | |
106 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const; | |
107 | |
108 private: | |
109 scoped_refptr<URLRequestContext> context_; | |
110 IOThread* io_thread_; | |
111 }; | |
112 | |
113 | |
114 URLRequestContext* | |
115 DeviceManagementBackendRequestContextGetter::GetURLRequestContext() { | |
116 if (!context_) | |
117 context_ = new DeviceManagementBackendRequestContext(io_thread_->globals()); | |
118 | |
119 return context_.get(); | |
120 } | |
121 | |
122 scoped_refptr<base::MessageLoopProxy> | |
123 DeviceManagementBackendRequestContextGetter::GetIOMessageLoopProxy() const { | |
124 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
125 } | |
126 | |
127 // Helper class for URL query parameter encoding/decoding. | 37 // Helper class for URL query parameter encoding/decoding. |
128 class URLQueryParameters { | 38 class URLQueryParameters { |
129 public: | 39 public: |
130 URLQueryParameters() {} | 40 URLQueryParameters() {} |
131 | 41 |
132 // Add a query parameter. | 42 // Add a query parameter. |
133 void Put(const std::string& name, const std::string& value); | 43 void Put(const std::string& name, const std::string& value); |
134 | 44 |
135 // Produce the query string, taking care of properly encoding and assembling | 45 // Produce the query string, taking care of properly encoding and assembling |
136 // the names and values. | 46 // the names and values. |
(...skipping 18 matching lines...) Expand all Loading... |
155 ++entry) { | 65 ++entry) { |
156 if (entry != params_.begin()) | 66 if (entry != params_.begin()) |
157 result += '&'; | 67 result += '&'; |
158 result += EscapeUrlEncodedData(entry->first); | 68 result += EscapeUrlEncodedData(entry->first); |
159 result += '='; | 69 result += '='; |
160 result += EscapeUrlEncodedData(entry->second); | 70 result += EscapeUrlEncodedData(entry->second); |
161 } | 71 } |
162 return result; | 72 return result; |
163 } | 73 } |
164 | 74 |
165 // Wraps common response parsing and handling functionality. | 75 } // namespace |
166 class ResponseHandler { | 76 |
| 77 // A base class containing the common code for the jobs created by the backend |
| 78 // implementation. Subclasses provide custom code for handling actual register, |
| 79 // unregister, and policy jobs. |
| 80 class DeviceManagementJobBase |
| 81 : public DeviceManagementService::DeviceManagementJob { |
167 public: | 82 public: |
168 ResponseHandler() {} | 83 virtual ~DeviceManagementJobBase() { |
169 virtual ~ResponseHandler() {} | 84 backend_impl_->JobDone(this); |
| 85 } |
170 | 86 |
171 // Handles the URL request response. | 87 // DeviceManagementJob overrides: |
172 void HandleResponse(const URLRequestStatus& status, | 88 virtual void HandleResponse(const URLRequestStatus& status, |
173 int response_code, | 89 int response_code, |
174 const ResponseCookies& cookies, | 90 const ResponseCookies& cookies, |
175 const std::string& data); | 91 const std::string& data); |
| 92 virtual GURL GetURL(const std::string& server_url); |
| 93 virtual void ConfigureRequest(URLFetcher* fetcher); |
176 | 94 |
177 // Forwards the given error to the delegate. | 95 protected: |
| 96 // Constructs a device management job running for the given backend. |
| 97 DeviceManagementJobBase(DeviceManagementBackendImpl* backend_impl, |
| 98 const std::string& request_type) |
| 99 : backend_impl_(backend_impl) { |
| 100 query_params_.Put(kServiceParamRequest, request_type); |
| 101 query_params_.Put(kServiceParamDeviceType, kServiceValueDeviceType); |
| 102 query_params_.Put(kServiceParamAppType, kServiceValueAppType); |
| 103 chrome::VersionInfo version_info; |
| 104 std::string agent = base::StringPrintf(kServiceValueAgent, |
| 105 version_info.Name().c_str(), |
| 106 version_info.Version().c_str(), |
| 107 version_info.LastChange().c_str()); |
| 108 query_params_.Put(kServiceParamAgent, agent); |
| 109 } |
| 110 |
| 111 void SetQueryParam(const std::string& name, const std::string& value) { |
| 112 query_params_.Put(name, value); |
| 113 } |
| 114 |
| 115 void SetAuthToken(const std::string& auth_token) { |
| 116 auth_token_ = auth_token; |
| 117 } |
| 118 |
| 119 void SetDeviceManagementToken(const std::string& device_management_token) { |
| 120 device_management_token_ = device_management_token; |
| 121 } |
| 122 |
| 123 void SetDeviceID(const std::string& device_id) { |
| 124 query_params_.Put(kServiceParamDeviceID, device_id); |
| 125 } |
| 126 |
| 127 void SetPayload(const em::DeviceManagementRequest& request) { |
| 128 if (!request.SerializeToString(&payload_)) { |
| 129 NOTREACHED(); |
| 130 LOG(ERROR) << "Failed to serialize request."; |
| 131 } |
| 132 } |
| 133 |
| 134 private: |
| 135 // Implemented by subclasses to handle decoded responses and errors. |
| 136 virtual void OnResponse( |
| 137 const em::DeviceManagementResponse& response) = 0; |
178 virtual void OnError(DeviceManagementBackend::ErrorCode error) = 0; | 138 virtual void OnError(DeviceManagementBackend::ErrorCode error) = 0; |
179 | 139 |
180 private: | 140 // The backend this job is handling a request for. |
181 // Implemented by subclasses to handle the decoded response. | 141 DeviceManagementBackendImpl* backend_impl_; |
182 virtual void ProcessResponse( | 142 |
183 const em::DeviceManagementResponse& response) = 0; | 143 // Query parameters. |
| 144 URLQueryParameters query_params_; |
| 145 |
| 146 // Auth token (if applicaple). |
| 147 std::string auth_token_; |
| 148 |
| 149 // Device management token (if applicable). |
| 150 std::string device_management_token_; |
| 151 |
| 152 // The payload. |
| 153 std::string payload_; |
| 154 |
| 155 DISALLOW_COPY_AND_ASSIGN(DeviceManagementJobBase); |
184 }; | 156 }; |
185 | 157 |
186 void ResponseHandler::HandleResponse(const URLRequestStatus& status, | 158 void DeviceManagementJobBase::HandleResponse(const URLRequestStatus& status, |
187 int response_code, | 159 int response_code, |
188 const ResponseCookies& cookies, | 160 const ResponseCookies& cookies, |
189 const std::string& data) { | 161 const std::string& data) { |
| 162 // Delete ourselves when this is done. |
| 163 scoped_ptr<DeviceManagementJob> scoped_killer(this); |
| 164 |
190 if (status.status() != URLRequestStatus::SUCCESS) { | 165 if (status.status() != URLRequestStatus::SUCCESS) { |
191 OnError(DeviceManagementBackend::kErrorRequestFailed); | 166 OnError(DeviceManagementBackend::kErrorRequestFailed); |
192 return; | 167 return; |
193 } | 168 } |
194 | 169 |
195 if (response_code != 200) { | 170 if (response_code != 200) { |
196 OnError(DeviceManagementBackend::kErrorHttpStatus); | 171 OnError(DeviceManagementBackend::kErrorHttpStatus); |
197 return; | 172 return; |
198 } | 173 } |
199 | 174 |
(...skipping 19 matching lines...) Expand all Loading... |
219 case em::DeviceManagementResponse::ACTIVATION_PENDING: | 194 case em::DeviceManagementResponse::ACTIVATION_PENDING: |
220 OnError(DeviceManagementBackend::kErrorServiceActivationPending); | 195 OnError(DeviceManagementBackend::kErrorServiceActivationPending); |
221 return; | 196 return; |
222 default: | 197 default: |
223 // This should be caught by the protobuf decoder. | 198 // This should be caught by the protobuf decoder. |
224 NOTREACHED(); | 199 NOTREACHED(); |
225 OnError(DeviceManagementBackend::kErrorResponseDecoding); | 200 OnError(DeviceManagementBackend::kErrorResponseDecoding); |
226 return; | 201 return; |
227 } | 202 } |
228 | 203 |
229 ProcessResponse(response); | 204 OnResponse(response); |
230 } | 205 } |
231 | 206 |
232 // Handles device registration responses. | 207 GURL DeviceManagementJobBase::GetURL( |
233 class RegisterResponseHandler : public ResponseHandler { | 208 const std::string& server_url) { |
| 209 return GURL(server_url + '?' + query_params_.Encode()); |
| 210 } |
| 211 |
| 212 void DeviceManagementJobBase::ConfigureRequest(URLFetcher* fetcher) { |
| 213 fetcher->set_upload_data("application/octet-stream", payload_); |
| 214 std::string extra_headers; |
| 215 if (!auth_token_.empty()) |
| 216 extra_headers += kServiceTokenAuthHeader + auth_token_ + "\n"; |
| 217 if (!device_management_token_.empty()) |
| 218 extra_headers += kDMTokenAuthHeader + device_management_token_ + "\n"; |
| 219 fetcher->set_extra_request_headers(extra_headers); |
| 220 } |
| 221 |
| 222 // Handles device registration jobs. |
| 223 class DeviceManagementRegisterJob : public DeviceManagementJobBase { |
234 public: | 224 public: |
235 RegisterResponseHandler( | 225 DeviceManagementRegisterJob( |
236 DeviceManagementBackend::DeviceRegisterResponseDelegate* delegate) | 226 DeviceManagementBackendImpl* backend_impl, |
237 : delegate_(delegate) {} | 227 const std::string& auth_token, |
| 228 const std::string& device_id, |
| 229 const em::DeviceRegisterRequest& request, |
| 230 DeviceManagementBackend::DeviceRegisterResponseDelegate* delegate); |
| 231 virtual ~DeviceManagementRegisterJob() {} |
238 | 232 |
239 private: | 233 private: |
240 // ResponseHandler overrides. | 234 // DeviceManagementJobBase overrides. |
241 virtual void OnError(DeviceManagementBackend::ErrorCode error) { | 235 virtual void OnError(DeviceManagementBackend::ErrorCode error) { |
242 delegate_->OnError(error); | 236 delegate_->OnError(error); |
243 } | 237 } |
244 virtual void ProcessResponse(const em::DeviceManagementResponse& response) { | 238 virtual void OnResponse(const em::DeviceManagementResponse& response) { |
245 delegate_->HandleRegisterResponse(response.register_response()); | 239 delegate_->HandleRegisterResponse(response.register_response()); |
246 } | 240 } |
247 | 241 |
248 DeviceManagementBackend::DeviceRegisterResponseDelegate* delegate_; | 242 DeviceManagementBackend::DeviceRegisterResponseDelegate* delegate_; |
| 243 |
| 244 DISALLOW_COPY_AND_ASSIGN(DeviceManagementRegisterJob); |
249 }; | 245 }; |
250 | 246 |
251 // Handles device unregister responses. | 247 DeviceManagementRegisterJob::DeviceManagementRegisterJob( |
252 class UnregisterResponseHandler : public ResponseHandler { | 248 DeviceManagementBackendImpl* backend_impl, |
| 249 const std::string& auth_token, |
| 250 const std::string& device_id, |
| 251 const em::DeviceRegisterRequest& request, |
| 252 DeviceManagementBackend::DeviceRegisterResponseDelegate* delegate) |
| 253 : DeviceManagementJobBase(backend_impl, "register"), |
| 254 delegate_(delegate) { |
| 255 SetDeviceID(device_id); |
| 256 SetAuthToken(auth_token); |
| 257 em::DeviceManagementRequest request_wrapper; |
| 258 request_wrapper.mutable_register_request()->CopyFrom(request); |
| 259 SetPayload(request_wrapper); |
| 260 } |
| 261 |
| 262 // Handles device unregistration jobs. |
| 263 class DeviceManagementUnregisterJob : public DeviceManagementJobBase { |
253 public: | 264 public: |
254 UnregisterResponseHandler( | 265 DeviceManagementUnregisterJob( |
255 DeviceManagementBackend::DeviceUnregisterResponseDelegate* delegate) | 266 DeviceManagementBackendImpl* backend_impl, |
256 : delegate_(delegate) {} | 267 const std::string& device_management_token, |
| 268 const em::DeviceUnregisterRequest& request, |
| 269 DeviceManagementBackend::DeviceUnregisterResponseDelegate* delegate); |
| 270 virtual ~DeviceManagementUnregisterJob() {} |
257 | 271 |
258 private: | 272 private: |
259 // ResponseHandler overrides. | 273 // DeviceManagementJobBase overrides. |
260 virtual void OnError(DeviceManagementBackend::ErrorCode error) { | 274 virtual void OnError(DeviceManagementBackend::ErrorCode error) { |
261 delegate_->OnError(error); | 275 delegate_->OnError(error); |
262 } | 276 } |
263 virtual void ProcessResponse(const em::DeviceManagementResponse& response) { | 277 virtual void OnResponse(const em::DeviceManagementResponse& response) { |
264 delegate_->HandleUnregisterResponse(response.unregister_response()); | 278 delegate_->HandleUnregisterResponse(response.unregister_response()); |
265 } | 279 } |
266 | 280 |
267 DeviceManagementBackend::DeviceUnregisterResponseDelegate* delegate_; | 281 DeviceManagementBackend::DeviceUnregisterResponseDelegate* delegate_; |
| 282 |
| 283 DISALLOW_COPY_AND_ASSIGN(DeviceManagementUnregisterJob); |
268 }; | 284 }; |
269 | 285 |
270 // Handles device policy responses. | 286 DeviceManagementUnregisterJob::DeviceManagementUnregisterJob( |
271 class PolicyResponseHandler : public ResponseHandler { | 287 DeviceManagementBackendImpl* backend_impl, |
| 288 const std::string& device_management_token, |
| 289 const em::DeviceUnregisterRequest& request, |
| 290 DeviceManagementBackend::DeviceUnregisterResponseDelegate* delegate) |
| 291 : DeviceManagementJobBase(backend_impl, "unregister"), |
| 292 delegate_(delegate) { |
| 293 SetDeviceManagementToken(device_management_token); |
| 294 em::DeviceManagementRequest request_wrapper; |
| 295 request_wrapper.mutable_unregister_request()->CopyFrom(request); |
| 296 SetPayload(request_wrapper); |
| 297 } |
| 298 |
| 299 // Handles policy request jobs. |
| 300 class DeviceManagementPolicyJob : public DeviceManagementJobBase { |
272 public: | 301 public: |
273 PolicyResponseHandler( | 302 DeviceManagementPolicyJob( |
274 DeviceManagementBackend::DevicePolicyResponseDelegate* delegate) | 303 DeviceManagementBackendImpl* backend_impl, |
275 : delegate_(delegate) {} | 304 const std::string& device_management_token, |
| 305 const em::DevicePolicyRequest& request, |
| 306 DeviceManagementBackend::DevicePolicyResponseDelegate* delegate); |
| 307 virtual ~DeviceManagementPolicyJob() {} |
276 | 308 |
277 private: | 309 private: |
278 // ResponseHandler overrides. | 310 // DeviceManagementJobBase overrides. |
279 virtual void OnError(DeviceManagementBackend::ErrorCode error) { | 311 virtual void OnError(DeviceManagementBackend::ErrorCode error) { |
280 delegate_->OnError(error); | 312 delegate_->OnError(error); |
281 } | 313 } |
282 virtual void ProcessResponse(const em::DeviceManagementResponse& response) { | 314 virtual void OnResponse(const em::DeviceManagementResponse& response) { |
283 delegate_->HandlePolicyResponse(response.policy_response()); | 315 delegate_->HandlePolicyResponse(response.policy_response()); |
284 } | 316 } |
285 | 317 |
286 DeviceManagementBackend::DevicePolicyResponseDelegate* delegate_; | 318 DeviceManagementBackend::DevicePolicyResponseDelegate* delegate_; |
| 319 |
| 320 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyJob); |
287 }; | 321 }; |
288 | 322 |
| 323 DeviceManagementPolicyJob::DeviceManagementPolicyJob( |
| 324 DeviceManagementBackendImpl* backend_impl, |
| 325 const std::string& device_management_token, |
| 326 const em::DevicePolicyRequest& request, |
| 327 DeviceManagementBackend::DevicePolicyResponseDelegate* delegate) |
| 328 : DeviceManagementJobBase(backend_impl, "policy"), |
| 329 delegate_(delegate) { |
| 330 SetDeviceManagementToken(device_management_token); |
| 331 em::DeviceManagementRequest request_wrapper; |
| 332 request_wrapper.mutable_policy_request()->CopyFrom(request); |
| 333 SetPayload(request_wrapper); |
| 334 } |
| 335 |
289 DeviceManagementBackendImpl::DeviceManagementBackendImpl( | 336 DeviceManagementBackendImpl::DeviceManagementBackendImpl( |
290 const std::string& server_url) | 337 DeviceManagementService* service) |
291 : server_url_(server_url), | 338 : service_(service) { |
292 request_context_getter_( | |
293 new DeviceManagementBackendRequestContextGetter()) { | |
294 } | 339 } |
295 | 340 |
296 DeviceManagementBackendImpl::~DeviceManagementBackendImpl() { | 341 DeviceManagementBackendImpl::~DeviceManagementBackendImpl() { |
297 // Cancel all pending requests. | 342 // Swap to a helper, so we don't interfere with the unregistration on delete. |
298 STLDeleteContainerPairPointers(response_handlers_.begin(), | 343 JobSet to_be_deleted; |
299 response_handlers_.end()); | 344 to_be_deleted.swap(pending_jobs_); |
| 345 for (JobSet::iterator job(to_be_deleted.begin()); |
| 346 job != to_be_deleted.end(); |
| 347 ++job) { |
| 348 service_->RemoveJob(*job); |
| 349 delete *job; |
| 350 } |
| 351 } |
| 352 |
| 353 void DeviceManagementBackendImpl::JobDone(DeviceManagementJobBase* job) { |
| 354 pending_jobs_.erase(job); |
| 355 } |
| 356 |
| 357 void DeviceManagementBackendImpl::AddJob(DeviceManagementJobBase* job) { |
| 358 pending_jobs_.insert(job); |
| 359 service_->AddJob(job); |
300 } | 360 } |
301 | 361 |
302 void DeviceManagementBackendImpl::ProcessRegisterRequest( | 362 void DeviceManagementBackendImpl::ProcessRegisterRequest( |
303 const std::string& auth_token, | 363 const std::string& auth_token, |
304 const std::string& device_id, | 364 const std::string& device_id, |
305 const em::DeviceRegisterRequest& request, | 365 const em::DeviceRegisterRequest& request, |
306 DeviceRegisterResponseDelegate* delegate) { | 366 DeviceRegisterResponseDelegate* delegate) { |
307 em::DeviceManagementRequest request_wrapper; | 367 AddJob(new DeviceManagementRegisterJob(this, auth_token, device_id, request, |
308 request_wrapper.mutable_register_request()->CopyFrom(request); | 368 delegate)); |
309 | |
310 URLQueryParameters params; | |
311 PutCommonQueryParameters(¶ms); | |
312 params.Put(kServiceParamRequest, "register"); | |
313 params.Put(kServiceParamDeviceID, device_id); | |
314 | |
315 CreateFetcher(request_wrapper, | |
316 new RegisterResponseHandler(delegate), | |
317 params.Encode(), | |
318 kServiceTokenAuthHeader + auth_token); | |
319 } | 369 } |
320 | 370 |
321 void DeviceManagementBackendImpl::ProcessUnregisterRequest( | 371 void DeviceManagementBackendImpl::ProcessUnregisterRequest( |
322 const std::string& device_management_token, | 372 const std::string& device_management_token, |
323 const em::DeviceUnregisterRequest& request, | 373 const em::DeviceUnregisterRequest& request, |
324 DeviceUnregisterResponseDelegate* delegate) { | 374 DeviceUnregisterResponseDelegate* delegate) { |
325 em::DeviceManagementRequest request_wrapper; | 375 AddJob(new DeviceManagementUnregisterJob(this, device_management_token, |
326 request_wrapper.mutable_unregister_request()->CopyFrom(request); | 376 request, delegate)); |
327 | |
328 URLQueryParameters params; | |
329 PutCommonQueryParameters(¶ms); | |
330 params.Put(kServiceParamRequest, "unregister"); | |
331 | |
332 CreateFetcher(request_wrapper, | |
333 new UnregisterResponseHandler(delegate), | |
334 params.Encode(), | |
335 kDMTokenAuthHeader + device_management_token); | |
336 } | 377 } |
337 | 378 |
338 void DeviceManagementBackendImpl::ProcessPolicyRequest( | 379 void DeviceManagementBackendImpl::ProcessPolicyRequest( |
339 const std::string& device_management_token, | 380 const std::string& device_management_token, |
340 const em::DevicePolicyRequest& request, | 381 const em::DevicePolicyRequest& request, |
341 DevicePolicyResponseDelegate* delegate) { | 382 DevicePolicyResponseDelegate* delegate) { |
342 em::DeviceManagementRequest request_wrapper; | 383 AddJob(new DeviceManagementPolicyJob(this, device_management_token, request, |
343 request_wrapper.mutable_policy_request()->CopyFrom(request); | 384 delegate)); |
344 | |
345 URLQueryParameters params; | |
346 PutCommonQueryParameters(¶ms); | |
347 params.Put(kServiceParamRequest, "policy"); | |
348 | |
349 CreateFetcher(request_wrapper, | |
350 new PolicyResponseHandler(delegate), | |
351 params.Encode(), | |
352 kDMTokenAuthHeader + device_management_token); | |
353 } | |
354 | |
355 // static | |
356 std::string DeviceManagementBackendImpl::GetAgentString() { | |
357 chrome::VersionInfo version_info; | |
358 return base::StringPrintf(kServiceValueAgent, | |
359 version_info.Name().c_str(), | |
360 version_info.Version().c_str(), | |
361 version_info.LastChange().c_str()); | |
362 } | |
363 | |
364 void DeviceManagementBackendImpl::OnURLFetchComplete( | |
365 const URLFetcher* source, | |
366 const GURL& url, | |
367 const URLRequestStatus& status, | |
368 int response_code, | |
369 const ResponseCookies& cookies, | |
370 const std::string& data) { | |
371 ResponseHandlerMap::iterator entry(response_handlers_.find(source)); | |
372 if (entry != response_handlers_.end()) { | |
373 ResponseHandler* handler = entry->second; | |
374 handler->HandleResponse(status, response_code, cookies, data); | |
375 response_handlers_.erase(entry); | |
376 delete handler; | |
377 } else { | |
378 NOTREACHED() << "Callback from foreign URL fetcher"; | |
379 } | |
380 delete source; | |
381 } | |
382 | |
383 void DeviceManagementBackendImpl::CreateFetcher( | |
384 const em::DeviceManagementRequest& request, | |
385 ResponseHandler* handler, | |
386 const std::string& query_params, | |
387 const std::string& extra_headers) { | |
388 scoped_ptr<ResponseHandler> handler_ptr(handler); | |
389 | |
390 // Construct the payload. | |
391 std::string payload; | |
392 if (!request.SerializeToString(&payload)) { | |
393 handler->OnError(DeviceManagementBackend::kErrorRequestInvalid); | |
394 return; | |
395 } | |
396 | |
397 // Instantiate the fetcher. | |
398 GURL url(server_url_ + '?' + query_params); | |
399 URLFetcher* fetcher = URLFetcher::Create(0, url, URLFetcher::POST, this); | |
400 fetcher->set_request_context(request_context_getter_.get()); | |
401 fetcher->set_upload_data("application/octet-stream", payload); | |
402 fetcher->set_extra_request_headers(extra_headers); | |
403 response_handlers_[fetcher] = handler_ptr.release(); | |
404 | |
405 // Start the request. The fetcher will call OnURLFetchComplete when done. | |
406 fetcher->Start(); | |
407 } | |
408 | |
409 void DeviceManagementBackendImpl::PutCommonQueryParameters( | |
410 URLQueryParameters* params) { | |
411 params->Put(kServiceParamDeviceType, kServiceValueDeviceType); | |
412 params->Put(kServiceParamAgent, GetAgentString()); | |
413 } | 385 } |
414 | 386 |
415 } // namespace policy | 387 } // namespace policy |
OLD | NEW |