Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_auth_service.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_auth_service.cc b/chrome/browser/chromeos/arc/arc_auth_service.cc |
| index 46a2cea720f2f280f36d5bb591665d685f3cd213..4638797e1a02b24f65153dcc10413027534e7856 100644 |
| --- a/chrome/browser/chromeos/arc/arc_auth_service.cc |
| +++ b/chrome/browser/chromeos/arc/arc_auth_service.cc |
| @@ -6,15 +6,53 @@ |
| #include <utility> |
| +#include "chrome/browser/chromeos/arc/arc_auth_ui.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "components/arc/arc_bridge_service.h" |
| + |
| namespace arc { |
| +namespace { |
| + |
| +// Weak pointer. This class is owned by ArcServiceManager. |
| +ArcAuthService* arc_auth_service = nullptr; |
| + |
| +// Skip creating UI in unit tests |
| +bool disable_ui_for_testing = false; |
| + |
| +const char kStateDisable[] = "DISABLE"; |
| +const char kStateFetchingCode[] = "FETCHING_CODE"; |
| +const char kStateNoCode[] = "NO_CODE"; |
| +const char kStateEnable[] = "ENABLE"; |
|
oshima
2016/01/28 22:28:38
While I prefer this way, IIRC, you could have just
|
| + |
| +} // namespace |
| + |
| ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
| : ArcService(bridge_service), binding_(this) { |
| + DCHECK(!arc_auth_service); |
| + arc_auth_service = this; |
| + |
| arc_bridge_service()->AddObserver(this); |
| } |
| ArcAuthService::~ArcAuthService() { |
| arc_bridge_service()->RemoveObserver(this); |
| + CloseUI(); |
| + |
| + DCHECK(arc_auth_service == this); |
| + arc_auth_service = nullptr; |
| +} |
| + |
| +// static |
| +ArcAuthService* ArcAuthService::Get() { |
| + DCHECK(arc_auth_service); |
| + DCHECK(arc_auth_service->thread_checker_.CalledOnValidThread()); |
| + return arc_auth_service; |
| +} |
| + |
| +// static |
| +void ArcAuthService::DisableUIForTesting() { |
| + disable_ui_for_testing = true; |
| } |
| void ArcAuthService::OnAuthInstanceReady() { |
| @@ -22,9 +60,126 @@ void ArcAuthService::OnAuthInstanceReady() { |
| binding_.CreateInterfacePtrAndBind()); |
| } |
| +std::string ArcAuthService::GetAndResetAutoCode() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + std::string auth_code; |
| + auth_code_.swap(auth_code); |
| + return auth_code; |
| +} |
| + |
| void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { |
| - // TODO(victorhsieh): request auth code from LSO (crbug.com/571146). |
| - callback.Run(mojo::String("fake auth code from ArcAuthService in Chrome")); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + callback.Run(mojo::String(GetAndResetAutoCode())); |
| +} |
| + |
| +void ArcAuthService::SetState(State state) { |
| + DCHECK_NE(state_, state); |
| + state_ = state; |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| +} |
| + |
| +void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { |
| + DCHECK(profile && profile != profile_); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + Shutdown(); |
| + |
| + profile_ = profile; |
| + |
| + // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume |
| + // we opted in by default. When UI is ready, this should be synced with |
| + // user's prefs. |
| + FetchAuthCode(); |
| +} |
| + |
| +void ArcAuthService::Shutdown() { |
| + profile_ = nullptr; |
| + ArcBridgeService::Get()->Shutdown(); |
| + if (state_ != State::DISABLE) { |
| + auth_fetcher_.reset(); |
| + SetState(State::DISABLE); |
| + } |
| +} |
| + |
| +void ArcAuthService::AddObserver(Observer* observer) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + observer_list_.AddObserver(observer); |
| +} |
| + |
| +void ArcAuthService::RemoveObserver(Observer* observer) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + observer_list_.RemoveObserver(observer); |
| +} |
| + |
| +void ArcAuthService::CloseUI() { |
| + if (auth_ui_) { |
| + auth_ui_->Close(); |
| + DCHECK(!auth_ui_); |
| + } |
| +} |
| + |
| +void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!auth_code.empty()); |
| + DCHECK_NE(state_, State::ENABLE); |
| + |
| + CloseUI(); |
| + auth_fetcher_.reset(); |
| + auth_code_ = auth_code; |
| + ArcBridgeService::Get()->HandleStartup(); |
| + |
| + SetState(State::ENABLE); |
| +} |
| + |
| +void ArcAuthService::FetchAuthCode() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK_EQ(state_, State::DISABLE); |
| + |
| + CloseUI(); |
| + auth_code_.clear(); |
| + |
| + SetState(State::FETCHING_CODE); |
| + |
| + auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { |
| + DCHECK_EQ(state_, State::FETCHING_CODE); |
| + SetAuthCodeAndStartArc(auth_code); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeNeedUI() { |
| + CloseUI(); |
| + if (!disable_ui_for_testing) |
| + auth_ui_ = new ArcAuthUI(profile_, this); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeFailed() { |
| + DCHECK_EQ(state_, State::FETCHING_CODE); |
| + CloseUI(); |
| + |
| + SetState(State::NO_CODE); |
| +} |
| + |
| +void ArcAuthService::OnAuthUIClosed() { |
| + DCHECK(auth_ui_); |
| + auth_ui_ = nullptr; |
| +} |
| + |
| +std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { |
| + switch (state) { |
| + case ArcAuthService::State::DISABLE: |
| + return os << kStateDisable; |
| + case ArcAuthService::State::FETCHING_CODE: |
| + return os << kStateFetchingCode; |
| + case ArcAuthService::State::NO_CODE: |
| + return os << kStateNoCode; |
| + case ArcAuthService::State::ENABLE: |
| + return os << kStateEnable; |
| + default: |
| + NOTREACHED(); |
| + return os; |
| + } |
| } |
| } // namespace arc |