OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
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_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ |
6 #define CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" |
15 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
16 #include "chrome/browser/notifications/notification_delegate.h" | 17 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
17 #include "chromeos/dbus/dbus_method_call_status.h" | 18 #include "chromeos/dbus/dbus_method_call_status.h" |
18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 #include "google_apis/gaia/oauth2_token_service.h" | 21 #include "google_apis/gaia/oauth2_token_service.h" |
21 | 22 |
22 class PrefRegistrySimple; | 23 class PrefRegistrySimple; |
23 class Profile; | 24 class Profile; |
24 | 25 |
25 namespace chromeos { | 26 namespace chromeos { |
26 class CryptohomeClient; | 27 class CryptohomeClient; |
(...skipping 16 matching lines...) Expand all Loading... |
43 // | 44 // |
44 // 2. Boot lockbox owner ID: Unlike the owner ID in CrosSettings, the owner ID | 45 // 2. Boot lockbox owner ID: Unlike the owner ID in CrosSettings, the owner ID |
45 // stored in the boot lockbox can only be modified after reboot and before | 46 // stored in the boot lockbox can only be modified after reboot and before |
46 // the first session starts. It is guaranteed that if the device is consumer | 47 // the first session starts. It is guaranteed that if the device is consumer |
47 // managed, the owner ID in the boot lockbox will be available, but not the | 48 // managed, the owner ID in the boot lockbox will be available, but not the |
48 // other way. | 49 // other way. |
49 // | 50 // |
50 // 3. Consumer management enrollment process: The service kicks off the last | 51 // 3. Consumer management enrollment process: The service kicks off the last |
51 // part of the consumer management enrollment process after the owner ID is | 52 // part of the consumer management enrollment process after the owner ID is |
52 // stored in the boot lockbox and the owner signs in. | 53 // stored in the boot lockbox and the owner signs in. |
53 class ConsumerManagementService : public content::NotificationObserver, | 54 class ConsumerManagementService |
54 public OAuth2TokenService::Consumer, | 55 : public chromeos::DeviceSettingsService::Observer, |
55 public OAuth2TokenService::Observer { | 56 public content::NotificationObserver, |
| 57 public OAuth2TokenService::Consumer, |
| 58 public OAuth2TokenService::Observer { |
56 public: | 59 public: |
57 enum ConsumerEnrollmentState { | 60 // The status indicates if the device is enrolled, or if enrollment or |
58 ENROLLMENT_NONE = 0, // Not enrolled, or enrollment is completed. | 61 // unenrollment is in progress. If you want to add a value here, please also |
59 ENROLLMENT_REQUESTED, // Enrollment is requested by the owner. | 62 // update |kStatusString| in the .cc file. |
60 ENROLLMENT_OWNER_STORED, // The owner ID is stored in the boot lockbox. | 63 enum Status { |
61 ENROLLMENT_SUCCESS, // Success. The notification is not sent yet. | 64 // The status is currently unavailable. |
| 65 STATUS_UNKNOWN = 0, |
62 | 66 |
63 // Error states. | 67 STATUS_ENROLLED, |
64 ENROLLMENT_CANCELED, // Canceled by the user. | 68 STATUS_ENROLLING, |
65 ENROLLMENT_BOOT_LOCKBOX_FAILED, // Failed to write to the boot lockbox. | 69 STATUS_UNENROLLED, |
66 ENROLLMENT_GET_TOKEN_FAILED, // Failed to get the access token. | 70 STATUS_UNENROLLING, |
67 ENROLLMENT_DM_SERVER_FAILED, // Failed to register the device. | |
68 | 71 |
69 ENROLLMENT_LAST, // This should always be the last one. | 72 // This should always be the last one. |
| 73 STATUS_LAST, |
| 74 }; |
| 75 |
| 76 // Indicating which stage the enrollment process is in. |
| 77 enum EnrollmentStage { |
| 78 // Not enrolled, or enrollment is completed. |
| 79 ENROLLMENT_STAGE_NONE = 0, |
| 80 // Enrollment is requested by the owner. |
| 81 ENROLLMENT_STAGE_REQUESTED, |
| 82 // The owner ID is stored in the boot lockbox. |
| 83 ENROLLMENT_STAGE_OWNER_STORED, |
| 84 // Success. The notification is not sent yet. |
| 85 ENROLLMENT_STAGE_SUCCESS, |
| 86 |
| 87 // Error stages. |
| 88 // Canceled by the user. |
| 89 ENROLLMENT_STAGE_CANCELED, |
| 90 // Failed to write to the boot lockbox. |
| 91 ENROLLMENT_STAGE_BOOT_LOCKBOX_FAILED, |
| 92 // Failed to get the access token. |
| 93 ENROLLMENT_STAGE_GET_TOKEN_FAILED, |
| 94 // Failed to register the device. |
| 95 ENROLLMENT_STAGE_DM_SERVER_FAILED, |
| 96 |
| 97 // This should always be the last one. |
| 98 ENROLLMENT_STAGE_LAST, |
| 99 }; |
| 100 |
| 101 class Observer { |
| 102 public: |
| 103 // Called when the status changes. |
| 104 virtual void OnConsumerManagementStatusChanged() = 0; |
70 }; | 105 }; |
71 | 106 |
72 // GetOwner() invokes this with an argument set to the owner user ID, | 107 // GetOwner() invokes this with an argument set to the owner user ID, |
73 // or an empty string on failure. | 108 // or an empty string on failure. |
74 typedef base::Callback<void(const std::string&)> GetOwnerCallback; | 109 typedef base::Callback<void(const std::string&)> GetOwnerCallback; |
75 | 110 |
76 // SetOwner() invokes this with an argument indicating success or failure. | 111 // SetOwner() invokes this with an argument indicating success or failure. |
77 typedef base::Callback<void(bool)> SetOwnerCallback; | 112 typedef base::Callback<void(bool)> SetOwnerCallback; |
78 | 113 |
79 explicit ConsumerManagementService(chromeos::CryptohomeClient* client); | 114 // |client| and |device_settings_service| should outlive this object. |
| 115 ConsumerManagementService( |
| 116 chromeos::CryptohomeClient* client, |
| 117 chromeos::DeviceSettingsService* device_settings_service); |
80 | 118 |
81 virtual ~ConsumerManagementService(); | 119 virtual ~ConsumerManagementService(); |
82 | 120 |
83 // Registers prefs. | 121 // Registers prefs. |
84 static void RegisterPrefs(PrefRegistrySimple* registry); | 122 static void RegisterPrefs(PrefRegistrySimple* registry); |
85 | 123 |
86 // Returns the enrollment state. | 124 void AddObserver(Observer* observer); |
87 ConsumerEnrollmentState GetEnrollmentState() const; | 125 void RemoveObserver(Observer* observer); |
88 | 126 |
89 // Sets the enrollment state. | 127 // Returns the status. |
90 void SetEnrollmentState(ConsumerEnrollmentState state); | 128 Status GetStatus() const; |
| 129 |
| 130 // Returns the string value of the status. |
| 131 std::string GetStatusString() const; |
| 132 |
| 133 // Returns the enrollment stage. |
| 134 EnrollmentStage GetEnrollmentStage() const; |
| 135 |
| 136 // Sets the enrollment stage. |
| 137 void SetEnrollmentStage(EnrollmentStage stage); |
91 | 138 |
92 // Returns the device owner stored in the boot lockbox via |callback|. | 139 // Returns the device owner stored in the boot lockbox via |callback|. |
93 void GetOwner(const GetOwnerCallback& callback); | 140 void GetOwner(const GetOwnerCallback& callback); |
94 | 141 |
95 // Stores the device owner user ID into the boot lockbox and signs it. | 142 // Stores the device owner user ID into the boot lockbox and signs it. |
96 // |callback| is invoked with an agument indicating success or failure. | 143 // |callback| is invoked with an agument indicating success or failure. |
97 void SetOwner(const std::string& user_id, const SetOwnerCallback& callback); | 144 void SetOwner(const std::string& user_id, const SetOwnerCallback& callback); |
98 | 145 |
| 146 // chromeos::DeviceSettingsService::Observer: |
| 147 virtual void OwnershipStatusChanged() OVERRIDE; |
| 148 virtual void DeviceSettingsUpdated() OVERRIDE; |
| 149 |
99 // content::NotificationObserver implmentation. | 150 // content::NotificationObserver implmentation. |
100 virtual void Observe(int type, | 151 virtual void Observe(int type, |
101 const content::NotificationSource& source, | 152 const content::NotificationSource& source, |
102 const content::NotificationDetails& details) OVERRIDE; | 153 const content::NotificationDetails& details) OVERRIDE; |
103 | 154 |
104 // OAuth2TokenService::Observer implementation. | 155 // OAuth2TokenService::Observer: |
105 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 156 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
106 | 157 |
107 // OAuth2TokenService::Consumer implementation. | 158 // OAuth2TokenService::Consumer: |
108 virtual void OnGetTokenSuccess( | 159 virtual void OnGetTokenSuccess( |
109 const OAuth2TokenService::Request* request, | 160 const OAuth2TokenService::Request* request, |
110 const std::string& access_token, | 161 const std::string& access_token, |
111 const base::Time& expiration_time) OVERRIDE; | 162 const base::Time& expiration_time) OVERRIDE; |
112 virtual void OnGetTokenFailure( | 163 virtual void OnGetTokenFailure( |
113 const OAuth2TokenService::Request* request, | 164 const OAuth2TokenService::Request* request, |
114 const GoogleServiceAuthError& error) OVERRIDE; | 165 const GoogleServiceAuthError& error) OVERRIDE; |
115 | 166 |
116 OAuth2TokenService::Request* GetTokenRequestForTesting() { | 167 OAuth2TokenService::Request* GetTokenRequestForTesting() { |
117 return token_request_.get(); | 168 return token_request_.get(); |
(...skipping 28 matching lines...) Expand all Loading... |
146 void OnOwnerRefreshTokenAvailable(); | 197 void OnOwnerRefreshTokenAvailable(); |
147 | 198 |
148 // Called when the owner's access token for device management is available. | 199 // Called when the owner's access token for device management is available. |
149 void OnOwnerAccessTokenAvailable(const std::string& access_token); | 200 void OnOwnerAccessTokenAvailable(const std::string& access_token); |
150 | 201 |
151 // Called upon the completion of the enrollment process. | 202 // Called upon the completion of the enrollment process. |
152 void OnEnrollmentCompleted(EnrollmentStatus status); | 203 void OnEnrollmentCompleted(EnrollmentStatus status); |
153 | 204 |
154 // Ends the enrollment process and shows a desktop notification if the | 205 // Ends the enrollment process and shows a desktop notification if the |
155 // current user is the owner. | 206 // current user is the owner. |
156 void EndEnrollment(ConsumerEnrollmentState state); | 207 void EndEnrollment(EnrollmentStage stage); |
157 | 208 |
158 // Shows a desktop notification and resets the enrollment state. | 209 // Shows a desktop notification and resets the enrollment stage. |
159 void ShowDesktopNotificationAndResetState(ConsumerEnrollmentState state, | 210 void ShowDesktopNotificationAndResetStage( |
160 Profile* profile); | 211 EnrollmentStage stage, |
| 212 Profile* profile); |
161 | 213 |
162 // Opens the settings page. | 214 // Opens the settings page. |
163 void OpenSettingsPage(Profile* profile) const; | 215 void OpenSettingsPage(Profile* profile) const; |
164 | 216 |
165 // Opens the enrollment confirmation dialog in the settings page. | 217 // Opens the enrollment confirmation dialog in the settings page. |
166 void TryEnrollmentAgain(Profile* profile) const; | 218 void TryEnrollmentAgain(Profile* profile) const; |
167 | 219 |
| 220 void NotifyStatusChanged(); |
| 221 |
168 chromeos::CryptohomeClient* client_; | 222 chromeos::CryptohomeClient* client_; |
| 223 chromeos::DeviceSettingsService* device_settings_service_; |
169 | 224 |
170 Profile* enrolling_profile_; | 225 Profile* enrolling_profile_; |
171 scoped_ptr<OAuth2TokenService::Request> token_request_; | 226 scoped_ptr<OAuth2TokenService::Request> token_request_; |
172 content::NotificationRegistrar registrar_; | 227 content::NotificationRegistrar registrar_; |
| 228 ObserverList<Observer, true> observers_; |
173 base::WeakPtrFactory<ConsumerManagementService> weak_ptr_factory_; | 229 base::WeakPtrFactory<ConsumerManagementService> weak_ptr_factory_; |
174 | 230 |
175 DISALLOW_COPY_AND_ASSIGN(ConsumerManagementService); | 231 DISALLOW_COPY_AND_ASSIGN(ConsumerManagementService); |
176 }; | 232 }; |
177 | 233 |
178 } // namespace policy | 234 } // namespace policy |
179 | 235 |
180 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ | 236 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ |
OLD | NEW |