Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(581)

Side by Side Diff: chrome/browser/policy/device_management_service.h

Issue 8952027: base::Callback-ify policy::DeviceManagementService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
Joao da Silva 2012/01/02 16:14:43 2012
Mattias Nissler (ping if slow) 2012/01/02 17:53:23 Done.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ 5 #ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ 6 #define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <deque> 9 #include <deque>
10 #include <map> 10 #include <map>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
Joao da Silva 2012/01/02 16:14:43 Nit: not used
Mattias Nissler (ping if slow) 2012/01/02 17:53:23 Done.
13 13
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/callback.h"
15 #include "base/compiler_specific.h" 16 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h" 17 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
19 #include "chrome/browser/policy/cloud_policy_constants.h"
20 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
18 #include "content/public/common/url_fetcher_delegate.h" 21 #include "content/public/common/url_fetcher_delegate.h"
19 #include "googleurl/src/gurl.h" 22 #include "googleurl/src/gurl.h"
Joao da Silva 2012/01/02 16:14:43 Nit: move to .cc
Mattias Nissler (ping if slow) 2012/01/02 17:53:23 Done.
20 23
21 namespace net { 24 namespace net {
22 class URLRequestContextGetter; 25 class URLRequestContextGetter;
23 class URLRequestStatus; 26 class URLRequestStatus;
Joao da Silva 2012/01/02 16:14:43 Nit: not used
Mattias Nissler (ping if slow) 2012/01/02 17:53:23 Done.
24 typedef std::vector<std::string> ResponseCookies;
25 } 27 }
26 28
27 namespace policy { 29 namespace policy {
28 30
29 class DeviceManagementBackend; 31 class DeviceManagementBackend;
32 class DeviceManagementRequestJobImpl;
33 class DeviceManagementService;
34
35 // DeviceManagementRequestJob describes a request to send to the device
36 // management service. Jobs are created by DeviceManagementService. They can be
37 // canceled by deleting the object.
38 class DeviceManagementRequestJob {
39 public:
40 // Describes the job type.
41 enum JobType {
42 TYPE_AUTO_ENROLLMENT,
43 TYPE_REGISTRATION,
44 TYPE_POLICY_FETCH,
45 TYPE_UNREGISTRATION,
46 };
47
48 typedef base::Callback<
49 void(DeviceManagementStatus,
50 const enterprise_management::DeviceManagementResponse&)> Callback;
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 // Starts the job. |callback| will be invoked on completion.
64 void Start(const DeviceManagementRequestJob::Callback& callback);
65
66 protected:
67 typedef std::map<std::string, std::string> ParameterMap;
68
69 explicit DeviceManagementRequestJob(JobType type);
70
71 // Fires the job, to be filled in by implementations.
72 virtual void Run() = 0;
73
74 ParameterMap query_params_;
Joao da Silva 2012/01/02 16:14:43 This broke some tests on linux_chromeos. It used t
Mattias Nissler (ping if slow) 2012/01/02 17:53:23 Sigh. Reverted.
75 std::string gaia_token_;
76 std::string dm_token_;
77 enterprise_management::DeviceManagementRequest request_;
78
79 Callback callback_;
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(DeviceManagementRequestJob);
83 };
30 84
31 // The device management service is responsible for everything related to 85 // The device management service is responsible for everything related to
32 // communication with the device management server. It creates the backends 86 // communication with the device management server. It creates the backends
33 // objects that the device management policy provider and friends use to issue 87 // objects that the device management policy provider and friends use to issue
34 // requests. 88 // requests.
35 class DeviceManagementService : public content::URLFetcherDelegate { 89 class DeviceManagementService : public content::URLFetcherDelegate {
36 public: 90 public:
37 // Describes a device management job handled by the service.
38 class DeviceManagementJob {
39 public:
40 virtual ~DeviceManagementJob() {}
41
42 // Handles the URL request response.
43 virtual void HandleResponse(const net::URLRequestStatus& status,
44 int response_code,
45 const net::ResponseCookies& cookies,
46 const std::string& data) = 0;
47
48 // Gets the URL to contact.
49 virtual GURL GetURL(const std::string& server_url) = 0;
50
51 // Configures the fetcher, setting up payload and headers.
52 virtual void ConfigureRequest(content::URLFetcher* fetcher) = 0;
53 };
54
55 explicit DeviceManagementService(const std::string& server_url); 91 explicit DeviceManagementService(const std::string& server_url);
56 virtual ~DeviceManagementService(); 92 virtual ~DeviceManagementService();
57 93
58 // Constructs a device management backend for use by client code. Ownership of 94 // Constructs a device management backend for use by client code. Ownership of
59 // the returned backend object is transferred to the caller. 95 // the returned backend object is transferred to the caller.
60 // Marked virtual for the benefit of tests. 96 // Marked virtual for the benefit of tests.
97 // TODO(mnissler): This is deprecated, remove. Use CreateJob() instead.
61 virtual DeviceManagementBackend* CreateBackend(); 98 virtual DeviceManagementBackend* CreateBackend();
62 99
100 // Creates a new device management request job. Ownership is transferred to
101 // the caller.
102 virtual DeviceManagementRequestJob* CreateJob(
103 DeviceManagementRequestJob::JobType type);
104
63 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed. 105 // Schedules a task to run |Initialize| after |delay_milliseconds| had passed.
64 void ScheduleInitialization(int64 delay_milliseconds); 106 void ScheduleInitialization(int64 delay_milliseconds);
65 107
66 // Makes the service stop all requests and drop the reference to the request 108 // Makes the service stop all requests and drop the reference to the request
67 // context. 109 // context.
68 void Shutdown(); 110 void Shutdown();
69 111
70 // Adds a job. Caller must make sure the job pointer stays valid until the job 112 private:
71 // completes or gets canceled via RemoveJob(). 113 typedef std::map<const content::URLFetcher*,
72 void AddJob(DeviceManagementJob* job); 114 DeviceManagementRequestJobImpl*> JobFetcherMap;
115 typedef std::deque<DeviceManagementRequestJobImpl*> JobQueue;
73 116
74 // Removes a job. The job will be removed and won't receive a completion 117 friend class DeviceManagementRequestJobImpl;
75 // callback.
76 void RemoveJob(DeviceManagementJob* job);
77
78 protected:
79 // Starts the given job.
80 virtual void StartJob(DeviceManagementJob* job, bool bypass_proxy);
81
82 private:
83 typedef std::map<const content::URLFetcher*, DeviceManagementJob*>
84 JobFetcherMap;
85 typedef std::deque<DeviceManagementJob*> JobQueue;
86 118
87 // content::URLFetcherDelegate override. 119 // content::URLFetcherDelegate override.
88 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; 120 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
89 121
90 // Does the actual initialization using the request context specified for 122 // Does the actual initialization using the request context specified for
91 // |PrepareInitialization|. This will also fire any pending network requests. 123 // |PrepareInitialization|. This will also fire any pending network requests.
92 void Initialize(); 124 void Initialize();
93 125
126 // Starts a job.
127 void StartJob(DeviceManagementRequestJobImpl* job, bool bypass_proxy);
128
129 // Adds a job. Caller must make sure the job pointer stays valid until the job
130 // completes or gets canceled via RemoveJob().
131 void AddJob(DeviceManagementRequestJobImpl* job);
132
133 // Removes a job. The job will be removed and won't receive a completion
134 // callback.
135 void RemoveJob(DeviceManagementRequestJobImpl* job);
136
94 // Server at which to contact the service. 137 // Server at which to contact the service.
95 const std::string server_url_; 138 const std::string server_url_;
96 139
97 // The request context we use. 140 // The request context we use.
98 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 141 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
99 142
100 // The jobs we currently have in flight. 143 // The jobs we currently have in flight.
101 JobFetcherMap pending_jobs_; 144 JobFetcherMap pending_jobs_;
102 145
103 // Jobs that are registered, but not started yet. 146 // Jobs that are registered, but not started yet.
104 JobQueue queued_jobs_; 147 JobQueue queued_jobs_;
105 148
106 // If this service is initialized, incoming requests get fired instantly. 149 // If this service is initialized, incoming requests get fired instantly.
107 // If it is not initialized, incoming requests are queued. 150 // If it is not initialized, incoming requests are queued.
108 bool initialized_; 151 bool initialized_;
109 152
110 // Used to create tasks to run |Initialize| delayed on the UI thread. 153 // Used to create tasks to run |Initialize| delayed on the UI thread.
111 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_; 154 base::WeakPtrFactory<DeviceManagementService> weak_ptr_factory_;
112 155
113 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService); 156 DISALLOW_COPY_AND_ASSIGN(DeviceManagementService);
114 }; 157 };
115 158
116 } // namespace policy 159 } // namespace policy
117 160
118 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_ 161 #endif // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698