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.
5 #include "components/signin/core/browser/webdata/token_web_data.h"
8 #include "base/memory/ref_counted_delete_on_message_loop.h"
9 #include "base/single_thread_task_runner.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"
17 class TokenWebDataBackend
18 : public base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
> {
21 TokenWebDataBackend(scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
)
22 : base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
>(db_thread
) {}
24 WebDatabase::State
RemoveAllTokens(WebDatabase
* db
) {
25 if (TokenServiceTable::FromWebDatabase(db
)->RemoveAllTokens()) {
26 return WebDatabase::COMMIT_NEEDED
;
28 return WebDatabase::COMMIT_NOT_NEEDED
;
31 WebDatabase::State
RemoveTokenForService(
32 const std::string
& service
, WebDatabase
* db
) {
33 if (TokenServiceTable::FromWebDatabase(db
)
34 ->RemoveTokenForService(service
)) {
35 return WebDatabase::COMMIT_NEEDED
;
37 return WebDatabase::COMMIT_NOT_NEEDED
;
40 WebDatabase::State
SetTokenForService(
41 const std::string
& service
, const std::string
& token
, WebDatabase
* db
) {
42 if (TokenServiceTable::FromWebDatabase(db
)->SetTokenForService(service
,
44 return WebDatabase::COMMIT_NEEDED
;
46 return WebDatabase::COMMIT_NOT_NEEDED
;
49 scoped_ptr
<WDTypedResult
> GetAllTokens(WebDatabase
* db
) {
50 std::map
<std::string
, std::string
> map
;
51 TokenServiceTable::FromWebDatabase(db
)->GetAllTokens(&map
);
52 return scoped_ptr
<WDTypedResult
>(
53 new WDResult
<std::map
<std::string
, std::string
> >(TOKEN_RESULT
, map
));
57 virtual ~TokenWebDataBackend() {
61 friend class base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
>;
62 friend class base::DeleteHelper
<TokenWebDataBackend
>;
65 TokenWebData::TokenWebData(
66 scoped_refptr
<WebDatabaseService
> wdbs
,
67 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread
,
68 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
,
69 const ProfileErrorCallback
& callback
)
70 : WebDataServiceBase(wdbs
, callback
, ui_thread
),
71 token_backend_(new TokenWebDataBackend(db_thread
)) {
74 void TokenWebData::SetTokenForService(const std::string
& service
,
75 const std::string
& token
) {
76 wdbs_
->ScheduleDBTask(FROM_HERE
,
77 Bind(&TokenWebDataBackend::SetTokenForService
, token_backend_
,
81 void TokenWebData::RemoveAllTokens() {
82 wdbs_
->ScheduleDBTask(FROM_HERE
,
83 Bind(&TokenWebDataBackend::RemoveAllTokens
, token_backend_
));
86 void TokenWebData::RemoveTokenForService(const std::string
& service
) {
87 wdbs_
->ScheduleDBTask(FROM_HERE
,
88 Bind(&TokenWebDataBackend::RemoveTokenForService
, token_backend_
,
92 // Null on failure. Success is WDResult<std::string>
93 WebDataServiceBase::Handle
TokenWebData::GetAllTokens(
94 WebDataServiceConsumer
* consumer
) {
95 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
96 Bind(&TokenWebDataBackend::GetAllTokens
, token_backend_
), consumer
);
99 TokenWebData::TokenWebData(
100 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread
,
101 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread
)
102 : WebDataServiceBase(NULL
, ProfileErrorCallback(), ui_thread
),
103 token_backend_(new TokenWebDataBackend(db_thread
)) {
106 TokenWebData::~TokenWebData() {