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, and |ConsumerManagementStatus| in |
60 ENROLLMENT_OWNER_STORED, // The owner ID is stored in the boot lockbox. | 63 // chrome/browser/resources/options/chromeos/consumer_management_overlay.js |
61 ENROLLMENT_SUCCESS, // Success. The notification is not sent yet. | 64 enum Status { |
| 65 // The status is currently unavailable. |
| 66 STATUS_UNKNOWN = 0, |
62 | 67 |
63 // Error states. | 68 STATUS_ENROLLED, |
64 ENROLLMENT_CANCELED, // Canceled by the user. | 69 STATUS_ENROLLING, |
65 ENROLLMENT_BOOT_LOCKBOX_FAILED, // Failed to write to the boot lockbox. | 70 STATUS_UNENROLLED, |
66 ENROLLMENT_GET_TOKEN_FAILED, // Failed to get the access token. | 71 STATUS_UNENROLLING, |
67 ENROLLMENT_DM_SERVER_FAILED, // Failed to register the device. | |
68 | 72 |
69 ENROLLMENT_LAST, // This should always be the last one. | 73 // This should always be the last one. |
| 74 STATUS_LAST, |
| 75 }; |
| 76 |
| 77 // Indicating which stage the enrollment process is in. |
| 78 enum EnrollmentStage { |
| 79 // Not enrolled, or enrollment is completed. |
| 80 ENROLLMENT_STAGE_NONE = 0, |
| 81 // Enrollment is requested by the owner. |
| 82 ENROLLMENT_STAGE_REQUESTED, |
| 83 // The owner ID is stored in the boot lockbox. |
| 84 ENROLLMENT_STAGE_OWNER_STORED, |
| 85 // Success. The notification is not sent yet. |
| 86 ENROLLMENT_STAGE_SUCCESS, |
| 87 |
| 88 // Error stages. |
| 89 // Canceled by the user. |
| 90 ENROLLMENT_STAGE_CANCELED, |
| 91 // Failed to write to the boot lockbox. |
| 92 ENROLLMENT_STAGE_BOOT_LOCKBOX_FAILED, |
| 93 // Failed to get the access token. |
| 94 ENROLLMENT_STAGE_GET_TOKEN_FAILED, |
| 95 // Failed to register the device. |
| 96 ENROLLMENT_STAGE_DM_SERVER_FAILED, |
| 97 |
| 98 // This should always be the last one. |
| 99 ENROLLMENT_STAGE_LAST, |
| 100 }; |
| 101 |
| 102 class Observer { |
| 103 public: |
| 104 // Called when the status changes. |
| 105 virtual void OnConsumerManagementStatusChanged() = 0; |
70 }; | 106 }; |
71 | 107 |
72 // GetOwner() invokes this with an argument set to the owner user ID, | 108 // GetOwner() invokes this with an argument set to the owner user ID, |
73 // or an empty string on failure. | 109 // or an empty string on failure. |
74 typedef base::Callback<void(const std::string&)> GetOwnerCallback; | 110 typedef base::Callback<void(const std::string&)> GetOwnerCallback; |
75 | 111 |
76 // SetOwner() invokes this with an argument indicating success or failure. | 112 // SetOwner() invokes this with an argument indicating success or failure. |
77 typedef base::Callback<void(bool)> SetOwnerCallback; | 113 typedef base::Callback<void(bool)> SetOwnerCallback; |
78 | 114 |
79 explicit ConsumerManagementService(chromeos::CryptohomeClient* client); | 115 // |client| and |device_settings_service| should outlive this object. |
| 116 ConsumerManagementService( |
| 117 chromeos::CryptohomeClient* client, |
| 118 chromeos::DeviceSettingsService* device_settings_service); |
80 | 119 |
81 virtual ~ConsumerManagementService(); | 120 virtual ~ConsumerManagementService(); |
82 | 121 |
83 // Registers prefs. | 122 // Registers prefs. |
84 static void RegisterPrefs(PrefRegistrySimple* registry); | 123 static void RegisterPrefs(PrefRegistrySimple* registry); |
85 | 124 |
86 // Returns the enrollment state. | 125 void AddObserver(Observer* observer); |
87 ConsumerEnrollmentState GetEnrollmentState() const; | 126 void RemoveObserver(Observer* observer); |
88 | 127 |
89 // Sets the enrollment state. | 128 // Returns the status. |
90 void SetEnrollmentState(ConsumerEnrollmentState state); | 129 Status GetStatus() const; |
| 130 |
| 131 // Returns the string value of the status. |
| 132 std::string GetStatusString() const; |
| 133 |
| 134 // Returns the enrollment stage. |
| 135 EnrollmentStage GetEnrollmentStage() const; |
| 136 |
| 137 // Sets the enrollment stage. |
| 138 void SetEnrollmentStage(EnrollmentStage stage); |
91 | 139 |
92 // Returns the device owner stored in the boot lockbox via |callback|. | 140 // Returns the device owner stored in the boot lockbox via |callback|. |
93 void GetOwner(const GetOwnerCallback& callback); | 141 void GetOwner(const GetOwnerCallback& callback); |
94 | 142 |
95 // Stores the device owner user ID into the boot lockbox and signs it. | 143 // Stores the device owner user ID into the boot lockbox and signs it. |
96 // |callback| is invoked with an agument indicating success or failure. | 144 // |callback| is invoked with an agument indicating success or failure. |
97 void SetOwner(const std::string& user_id, const SetOwnerCallback& callback); | 145 void SetOwner(const std::string& user_id, const SetOwnerCallback& callback); |
98 | 146 |
| 147 // chromeos::DeviceSettingsService::Observer: |
| 148 virtual void OwnershipStatusChanged() OVERRIDE; |
| 149 virtual void DeviceSettingsUpdated() OVERRIDE; |
| 150 |
99 // content::NotificationObserver implmentation. | 151 // content::NotificationObserver implmentation. |
100 virtual void Observe(int type, | 152 virtual void Observe(int type, |
101 const content::NotificationSource& source, | 153 const content::NotificationSource& source, |
102 const content::NotificationDetails& details) OVERRIDE; | 154 const content::NotificationDetails& details) OVERRIDE; |
103 | 155 |
104 // OAuth2TokenService::Observer implementation. | 156 // OAuth2TokenService::Observer: |
105 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 157 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
106 | 158 |
107 // OAuth2TokenService::Consumer implementation. | 159 // OAuth2TokenService::Consumer: |
108 virtual void OnGetTokenSuccess( | 160 virtual void OnGetTokenSuccess( |
109 const OAuth2TokenService::Request* request, | 161 const OAuth2TokenService::Request* request, |
110 const std::string& access_token, | 162 const std::string& access_token, |
111 const base::Time& expiration_time) OVERRIDE; | 163 const base::Time& expiration_time) OVERRIDE; |
112 virtual void OnGetTokenFailure( | 164 virtual void OnGetTokenFailure( |
113 const OAuth2TokenService::Request* request, | 165 const OAuth2TokenService::Request* request, |
114 const GoogleServiceAuthError& error) OVERRIDE; | 166 const GoogleServiceAuthError& error) OVERRIDE; |
115 | 167 |
116 OAuth2TokenService::Request* GetTokenRequestForTesting() { | 168 OAuth2TokenService::Request* GetTokenRequestForTesting() { |
117 return token_request_.get(); | 169 return token_request_.get(); |
(...skipping 28 matching lines...) Expand all Loading... |
146 void OnOwnerRefreshTokenAvailable(); | 198 void OnOwnerRefreshTokenAvailable(); |
147 | 199 |
148 // Called when the owner's access token for device management is available. | 200 // Called when the owner's access token for device management is available. |
149 void OnOwnerAccessTokenAvailable(const std::string& access_token); | 201 void OnOwnerAccessTokenAvailable(const std::string& access_token); |
150 | 202 |
151 // Called upon the completion of the enrollment process. | 203 // Called upon the completion of the enrollment process. |
152 void OnEnrollmentCompleted(EnrollmentStatus status); | 204 void OnEnrollmentCompleted(EnrollmentStatus status); |
153 | 205 |
154 // Ends the enrollment process and shows a desktop notification if the | 206 // Ends the enrollment process and shows a desktop notification if the |
155 // current user is the owner. | 207 // current user is the owner. |
156 void EndEnrollment(ConsumerEnrollmentState state); | 208 void EndEnrollment(EnrollmentStage stage); |
157 | 209 |
158 // Shows a desktop notification and resets the enrollment state. | 210 // Shows a desktop notification and resets the enrollment stage. |
159 void ShowDesktopNotificationAndResetState(ConsumerEnrollmentState state, | 211 void ShowDesktopNotificationAndResetStage( |
160 Profile* profile); | 212 EnrollmentStage stage, |
| 213 Profile* profile); |
161 | 214 |
162 // Opens the settings page. | 215 // Opens the settings page. |
163 void OpenSettingsPage(Profile* profile) const; | 216 void OpenSettingsPage(Profile* profile) const; |
164 | 217 |
165 // Opens the enrollment confirmation dialog in the settings page. | 218 // Opens the enrollment confirmation dialog in the settings page. |
166 void TryEnrollmentAgain(Profile* profile) const; | 219 void TryEnrollmentAgain(Profile* profile) const; |
167 | 220 |
| 221 void NotifyStatusChanged(); |
| 222 |
168 chromeos::CryptohomeClient* client_; | 223 chromeos::CryptohomeClient* client_; |
| 224 chromeos::DeviceSettingsService* device_settings_service_; |
169 | 225 |
170 Profile* enrolling_profile_; | 226 Profile* enrolling_profile_; |
171 scoped_ptr<OAuth2TokenService::Request> token_request_; | 227 scoped_ptr<OAuth2TokenService::Request> token_request_; |
172 content::NotificationRegistrar registrar_; | 228 content::NotificationRegistrar registrar_; |
| 229 ObserverList<Observer, true> observers_; |
173 base::WeakPtrFactory<ConsumerManagementService> weak_ptr_factory_; | 230 base::WeakPtrFactory<ConsumerManagementService> weak_ptr_factory_; |
174 | 231 |
175 DISALLOW_COPY_AND_ASSIGN(ConsumerManagementService); | 232 DISALLOW_COPY_AND_ASSIGN(ConsumerManagementService); |
176 }; | 233 }; |
177 | 234 |
178 } // namespace policy | 235 } // namespace policy |
179 | 236 |
180 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ | 237 #endif // CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_ |
OLD | NEW |