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 "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "chrome/browser/chromeos/arc/arc_auth_ui.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
xiyuan
2016/02/11 17:57:20
remove?
khmel
2016/02/12 02:45:23
Done.
| |
12 #include "chrome/browser/extensions/extension_util.h" | |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
15 #include "chrome/browser/signin/signin_manager_factory.h" | |
16 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h" | |
17 #include "chrome/browser/ui/app_list/app_list_service.h" | |
12 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
13 #include "chromeos/chromeos_switches.h" | 19 #include "chromeos/chromeos_switches.h" |
14 #include "components/arc/arc_bridge_service.h" | 20 #include "components/arc/arc_bridge_service.h" |
15 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
16 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
23 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
24 #include "components/signin/core/browser/signin_manager_base.h" | |
25 #include "content/public/browser/storage_partition.h" | |
26 #include "content/public/common/url_constants.h" | |
27 #include "extensions/browser/extension_registry.h" | |
28 #include "extensions/common/extension.h" | |
29 #include "google_apis/gaia/gaia_constants.h" | |
17 | 30 |
18 namespace arc { | 31 namespace arc { |
19 | 32 |
20 namespace { | 33 namespace { |
21 | 34 |
22 // Weak pointer. This class is owned by ArcServiceManager. | 35 // Weak pointer. This class is owned by ArcServiceManager. |
23 ArcAuthService* arc_auth_service = nullptr; | 36 ArcAuthService* arc_auth_service = nullptr; |
24 | 37 |
38 const char kArcOptInExtensionId[] = "cnbgggchhmkkdmeppjobngjoejnihlei"; | |
39 const char kArcOptStorageId[] = "arc_opt_in"; | |
40 | |
25 // Skip creating UI in unit tests | 41 // Skip creating UI in unit tests |
26 bool disable_ui_for_testing = false; | 42 bool disable_ui_for_testing = false; |
27 | 43 |
28 const char kStateDisable[] = "DISABLE"; | 44 const char kStateDisable[] = "DISABLE"; |
29 const char kStateFetchingCode[] = "FETCHING_CODE"; | 45 const char kStateFetchingCode[] = "FETCHING_CODE"; |
30 const char kStateNoCode[] = "NO_CODE"; | 46 const char kStateNoCode[] = "NO_CODE"; |
31 const char kStateEnable[] = "ENABLE"; | 47 const char kStateEnable[] = "ENABLE"; |
32 } // namespace | 48 } // namespace |
33 | 49 |
34 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) | 50 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
35 : ArcService(bridge_service), binding_(this) { | 51 : ArcService(bridge_service), binding_(this) { |
36 DCHECK(!arc_auth_service); | 52 DCHECK(!arc_auth_service); |
37 arc_auth_service = this; | 53 arc_auth_service = this; |
38 | 54 |
39 arc_bridge_service()->AddObserver(this); | 55 arc_bridge_service()->AddObserver(this); |
40 } | 56 } |
41 | 57 |
42 ArcAuthService::~ArcAuthService() { | 58 ArcAuthService::~ArcAuthService() { |
43 DCHECK(!auth_ui_ && !profile_); | 59 DCHECK(!profile_); |
44 arc_bridge_service()->RemoveObserver(this); | 60 arc_bridge_service()->RemoveObserver(this); |
45 | 61 |
46 DCHECK(arc_auth_service == this); | 62 DCHECK(arc_auth_service == this); |
47 arc_auth_service = nullptr; | 63 arc_auth_service = nullptr; |
48 } | 64 } |
49 | 65 |
50 // static | 66 // static |
51 ArcAuthService* ArcAuthService::Get() { | 67 ArcAuthService* ArcAuthService::Get() { |
52 DCHECK(arc_auth_service); | 68 DCHECK(arc_auth_service); |
53 DCHECK(arc_auth_service->thread_checker_.CalledOnValidThread()); | 69 DCHECK(arc_auth_service->thread_checker_.CalledOnValidThread()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | 121 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); |
106 } | 122 } |
107 | 123 |
108 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { | 124 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { |
109 DCHECK(profile && profile != profile_); | 125 DCHECK(profile && profile != profile_); |
110 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DCHECK(thread_checker_.CalledOnValidThread()); |
111 | 127 |
112 Shutdown(); | 128 Shutdown(); |
113 | 129 |
114 profile_ = profile; | 130 profile_ = profile; |
131 // Reuse storage used in ARC OptIn platform app. | |
132 const std::string site_url = | |
133 base::StringPrintf("%s://%s/persist?%s", content::kGuestScheme, | |
134 kArcOptInExtensionId, kArcOptStorageId); | |
135 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( | |
136 profile_, GURL(site_url)); | |
137 CHECK(storage_partition_); | |
115 | 138 |
116 // In case UI is disabled we assume that ARC is opted-in. | 139 // In case UI is disabled we assume that ARC is opted-in. |
117 if (!IsOptInVerificationDisabled()) { | 140 if (!IsOptInVerificationDisabled()) { |
118 pref_change_registrar_.Init(profile_->GetPrefs()); | 141 pref_change_registrar_.Init(profile_->GetPrefs()); |
119 pref_change_registrar_.Add( | 142 pref_change_registrar_.Add( |
120 prefs::kArcEnabled, | 143 prefs::kArcEnabled, |
121 base::Bind(&ArcAuthService::OnOptInPreferenceChanged, | 144 base::Bind(&ArcAuthService::OnOptInPreferenceChanged, |
122 base::Unretained(this))); | 145 base::Unretained(this))); |
123 OnOptInPreferenceChanged(); | 146 OnOptInPreferenceChanged(); |
124 } else { | 147 } else { |
125 SetAuthCodeAndStartArc(std::string()); | 148 auth_code_ = std::string(); |
xiyuan
2016/02/11 17:57:20
nit: auth_code_.clear();
khmel
2016/02/12 02:45:23
Done.
| |
149 ArcBridgeService::Get()->HandleStartup(); | |
150 SetState(State::ENABLE); | |
126 } | 151 } |
127 } | 152 } |
128 | 153 |
129 void ArcAuthService::Shutdown() { | 154 void ArcAuthService::Shutdown() { |
130 ShutdownBridgeAndCloseUI(); | 155 ShutdownBridgeAndCloseUI(); |
131 profile_ = nullptr; | 156 profile_ = nullptr; |
132 pref_change_registrar_.RemoveAll(); | 157 pref_change_registrar_.RemoveAll(); |
133 } | 158 } |
134 | 159 |
160 void ArcAuthService::OnMergeSessionSuccess(const std::string& data) { | |
161 DCHECK(thread_checker_.CalledOnValidThread()); | |
162 | |
163 const extensions::Extension* extension = | |
164 extensions::ExtensionRegistry::Get(profile_)->GetInstalledExtension( | |
165 kArcOptInExtensionId); | |
166 CHECK(extension && | |
167 extensions::util::IsAppLaunchable(kArcOptInExtensionId, profile_)); | |
168 | |
169 AppListControllerDelegate* controller = | |
170 AppListService::Get(chrome::GetActiveDesktop())->GetControllerDelegate(); | |
171 controller->ActivateApp(profile_, extension, | |
xiyuan
2016/02/11 17:57:20
Think we can just call OpenApplication() instead o
khmel
2016/02/12 02:45:23
Yes, more convenient
| |
172 AppListControllerDelegate::LAUNCH_FROM_UNKNOWN, 0); | |
173 } | |
174 | |
175 void ArcAuthService::OnMergeSessionFailure( | |
176 const GoogleServiceAuthError& error) { | |
177 DCHECK(thread_checker_.CalledOnValidThread()); | |
178 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | |
179 OnAuthCodeFailed(); | |
180 } | |
181 | |
182 void ArcAuthService::OnUbertokenSuccess(const std::string& token) { | |
183 DCHECK(thread_checker_.CalledOnValidThread()); | |
184 merger_fetcher_.reset( | |
185 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | |
186 storage_partition_->GetURLRequestContext())); | |
187 merger_fetcher_->StartMergeSession(token, std::string()); | |
188 } | |
189 | |
190 void ArcAuthService::OnUbertokenFailure(const GoogleServiceAuthError& error) { | |
191 DCHECK(thread_checker_.CalledOnValidThread()); | |
192 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; | |
193 OnAuthCodeFailed(); | |
194 } | |
195 | |
135 void ArcAuthService::OnOptInPreferenceChanged() { | 196 void ArcAuthService::OnOptInPreferenceChanged() { |
136 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
137 DCHECK(profile_); | 198 DCHECK(profile_); |
138 | 199 |
139 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { | 200 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { |
140 switch (state_) { | 201 switch (state_) { |
141 case State::DISABLE: | 202 case State::DISABLE: |
142 FetchAuthCode(); | 203 FetchAuthCode(); |
143 break; | 204 break; |
144 case State::NO_CODE: // Retry | 205 case State::NO_CODE: // Retry |
145 FetchAuthCode(); | 206 FetchAuthCode(); |
146 break; | 207 break; |
147 default: | 208 default: |
148 break; | 209 break; |
149 } | 210 } |
150 } else { | 211 } else { |
151 ShutdownBridgeAndCloseUI(); | 212 ShutdownBridgeAndCloseUI(); |
152 } | 213 } |
153 } | 214 } |
154 | 215 |
155 void ArcAuthService::ShutdownBridgeAndCloseUI() { | 216 void ArcAuthService::ShutdownBridgeAndCloseUI() { |
156 CloseUI(); | 217 CloseUI(); |
157 auth_fetcher_.reset(); | 218 auth_fetcher_.reset(); |
219 ubertoken_fethcher_.reset(); | |
220 merger_fetcher_.reset(); | |
158 ArcBridgeService::Get()->Shutdown(); | 221 ArcBridgeService::Get()->Shutdown(); |
159 SetState(State::DISABLE); | 222 SetState(State::DISABLE); |
160 } | 223 } |
161 | 224 |
162 void ArcAuthService::AddObserver(Observer* observer) { | 225 void ArcAuthService::AddObserver(Observer* observer) { |
163 DCHECK(thread_checker_.CalledOnValidThread()); | 226 DCHECK(thread_checker_.CalledOnValidThread()); |
164 observer_list_.AddObserver(observer); | 227 observer_list_.AddObserver(observer); |
165 } | 228 } |
166 | 229 |
167 void ArcAuthService::RemoveObserver(Observer* observer) { | 230 void ArcAuthService::RemoveObserver(Observer* observer) { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 231 DCHECK(thread_checker_.CalledOnValidThread()); |
169 observer_list_.RemoveObserver(observer); | 232 observer_list_.RemoveObserver(observer); |
170 } | 233 } |
171 | 234 |
172 void ArcAuthService::CloseUI() { | 235 void ArcAuthService::CloseUI() { |
173 if (auth_ui_) { | 236 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInUINeedToClose()); |
174 auth_ui_->Close(); | |
175 DCHECK(!auth_ui_); | |
176 } | |
177 } | 237 } |
178 | 238 |
179 void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { | 239 void ArcAuthService::SetAuthCodeAndStartArc(const std::string& auth_code) { |
180 DCHECK(thread_checker_.CalledOnValidThread()); | 240 DCHECK(thread_checker_.CalledOnValidThread()); |
181 DCHECK(!auth_code.empty() || IsOptInVerificationDisabled()); | 241 DCHECK(!auth_code.empty()); |
182 DCHECK_NE(state_, State::ENABLE); | 242 |
243 State state = state_; | |
183 | 244 |
184 ShutdownBridgeAndCloseUI(); | 245 ShutdownBridgeAndCloseUI(); |
185 | 246 |
247 if (state != State::FETCHING_CODE) | |
248 return; | |
249 | |
186 auth_code_ = auth_code; | 250 auth_code_ = auth_code; |
187 ArcBridgeService::Get()->HandleStartup(); | 251 ArcBridgeService::Get()->HandleStartup(); |
252 SetState(State::ENABLE); | |
253 } | |
188 | 254 |
189 SetState(State::ENABLE); | 255 void ArcAuthService::CheckAuthCode() { |
xiyuan
2016/02/11 17:57:20
Can we merge this with FetchAuthCode? It seems to
khmel
2016/02/12 02:45:23
Was also thinking about this, Thanks for confirmat
| |
256 DCHECK(thread_checker_.CalledOnValidThread()); | |
257 if (state_ != State::FETCHING_CODE) | |
258 return; | |
259 | |
260 auth_fetcher_.reset( | |
261 new ArcAuthFetcher(storage_partition_->GetURLRequestContext(), this)); | |
190 } | 262 } |
191 | 263 |
192 void ArcAuthService::FetchAuthCode() { | 264 void ArcAuthService::FetchAuthCode() { |
193 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
194 DCHECK(state_ == State::DISABLE || state_ == State::NO_CODE); | 266 DCHECK(state_ == State::DISABLE || state_ == State::NO_CODE); |
195 | 267 |
196 CloseUI(); | 268 CloseUI(); |
197 auth_code_.clear(); | 269 auth_code_.clear(); |
198 | 270 |
199 SetState(State::FETCHING_CODE); | 271 SetState(State::FETCHING_CODE); |
200 | 272 |
201 auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); | 273 auth_fetcher_.reset( |
274 new ArcAuthFetcher(storage_partition_->GetURLRequestContext(), this)); | |
202 } | 275 } |
203 | 276 |
204 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { | 277 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { |
205 DCHECK_EQ(state_, State::FETCHING_CODE); | 278 DCHECK_EQ(state_, State::FETCHING_CODE); |
206 SetAuthCodeAndStartArc(auth_code); | 279 SetAuthCodeAndStartArc(auth_code); |
207 } | 280 } |
208 | 281 |
282 void ArcAuthService::ShowUI() { | |
283 DCHECK(thread_checker_.CalledOnValidThread()); | |
284 | |
285 // Get auth token to continue. | |
286 ProfileOAuth2TokenService* token_service = | |
287 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
288 SigninManagerBase* signin_manager = | |
289 SigninManagerFactory::GetForProfile(profile_); | |
290 CHECK(token_service && signin_manager); | |
291 const std::string& account_id = signin_manager->GetAuthenticatedAccountId(); | |
292 ubertoken_fethcher_.reset( | |
293 new UbertokenFetcher(token_service, this, GaiaConstants::kChromeOSSource, | |
294 storage_partition_->GetURLRequestContext())); | |
295 ubertoken_fethcher_->StartFetchingToken(account_id); | |
296 } | |
297 | |
209 void ArcAuthService::OnAuthCodeNeedUI() { | 298 void ArcAuthService::OnAuthCodeNeedUI() { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | |
210 CloseUI(); | 300 CloseUI(); |
211 if (!disable_ui_for_testing && !IsOptInVerificationDisabled()) | 301 |
212 auth_ui_ = new ArcAuthUI(profile_, this); | 302 if (disable_ui_for_testing || IsOptInVerificationDisabled()) |
303 return; | |
304 | |
305 ShowUI(); | |
213 } | 306 } |
214 | 307 |
215 void ArcAuthService::OnAuthCodeFailed() { | 308 void ArcAuthService::OnAuthCodeFailed() { |
216 DCHECK_EQ(state_, State::FETCHING_CODE); | 309 DCHECK_EQ(state_, State::FETCHING_CODE); |
217 CloseUI(); | 310 CloseUI(); |
218 | 311 |
219 SetState(State::NO_CODE); | 312 SetState(State::NO_CODE); |
220 } | 313 } |
221 | 314 |
222 void ArcAuthService::OnAuthUIClosed() { | |
223 DCHECK(auth_ui_); | |
224 auth_ui_ = nullptr; | |
225 } | |
226 | |
227 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { | 315 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { |
228 switch (state) { | 316 switch (state) { |
229 case ArcAuthService::State::DISABLE: | 317 case ArcAuthService::State::DISABLE: |
230 return os << kStateDisable; | 318 return os << kStateDisable; |
231 case ArcAuthService::State::FETCHING_CODE: | 319 case ArcAuthService::State::FETCHING_CODE: |
232 return os << kStateFetchingCode; | 320 return os << kStateFetchingCode; |
233 case ArcAuthService::State::NO_CODE: | 321 case ArcAuthService::State::NO_CODE: |
234 return os << kStateNoCode; | 322 return os << kStateNoCode; |
235 case ArcAuthService::State::ENABLE: | 323 case ArcAuthService::State::ENABLE: |
236 return os << kStateEnable; | 324 return os << kStateEnable; |
237 default: | 325 default: |
238 NOTREACHED(); | 326 NOTREACHED(); |
239 return os; | 327 return os; |
240 } | 328 } |
241 } | 329 } |
242 | 330 |
243 } // namespace arc | 331 } // namespace arc |
OLD | NEW |