OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/arc/arc_auth_context.h" | |
6 | |
7 #include "base/strings/stringprintf.h" | |
8 #include "chrome/browser/chromeos/arc/arc_auth_context_delegate.h" | |
9 #include "chrome/browser/chromeos/arc/arc_support_host.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
12 #include "chrome/browser/signin/signin_manager_factory.h" | |
13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
14 #include "components/signin/core/browser/signin_manager_base.h" | |
15 #include "content/public/browser/browser_context.h" | |
16 #include "content/public/browser/storage_partition.h" | |
17 #include "content/public/common/url_constants.h" | |
18 #include "google_apis/gaia/gaia_auth_fetcher.h" | |
19 #include "google_apis/gaia/gaia_constants.h" | |
20 | |
21 namespace arc { | |
22 | |
23 namespace { | |
24 | |
25 constexpr int kRefreshTokenTimeoutMs = 10 * 1000; // 10 sec. | |
26 | |
27 } // namespace | |
28 | |
29 ArcAuthContext::ArcAuthContext(ArcAuthContextDelegate* delegate, | |
30 Profile* profile) | |
31 : delegate_(delegate) { | |
32 // Reuse storage used in ARC OptIn platform app. | |
33 const std::string site_url = base::StringPrintf( | |
34 "%s://%s/persist?%s", content::kGuestScheme, ArcSupportHost::kHostAppId, | |
35 ArcSupportHost::kStorageId); | |
36 storage_partition_ = content::BrowserContext::GetStoragePartitionForSite( | |
37 profile, GURL(site_url)); | |
38 CHECK(storage_partition_); | |
39 | |
40 // Get token service and account ID to fetch auth tokens. | |
41 token_service_ = ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
42 const SigninManagerBase* const signin_manager = | |
43 SigninManagerFactory::GetForProfile(profile); | |
44 CHECK(token_service_ && signin_manager); | |
45 account_id_ = signin_manager->GetAuthenticatedAccountId(); | |
46 } | |
47 | |
48 ArcAuthContext::~ArcAuthContext() { | |
49 token_service_->RemoveObserver(this); | |
50 } | |
51 | |
52 void ArcAuthContext::OnRefreshTokenAvailable(const std::string& account_id) { | |
53 if (account_id != account_id_) | |
54 return; | |
55 OnRefreshTokensLoaded(); | |
56 } | |
57 | |
58 void ArcAuthContext::OnRefreshTokensLoaded() { | |
59 token_service_->RemoveObserver(this); | |
60 refresh_token_timeout_.Stop(); | |
61 StartFetchers(); | |
62 } | |
63 | |
64 void ArcAuthContext::OnRefreshTokenTimeout() { | |
65 VLOG(2) << "Failed to wait for refresh token."; | |
66 token_service_->RemoveObserver(this); | |
67 delegate_->OnPrepareContextFailed(); | |
68 } | |
69 | |
70 void ArcAuthContext::OnMergeSessionSuccess(const std::string& data) { | |
71 context_prepared_ = true; | |
72 delegate_->OnContextReady(); | |
73 } | |
74 | |
75 void ArcAuthContext::OnMergeSessionFailure( | |
76 const GoogleServiceAuthError& error) { | |
77 VLOG(2) << "Failed to merge gaia session " << error.ToString() << "."; | |
78 delegate_->OnPrepareContextFailed(); | |
79 } | |
80 | |
81 void ArcAuthContext::OnUbertokenSuccess(const std::string& token) { | |
82 merger_fetcher_.reset( | |
83 new GaiaAuthFetcher(this, GaiaConstants::kChromeOSSource, | |
84 storage_partition_->GetURLRequestContext())); | |
85 merger_fetcher_->StartMergeSession(token, std::string()); | |
86 } | |
87 | |
88 void ArcAuthContext::OnUbertokenFailure(const GoogleServiceAuthError& error) { | |
89 VLOG(2) << "Failed to get ubertoken " << error.ToString() << "."; | |
90 delegate_->OnPrepareContextFailed(); | |
91 } | |
92 | |
93 void ArcAuthContext::PrepareContext() { | |
94 if (context_prepared_) { | |
95 delegate_->OnContextReady(); | |
96 return; | |
97 } | |
98 | |
99 token_service_->RemoveObserver(this); | |
100 refresh_token_timeout_.Stop(); | |
101 | |
102 if (!token_service_->RefreshTokenIsAvailable(account_id_)) { | |
103 token_service_->AddObserver(this); | |
104 refresh_token_timeout_.Start( | |
105 FROM_HERE, base::TimeDelta::FromMilliseconds(kRefreshTokenTimeoutMs), | |
106 this, &ArcAuthContext::OnRefreshTokenTimeout); | |
107 return; | |
108 } | |
109 | |
110 StartFetchers(); | |
111 } | |
112 | |
113 void ArcAuthContext::StartFetchers() { | |
114 DCHECK(!refresh_token_timeout_.IsRunning()); | |
115 merger_fetcher_.reset(); | |
116 ubertoken_fetcher_.reset( | |
117 new UbertokenFetcher(token_service_, this, GaiaConstants::kChromeOSSource, | |
118 storage_partition_->GetURLRequestContext())); | |
119 ubertoken_fetcher_->StartFetchingToken(account_id_); | |
120 } | |
121 | |
122 } // namespace arc | |
OLD | NEW |