Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/chromeos/arc/arc_auth_ui.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "components/arc/arc_bridge_service.h" | |
| 12 | |
| 9 namespace arc { | 13 namespace arc { |
| 10 | 14 |
| 15 namespace { | |
| 16 | |
| 17 // Weak pointer. This class is owned by ArcServiceManager. | |
| 18 ArcAuthService* g_arc_auth_service = nullptr; | |
| 19 | |
| 20 // Skip creating UI in unit tests | |
| 21 bool g_disable_ui_for_testing = false; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 11 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) | 25 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
| 12 : ArcService(bridge_service), binding_(this) { | 26 : ArcService(bridge_service), binding_(this) { |
| 27 DCHECK(!g_arc_auth_service); | |
| 28 g_arc_auth_service = this; | |
| 29 | |
| 13 arc_bridge_service()->AddObserver(this); | 30 arc_bridge_service()->AddObserver(this); |
| 14 } | 31 } |
| 15 | 32 |
| 16 ArcAuthService::~ArcAuthService() { | 33 ArcAuthService::~ArcAuthService() { |
| 17 arc_bridge_service()->RemoveObserver(this); | 34 arc_bridge_service()->RemoveObserver(this); |
| 35 CloseUI(); | |
| 36 | |
| 37 DCHECK(g_arc_auth_service == this); | |
| 38 g_arc_auth_service = nullptr; | |
| 39 } | |
| 40 | |
| 41 // static | |
| 42 ArcAuthService* ArcAuthService::Get() { | |
| 43 DCHECK(g_arc_auth_service); | |
| 44 DCHECK(g_arc_auth_service->thread_checker_.CalledOnValidThread()); | |
| 45 return g_arc_auth_service; | |
| 46 } | |
| 47 | |
| 48 // static | |
| 49 void ArcAuthService::DisableUIForTesting() { | |
| 50 g_disable_ui_for_testing = true; | |
| 18 } | 51 } |
| 19 | 52 |
| 20 void ArcAuthService::OnAuthInstanceReady() { | 53 void ArcAuthService::OnAuthInstanceReady() { |
| 21 arc::AuthHostPtr host; | 54 arc::AuthHostPtr host; |
| 22 binding_.Bind(GetProxy(&host)); | 55 binding_.Bind(GetProxy(&host)); |
| 23 arc_bridge_service()->auth_instance()->Init(std::move(host)); | 56 arc_bridge_service()->auth_instance()->Init(std::move(host)); |
| 24 } | 57 } |
| 25 | 58 |
| 59 std::string ArcAuthService::GetAuthCode() { | |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 61 std::string auth_code; | |
| 62 auth_code_.swap(auth_code); | |
| 63 return auth_code; | |
| 64 } | |
| 65 | |
| 26 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { | 66 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { |
| 27 // TODO(victorhsieh): request auth code from LSO (crbug.com/571146). | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 28 callback.Run(mojo::String("fake auth code from ArcAuthService in Chrome")); | 68 callback.Run(mojo::String(GetAuthCode())); |
| 69 } | |
| 70 | |
| 71 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { | |
| 72 DCHECK(profile != nullptr && profile != profile_); | |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 74 | |
| 75 Shutdown(); | |
| 76 | |
| 77 profile_ = profile; | |
| 78 | |
| 79 // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume | |
| 80 // we opted in by default. When UI is ready, this should be synced with | |
| 81 // user's prefs. | |
| 82 FetchAuthCode(); | |
| 83 } | |
| 84 | |
| 85 void ArcAuthService::Shutdown() { | |
| 86 profile_ = nullptr; | |
| 87 ArcBridgeService::Get()->Shutdown(); | |
| 88 if (state_ != State::DISABLE) { | |
| 89 auth_fetcher_.reset(); | |
| 90 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
| |
| 91 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void ArcAuthService::AddObserver(Observer* observer) { | |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 97 observer_list_.AddObserver(observer); | |
| 98 } | |
| 99 | |
| 100 void ArcAuthService::RemoveObserver(Observer* observer) { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 observer_list_.RemoveObserver(observer); | |
| 103 } | |
| 104 | |
| 105 void ArcAuthService::CloseUI() { | |
| 106 if (auth_ui_) { | |
| 107 auth_ui_->Close(); | |
| 108 DCHECK(auth_ui_ == nullptr); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void ArcAuthService::SetAuthCodeAndStartArc(const std::string auth_code) { | |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 114 DCHECK(!auth_code.empty()); | |
| 115 DCHECK(state_ != State::ENABLE); | |
| 116 | |
| 117 CloseUI(); | |
| 118 auth_fetcher_.reset(); | |
| 119 auth_code_ = auth_code; | |
| 120 state_ = State::ENABLE; | |
| 121 | |
| 122 ArcBridgeService::Get()->HandleStartup(); | |
| 123 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | |
| 124 } | |
| 125 | |
| 126 void ArcAuthService::FetchAuthCode() { | |
| 127 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 128 DCHECK(state_ == State::DISABLE); | |
| 129 | |
| 130 CloseUI(); | |
| 131 auth_code_ = ""; | |
| 132 state_ = State::FETCHING_CODE; | |
| 133 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | |
| 134 | |
| 135 auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); | |
| 136 } | |
| 137 | |
| 138 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { | |
| 139 SetAuthCodeAndStartArc(auth_code); | |
| 140 } | |
| 141 | |
| 142 void ArcAuthService::OnAuthCodeNeedUI() { | |
| 143 CloseUI(); | |
| 144 if (!g_disable_ui_for_testing) | |
| 145 auth_ui_ = new ArcAuthUI(profile_, this); | |
| 146 } | |
| 147 | |
| 148 void ArcAuthService::OnAuthCodeFailed() { | |
| 149 CloseUI(); | |
| 150 state_ = State::NO_CODE; | |
| 151 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | |
| 152 } | |
| 153 | |
| 154 void ArcAuthService::OnAuthUIClosed() { | |
| 155 DCHECK(auth_ui_ != nullptr); | |
| 156 auth_ui_ = nullptr; | |
| 29 } | 157 } |
| 30 | 158 |
| 31 } // namespace arc | 159 } // namespace arc |
| OLD | NEW |