Home | History | Annotate | Download | only in webdata
      1 // 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 
      7 #include "base/bind.h"
      8 #include "base/stl_util.h"
      9 #include "chrome/browser/webdata/token_service_table.h"
     10 #include "components/webdata/common/web_database_service.h"
     11 #include "content/public/browser/browser_thread.h"
     12 
     13 using base::Bind;
     14 using base::Time;
     15 using content::BrowserThread;
     16 
     17 class 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 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 struct BrowserThread::DeleteOnThread<BrowserThread::DB>;
     63   friend class base::DeleteHelper<TokenWebDataBackend>;
     64   // We have to friend RCTS<> so WIN shared-lib build is happy
     65   // (http://crbug/112250).
     66   friend class base::RefCountedThreadSafe<TokenWebDataBackend,
     67                                           BrowserThread::DeleteOnDBThread>;
     68 
     69 };
     70 
     71 TokenWebData::TokenWebData(scoped_refptr<WebDatabaseService> wdbs,
     72                                const ProfileErrorCallback& callback)
     73     : WebDataServiceBase(wdbs, callback,
     74           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
     75       token_backend_(new TokenWebDataBackend()) {
     76 }
     77 
     78 void TokenWebData::SetTokenForService(const std::string& service,
     79                                       const std::string& token) {
     80   wdbs_->ScheduleDBTask(FROM_HERE,
     81       Bind(&TokenWebDataBackend::SetTokenForService, token_backend_,
     82            service, token));
     83 }
     84 
     85 void TokenWebData::RemoveAllTokens() {
     86   wdbs_->ScheduleDBTask(FROM_HERE,
     87       Bind(&TokenWebDataBackend::RemoveAllTokens, token_backend_));
     88 }
     89 
     90 void TokenWebData::RemoveTokenForService(const std::string& service) {
     91   wdbs_->ScheduleDBTask(FROM_HERE,
     92       Bind(&TokenWebDataBackend::RemoveTokenForService, token_backend_,
     93            service));
     94 }
     95 
     96 // Null on failure. Success is WDResult<std::string>
     97 WebDataServiceBase::Handle TokenWebData::GetAllTokens(
     98     WebDataServiceConsumer* consumer) {
     99   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
    100       Bind(&TokenWebDataBackend::GetAllTokens, token_backend_), consumer);
    101 }
    102 
    103 TokenWebData::TokenWebData()
    104     : WebDataServiceBase(NULL, ProfileErrorCallback(),
    105           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)),
    106       token_backend_(new TokenWebDataBackend()) {
    107 }
    108 
    109 TokenWebData::~TokenWebData() {
    110 }
    111