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