// 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/bind.h" #include "base/stl_util.h" #include "chrome/browser/webdata/token_service_table.h" #include "components/webdata/common/web_database_service.h" #include "content/public/browser/browser_thread.h" using base::Bind; using base::Time; using content::BrowserThread; class TokenWebDataBackend : public base::RefCountedThreadSafe { public: TokenWebDataBackend() { } WebDatabase::State RemoveAllTokens(WebDatabase* db) { if (TokenServiceTable::FromWebDatabase(db)->RemoveAllTokens()) { return WebDatabase::COMMIT_NEEDED; } return WebDatabase::COMMIT_NOT_NEEDED; } WebDatabase::State RemoveTokenForService( const std::string& service, WebDatabase* db) { if (TokenServiceTable::FromWebDatabase(db) ->RemoveTokenForService(service)) { 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 GetAllTokens(WebDatabase* db) { std::map map; TokenServiceTable::FromWebDatabase(db)->GetAllTokens(&map); return scoped_ptr( new WDResult >(TOKEN_RESULT, map)); } protected: virtual ~TokenWebDataBackend() { } private: friend struct BrowserThread::DeleteOnThread; friend class base::DeleteHelper; // We have to friend RCTS<> so WIN shared-lib build is happy // (http://crbug/112250). friend class base::RefCountedThreadSafe; }; TokenWebData::TokenWebData(scoped_refptr wdbs, const ProfileErrorCallback& callback) : WebDataServiceBase(wdbs, callback, BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), 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_)); } void TokenWebData::RemoveTokenForService(const std::string& service) { wdbs_->ScheduleDBTask(FROM_HERE, Bind(&TokenWebDataBackend::RemoveTokenForService, token_backend_, service)); } // Null on failure. Success is WDResult WebDataServiceBase::Handle TokenWebData::GetAllTokens( WebDataServiceConsumer* consumer) { return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, Bind(&TokenWebDataBackend::GetAllTokens, token_backend_), consumer); } TokenWebData::TokenWebData() : WebDataServiceBase(NULL, ProfileErrorCallback(), BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), token_backend_(new TokenWebDataBackend()) { } TokenWebData::~TokenWebData() { }