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