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