| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ |
| 6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ | 6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 20 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 18 #include "content/public/common/url_fetcher_delegate.h" | 21 #include "content/public/common/url_fetcher_delegate.h" |
| 19 #include "googleurl/src/gurl.h" | |
| 20 | 22 |
| 21 namespace net { | 23 namespace net { |
| 22 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 23 class URLRequestStatus; | |
| 24 typedef std::vector<std::string> ResponseCookies; | |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace policy { | 27 namespace policy { |
| 28 | 28 |
| 29 class DeviceManagementBackend; | 29 class DeviceManagementBackend; |
| 30 class DeviceManagementRequestJobImpl; |
| 31 class DeviceManagementService; |
| 32 |
| 33 // DeviceManagementRequestJob describes a request to send to the device |
| 34 // management service. Jobs are created by DeviceManagementService. They can be |
| 35 // canceled by deleting the object. |
| 36 class DeviceManagementRequestJob { |
| 37 public: |
| 38 // Describes the job type. |
| 39 enum JobType { |
| 40 TYPE_AUTO_ENROLLMENT, |
| 41 TYPE_REGISTRATION, |
| 42 TYPE_POLICY_FETCH, |
| 43 TYPE_UNREGISTRATION, |
| 44 }; |
| 45 |
| 46 typedef base::Callback< |
| 47 void(DeviceManagementStatus, |
| 48 const enterprise_management::DeviceManagementResponse&)> Callback; |
| 49 |
| 50 virtual ~DeviceManagementRequestJob(); |
| 51 |
| 52 // Functions for configuring the job. These should only be called before |
| 53 // Start()ing the job, but never afterwards. |
| 54 void SetGaiaToken(const std::string& gaia_token); |
| 55 void SetOAuthToken(const std::string& oauth_token); |
| 56 void SetUserAffiliation(UserAffiliation user_affiliation); |
| 57 void SetDMToken(const std::string& dm_token); |
| 58 void SetClientID(const std::string& client_id); |
| 59 enterprise_management::DeviceManagementRequest* GetRequest(); |
| 60 |
| 61 // Starts the job. |callback| will be invoked on completion. |
| 62 void Start(const DeviceManagementRequestJob::Callback& callback); |
| 63 |
| 64 protected: |
| 65 typedef std::vector<std::pair<std::string, std::string> > ParameterMap; |
| 66 |
| 67 explicit DeviceManagementRequestJob(JobType type); |
| 68 |
| 69 // Appends a parameter to |query_params|. |
| 70 void AddParameter(const std::string& name, const std::string& value); |
| 71 |
| 72 // Fires the job, to be filled in by implementations. |
| 73 virtual void Run() = 0; |
| 74 |
| 75 ParameterMap query_params_; |
| 76 std::string gaia_token_; |
| 77 std::string dm_token_; |
| 78 enterprise_management::DeviceManagementRequest request_; |
| 79 |
| 80 Callback callback_; |
| 81 |
| 82 private: |
| 83 DISALLOW_COPY_AND_ASSIGN(DeviceManagementRequestJob); |
| 84 }; |
| 30 | 85 |
| 31 // The device management service is responsible for everything related to | 86 // The device management service is responsible for everything related to |
| 32 // communication with the device management server. It creates the backends | 87 // communication with the device management server. It creates the backends |
| 33 // objects that the device management policy provider and friends use to issue | 88 // objects that the device management policy provider and friends use to issue |
| 34 // requests. | 89 // requests. |
| 35 class DeviceManagementService : public content::URLFetcherDelegate { | 90 class DeviceManagementService : public content::URLFetcherDelegate { |
| 36 public: | 91 public: |
| 37 // Describes a device management job handled by the service. | |
| 38 class DeviceManagementJob { | |
| 39 public: | |
| 40 virtual ~DeviceManagementJob() {} | |
| 41 | |
| 42 // Handles the URL request response. | |
| 43 virtual void HandleResponse(const net::URLRequestStatus& status, | |
| 44 int response_code, | |
| 45 const net::ResponseCookies& cookies, | |
| 46 const std::string& data) = 0; | |
| 47 | |
| 48 // Gets the URL to contact. | |
| 49 virtual GURL GetURL(const std::string& server_url) = 0; | |
| 50 | |
| 51 // Configures the fetcher, setting up payload and headers. | |
| 52 virtual void ConfigureRequest(content::URLFetcher* fetcher) = 0; | |
| 53 }; | |
| 54 | |
| 55 explicit DeviceManagementService(const std::string& server_url); | 92 explicit DeviceManagementService(const std::string& server_url); |
| 56 virtual ~DeviceManagementService(); | 93 virtual ~DeviceManagementService(); |
| 57 | 94 |
| 58 // Constructs a device management backend for use by client code. Ownership of | 95 // Constructs a device management backend for use by client code. Ownership of |
| 59 // the returned backend object is transferred to the caller. | 96 // the returned backend object is transferred to the caller. |
| 60 // Marked virtual for the benefit of tests. | 97 // Marked virtual for the benefit of tests. |
| 98 // TODO(mnissler): This is deprecated, remove. Use CreateJob() instead. |
| 61 virtual DeviceManagementBackend* CreateBackend(); | 99 virtual DeviceManagementBackend* CreateBackend(); |
| 62 | 100 |
| 101 // Creates a new device management request job. Ownership is transferred to |
| 102 // the caller. |
| 103 virtual DeviceManagementRequestJob* CreateJob( |
| 104 DeviceManagementRequestJob::JobType type); |
| 105 |
| 63 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed. | 106 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed. |
| 64 void ScheduleInitialization(int64 delay_milliseconds); | 107 void ScheduleInitialization(int64 delay_milliseconds); |
| 65 | 108 |
| 66 // Makes the service stop all requests and drop the reference to the request | 109 // Makes the service stop all requests and drop the reference to the request |
| 67 // context. | 110 // context. |
| 68 void Shutdown(); | 111 void Shutdown(); |
| 69 | 112 |
| 70 // Adds a job. Caller must make sure the job pointer stays valid until the job | 113 private: |
| 71 // completes or gets canceled via RemoveJob(). | 114 typedef std::map<const content::URLFetcher*, |
| 72 void AddJob(DeviceManagementJob* job); | 115 DeviceManagementRequestJobImpl*> JobFetcherMap; |
| 116 typedef std::deque<DeviceManagementRequestJobImpl*> JobQueue; |
| 73 | 117 |
| 74 // Removes a job. The job will be removed and won't receive a completion | 118 friend class DeviceManagementRequestJobImpl; |
| 75 // callback. | |
| 76 void RemoveJob(DeviceManagementJob* job); | |
| 77 | |
| 78 protected: | |
| 79 // Starts the given job. | |
| 80 virtual void StartJob(DeviceManagementJob* job, bool bypass_proxy); | |
| 81 | |
| 82 private: | |
| 83 typedef std::map<const content::URLFetcher*, DeviceManagementJob*> | |
| 84 JobFetcherMap; | |
| 85 typedef std::deque<DeviceManagementJob*> JobQueue; | |
| 86 | 119 |
| 87 // content::URLFetcherDelegate override. | 120 // content::URLFetcherDelegate override. |
| 88 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 121 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 89 | 122 |
| 90 // Does the actual initialization using the request context specified for | 123 // Does the actual initialization using the request context specified for |
| 91 // |PrepareInitialization|. This will also fire any pending network requests. | 124 // |PrepareInitialization|. This will also fire any pending network requests. |
| 92 void Initialize(); | 125 void Initialize(); |
| 93 | 126 |
| 127 // Starts a job. |
| 128 void StartJob(DeviceManagementRequestJobImpl* job, bool bypass_proxy); |
| 129 |
| 130 // Adds a job. Caller must make sure the job pointer stays valid until the job |
| 131 // completes or gets canceled via RemoveJob(). |
| 132 void AddJob(DeviceManagementRequestJobImpl* job); |
| 133 |
| 134 // Removes a job. The job will be removed and won't receive a completion |
| 135 // callback. |
| 136 void RemoveJob(DeviceManagementRequestJobImpl* job); |
| 137 |
| 94 // Server at which to contact the service. | 138 // Server at which to contact the service. |
| 95 const std::string server_url_; | 139 const std::string server_url_; |
| 96 | 140 |
| 97 // The request context we use. | 141 // The request context we use. |
| 98 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 142 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 99 | 143 |
| 100 // The jobs we currently have in flight. | 144 // The jobs we currently have in flight. |
| 101 JobFetcherMap pending_jobs_; | 145 JobFetcherMap pending_jobs_; |
| 102 | 146 |
| 103 // Jobs that are registered, but not started yet. | 147 // Jobs that are registered, but not started yet. |
| 104 JobQueue queued_jobs_; | 148 JobQueue queued_jobs_; |
| 105 | 149 |
| 106 // If this service is initialized, incoming requests get fired instantly. | 150 // If this service is initialized, incoming requests get fired instantly. |
| 107 // If it is not initialized, incoming requests are queued. | 151 // If it is not initialized, incoming requests are queued. |
| 108 bool initialized_; | 152 bool initialized_; |
| 109 | 153 |
| 110 // Used to create tasks to run |Initialize| delayed on the UI thread. | 154 // Used to create tasks to run |Initialize| delayed on the UI thread. |
| 111 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_; | 155 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_; |
| 112 | 156 |
| 113 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService); | 157 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService); |
| 114 }; | 158 }; |
| 115 | 159 |
| 116 } // namespace policy | 160 } // namespace policy |
| 117 | 161 |
| 118 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ | 162 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ |
| OLD | NEW |