Skip to content

Commit

Permalink
Split token-related methods from WebDataService
Browse files Browse the repository at this point in the history
These are only used bu Signin/ and will move into the sign-in component once it gets created.

TBR=atwilson@chromium.org,ben@chromium.org 
BUG=233552

Review URL: https://chromiumcodereview.appspot.com/15734014

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202463 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
caitkp@chromium.org committed May 27, 2013
1 parent 9993d32 commit b69d631
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 108 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/signin/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ include_rules = [
"!chrome/browser/ui/webui/signin/login_ui_service_factory.h",
"!chrome/browser/ui/webui/signin/profile_signin_confirmation_dialog.h",
"!chrome/browser/ui/webui/signin_internals_ui.h",
"!chrome/browser/webdata/web_data_service.h",
"!chrome/browser/webdata/token_web_data.h",
"!chrome/browser/webdata/web_data_service_factory.h",
"!chrome/common/chrome_notification_types.h",
"!chrome/common/chrome_switches.h",
Expand Down
1 change: 0 additions & 1 deletion chrome/browser/signin/signin_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "chrome/browser/signin/chrome_signin_manager_delegate.h"
#include "chrome/browser/signin/token_service.h"
#include "chrome/browser/signin/token_service_unittest.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
Expand Down
24 changes: 12 additions & 12 deletions chrome/browser/signin/token_service.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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.

Expand All @@ -11,7 +11,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/browser/webdata/token_web_data.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -66,7 +66,7 @@ void TokenService::Shutdown() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ResetCredentialsInMemory();
}
web_data_service_ = NULL;
token_web_data_ = NULL;
}

void TokenService::Initialize(const char* const source,
Expand All @@ -81,7 +81,7 @@ void TokenService::Initialize(const char* const source,
getter_ = profile->GetRequestContext();
// Since the user can create a bookmark in incognito, sync may be running.
// Thus we have to go for explicit access.
web_data_service_ = WebDataService::FromBrowserContext(profile);
token_web_data_ = TokenWebData::FromBrowserContext(profile);
source_ = std::string(source);

CommandLine* cmd_line = CommandLine::ForCurrentProcess();
Expand Down Expand Up @@ -140,7 +140,7 @@ void TokenService::ResetCredentialsInMemory() {

// Cancel pending loads. Callbacks will not return.
if (token_loading_query_) {
web_data_service_->CancelRequest(token_loading_query_);
token_web_data_->CancelRequest(token_loading_query_);
token_loading_query_ = 0;
}

Expand Down Expand Up @@ -175,21 +175,21 @@ void TokenService::UpdateCredentialsWithOAuth2(

void TokenService::LoadTokensFromDB() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (web_data_service_.get())
token_loading_query_ = web_data_service_->GetAllTokens(this);
if (token_web_data_.get())
token_loading_query_ = token_web_data_->GetAllTokens(this);
}

void TokenService::SaveAuthTokenToDB(const std::string& service,
const std::string& auth_token) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (web_data_service_.get())
web_data_service_->SetTokenForService(service, auth_token);
if (token_web_data_.get())
token_web_data_->SetTokenForService(service, auth_token);
}

void TokenService::EraseTokensFromDB() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (web_data_service_.get())
web_data_service_->RemoveAllTokens();
if (token_web_data_.get())
token_web_data_->RemoveAllTokens();

content::NotificationService::current()->Notify(
chrome::NOTIFICATION_TOKENS_CLEARED,
Expand Down Expand Up @@ -363,7 +363,7 @@ void TokenService::OnClientOAuthFailure(
GaiaConstants::kGaiaOAuth2LoginRefreshToken, error);
}

void TokenService::OnWebDataServiceRequestDone(WebDataService::Handle h,
void TokenService::OnWebDataServiceRequestDone(WebDataServiceBase::Handle h,
const WDTypedResult* result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(token_loading_query_);
Expand Down
10 changes: 6 additions & 4 deletions chrome/browser/signin/token_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "chrome/browser/signin/signin_internals_util.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
#include "components/webdata/common/web_data_service_base.h"
#include "components/webdata/common/web_data_service_consumer.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "google_apis/gaia/google_service_auth_error.h"

class Profile;
class TokenServiceTest;
class TokenWebData;

namespace net {
class URLRequestContextGetter;
Expand Down Expand Up @@ -188,7 +190,7 @@ class TokenService : public GaiaAuthConsumer,

// WebDataServiceConsumer implementation.
virtual void OnWebDataServiceRequestDone(
WebDataService::Handle h,
WebDataServiceBase::Handle h,
const WDTypedResult* result) OVERRIDE;

// Gets the list of all service names for which tokens will be retrieved.
Expand Down Expand Up @@ -227,11 +229,11 @@ class TokenService : public GaiaAuthConsumer,
Profile* profile_;

// Web data service to access tokens from.
scoped_refptr<WebDataService> web_data_service_;
scoped_refptr<TokenWebData> token_web_data_;
// Getter to use for fetchers.
scoped_refptr<net::URLRequestContextGetter> getter_;
// Request handle to load Gaia tokens from DB.
WebDataService::Handle token_loading_query_;
WebDataServiceBase::Handle token_loading_query_;
// True if token loading has completed (regardless of success).
bool tokens_loaded_;

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/signin/token_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/signin/token_service_factory.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/browser/webdata/token_web_data.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "components/webdata/encryptor/encryptor.h"
Expand Down Expand Up @@ -104,7 +104,7 @@ void TokenServiceTestHarness::TearDown() {

void TokenServiceTestHarness::WaitForDBLoadCompletion() {
// Force the loading of the WebDataService.
WebDataService::FromBrowserContext(profile_.get());
TokenWebData::FromBrowserContext(profile_.get());

// The WebDB does all work on the DB thread. This will add an event
// to the end of the DB thread, so when we reach this task, all DB
Expand Down
1 change: 0 additions & 1 deletion chrome/browser/signin/token_service_unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/signin/token_service.h"
#include "chrome/browser/webdata/web_data_service.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MockWebDataServiceWrapperSyncable : public MockWebDataServiceWrapper {
}

MockWebDataServiceWrapperSyncable()
: MockWebDataServiceWrapper(NULL, new FakeWebDataService()) {
: MockWebDataServiceWrapper(NULL, new FakeWebDataService(), NULL) {
}

virtual void Shutdown() OVERRIDE {
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/sync/profile_sync_service_autofill_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ syncer::ModelType GetModelType<AutofillProfile>() {
return syncer::AUTOFILL_PROFILE;
}

class TokenWebDataServiceFake : public WebDataService {
class TokenWebDataServiceFake : public TokenWebData {
public:
TokenWebDataServiceFake()
: WebDataService() {
: TokenWebData() {
}

virtual bool IsDatabaseLoaded() OVERRIDE {
Expand Down Expand Up @@ -333,8 +333,9 @@ class WebDataServiceFake : public AutofillWebDataService {
BrowserContextKeyedService* BuildMockWebDataServiceWrapper(
content::BrowserContext* profile) {
return new MockWebDataServiceWrapper(
new TokenWebDataServiceFake(),
new WebDataServiceFake());
NULL,
new WebDataServiceFake(),
new TokenWebDataServiceFake());
}

ACTION_P(MakeAutocompleteSyncComponents, wds) {
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/webdata/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ specific_include_rules = {
# TODO(caitkp): Extract unit tests from //chrome, at lower priority
# than production code.
r"(.*_unittest|.*_test_util)\.(cc|h)": [
"+chrome/browser/webdata/token_web_data.h",
"+chrome/browser/webdata/web_data_service.h",
"+chrome/browser/webdata/web_data_service_factory.h",
],
Expand Down
91 changes: 91 additions & 0 deletions chrome/browser/webdata/token_web_data.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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.

#include "chrome/browser/webdata/token_web_data.h"

#include "base/stl_util.h"
#include "chrome/browser/webdata/token_service_table.h"
#include "components/webdata/common/web_database_service.h"

using base::Bind;
using base::Time;
using content::BrowserThread;

class TokenWebDataBackend
: public base::RefCountedThreadSafe<TokenWebDataBackend,
BrowserThread::DeleteOnDBThread> {

public:
TokenWebDataBackend() {
}

WebDatabase::State RemoveAllTokens(WebDatabase* db) {
if (TokenServiceTable::FromWebDatabase(db)->RemoveAllTokens()) {
return WebDatabase::COMMIT_NEEDED;
}
return WebDatabase::COMMIT_NOT_NEEDED;
}

WebDatabase::State SetTokenForService(
const std::string& service, const std::string& token, WebDatabase* db) {
if (TokenServiceTable::FromWebDatabase(db)->SetTokenForService(service,
token)) {
return WebDatabase::COMMIT_NEEDED;
}
return WebDatabase::COMMIT_NOT_NEEDED;
}

scoped_ptr<WDTypedResult> GetAllTokens(WebDatabase* db) {
std::map<std::string, std::string> map;
TokenServiceTable::FromWebDatabase(db)->GetAllTokens(&map);
return scoped_ptr<WDTypedResult>(
new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map));
}

protected:
virtual ~TokenWebDataBackend() {
}

private:
friend struct BrowserThread::DeleteOnThread<BrowserThread::DB>;
friend class base::DeleteHelper<TokenWebDataBackend>;
// We have to friend RCTS<> so WIN shared-lib build is happy
// (http://crbug/112250).
friend class base::RefCountedThreadSafe<TokenWebDataBackend,
BrowserThread::DeleteOnDBThread>;

};

TokenWebData::TokenWebData(scoped_refptr<WebDatabaseService> wdbs,
const ProfileErrorCallback& callback)
: WebDataServiceBase(wdbs, callback),
token_backend_(new TokenWebDataBackend()) {
}

void TokenWebData::SetTokenForService(const std::string& service,
const std::string& token) {
wdbs_->ScheduleDBTask(FROM_HERE,
Bind(&TokenWebDataBackend::SetTokenForService, token_backend_,
service, token));
}

void TokenWebData::RemoveAllTokens() {
wdbs_->ScheduleDBTask(FROM_HERE,
Bind(&TokenWebDataBackend::RemoveAllTokens, token_backend_));
}

// Null on failure. Success is WDResult<std::string>
WebDataServiceBase::Handle TokenWebData::GetAllTokens(
WebDataServiceConsumer* consumer) {
return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
Bind(&TokenWebDataBackend::GetAllTokens, token_backend_), consumer);
}

TokenWebData::TokenWebData()
: WebDataServiceBase(NULL, ProfileErrorCallback()),
token_backend_(new TokenWebDataBackend()) {
}

TokenWebData::~TokenWebData() {
}
66 changes: 66 additions & 0 deletions chrome/browser/webdata/token_web_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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.

// Chromium settings and storage represent user-selected preferences and
// information and MUST not be extracted, overwritten or modified except
// through Chromium defined APIs.

#ifndef CHROME_BROWSER_WEBDATA_TOKEN_WEB_DATA_H__
#define CHROME_BROWSER_WEBDATA_TOKEN_WEB_DATA_H__

#include <map>
#include <string>
#include <vector>

#include "base/callback_forward.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "components/webdata/common/web_data_results.h"
#include "components/webdata/common/web_data_service_base.h"
#include "components/webdata/common/web_data_service_consumer.h"
#include "components/webdata/common/web_database.h"

class TokenWebDataBackend;
class WebDatabaseService;
class WebDataServiceConsumer;

namespace content {
class BrowserContext;
}

// TokenWebData is a data repository for storage of authentication tokens.

class TokenWebData : public WebDataServiceBase {
public:
// Retrieve a WebDataService for the given context.
static scoped_refptr<TokenWebData> FromBrowserContext(
content::BrowserContext* context);

TokenWebData(scoped_refptr<WebDatabaseService> wdbs,
const ProfileErrorCallback& callback);

// Set a token to use for a specified service.
void SetTokenForService(const std::string& service,
const std::string& token);

// Remove all tokens stored in the web database.
void RemoveAllTokens();

// Null on failure. Success is WDResult<std::vector<std::string> >
virtual Handle GetAllTokens(WebDataServiceConsumer* consumer);

protected:
// For unit tests, passes a null callback.
TokenWebData();

virtual ~TokenWebData();

private:
scoped_refptr<TokenWebDataBackend> token_backend_;

DISALLOW_COPY_AND_ASSIGN(TokenWebData);
};

#endif // CHROME_BROWSER_WEBDATA_TOKEN_WEB_DATA_H__
Loading

0 comments on commit b69d631

Please sign in to comment.