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/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"
17 class TokenWebDataBackend
18 : public base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
> {
21 TokenWebDataBackend(scoped_refptr
<base::MessageLoopProxy
> db_thread
)
22 : base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
>(db_thread
) {
25 WebDatabase::State
RemoveAllTokens(WebDatabase
* db
) {
26 if (TokenServiceTable::FromWebDatabase(db
)->RemoveAllTokens()) {
27 return WebDatabase::COMMIT_NEEDED
;
29 return WebDatabase::COMMIT_NOT_NEEDED
;
32 WebDatabase::State
RemoveTokenForService(
33 const std::string
& service
, WebDatabase
* db
) {
34 if (TokenServiceTable::FromWebDatabase(db
)
35 ->RemoveTokenForService(service
)) {
36 return WebDatabase::COMMIT_NEEDED
;
38 return WebDatabase::COMMIT_NOT_NEEDED
;
41 WebDatabase::State
SetTokenForService(
42 const std::string
& service
, const std::string
& token
, WebDatabase
* db
) {
43 if (TokenServiceTable::FromWebDatabase(db
)->SetTokenForService(service
,
45 return WebDatabase::COMMIT_NEEDED
;
47 return WebDatabase::COMMIT_NOT_NEEDED
;
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
));
58 virtual ~TokenWebDataBackend() {
62 friend class base::RefCountedDeleteOnMessageLoop
<TokenWebDataBackend
>;
63 friend class base::DeleteHelper
<TokenWebDataBackend
>;
66 TokenWebData::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
)) {
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(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
)) {
105 TokenWebData::~TokenWebData() {