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 5068550da19750ab584f498922c639929b9fd906..1812c0c53678d0420fa403a1306e9772371b9e9f 100644 |
| --- a/chrome/browser/chromeos/arc/arc_auth_service.cc |
| +++ b/chrome/browser/chromeos/arc/arc_auth_service.cc |
| @@ -6,15 +6,48 @@ |
| #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* g_arc_auth_service = nullptr; |
| + |
| +// Skip creating UI in unit tests |
| +bool g_disable_ui_for_testing = false; |
| + |
| +} // namespace |
| + |
| ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
| : ArcService(bridge_service), binding_(this) { |
| + DCHECK(!g_arc_auth_service); |
| + g_arc_auth_service = this; |
| + |
| arc_bridge_service()->AddObserver(this); |
| } |
| ArcAuthService::~ArcAuthService() { |
| arc_bridge_service()->RemoveObserver(this); |
| + CloseUI(); |
| + |
| + DCHECK(g_arc_auth_service == this); |
| + g_arc_auth_service = nullptr; |
| +} |
| + |
| +// static |
| +ArcAuthService* ArcAuthService::Get() { |
| + DCHECK(g_arc_auth_service); |
| + DCHECK(g_arc_auth_service->thread_checker_.CalledOnValidThread()); |
| + return g_arc_auth_service; |
| +} |
| + |
| +// static |
| +void ArcAuthService::DisableUIForTesting() { |
| + g_disable_ui_for_testing = true; |
| } |
| void ArcAuthService::OnAuthInstanceReady() { |
| @@ -23,9 +56,104 @@ void ArcAuthService::OnAuthInstanceReady() { |
| arc_bridge_service()->auth_instance()->Init(std::move(host)); |
| } |
| +std::string ArcAuthService::GetAuthCode() { |
| + 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(GetAuthCode())); |
| +} |
| + |
| +void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { |
| + DCHECK(profile != nullptr && 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(); |
| + state_ = State::DISABLE; |
|
xiyuan
2016/01/27 23:56:05
nit: We have multiple places using state_ then FOR
khmel
2016/01/28 05:22:08
Done
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| + } |
| +} |
| + |
| +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_ == nullptr); |
| + } |
| +} |
| + |
| +void ArcAuthService::SetAuthCodeAndStartArc(const std::string auth_code) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!auth_code.empty()); |
| + DCHECK(state_ != State::ENABLE); |
| + |
| + CloseUI(); |
| + auth_fetcher_.reset(); |
| + auth_code_ = auth_code; |
| + state_ = State::ENABLE; |
| + |
| + ArcBridgeService::Get()->HandleStartup(); |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| +} |
| + |
| +void ArcAuthService::FetchAuthCode() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(state_ == State::DISABLE); |
| + |
| + CloseUI(); |
| + auth_code_ = ""; |
| + state_ = State::FETCHING_CODE; |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| + |
| + auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { |
| + SetAuthCodeAndStartArc(auth_code); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeNeedUI() { |
| + CloseUI(); |
| + if (!g_disable_ui_for_testing) |
| + auth_ui_ = new ArcAuthUI(profile_, this); |
| +} |
| + |
| +void ArcAuthService::OnAuthCodeFailed() { |
| + CloseUI(); |
| + state_ = State::NO_CODE; |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| +} |
| + |
| +void ArcAuthService::OnAuthUIClosed() { |
| + DCHECK(auth_ui_ != nullptr); |
| + auth_ui_ = nullptr; |
| } |
| } // namespace arc |