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::SetProfile(Profile* profile) { |
| 72 DCHECK(profile == nullptr || profile != profile_); |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 |
| 75 ArcBridgeService::Get()->Shutdown(); |
| 76 if (state_ != State::DISABLE) { |
| 77 auth_fetcher_.reset(); |
| 78 state_ = State::DISABLE; |
| 79 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 80 } |
| 81 |
| 82 profile_ = profile; |
| 83 |
| 84 // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume |
| 85 // we opted in by default. When UI is ready, this should be synced with |
| 86 // user's prefs. |
| 87 if (profile_ != nullptr) |
| 88 FetchAuthCode(); |
| 89 } |
| 90 |
| 91 void ArcAuthService::AddObserver(Observer* observer) { |
| 92 DCHECK(thread_checker_.CalledOnValidThread()); |
| 93 observer_list_.AddObserver(observer); |
| 94 } |
| 95 |
| 96 void ArcAuthService::RemoveObserver(Observer* observer) { |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 observer_list_.RemoveObserver(observer); |
| 99 } |
| 100 |
| 101 void ArcAuthService::CloseUI() { |
| 102 if (auth_ui_) { |
| 103 auth_ui_->Close(); |
| 104 DCHECK(auth_ui_ == nullptr); |
| 105 } |
| 106 } |
| 107 |
| 108 void ArcAuthService::SetAuthCodeAndStartArc(const std::string auth_code) { |
| 109 DCHECK(thread_checker_.CalledOnValidThread()); |
| 110 DCHECK(!auth_code.empty()); |
| 111 DCHECK(state_ != State::ENABLE); |
| 112 |
| 113 CloseUI(); |
| 114 auth_fetcher_.reset(); |
| 115 auth_code_ = auth_code; |
| 116 state_ = State::ENABLE; |
| 117 |
| 118 ArcBridgeService::Get()->HandleStartup(); |
| 119 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 120 } |
| 121 |
| 122 void ArcAuthService::FetchAuthCode() { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 DCHECK(state_ == State::DISABLE); |
| 125 |
| 126 CloseUI(); |
| 127 auth_code_ = ""; |
| 128 state_ = State::FETCHING_CODE; |
| 129 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 130 |
| 131 auth_fetcher_.reset(new ArcAuthFetcher(profile_, this)); |
| 132 } |
| 133 |
| 134 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { |
| 135 SetAuthCodeAndStartArc(auth_code); |
| 136 } |
| 137 |
| 138 void ArcAuthService::OnAuthCodeNeedUI() { |
| 139 CloseUI(); |
| 140 if (!g_disable_ui_for_testing) |
| 141 auth_ui_ = new ArcAuthUI(profile_, this); |
| 142 } |
| 143 |
| 144 void ArcAuthService::OnAuthCodeFailed() { |
| 145 CloseUI(); |
| 146 state_ = State::NO_CODE; |
| 147 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 148 } |
| 149 |
| 150 void ArcAuthService::OnAuthUIClosed() { |
| 151 DCHECK(auth_ui_ != nullptr); |
| 152 auth_ui_ = nullptr; |
29 } | 153 } |
30 | 154 |
31 } // namespace arc | 155 } // namespace arc |
OLD | NEW |