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* arc_auth_service = nullptr; |
| 19 |
| 20 // Skip creating UI in unit tests |
| 21 bool disable_ui_for_testing = false; |
| 22 |
| 23 const char kStateDisable[] = "DISABLE"; |
| 24 const char kStateFetchingCode[] = "FETCHING_CODE"; |
| 25 const char kStateNoCode[] = "NO_CODE"; |
| 26 const char kStateEnable[] = "ENABLE"; |
| 27 |
| 28 } // namespace |
| 29 |
11 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) | 30 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
12 : ArcService(bridge_service), binding_(this) { | 31 : ArcService(bridge_service), binding_(this) { |
| 32 DCHECK(!arc_auth_service); |
| 33 arc_auth_service = this; |
| 34 |
13 arc_bridge_service()->AddObserver(this); | 35 arc_bridge_service()->AddObserver(this); |
14 } | 36 } |
15 | 37 |
16 ArcAuthService::~ArcAuthService() { | 38 ArcAuthService::~ArcAuthService() { |
17 arc_bridge_service()->RemoveObserver(this); | 39 arc_bridge_service()->RemoveObserver(this); |
| 40 CloseUI(); |
| 41 |
| 42 DCHECK(arc_auth_service == this); |
| 43 arc_auth_service = nullptr; |
| 44 } |
| 45 |
| 46 // static |
| 47 ArcAuthService* ArcAuthService::Get() { |
| 48 DCHECK(arc_auth_service); |
| 49 DCHECK(arc_auth_service->thread_checker_.CalledOnValidThread()); |
| 50 return arc_auth_service; |
| 51 } |
| 52 |
| 53 // static |
| 54 void ArcAuthService::DisableUIForTesting() { |
| 55 disable_ui_for_testing = true; |
18 } | 56 } |
19 | 57 |
20 void ArcAuthService::OnAuthInstanceReady() { | 58 void ArcAuthService::OnAuthInstanceReady() { |
21 arc_bridge_service()->auth_instance()->Init( | 59 arc_bridge_service()->auth_instance()->Init( |
22 binding_.CreateInterfacePtrAndBind()); | 60 binding_.CreateInterfacePtrAndBind()); |
23 } | 61 } |
24 | 62 |
| 63 std::string ArcAuthService::GetAndResetAutoCode() { |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 std::string auth_code; |
| 66 auth_code_.swap(auth_code); |
| 67 return auth_code; |
| 68 } |
| 69 |
25 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { | 70 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { |
26 // TODO(victorhsieh): request auth code from LSO (crbug.com/571146). | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
27 callback.Run(mojo::String("fake auth code from ArcAuthService in Chrome")); | 72 callback.Run(mojo::String(GetAndResetAutoCode())); |
| 73 } |
| 74 |
| 75 void ArcAuthService::SetState(State state) { |
| 76 DCHECK_NE(state_, state); |
| 77 state_ = state; |
| 78 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
| 79 } |
| 80 |
| 81 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { |
| 82 DCHECK(profile && profile != profile_); |
| 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 |
| 85 Shutdown(); |
| 86 |
| 87 profile_ = profile; |
| 88 |
| 89 // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume |
| 90 // we opted in by default. When UI is ready, this should be synced with |
| 91 // user's prefs. |
| 92 FetchAuthCode(); |
| 93 } |
| 94 |
| 95 void ArcAuthService::Shutdown() { |
| 96 profile_ = nullptr; |
| 97 ArcBridgeService::Get()->Shutdown(); |
| 98 if (state_ != State::DISABLE) { |
| 99 auth_fetcher_.reset(); |
| 100 SetState(State::DISABLE); |
| 101 } |
| 102 } |
| 103 |
| 104 void ArcAuthService::AddObserver(Observer* observer) { |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 observer_list_.AddObserver(observer); |
| 107 } |
| 108 |
| 109 void ArcAuthService::RemoveObserver(Observer* observer) { |
| 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 observer_list_.RemoveObserver(observer); |
| 112 } |
| 113 |
| 114 void ArcAuthService::CloseUI() { |
| 115 if (auth_ui_) { |
| 116 auth_ui_->Close(); |
| 117 DCHECK(!auth_ui_); |
| 118 } |
| 119 } |
| 120 |
| 121 void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); |
| 123 DCHECK(!auth_code.empty()); |
| 124 DCHECK_NE(state_, State::ENABLE); |
| 125 |
| 126 CloseUI(); |
| 127 auth_fetcher_.reset(); |
| 128 auth_code_ = auth_code; |
| 129 ArcBridgeService::Get()->HandleStartup(); |
| 130 |
| 131 SetState(State::ENABLE); |
| 132 } |
| 133 |
| 134 void ArcAuthService::FetchAuthCode() { |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); |
| 136 DCHECK_EQ(state_, State::DISABLE); |
| 137 |
| 138 CloseUI(); |
| 139 auth_code_.clear(); |
| 140 |
| 141 SetState(State::FETCHING_CODE); |
| 142 |
| 143 auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); |
| 144 } |
| 145 |
| 146 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { |
| 147 DCHECK_EQ(state_, State::FETCHING_CODE); |
| 148 SetAuthCodeAndStartArc(auth_code); |
| 149 } |
| 150 |
| 151 void ArcAuthService::OnAuthCodeNeedUI() { |
| 152 CloseUI(); |
| 153 if (!disable_ui_for_testing) |
| 154 auth_ui_ = new ArcAuthUI(profile_, this); |
| 155 } |
| 156 |
| 157 void ArcAuthService::OnAuthCodeFailed() { |
| 158 DCHECK_EQ(state_, State::FETCHING_CODE); |
| 159 CloseUI(); |
| 160 |
| 161 SetState(State::NO_CODE); |
| 162 } |
| 163 |
| 164 void ArcAuthService::OnAuthUIClosed() { |
| 165 DCHECK(auth_ui_); |
| 166 auth_ui_ = nullptr; |
| 167 } |
| 168 |
| 169 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { |
| 170 switch (state) { |
| 171 case ArcAuthService::State::DISABLE: |
| 172 return os << kStateDisable; |
| 173 case ArcAuthService::State::FETCHING_CODE: |
| 174 return os << kStateFetchingCode; |
| 175 case ArcAuthService::State::NO_CODE: |
| 176 return os << kStateNoCode; |
| 177 case ArcAuthService::State::ENABLE: |
| 178 return os << kStateEnable; |
| 179 default: |
| 180 NOTREACHED(); |
| 181 return os; |
| 182 } |
28 } | 183 } |
29 | 184 |
30 } // namespace arc | 185 } // namespace arc |
OLD | NEW |