blob: e659b67557b004043e8361d875f7fe7ba24e844a [file] [log] [blame]
[email protected]b69d6312013-05-27 21:07:361// Copyright 2013 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 "chrome/browser/webdata/token_web_data.h"
6
[email protected]d57ec152013-06-17 15:33:367#include "base/bind.h"
[email protected]b69d6312013-05-27 21:07:368#include "base/stl_util.h"
9#include "chrome/browser/webdata/token_service_table.h"
10#include "components/webdata/common/web_database_service.h"
[email protected]d57ec152013-06-17 15:33:3611#include "content/public/browser/browser_thread.h"
[email protected]b69d6312013-05-27 21:07:3612
13using base::Bind;
14using base::Time;
15using content::BrowserThread;
16
17class TokenWebDataBackend
18 : public base::RefCountedThreadSafe<TokenWebDataBackend,
19 BrowserThread::DeleteOnDBThread> {
20
21 public:
22 TokenWebDataBackend() {
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 SetTokenForService(
33 const std::string& service, const std::string& token, WebDatabase* db) {
34 if (TokenServiceTable::FromWebDatabase(db)->SetTokenForService(service,
35 token)) {
36 return WebDatabase::COMMIT_NEEDED;
37 }
38 return WebDatabase::COMMIT_NOT_NEEDED;
39 }
40
41 scoped_ptr<WDTypedResult> GetAllTokens(WebDatabase* db) {
42 std::map<std::string, std::string> map;
43 TokenServiceTable::FromWebDatabase(db)->GetAllTokens(&map);
44 return scoped_ptr<WDTypedResult>(
45 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map));
46 }
47
48 protected:
49 virtual ~TokenWebDataBackend() {
50 }
51
52 private:
53 friend struct BrowserThread::DeleteOnThread<BrowserThread::DB>;
54 friend class base::DeleteHelper<TokenWebDataBackend>;
55 // We have to friend RCTS<> so WIN shared-lib build is happy
56 // (http://crbug/112250).
57 friend class base::RefCountedThreadSafe<TokenWebDataBackend,
58 BrowserThread::DeleteOnDBThread>;
59
60};
61
62TokenWebData::TokenWebData(scoped_refptr<WebDatabaseService> wdbs,
63 const ProfileErrorCallback& callback)
[email protected]d57ec152013-06-17 15:33:3664 : WebDataServiceBase(wdbs, callback,
65 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
[email protected]b69d6312013-05-27 21:07:3666 token_backend_(new TokenWebDataBackend()) {
67}
68
69void TokenWebData::SetTokenForService(const std::string& service,
70 const std::string& token) {
71 wdbs_->ScheduleDBTask(FROM_HERE,
72 Bind(&TokenWebDataBackend::SetTokenForService, token_backend_,
73 service, token));
74}
75
76void TokenWebData::RemoveAllTokens() {
77 wdbs_->ScheduleDBTask(FROM_HERE,
78 Bind(&TokenWebDataBackend::RemoveAllTokens, token_backend_));
79}
80
81// Null on failure. Success is WDResult<std::string>
82WebDataServiceBase::Handle TokenWebData::GetAllTokens(
83 WebDataServiceConsumer* consumer) {
84 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
85 Bind(&TokenWebDataBackend::GetAllTokens, token_backend_), consumer);
86}
87
88TokenWebData::TokenWebData()
[email protected]d57ec152013-06-17 15:33:3689 : WebDataServiceBase(NULL, ProfileErrorCallback(),
90 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
[email protected]b69d6312013-05-27 21:07:3691 token_backend_(new TokenWebDataBackend()) {
92}
[email protected]ce5b58052013-06-12 00:26:5193
94TokenWebData::~TokenWebData() {
95}