Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/browser/signin/profile_oauth2_token_service.cc

Issue 12647008: Refactor OAuth2TokenService to have profile- and device-based implementations. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rebase again Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/signin/profile_oauth2_token_service.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/time.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/signin_manager.h"
13 #include "chrome/browser/signin/signin_manager_factory.h"
14 #include "chrome/browser/signin/token_service.h"
15 #include "chrome/browser/signin/token_service_factory.h"
16 #include "chrome/common/chrome_notification_types.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
20 #include "google_apis/gaia/gaia_constants.h"
21 #include "google_apis/gaia/google_service_auth_error.h"
22 #include "net/url_request/url_request_context_getter.h"
23
24 ProfileOAuth2TokenService::ProfileOAuth2TokenService(
25 net::URLRequestContextGetter* getter)
26 : OAuth2TokenService(getter),
27 profile_(NULL),
28 last_auth_error_(GoogleServiceAuthError::NONE) {
29 }
30
31 ProfileOAuth2TokenService::~ProfileOAuth2TokenService() {
32 }
33
34 void ProfileOAuth2TokenService::Initialize(Profile* profile) {
35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
36
37 DCHECK(profile);
38 DCHECK(!profile_);
39 profile_ = profile;
40
41 content::Source<TokenService> token_service_source(
42 TokenServiceFactory::GetForProfile(profile));
43 registrar_.Add(this,
44 chrome::NOTIFICATION_TOKENS_CLEARED,
45 token_service_source);
46 registrar_.Add(this,
47 chrome::NOTIFICATION_TOKEN_AVAILABLE,
48 token_service_source);
49 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()->
50 AddProvider(this);
51 }
52
53 void ProfileOAuth2TokenService::Shutdown() {
54 if (profile_) {
55 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()->
56 RemoveProvider(this);
57 }
58 }
59
60 std::string ProfileOAuth2TokenService::GetRefreshToken() {
61 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
62 if (!token_service || !token_service->HasOAuthLoginToken()) {
63 return std::string();
64 }
65 return token_service->GetOAuth2LoginRefreshToken();
66 }
67
68 void ProfileOAuth2TokenService::UpdateAuthError(
69 const GoogleServiceAuthError& error) {
70 // Do not report connection errors as these are not actually auth errors.
71 // We also want to avoid masking a "real" auth error just because we
72 // subsequently get a transient network error.
73 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED)
74 return;
75
76 if (error.state() != last_auth_error_.state()) {
77 last_auth_error_ = error;
78 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()->
79 AuthStatusChanged();
80 }
81 }
82
83 void ProfileOAuth2TokenService::Observe(
84 int type,
85 const content::NotificationSource& source,
86 const content::NotificationDetails& details) {
87 DCHECK(type == chrome::NOTIFICATION_TOKENS_CLEARED ||
88 type == chrome::NOTIFICATION_TOKEN_AVAILABLE);
89 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
90 TokenService::TokenAvailableDetails* tok_details =
91 content::Details<TokenService::TokenAvailableDetails>(details).ptr();
92 if (tok_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken)
93 return;
94 }
95 // The GaiaConstants::kGaiaOAuth2LoginRefreshToken token is used to create
96 // OAuth2 access tokens. If this token either changes or is cleared, any
97 // available tokens must be invalidated.
98 ClearCache();
99 UpdateAuthError(GoogleServiceAuthError::AuthErrorNone());
100 }
101
102 GoogleServiceAuthError ProfileOAuth2TokenService::GetAuthStatus() const {
103 return last_auth_error_;
104 }
105
106 void ProfileOAuth2TokenService::RegisterCacheEntry(
107 const std::string& refresh_token,
108 const ScopeSet& scopes,
109 const std::string& access_token,
110 const base::Time& expiration_date) {
111 if (ShouldCacheForRefreshToken(TokenServiceFactory::GetForProfile(profile_),
112 refresh_token)) {
113 OAuth2TokenService::RegisterCacheEntry(refresh_token,
114 scopes,
115 access_token,
116 expiration_date);
117 }
118 }
119
120 bool ProfileOAuth2TokenService::ShouldCacheForRefreshToken(
121 TokenService *token_service,
122 const std::string& refresh_token) {
123 if (!token_service ||
124 !token_service->HasOAuthLoginToken() ||
125 token_service->GetOAuth2LoginRefreshToken().compare(refresh_token) != 0) {
126 DLOG(INFO) <<
127 "Received a token with a refresh token not maintained by TokenService.";
128 return false;
129 }
130 return true;
131 }
OLDNEW
« no previous file with comments | « chrome/browser/signin/profile_oauth2_token_service.h ('k') | chrome/browser/signin/profile_oauth2_token_service_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698