| OLD | NEW |
| (Empty) |
| 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_DEVICE_MANAGEMENT_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_POLICY_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_policy_constants.h" | |
| 19 #include "chrome/browser/policy/proto/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_POLICY_FETCH, | |
| 41 TYPE_UNREGISTRATION, | |
| 42 }; | |
| 43 | |
| 44 typedef base::Callback< | |
| 45 void(DeviceManagementStatus, | |
| 46 const enterprise_management::DeviceManagementResponse&)> Callback; | |
| 47 | |
| 48 typedef base::Callback<void(DeviceManagementRequestJob*)> RetryCallback; | |
| 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 // A job may automatically retry if it fails due to a temporary condition, or | |
| 62 // due to proxy misconfigurations. If a |retry_callback| is set then it will | |
| 63 // be invoked with the DeviceManagementRequestJob as an argument when that | |
| 64 // happens, so that the job's owner can customize the retry request before | |
| 65 // it's sent. | |
| 66 void SetRetryCallback(const RetryCallback& retry_callback); | |
| 67 | |
| 68 // Starts the job. |callback| will be invoked on completion. | |
| 69 void Start(const Callback& callback); | |
| 70 | |
| 71 protected: | |
| 72 typedef std::vector<std::pair<std::string, std::string> > ParameterMap; | |
| 73 | |
| 74 explicit DeviceManagementRequestJob(JobType type); | |
| 75 | |
| 76 // Appends a parameter to |query_params|. | |
| 77 void AddParameter(const std::string& name, const std::string& value); | |
| 78 | |
| 79 // Fires the job, to be filled in by implementations. | |
| 80 virtual void Run() = 0; | |
| 81 | |
| 82 ParameterMap query_params_; | |
| 83 std::string gaia_token_; | |
| 84 std::string dm_token_; | |
| 85 enterprise_management::DeviceManagementRequest request_; | |
| 86 RetryCallback retry_callback_; | |
| 87 | |
| 88 Callback callback_; | |
| 89 | |
| 90 private: | |
| 91 DISALLOW_COPY_AND_ASSIGN(DeviceManagementRequestJob); | |
| 92 }; | |
| 93 | |
| 94 // The device management service is responsible for everything related to | |
| 95 // communication with the device management server. It creates the backends | |
| 96 // objects that the device management policy provider and friends use to issue | |
| 97 // requests. | |
| 98 class DeviceManagementService : public net::URLFetcherDelegate { | |
| 99 public: | |
| 100 explicit DeviceManagementService(const std::string& server_url); | |
| 101 virtual ~DeviceManagementService(); | |
| 102 | |
| 103 // The ID of URLFetchers created by the DeviceManagementService. This can be | |
| 104 // used by tests that use a TestURLFetcherFactory to get the pending fetchers | |
| 105 // created by the DeviceManagementService. | |
| 106 static const int kURLFetcherID; | |
| 107 | |
| 108 // Creates a new device management request job. Ownership is transferred to | |
| 109 // the caller. | |
| 110 virtual DeviceManagementRequestJob* CreateJob( | |
| 111 DeviceManagementRequestJob::JobType type); | |
| 112 | |
| 113 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed. | |
| 114 void ScheduleInitialization(int64 delay_milliseconds); | |
| 115 | |
| 116 // Makes the service stop all requests and drop the reference to the request | |
| 117 // context. | |
| 118 void Shutdown(); | |
| 119 | |
| 120 private: | |
| 121 typedef std::map<const net::URLFetcher*, | |
| 122 DeviceManagementRequestJobImpl*> JobFetcherMap; | |
| 123 typedef std::deque<DeviceManagementRequestJobImpl*> JobQueue; | |
| 124 | |
| 125 friend class DeviceManagementRequestJobImpl; | |
| 126 | |
| 127 // net::URLFetcherDelegate override. | |
| 128 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 129 | |
| 130 // Does the actual initialization using the request context specified for | |
| 131 // |PrepareInitialization|. This will also fire any pending network requests. | |
| 132 void Initialize(); | |
| 133 | |
| 134 // Starts a job. | |
| 135 void StartJob(DeviceManagementRequestJobImpl* job); | |
| 136 | |
| 137 // Adds a job. Caller must make sure the job pointer stays valid until the job | |
| 138 // completes or gets canceled via RemoveJob(). | |
| 139 void AddJob(DeviceManagementRequestJobImpl* job); | |
| 140 | |
| 141 // Removes a job. The job will be removed and won't receive a completion | |
| 142 // callback. | |
| 143 void RemoveJob(DeviceManagementRequestJobImpl* job); | |
| 144 | |
| 145 // Server at which to contact the service. | |
| 146 const std::string server_url_; | |
| 147 | |
| 148 // The request context we use. | |
| 149 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 150 | |
| 151 // The jobs we currently have in flight. | |
| 152 JobFetcherMap pending_jobs_; | |
| 153 | |
| 154 // Jobs that are registered, but not started yet. | |
| 155 JobQueue queued_jobs_; | |
| 156 | |
| 157 // If this service is initialized, incoming requests get fired instantly. | |
| 158 // If it is not initialized, incoming requests are queued. | |
| 159 bool initialized_; | |
| 160 | |
| 161 // Used to create tasks to run |Initialize| delayed on the UI thread. | |
| 162 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_; | |
| 163 | |
| 164 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService); | |
| 165 }; | |
| 166 | |
| 167 } // namespace policy | |
| 168 | |
| 169 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ | |
| OLD | NEW |