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 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" | 5 #include "chrome/browser/chromeos/policy/consumer_enrollment_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | |
9 #include "base/location.h" | 8 #include "base/location.h" |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "base/time/time.h" | 11 #include "base/time/time.h" |
bartfab (slow)
2014/11/27 14:09:09
Nit: No longer used.
davidyu
2014/11/28 02:45:25
This is used by the parameter of OnGetTokenSuccess
bartfab (slow)
2014/11/28 14:52:14
You never dereference that type though. But sure,
davidyu
2014/11/30 04:16:04
Ok. I'll fix it in another CL.
| |
14 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/browser_process_platform_part.h" | 13 #include "chrome/browser/browser_process_platform_part.h" |
16 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 14 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
17 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" | 15 #include "chrome/browser/chromeos/policy/device_cloud_policy_initializer.h" |
18 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" | 16 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" |
19 #include "chrome/browser/notifications/notification.h" | |
20 #include "chrome/browser/notifications/notification_delegate.h" | |
21 #include "chrome/browser/notifications/notification_ui_manager.h" | |
22 #include "chrome/browser/profiles/profile.h" | |
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
24 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
25 #include "chrome/browser/ui/browser_navigator.h" | |
26 #include "chrome/common/url_constants.h" | |
27 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 19 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
28 #include "components/signin/core/browser/signin_manager_base.h" | 20 #include "components/signin/core/browser/signin_manager_base.h" |
29 #include "google_apis/gaia/gaia_constants.h" | 21 #include "google_apis/gaia/gaia_constants.h" |
30 #include "google_apis/gaia/google_service_auth_error.h" | 22 #include "google_apis/gaia/google_service_auth_error.h" |
31 #include "grit/generated_resources.h" | |
32 #include "grit/theme_resources.h" | |
33 #include "policy/proto/device_management_backend.pb.h" | 23 #include "policy/proto/device_management_backend.pb.h" |
34 #include "third_party/WebKit/public/web/WebTextDirection.h" | |
35 #include "ui/base/l10n/l10n_util.h" | |
36 #include "ui/base/page_transition_types.h" | |
37 #include "ui/base/resource/resource_bundle.h" | |
38 #include "ui/base/window_open_disposition.h" | |
39 #include "ui/message_center/notification.h" | |
40 #include "ui/message_center/notification_types.h" | |
41 #include "ui/message_center/notifier_settings.h" | |
42 #include "url/gurl.h" | |
43 | 24 |
44 namespace em = enterprise_management; | 25 namespace em = enterprise_management; |
45 | 26 |
46 namespace { | |
47 | |
48 // Desktop notification constants. | |
49 const char kEnrollmentNotificationId[] = "consumer_management.enroll"; | |
50 const char kEnrollmentNotificationUrl[] = "chrome://consumer-management/enroll"; | |
51 | |
52 // The path to the consumer management enrollment/unenrollment confirmation | |
53 // overlay, relative to the settings page URL. | |
54 const char kConsumerManagementOverlay[] = "consumer-management-overlay"; | |
55 | |
56 class DesktopNotificationDelegate : public NotificationDelegate { | |
57 public: | |
58 // |button_click_callback| is called when the button in the notification is | |
59 // clicked. | |
60 DesktopNotificationDelegate(const std::string& id, | |
61 const base::Closure& button_click_callback); | |
62 | |
63 // NotificationDelegate: | |
64 virtual std::string id() const override; | |
65 virtual void ButtonClick(int button_index) override; | |
66 | |
67 private: | |
68 virtual ~DesktopNotificationDelegate(); | |
69 | |
70 std::string id_; | |
71 base::Closure button_click_callback_; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(DesktopNotificationDelegate); | |
74 }; | |
75 | |
76 DesktopNotificationDelegate::DesktopNotificationDelegate( | |
77 const std::string& id, | |
78 const base::Closure& button_click_callback) | |
79 : id_(id), button_click_callback_(button_click_callback) { | |
80 } | |
81 | |
82 DesktopNotificationDelegate::~DesktopNotificationDelegate() { | |
83 } | |
84 | |
85 std::string DesktopNotificationDelegate::id() const { | |
86 return id_; | |
87 } | |
88 | |
89 void DesktopNotificationDelegate::ButtonClick(int button_index) { | |
90 button_click_callback_.Run(); | |
91 } | |
92 | |
93 } // namespace | |
94 | |
95 namespace policy { | 27 namespace policy { |
96 | 28 |
97 ConsumerEnrollmentHandler::ConsumerEnrollmentHandler( | 29 ConsumerEnrollmentHandler::ConsumerEnrollmentHandler( |
98 Profile* profile, | 30 Profile* profile, |
99 ConsumerManagementService* consumer_management_service, | 31 ConsumerManagementService* consumer_management_service, |
100 DeviceManagementService* device_management_service) | 32 DeviceManagementService* device_management_service) |
101 : Consumer("consumer_enrollment_handler"), | 33 : Consumer("consumer_enrollment_handler"), |
102 profile_(profile), | 34 profile_(profile), |
103 consumer_management_service_(consumer_management_service), | 35 consumer_management_service_(consumer_management_service), |
104 device_management_service_(device_management_service), | 36 device_management_service_(device_management_service), |
105 weak_ptr_factory_(this) { | 37 weak_ptr_factory_(this) { |
106 gaia_account_id_ = SigninManagerFactory::GetForProfile(profile)-> | 38 gaia_account_id_ = SigninManagerFactory::GetForProfile(profile)-> |
107 GetAuthenticatedAccountId(); | 39 GetAuthenticatedAccountId(); |
108 Start(); | 40 ContinueEnrollmentProcess(); |
109 } | 41 } |
110 | 42 |
111 ConsumerEnrollmentHandler::~ConsumerEnrollmentHandler() { | 43 ConsumerEnrollmentHandler::~ConsumerEnrollmentHandler() { |
112 } | 44 } |
113 | 45 |
114 void ConsumerEnrollmentHandler::Shutdown() { | 46 void ConsumerEnrollmentHandler::Shutdown() { |
115 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> | 47 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)-> |
116 RemoveObserver(this); | 48 RemoveObserver(this); |
117 } | 49 } |
118 | 50 |
(...skipping 21 matching lines...) Expand all Loading... | |
140 const OAuth2TokenService::Request* request, | 72 const OAuth2TokenService::Request* request, |
141 const GoogleServiceAuthError& error) { | 73 const GoogleServiceAuthError& error) { |
142 DCHECK_EQ(token_request_, request); | 74 DCHECK_EQ(token_request_, request); |
143 base::MessageLoopProxy::current()->DeleteSoon( | 75 base::MessageLoopProxy::current()->DeleteSoon( |
144 FROM_HERE, token_request_.release()); | 76 FROM_HERE, token_request_.release()); |
145 | 77 |
146 LOG(ERROR) << "Failed to get the access token: " << error.ToString(); | 78 LOG(ERROR) << "Failed to get the access token: " << error.ToString(); |
147 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED); | 79 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED); |
148 } | 80 } |
149 | 81 |
150 void ConsumerEnrollmentHandler::Start() { | |
151 const ConsumerManagementService::EnrollmentStage stage = | |
152 consumer_management_service_->GetEnrollmentStage(); | |
153 switch (stage) { | |
154 case ConsumerManagementService::ENROLLMENT_STAGE_NONE: | |
155 // Do nothing. | |
156 return; | |
157 | |
158 case ConsumerManagementService::ENROLLMENT_STAGE_OWNER_STORED: | |
159 ContinueEnrollmentProcess(); | |
160 return; | |
161 | |
162 case ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS: | |
163 case ConsumerManagementService::ENROLLMENT_STAGE_CANCELED: | |
164 case ConsumerManagementService::ENROLLMENT_STAGE_BOOT_LOCKBOX_FAILED: | |
165 case ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED: | |
166 case ConsumerManagementService::ENROLLMENT_STAGE_GET_TOKEN_FAILED: | |
167 ShowDesktopNotificationAndResetStage(stage); | |
168 return; | |
169 | |
170 case ConsumerManagementService::ENROLLMENT_STAGE_REQUESTED: | |
171 case ConsumerManagementService::ENROLLMENT_STAGE_LAST: | |
172 NOTREACHED() << "Unexpected enrollment stage " << stage; | |
173 return; | |
174 } | |
175 } | |
176 | |
177 void ConsumerEnrollmentHandler::ContinueEnrollmentProcess() { | 82 void ConsumerEnrollmentHandler::ContinueEnrollmentProcess() { |
178 // First, we need to ensure that the refresh token is available. | 83 // First, we need to ensure that the refresh token is available. |
179 ProfileOAuth2TokenService* token_service = | 84 ProfileOAuth2TokenService* token_service = |
180 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | 85 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); |
181 if (token_service->RefreshTokenIsAvailable(gaia_account_id_)) { | 86 if (token_service->RefreshTokenIsAvailable(gaia_account_id_)) { |
182 OnOwnerRefreshTokenAvailable(); | 87 OnOwnerRefreshTokenAvailable(); |
183 } else { | 88 } else { |
184 token_service->AddObserver(this); | 89 token_service->AddObserver(this); |
185 } | 90 } |
186 } | 91 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED); | 133 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_DM_SERVER_FAILED); |
229 return; | 134 return; |
230 } | 135 } |
231 | 136 |
232 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS); | 137 EndEnrollment(ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS); |
233 } | 138 } |
234 | 139 |
235 void ConsumerEnrollmentHandler::EndEnrollment( | 140 void ConsumerEnrollmentHandler::EndEnrollment( |
236 ConsumerManagementService::EnrollmentStage stage) { | 141 ConsumerManagementService::EnrollmentStage stage) { |
237 consumer_management_service_->SetEnrollmentStage(stage); | 142 consumer_management_service_->SetEnrollmentStage(stage); |
238 ShowDesktopNotificationAndResetStage(stage); | |
239 } | |
240 | |
241 void ConsumerEnrollmentHandler::ShowDesktopNotificationAndResetStage( | |
242 ConsumerManagementService::EnrollmentStage stage) { | |
243 base::string16 title; | |
244 base::string16 body; | |
245 base::string16 button_label; | |
246 base::Closure button_click_callback; | |
247 | |
248 if (stage == ConsumerManagementService::ENROLLMENT_STAGE_SUCCESS) { | |
249 title = l10n_util::GetStringUTF16( | |
250 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_TITLE); | |
251 body = l10n_util::GetStringUTF16( | |
252 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_NOTIFICATION_BODY); | |
253 button_label = l10n_util::GetStringUTF16( | |
254 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_MODIFY_SETTINGS_BUTTON); | |
255 button_click_callback = base::Bind( | |
256 &ConsumerEnrollmentHandler::OpenSettingsPage, | |
257 weak_ptr_factory_.GetWeakPtr()); | |
258 } else { | |
259 title = l10n_util::GetStringUTF16( | |
260 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_TITLE); | |
261 body = l10n_util::GetStringUTF16( | |
262 IDS_CONSUMER_MANAGEMENT_ENROLLMENT_FAILURE_NOTIFICATION_BODY); | |
263 button_label = l10n_util::GetStringUTF16( | |
264 IDS_CONSUMER_MANAGEMENT_NOTIFICATION_TRY_AGAIN_BUTTON); | |
265 button_click_callback = base::Bind( | |
266 &ConsumerEnrollmentHandler::TryEnrollmentAgain, | |
267 weak_ptr_factory_.GetWeakPtr()); | |
268 } | |
269 | |
270 message_center::RichNotificationData optional_field; | |
271 optional_field.buttons.push_back(message_center::ButtonInfo(button_label)); | |
272 Notification notification( | |
273 message_center::NOTIFICATION_TYPE_SIMPLE, | |
274 GURL(kEnrollmentNotificationUrl), | |
275 title, | |
276 body, | |
277 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
278 IDR_CONSUMER_MANAGEMENT_NOTIFICATION_ICON), | |
279 blink::WebTextDirectionDefault, | |
280 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
281 kEnrollmentNotificationId), | |
282 base::string16(), // display_source | |
283 base::UTF8ToUTF16(kEnrollmentNotificationId), | |
284 optional_field, | |
285 new DesktopNotificationDelegate(kEnrollmentNotificationId, | |
286 button_click_callback)); | |
287 notification.SetSystemPriority(); | |
288 g_browser_process->notification_ui_manager()->Add(notification, profile_); | |
289 | |
290 consumer_management_service_->SetEnrollmentStage( | |
291 ConsumerManagementService::ENROLLMENT_STAGE_NONE); | |
292 } | |
293 | |
294 void ConsumerEnrollmentHandler::OpenSettingsPage() const { | |
295 const GURL url(chrome::kChromeUISettingsURL); | |
296 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
297 params.disposition = NEW_FOREGROUND_TAB; | |
298 chrome::Navigate(¶ms); | |
299 } | |
300 | |
301 void ConsumerEnrollmentHandler::TryEnrollmentAgain() const { | |
302 const GURL base_url(chrome::kChromeUISettingsURL); | |
303 const GURL url = base_url.Resolve(kConsumerManagementOverlay); | |
304 | |
305 chrome::NavigateParams params(profile_, url, ui::PAGE_TRANSITION_LINK); | |
306 params.disposition = NEW_FOREGROUND_TAB; | |
307 chrome::Navigate(¶ms); | |
308 } | 143 } |
309 | 144 |
310 } // namespace policy | 145 } // namespace policy |
OLD | NEW |