OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/app_notify_channel_setup.h" | 5 #include "chrome/browser/extensions/app_notify_channel_setup.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 AppNotifyChannelUI* ui, | 74 AppNotifyChannelUI* ui, |
75 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) | 75 base::WeakPtr<AppNotifyChannelSetup::Delegate> delegate) |
76 : profile_(profile), | 76 : profile_(profile), |
77 extension_id_(extension_id), | 77 extension_id_(extension_id), |
78 client_id_(client_id), | 78 client_id_(client_id), |
79 requestor_url_(requestor_url), | 79 requestor_url_(requestor_url), |
80 return_route_id_(return_route_id), | 80 return_route_id_(return_route_id), |
81 callback_id_(callback_id), | 81 callback_id_(callback_id), |
82 delegate_(delegate), | 82 delegate_(delegate), |
83 ui_(ui), | 83 ui_(ui), |
84 state_(INITIAL) {} | 84 state_(INITIAL), |
| 85 oauth2_access_token_failure_(false) {} |
85 | 86 |
86 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} | 87 AppNotifyChannelSetup::~AppNotifyChannelSetup() {} |
87 | 88 |
88 void AppNotifyChannelSetup::Start() { | 89 void AppNotifyChannelSetup::Start() { |
89 if (g_interceptor_for_tests) { | 90 if (g_interceptor_for_tests) { |
90 std::string channel_id; | 91 std::string channel_id; |
91 std::string error; | 92 std::string error; |
92 g_interceptor_for_tests->DoIntercept(this, &channel_id, &error); | 93 g_interceptor_for_tests->DoIntercept(this, &channel_id, &error); |
93 delegate_->AppNotifyChannelSetupComplete(channel_id, error, this); | 94 delegate_->AppNotifyChannelSetupComplete(channel_id, error, this); |
94 return; | 95 return; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 fetcher->SetExtraRequestHeaders(MakeAuthorizationHeader(auth_token)); | 141 fetcher->SetExtraRequestHeaders(MakeAuthorizationHeader(auth_token)); |
141 if (!body.empty()) { | 142 if (!body.empty()) { |
142 fetcher->SetUploadData("application/x-www-form-urlencoded", body); | 143 fetcher->SetUploadData("application/x-www-form-urlencoded", body); |
143 } | 144 } |
144 return fetcher; | 145 return fetcher; |
145 } | 146 } |
146 | 147 |
147 bool AppNotifyChannelSetup::ShouldPromptForLogin() const { | 148 bool AppNotifyChannelSetup::ShouldPromptForLogin() const { |
148 std::string username = profile_->GetPrefs()->GetString( | 149 std::string username = profile_->GetPrefs()->GetString( |
149 prefs::kGoogleServicesUsername); | 150 prefs::kGoogleServicesUsername); |
150 // Prompt for login if either the user has not logged in at all or | 151 // Prompt for login if either: |
151 // if the user is logged in but there is no OAuth2 login token. | 152 // 1. the user has not logged in at all or |
152 // The latter happens for users who are already logged in before the | 153 // 2. if the user is logged in but there is no OAuth2 login token. |
153 // code to generate OAuth2 login token is released. | 154 // The latter happens for users who are already logged in before the |
| 155 // code to generate OAuth2 login token is released. |
| 156 // 3. If the OAuth2 login token does not work anymore. |
| 157 // This can happen if the user explicitly revoked access to Google Chrome |
| 158 // from Google Accounts page. |
154 return username.empty() || | 159 return username.empty() || |
155 !profile_->GetTokenService()->HasOAuthLoginToken(); | 160 !profile_->GetTokenService()->HasOAuthLoginToken() || |
| 161 oauth2_access_token_failure_; |
156 } | 162 } |
157 | 163 |
158 void AppNotifyChannelSetup::BeginLogin() { | 164 void AppNotifyChannelSetup::BeginLogin() { |
159 CHECK_EQ(INITIAL, state_); | 165 CHECK_EQ(INITIAL, state_); |
160 state_ = LOGIN_STARTED; | 166 state_ = LOGIN_STARTED; |
161 if (ShouldPromptForLogin()) { | 167 if (ShouldPromptForLogin()) { |
162 ui_->PromptSyncSetup(this); | 168 ui_->PromptSyncSetup(this); |
163 // We'll get called back in OnSyncSetupResult | 169 // We'll get called back in OnSyncSetupResult |
164 } else { | 170 } else { |
165 EndLogin(true); | 171 EndLogin(true); |
(...skipping 25 matching lines...) Expand all Loading... |
191 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | 197 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
192 profile_->GetTokenService()->GetOAuth2LoginRefreshToken(), | 198 profile_->GetTokenService()->GetOAuth2LoginRefreshToken(), |
193 scopes); | 199 scopes); |
194 } | 200 } |
195 | 201 |
196 void AppNotifyChannelSetup::EndGetAccessToken(bool success) { | 202 void AppNotifyChannelSetup::EndGetAccessToken(bool success) { |
197 CHECK_EQ(FETCH_ACCESS_TOKEN_STARTED, state_); | 203 CHECK_EQ(FETCH_ACCESS_TOKEN_STARTED, state_); |
198 if (success) { | 204 if (success) { |
199 state_ = FETCH_ACCESS_TOKEN_DONE; | 205 state_ = FETCH_ACCESS_TOKEN_DONE; |
200 BeginRecordGrant(); | 206 BeginRecordGrant(); |
| 207 } else if (!oauth2_access_token_failure_) { |
| 208 oauth2_access_token_failure_ = true; |
| 209 // If access token generation fails, then it means somehow the |
| 210 // OAuth2 login scoped token became invalid. One way this cna happen |
| 211 // is if a user explicitly revoked access to Google Chrome from |
| 212 // Google Accounts page. In such a case, we should try to show the |
| 213 // login setup again to the user, but only if we have not already |
| 214 // done so once (to avoid infinite loop). |
| 215 state_ = INITIAL; |
| 216 BeginLogin(); |
201 } else { | 217 } else { |
202 state_ = ERROR_STATE; | 218 state_ = ERROR_STATE; |
203 ReportResult("", kChannelSetupInternalError); | 219 ReportResult("", kChannelSetupInternalError); |
204 } | 220 } |
205 } | 221 } |
206 | 222 |
207 void AppNotifyChannelSetup::BeginRecordGrant() { | 223 void AppNotifyChannelSetup::BeginRecordGrant() { |
208 CHECK_EQ(FETCH_ACCESS_TOKEN_DONE, state_); | 224 CHECK_EQ(FETCH_ACCESS_TOKEN_DONE, state_); |
209 state_ = RECORD_GRANT_STARTED; | 225 state_ = RECORD_GRANT_STARTED; |
210 | 226 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | 353 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
338 if (!dict->Get("id", &channel_id_value)) | 354 if (!dict->Get("id", &channel_id_value)) |
339 return false; | 355 return false; |
340 if (channel_id_value->GetType() != base::Value::TYPE_STRING) | 356 if (channel_id_value->GetType() != base::Value::TYPE_STRING) |
341 return false; | 357 return false; |
342 | 358 |
343 StringValue* channel_id = static_cast<StringValue*>(channel_id_value); | 359 StringValue* channel_id = static_cast<StringValue*>(channel_id_value); |
344 channel_id->GetAsString(result); | 360 channel_id->GetAsString(result); |
345 return true; | 361 return true; |
346 } | 362 } |
OLD | NEW |