Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / components / signin / core / browser / webdata / token_web_data.cc
blobfc4b82d208f675560d7e3064ed7c57ad32582a72
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"
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"
14 using base::Bind;
15 using base::Time;
17 class TokenWebDataBackend
18 : public base::RefCountedDeleteOnMessageLoop<TokenWebDataBackend> {
20 public:
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,
44 token)) {
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));
57 protected:
58 virtual ~TokenWebDataBackend() {
61 private:
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_,
78 service, token));
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_,
89 service));
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() {