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