token_web_data.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// Copyright 2014 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 "components/signin/core/browser/webdata/token_web_data.h" 6 7#include "base/bind.h" 8#include "base/memory/ref_counted_delete_on_message_loop.h" 9#include "base/message_loop/message_loop_proxy.h" 10#include "base/stl_util.h" 11#include "components/signin/core/browser/webdata/token_service_table.h" 12#include "components/webdata/common/web_database_service.h" 13 14using base::Bind; 15using base::Time; 16 17class TokenWebDataBackend 18 : public base::RefCountedDeleteOnMessageLoop<TokenWebDataBackend> { 19 20 public: 21 TokenWebDataBackend(scoped_refptr<base::MessageLoopProxy> db_thread) 22 : base::RefCountedDeleteOnMessageLoop<TokenWebDataBackend>(db_thread) { 23 } 24 25 WebDatabase::State RemoveAllTokens(WebDatabase* db) { 26 if (TokenServiceTable::FromWebDatabase(db)->RemoveAllTokens()) { 27 return WebDatabase::COMMIT_NEEDED; 28 } 29 return WebDatabase::COMMIT_NOT_NEEDED; 30 } 31 32 WebDatabase::State RemoveTokenForService( 33 const std::string& service, WebDatabase* db) { 34 if (TokenServiceTable::FromWebDatabase(db) 35 ->RemoveTokenForService(service)) { 36 return WebDatabase::COMMIT_NEEDED; 37 } 38 return WebDatabase::COMMIT_NOT_NEEDED; 39 } 40 41 WebDatabase::State SetTokenForService( 42 const std::string& service, const std::string& token, WebDatabase* db) { 43 if (TokenServiceTable::FromWebDatabase(db)->SetTokenForService(service, 44 token)) { 45 return WebDatabase::COMMIT_NEEDED; 46 } 47 return WebDatabase::COMMIT_NOT_NEEDED; 48 } 49 50 scoped_ptr<WDTypedResult> GetAllTokens(WebDatabase* db) { 51 std::map<std::string, std::string> map; 52 TokenServiceTable::FromWebDatabase(db)->GetAllTokens(&map); 53 return scoped_ptr<WDTypedResult>( 54 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map)); 55 } 56 57 protected: 58 virtual ~TokenWebDataBackend() { 59 } 60 61 private: 62 friend class base::RefCountedDeleteOnMessageLoop<TokenWebDataBackend>; 63 friend class base::DeleteHelper<TokenWebDataBackend>; 64}; 65 66TokenWebData::TokenWebData(scoped_refptr<WebDatabaseService> wdbs, 67 scoped_refptr<base::MessageLoopProxy> ui_thread, 68 scoped_refptr<base::MessageLoopProxy> db_thread, 69 const ProfileErrorCallback& callback) 70 : WebDataServiceBase(wdbs, callback, ui_thread), 71 token_backend_(new TokenWebDataBackend(db_thread)) { 72} 73 74void TokenWebData::SetTokenForService(const std::string& service, 75 const std::string& token) { 76 wdbs_->ScheduleDBTask(FROM_HERE, 77 Bind(&TokenWebDataBackend::SetTokenForService, token_backend_, 78 service, token)); 79} 80 81void TokenWebData::RemoveAllTokens() { 82 wdbs_->ScheduleDBTask(FROM_HERE, 83 Bind(&TokenWebDataBackend::RemoveAllTokens, token_backend_)); 84} 85 86void TokenWebData::RemoveTokenForService(const std::string& service) { 87 wdbs_->ScheduleDBTask(FROM_HERE, 88 Bind(&TokenWebDataBackend::RemoveTokenForService, token_backend_, 89 service)); 90} 91 92// Null on failure. Success is WDResult<std::string> 93WebDataServiceBase::Handle TokenWebData::GetAllTokens( 94 WebDataServiceConsumer* consumer) { 95 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, 96 Bind(&TokenWebDataBackend::GetAllTokens, token_backend_), consumer); 97} 98 99TokenWebData::TokenWebData(scoped_refptr<base::MessageLoopProxy> ui_thread, 100 scoped_refptr<base::MessageLoopProxy> db_thread) 101 : WebDataServiceBase(NULL, ProfileErrorCallback(), ui_thread), 102 token_backend_(new TokenWebDataBackend(db_thread)) { 103} 104 105TokenWebData::~TokenWebData() { 106} 107