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

Side by Side Diff: chrome/browser/chromeos/arc/arc_auth_service.cc

Issue 2474663003: arc: Shuffle ArcAuthService's interface (Closed)
Patch Set: Final nits Created 4 years, 1 month 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
« no previous file with comments | « chrome/browser/chromeos/arc/arc_auth_service.h ('k') | components/arc/common/auth.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/arc/arc_auth_service.h" 5 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "ash/common/shelf/shelf_delegate.h" 9 #include "ash/common/shelf/shelf_delegate.h"
10 #include "ash/common/wm_shell.h" 10 #include "ash/common/wm_shell.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 #include "content/public/browser/browser_thread.h" 45 #include "content/public/browser/browser_thread.h"
46 #include "extensions/browser/app_window/app_window_registry.h" 46 #include "extensions/browser/app_window/app_window_registry.h"
47 #include "extensions/browser/extension_prefs.h" 47 #include "extensions/browser/extension_prefs.h"
48 #include "extensions/browser/extension_registry.h" 48 #include "extensions/browser/extension_registry.h"
49 #include "ui/base/l10n/l10n_util.h" 49 #include "ui/base/l10n/l10n_util.h"
50 50
51 namespace arc { 51 namespace arc {
52 52
53 namespace { 53 namespace {
54 54
55 constexpr size_t kMinVersionForOnAccountInfoReady = 5;
56
55 // Weak pointer. This class is owned by ArcServiceManager. 57 // Weak pointer. This class is owned by ArcServiceManager.
56 ArcAuthService* g_arc_auth_service = nullptr; 58 ArcAuthService* g_arc_auth_service = nullptr;
57 59
58 // Skip creating UI in unit tests 60 // Skip creating UI in unit tests
59 bool g_disable_ui_for_testing = false; 61 bool g_disable_ui_for_testing = false;
60 62
61 // Use specified ash::ShelfDelegate for unit tests. 63 // Use specified ash::ShelfDelegate for unit tests.
62 ash::ShelfDelegate* g_shelf_delegate_for_testing = nullptr; 64 ash::ShelfDelegate* g_shelf_delegate_for_testing = nullptr;
63 65
64 // The Android management check is disabled by default, it's used only for 66 // The Android management check is disabled by default, it's used only for
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 MAP_PROVISIONING_RESULT(CLOUD_PROVISION_FLOW_INTERNAL_ERROR); 108 MAP_PROVISIONING_RESULT(CLOUD_PROVISION_FLOW_INTERNAL_ERROR);
107 } 109 }
108 #undef MAP_PROVISIONING_RESULT 110 #undef MAP_PROVISIONING_RESULT
109 111
110 NOTREACHED() << "unknown reason: " << static_cast<int>(reason); 112 NOTREACHED() << "unknown reason: " << static_cast<int>(reason);
111 return ProvisioningResult::UNKNOWN_ERROR; 113 return ProvisioningResult::UNKNOWN_ERROR;
112 } 114 }
113 115
114 } // namespace 116 } // namespace
115 117
118 // TODO(lhchavez): Get rid of this class once we can safely remove all the
119 // deprecated interfaces and only need to care about one type of callback.
120 class ArcAuthService::AccountInfoNotifier {
121 public:
122 explicit AccountInfoNotifier(
123 const GetAuthCodeDeprecatedCallback& auth_callback)
124 : callback_type_(CallbackType::AUTH_CODE),
125 auth_callback_(auth_callback) {}
126
127 explicit AccountInfoNotifier(
128 const GetAuthCodeAndAccountTypeDeprecatedCallback& auth_account_callback)
129 : callback_type_(CallbackType::AUTH_CODE_AND_ACCOUNT),
130 auth_account_callback_(auth_account_callback) {}
131
132 explicit AccountInfoNotifier(const AccountInfoCallback& account_info_callback)
133 : callback_type_(CallbackType::ACCOUNT_INFO),
134 account_info_callback_(account_info_callback) {}
135
136 void Notify(bool is_enforced,
137 const std::string& auth_code,
138 mojom::ChromeAccountType account_type,
139 bool is_managed) {
140 switch (callback_type_) {
141 case CallbackType::AUTH_CODE:
142 DCHECK(!auth_callback_.is_null());
143 auth_callback_.Run(auth_code, is_enforced);
144 break;
145 case CallbackType::AUTH_CODE_AND_ACCOUNT:
146 DCHECK(!auth_account_callback_.is_null());
147 auth_account_callback_.Run(auth_code, is_enforced, account_type);
148 break;
149 case CallbackType::ACCOUNT_INFO:
150 DCHECK(!account_info_callback_.is_null());
151 mojom::AccountInfoPtr account_info = mojom::AccountInfo::New();
152 if (!is_enforced) {
153 account_info->auth_code = nullptr;
154 } else {
155 account_info->auth_code = auth_code;
156 }
157 account_info->account_type = account_type;
158 account_info->is_managed = is_managed;
159 account_info_callback_.Run(std::move(account_info));
160 break;
161 }
162 }
163
164 private:
165 enum class CallbackType { AUTH_CODE, AUTH_CODE_AND_ACCOUNT, ACCOUNT_INFO };
166
167 const CallbackType callback_type_;
168 const GetAuthCodeDeprecatedCallback auth_callback_;
169 const GetAuthCodeAndAccountTypeDeprecatedCallback auth_account_callback_;
170 const AccountInfoCallback account_info_callback_;
171 };
172
116 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) 173 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service)
117 : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) { 174 : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) {
118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 175 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
119 DCHECK(!g_arc_auth_service); 176 DCHECK(!g_arc_auth_service);
120 177
121 g_arc_auth_service = this; 178 g_arc_auth_service = this;
122 179
123 arc_bridge_service()->AddObserver(this); 180 arc_bridge_service()->AddObserver(this);
124 arc_bridge_service()->auth()->AddObserver(this); 181 arc_bridge_service()->auth()->AddObserver(this);
125 } 182 }
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 EnableArc(); 328 EnableArc();
272 } 329 }
273 330
274 std::string ArcAuthService::GetAndResetAuthCode() { 331 std::string ArcAuthService::GetAndResetAuthCode() {
275 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 332 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
276 std::string auth_code; 333 std::string auth_code;
277 auth_code_.swap(auth_code); 334 auth_code_.swap(auth_code);
278 return auth_code; 335 return auth_code;
279 } 336 }
280 337
281 void ArcAuthService::GetAuthCodeDeprecated( 338 void ArcAuthService::GetAuthCodeDeprecated0(
282 const GetAuthCodeDeprecatedCallback& callback) { 339 const GetAuthCodeDeprecated0Callback& callback) {
283 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 340 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
284 DCHECK(!IsOptInVerificationDisabled()); 341 DCHECK(!IsOptInVerificationDisabled());
285 callback.Run(GetAndResetAuthCode()); 342 callback.Run(GetAndResetAuthCode());
286 } 343 }
287 344
288 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { 345 void ArcAuthService::GetAuthCodeDeprecated(
346 const GetAuthCodeDeprecatedCallback& callback) {
347 RequestAccountInfoInternal(
348 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(callback));
349 }
350
351 void ArcAuthService::GetAuthCodeAndAccountTypeDeprecated(
352 const GetAuthCodeAndAccountTypeDeprecatedCallback& callback) {
353 RequestAccountInfoInternal(
354 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(callback));
355 }
356
357 void ArcAuthService::RequestAccountInfo() {
358 RequestAccountInfoInternal(
359 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(
360 base::Bind(&ArcAuthService::OnAccountInfoReady,
361 weak_ptr_factory_.GetWeakPtr())));
362 }
363
364 void ArcAuthService::OnAccountInfoReady(mojom::AccountInfoPtr account_info) {
289 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 365 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
290 // GetAuthCodeAndAccountType operation must not be in progress. 366 auto* instance = arc_bridge_service()->auth()->GetInstanceForMethod(
291 DCHECK(auth_account_callback_.is_null()); 367 "OnAccountInfoReady", kMinVersionForOnAccountInfoReady);
368 DCHECK(instance);
369 instance->OnAccountInfoReady(std::move(account_info));
370 }
371
372 void ArcAuthService::RequestAccountInfoInternal(
373 std::unique_ptr<ArcAuthService::AccountInfoNotifier>
374 account_info_notifier) {
375 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
376 // No other auth code-related operation may be in progress.
377 DCHECK(!account_info_notifier_);
292 378
293 const std::string auth_code = GetAndResetAuthCode(); 379 const std::string auth_code = GetAndResetAuthCode();
294 const bool verification_disabled = IsOptInVerificationDisabled(); 380 const bool is_enforced = !IsOptInVerificationDisabled();
295 if (!auth_code.empty() || verification_disabled) { 381 if (!auth_code.empty() || !is_enforced) {
296 callback.Run(auth_code, !verification_disabled); 382 account_info_notifier->Notify(is_enforced, auth_code,
383 mojom::ChromeAccountType::USER_ACCOUNT,
384 policy_util::IsAccountManaged(profile_));
297 return; 385 return;
298 } 386 }
299 387
300 auth_callback_ = callback; 388 account_info_notifier_ = std::move(account_info_notifier);
301 PrepareContextForAuthCodeRequest();
302 }
303
304 void ArcAuthService::GetAuthCodeAndAccountType(
305 const GetAuthCodeAndAccountTypeCallback& callback) {
306 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
307 // GetAuthCode operation must not be in progress.
308 DCHECK(auth_callback_.is_null());
309
310 const std::string auth_code = GetAndResetAuthCode();
311 const bool verification_disabled = IsOptInVerificationDisabled();
312 if (!auth_code.empty() || verification_disabled) {
313 callback.Run(auth_code, !verification_disabled,
314 mojom::ChromeAccountType::USER_ACCOUNT);
315 return;
316 }
317
318 auth_account_callback_ = callback;
319 PrepareContextForAuthCodeRequest(); 389 PrepareContextForAuthCodeRequest();
320 } 390 }
321 391
322 bool ArcAuthService::IsAuthCodeRequest() const { 392 bool ArcAuthService::IsAuthCodeRequest() const {
323 return !auth_callback_.is_null() || !auth_account_callback_.is_null(); 393 return account_info_notifier_ != nullptr;
324 } 394 }
325 395
326 void ArcAuthService::PrepareContextForAuthCodeRequest() { 396 void ArcAuthService::PrepareContextForAuthCodeRequest() {
327 // Requesting auth code on demand happens in following cases: 397 // Requesting auth code on demand happens in following cases:
328 // 1. To handle account password revoke. 398 // 1. To handle account password revoke.
329 // 2. In case Arc is activated in OOBE flow. 399 // 2. In case Arc is activated in OOBE flow.
330 // 3. For any other state on Android side that leads device appears in 400 // 3. For any other state on Android side that leads device appears in
331 // non-signed state. 401 // non-signed state.
332 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 402 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
333 DCHECK(IsAuthCodeRequest()); 403 DCHECK(IsAuthCodeRequest());
334 DCHECK_EQ(state_, State::ACTIVE); 404 DCHECK_EQ(state_, State::ACTIVE);
335 initial_opt_in_ = false; 405 initial_opt_in_ = false;
336 context_->PrepareContext(); 406 context_->PrepareContext();
337 } 407 }
338 408
339 void ArcAuthService::OnSignInComplete() { 409 void ArcAuthService::OnSignInComplete() {
340 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 410 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
341 DCHECK_EQ(state_, State::ACTIVE); 411 DCHECK_EQ(state_, State::ACTIVE);
342 DCHECK(!sign_in_time_.is_null());
343 412
344 arc_sign_in_timer_.Stop(); 413 if (!sign_in_time_.is_null()) {
414 arc_sign_in_timer_.Stop();
415 UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, true,
416 policy_util::IsAccountManaged(profile_));
417 UpdateProvisioningResultUMA(ProvisioningResult::SUCCESS,
418 policy_util::IsAccountManaged(profile_));
419 }
345 420
346 if (!IsOptInVerificationDisabled() && 421 CloseUI();
347 !profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn)) { 422
423 if (profile_->GetPrefs()->GetBoolean(prefs::kArcSignedIn))
424 return;
425
426 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true);
427 if (!IsOptInVerificationDisabled()) {
348 playstore_launcher_.reset( 428 playstore_launcher_.reset(
349 new ArcAppLauncher(profile_, kPlayStoreAppId, true)); 429 new ArcAppLauncher(profile_, kPlayStoreAppId, true));
350 } 430 }
351 431
352 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true);
353 CloseUI();
354 UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, true,
355 policy_util::IsAccountManaged(profile_));
356 UpdateProvisioningResultUMA(ProvisioningResult::SUCCESS,
357 policy_util::IsAccountManaged(profile_));
358
359 for (auto& observer : observer_list_) 432 for (auto& observer : observer_list_)
360 observer.OnInitialStart(); 433 observer.OnInitialStart();
361 } 434 }
362 435
363 void ArcAuthService::OnSignInFailed(mojom::ArcSignInFailureReason reason) { 436 void ArcAuthService::OnSignInFailed(mojom::ArcSignInFailureReason reason) {
364 OnSignInFailedInternal( 437 OnSignInFailedInternal(
365 ConvertArcSignInFailureReasonToProvisioningResult(reason)); 438 ConvertArcSignInFailureReasonToProvisioningResult(reason));
366 } 439 }
367 440
368 void ArcAuthService::OnSignInFailedInternal(ProvisioningResult result) { 441 void ArcAuthService::OnSignInFailedInternal(ProvisioningResult result) {
369 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 442 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
370 DCHECK_EQ(state_, State::ACTIVE); 443 DCHECK_EQ(state_, State::ACTIVE);
371 DCHECK(!sign_in_time_.is_null());
372 444
373 arc_sign_in_timer_.Stop(); 445 if (!sign_in_time_.is_null()) {
446 arc_sign_in_timer_.Stop();
374 447
375 UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, false, 448 UpdateProvisioningTiming(base::Time::Now() - sign_in_time_, false,
376 policy_util::IsAccountManaged(profile_)); 449 policy_util::IsAccountManaged(profile_));
377 UpdateOptInCancelUMA(OptInCancelReason::CLOUD_PROVISION_FLOW_FAIL); 450 UpdateOptInCancelUMA(OptInCancelReason::CLOUD_PROVISION_FLOW_FAIL);
378 UpdateProvisioningResultUMA(result, policy_util::IsAccountManaged(profile_)); 451 UpdateProvisioningResultUMA(result,
452 policy_util::IsAccountManaged(profile_));
453 }
379 454
380 int error_message_id; 455 int error_message_id;
381 switch (result) { 456 switch (result) {
382 case ProvisioningResult::GMS_NETWORK_ERROR: 457 case ProvisioningResult::GMS_NETWORK_ERROR:
383 error_message_id = IDS_ARC_SIGN_IN_NETWORK_ERROR; 458 error_message_id = IDS_ARC_SIGN_IN_NETWORK_ERROR;
384 break; 459 break;
385 case ProvisioningResult::GMS_SERVICE_UNAVAILABLE: 460 case ProvisioningResult::GMS_SERVICE_UNAVAILABLE:
386 case ProvisioningResult::GMS_SIGN_IN_FAILED: 461 case ProvisioningResult::GMS_SIGN_IN_FAILED:
387 case ProvisioningResult::GMS_SIGN_IN_TIMEOUT: 462 case ProvisioningResult::GMS_SIGN_IN_TIMEOUT:
388 case ProvisioningResult::GMS_SIGN_IN_INTERNAL_ERROR: 463 case ProvisioningResult::GMS_SIGN_IN_INTERNAL_ERROR:
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 result == ProvisioningResult::UNKNOWN_ERROR) { 499 result == ProvisioningResult::UNKNOWN_ERROR) {
425 RemoveArcData(); 500 RemoveArcData();
426 } 501 }
427 502
428 // We'll delay shutting down the bridge in this case to allow people to send 503 // We'll delay shutting down the bridge in this case to allow people to send
429 // feedback. 504 // feedback.
430 ShowUI(UIPage::ERROR_WITH_FEEDBACK, 505 ShowUI(UIPage::ERROR_WITH_FEEDBACK,
431 l10n_util::GetStringUTF16(error_message_id)); 506 l10n_util::GetStringUTF16(error_message_id));
432 } 507 }
433 508
434 void ArcAuthService::GetIsAccountManaged( 509 void ArcAuthService::GetIsAccountManagedDeprecated(
435 const GetIsAccountManagedCallback& callback) { 510 const GetIsAccountManagedDeprecatedCallback& callback) {
436 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 511 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
437 512
438 callback.Run(policy_util::IsAccountManaged(profile_)); 513 callback.Run(policy_util::IsAccountManaged(profile_));
439 } 514 }
440 515
441 void ArcAuthService::SetState(State state) { 516 void ArcAuthService::SetState(State state) {
442 if (state_ == state) 517 if (state_ == state)
443 return; 518 return;
444 519
445 state_ = state; 520 state_ = state;
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 weak_ptr_factory_.GetWeakPtr())); 720 weak_ptr_factory_.GetWeakPtr()));
646 } 721 }
647 } 722 }
648 723
649 UpdateEnabledStateUMA(true); 724 UpdateEnabledStateUMA(true);
650 } 725 }
651 726
652 void ArcAuthService::ShutdownBridge() { 727 void ArcAuthService::ShutdownBridge() {
653 arc_sign_in_timer_.Stop(); 728 arc_sign_in_timer_.Stop();
654 playstore_launcher_.reset(); 729 playstore_launcher_.reset();
655 auth_callback_.Reset(); 730 account_info_notifier_.reset();
656 auth_account_callback_.Reset();
657 android_management_checker_.reset(); 731 android_management_checker_.reset();
658 auth_code_fetcher_.reset(); 732 auth_code_fetcher_.reset();
659 arc_bridge_service()->RequestStop(); 733 arc_bridge_service()->RequestStop();
660 if (state_ != State::NOT_INITIALIZED) 734 if (state_ != State::NOT_INITIALIZED)
661 SetState(State::STOPPED); 735 SetState(State::STOPPED);
662 for (auto& observer : observer_list_) 736 for (auto& observer : observer_list_)
663 observer.OnShutdownBridge(); 737 observer.OnShutdownBridge();
664 } 738 }
665 739
666 void ArcAuthService::ShutdownBridgeAndCloseUI() { 740 void ArcAuthService::ShutdownBridgeAndCloseUI() {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
715 SetState(State::ACTIVE); 789 SetState(State::ACTIVE);
716 } 790 }
717 791
718 void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { 792 void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) {
719 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 793 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
720 DCHECK(!auth_code.empty()); 794 DCHECK(!auth_code.empty());
721 795
722 if (IsAuthCodeRequest()) { 796 if (IsAuthCodeRequest()) {
723 DCHECK_EQ(state_, State::FETCHING_CODE); 797 DCHECK_EQ(state_, State::FETCHING_CODE);
724 SetState(State::ACTIVE); 798 SetState(State::ACTIVE);
725 if (!auth_callback_.is_null()) { 799 account_info_notifier_->Notify(!IsOptInVerificationDisabled(), auth_code,
726 auth_callback_.Run(auth_code, !IsOptInVerificationDisabled()); 800 mojom::ChromeAccountType::USER_ACCOUNT,
727 auth_callback_.Reset(); 801 policy_util::IsAccountManaged(profile_));
728 return; 802 account_info_notifier_.reset();
729 } else { 803 return;
730 auth_account_callback_.Run(auth_code, !IsOptInVerificationDisabled(),
731 mojom::ChromeAccountType::USER_ACCOUNT);
732 auth_account_callback_.Reset();
733 return;
734 }
735 } 804 }
736 805
737 if (state_ != State::FETCHING_CODE) { 806 if (state_ != State::FETCHING_CODE) {
738 ShutdownBridgeAndCloseUI(); 807 ShutdownBridgeAndCloseUI();
739 return; 808 return;
740 } 809 }
741 810
742 sign_in_time_ = base::Time::Now(); 811 sign_in_time_ = base::Time::Now();
743 VLOG(1) << "Starting ARC for first sign in."; 812 VLOG(1) << "Starting ARC for first sign in.";
744 813
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 return os << "FETCHING_CODE"; 1033 return os << "FETCHING_CODE";
965 case ArcAuthService::State::ACTIVE: 1034 case ArcAuthService::State::ACTIVE:
966 return os << "ACTIVE"; 1035 return os << "ACTIVE";
967 default: 1036 default:
968 NOTREACHED(); 1037 NOTREACHED();
969 return os; 1038 return os;
970 } 1039 }
971 } 1040 }
972 1041
973 } // namespace arc 1042 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_auth_service.h ('k') | components/arc/common/auth.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698