Skip to content

Commit

Permalink
Enable x-chrome-connected header for drive without flags
Browse files Browse the repository at this point in the history
BUG=293733

Review URL: https://codereview.chromium.org/109703003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@241369 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
guohui@chromium.org committed Dec 17, 2013
1 parent 8539cdb commit 22c17bc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 34 deletions.
3 changes: 3 additions & 0 deletions chrome/browser/signin/fake_profile_oauth2_token_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
#include "chrome/browser/signin/signin_account_id_helper.h"

FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
}
Expand All @@ -19,9 +20,11 @@ BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
}

FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() {
SigninAccountIdHelper::SetDisableForTest(true);
}

FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
SigninAccountIdHelper::SetDisableForTest(false);
}

bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "chrome/browser/signin/mutable_profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_account_id_helper.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/webdata/token_web_data.h"
Expand Down Expand Up @@ -46,6 +47,9 @@ class MutableProfileOAuth2TokenServiceTest :

profile_.reset(new TestingProfile);
profile_->CreateWebDataService();

SigninAccountIdHelper::SetDisableForTest(true);

factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(),
"", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
oauth2_service_ =
Expand All @@ -59,6 +63,7 @@ class MutableProfileOAuth2TokenServiceTest :
virtual void TearDown() OVERRIDE {
oauth2_service_->RemoveObserver(this);
profile_.reset();
SigninAccountIdHelper::SetDisableForTest(false);
}

TestingProfile* profile() { return profile_.get(); }
Expand Down
34 changes: 16 additions & 18 deletions chrome/browser/signin/signin_account_id_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

#include "chrome/browser/signin/signin_account_id_helper.h"

#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_service.h"
#include "google_apis/gaia/gaia_oauth_client.h"
Expand Down Expand Up @@ -85,26 +82,20 @@ void SigninAccountIdHelper::AccountIdFetcher::OnGetTokenFailure(
VLOG(1) << "OnGetTokenFailure: " << error.error_message();
DCHECK_EQ(request, login_token_request_.get());
signin_account_id_helper_->OnPrimaryAccountIdFetched("");
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void SigninAccountIdHelper::AccountIdFetcher::OnGetUserIdResponse(
const std::string& account_id) {
signin_account_id_helper_->OnPrimaryAccountIdFetched(account_id);
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void SigninAccountIdHelper::AccountIdFetcher::OnOAuthError() {
VLOG(1) << "OnOAuthError";
signin_account_id_helper_->OnPrimaryAccountIdFetched("");
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

void SigninAccountIdHelper::AccountIdFetcher::OnNetworkError(
int response_code) {
VLOG(1) << "OnNetworkError " << response_code;
signin_account_id_helper_->OnPrimaryAccountIdFetched("");
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}

SigninAccountIdHelper::SigninAccountIdHelper(Profile* profile)
Expand All @@ -116,9 +107,9 @@ SigninAccountIdHelper::SigninAccountIdHelper(Profile* profile)
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
std::string primary_email = token_service->GetPrimaryAccountId();
if (!primary_email.empty() &&
token_service->RefreshTokenIsAvailable(primary_email)) {
// AccountIdFetcher will delete itself.
new AccountIdFetcher(profile_, this);
token_service->RefreshTokenIsAvailable(primary_email) &&
!disable_for_test_) {
id_fetcher_.reset(new AccountIdFetcher(profile_, this));
}
token_service->AddObserver(this);
}
Expand All @@ -137,14 +128,13 @@ void SigninAccountIdHelper::Observe(
}

void SigninAccountIdHelper::OnRefreshTokenAvailable(const std::string& email) {
SigninManagerBase* manager =
SigninManagerFactory::GetForProfile(profile_);
if (email == manager->GetAuthenticatedUsername()) {
ProfileOAuth2TokenService* service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
if (email == service->GetPrimaryAccountId()) {
std::string current_account_id =
profile_->GetPrefs()->GetString(prefs::kGoogleServicesUserAccountId);
if (current_account_id.empty()) {
// AccountIdFetcher will delete itself.
new AccountIdFetcher(profile_, this);
if (current_account_id.empty() && !disable_for_test_) {
id_fetcher_.reset(new AccountIdFetcher(profile_, this));
}
}
}
Expand All @@ -157,3 +147,11 @@ void SigninAccountIdHelper::OnPrimaryAccountIdFetched(
}
}

// static
bool SigninAccountIdHelper::disable_for_test_ = false;

// static
void SigninAccountIdHelper::SetDisableForTest(bool disable_for_test) {
disable_for_test_ = disable_for_test;
}

16 changes: 11 additions & 5 deletions chrome/browser/signin/signin_account_id_helper.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// The helper class for managing the obfuscated account ID of the primary
// account. It fetches the ID when user first signs into Chrome or when user
// opens a connected Chrome profile without an obfuscated account ID, and stores
// the ID in the profile preference.

#ifndef CHROME_BROWSER_SIGNIN_SIGNIN_ACCOUNT_ID_HELPER_H_
#define CHROME_BROWSER_SIGNIN_SIGNIN_ACCOUNT_ID_HELPER_H_
Expand All @@ -19,6 +14,10 @@ class CookieSettings;
class GaiaAuthFetcher;
class Profile;

// The helper class for managing the obfuscated account ID of the primary
// account. It fetches the ID when user first signs into Chrome or when user
// opens a connected Chrome profile without an obfuscated account ID, and stores
// the ID in the profile preference.
class SigninAccountIdHelper
: public content::NotificationObserver,
public OAuth2TokenService::Observer {
Expand All @@ -34,12 +33,19 @@ class SigninAccountIdHelper
// OAuth2TokenService::Observer:
virtual void OnRefreshTokenAvailable(const std::string& email) OVERRIDE;

// Disables network requests for testing to avoid messing up with irrelevant
// tests.
static void SetDisableForTest(bool disable_for_test);

private:
// Invoked when receiving the response for |account_id_fetcher_|.
void OnPrimaryAccountIdFetched(const std::string& account_id);

// Helper class for fetching the obfuscated account ID.
class AccountIdFetcher;
scoped_ptr<AccountIdFetcher> id_fetcher_;

static bool disable_for_test_;

Profile* profile_;
content::NotificationRegistrar registrar_;
Expand Down
16 changes: 9 additions & 7 deletions chrome/browser/signin/signin_header_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ void AppendMirrorRequestHeaderIfPossible(
int route_id) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

if (io_data->is_incognito() ||
io_data->google_services_username()->GetValue().empty()) {
return;
}

// Only set the header for Gaia (in the mirror world) and Drive. Gaia needs
// the header to redirect certain user actions to Chrome native UI. Drive
// needs the header to tell if the current user is connected. The drive path
Expand All @@ -71,14 +76,11 @@ void AppendMirrorRequestHeaderIfPossible(
GURL origin(url.GetOrigin());
bool enable_inline = CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableInlineSignin);
if (!enable_inline ||
!profiles::IsNewProfileManagementEnabled() ||
io_data->is_incognito() ||
io_data->google_services_username()->GetValue().empty() ||
(!IsDriveOrigin(origin) &&
!gaia::IsGaiaSignonRealm(origin))) {
bool is_gaia_signin = enable_inline &&
profiles::IsNewProfileManagementEnabled() &&
gaia::IsGaiaSignonRealm(origin);
if (!is_gaia_signin && !IsDriveOrigin(origin))
return;
}

ExtensionRendererState* renderer_state =
ExtensionRendererState::GetInstance();
Expand Down
5 changes: 1 addition & 4 deletions chrome/browser/signin/signin_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,7 @@ void SigninManager::Initialize(Profile* profile, PrefService* local_state) {
SignOut();
}

if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNewProfileManagement)) {
account_id_helper_.reset(new SigninAccountIdHelper(profile));
}
account_id_helper_.reset(new SigninAccountIdHelper(profile));
}

void SigninManager::Shutdown() {
Expand Down
7 changes: 7 additions & 0 deletions chrome/browser/sync/test/integration/sync_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ void SyncTest::SetupMockGaiaResponses() {
"}",
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
fake_factory_->SetFakeResponse(
GaiaUrls::GetInstance()->oauth_user_info_url(),
"{"
" \"id\": \"12345\""
"}",
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
fake_factory_->SetFakeResponse(
GaiaUrls::GetInstance()->oauth1_login_url(),
"SID=sid\nLSID=lsid\nAuth=auth_token",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/base/web_ui_browsertest.h"
#include "google_apis/gaia/gaia_urls.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
Expand Down Expand Up @@ -228,6 +229,10 @@ const char kResponseGaiaToken[] = "{"
" \"token_type\": \"Bearer\""
"}";

const char kResponseGaiaId[] = "{"
" \"id\": \"12345\""
"}";

const char kURLInfo[] = "http://1.2.3.4:8888/privet/info";

const char kURLRegisterStart[] =
Expand Down Expand Up @@ -377,6 +382,16 @@ class LocalDiscoveryUITest : public WebUIBrowserTest {
kURLGaiaToken))
.Times(AnyNumber());

fake_fetcher_factory().SetFakeResponse(
GaiaUrls::GetInstance()->oauth_user_info_url(),
kResponseGaiaId,
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);

EXPECT_CALL(fake_url_fetcher_creator(), OnCreateFakeURLFetcher(
GaiaUrls::GetInstance()->oauth_user_info_url().spec()))
.Times(AnyNumber());

ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(browser()->profile());

Expand Down

0 comments on commit 22c17bc

Please sign in to comment.