Home | History | Annotate | Download | only in webdata
      1 // Copyright (c) 2012 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/web_data_service.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/stl_util.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/search_engines/template_url.h"
     11 #include "chrome/browser/webdata/keyword_table.h"
     12 #include "chrome/browser/webdata/logins_table.h"
     13 #include "chrome/browser/webdata/token_service_table.h"
     14 #include "chrome/browser/webdata/web_apps_table.h"
     15 #include "chrome/browser/webdata/web_intents_table.h"
     16 #include "components/webdata/common/web_database_service.h"
     17 #include "content/public/browser/browser_thread.h"
     18 #include "content/public/browser/notification_details.h"
     19 #include "content/public/browser/notification_service.h"
     20 #include "content/public/browser/notification_source.h"
     21 #include "third_party/skia/include/core/SkBitmap.h"
     22 
     23 ////////////////////////////////////////////////////////////////////////////////
     24 //
     25 // WebDataService implementation.
     26 //
     27 ////////////////////////////////////////////////////////////////////////////////
     28 
     29 using base::Bind;
     30 using base::Time;
     31 using content::BrowserThread;
     32 
     33 WDAppImagesResult::WDAppImagesResult() : has_all_images(false) {}
     34 
     35 WDAppImagesResult::~WDAppImagesResult() {}
     36 
     37 WDKeywordsResult::WDKeywordsResult()
     38   : default_search_provider_id(0),
     39     builtin_keyword_version(0) {
     40 }
     41 
     42 WDKeywordsResult::~WDKeywordsResult() {}
     43 
     44 WebDataService::WebDataService(scoped_refptr<WebDatabaseService> wdbs,
     45                                const ProfileErrorCallback& callback)
     46     : WebDataServiceBase(wdbs, callback,
     47           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)) {
     48 }
     49 
     50 //////////////////////////////////////////////////////////////////////////////
     51 //
     52 // Keywords.
     53 //
     54 //////////////////////////////////////////////////////////////////////////////
     55 
     56 void WebDataService::AddKeyword(const TemplateURLData& data) {
     57   wdbs_->ScheduleDBTask(
     58       FROM_HERE, Bind(&WebDataService::AddKeywordImpl, this, data));
     59 }
     60 
     61 void WebDataService::RemoveKeyword(TemplateURLID id) {
     62   wdbs_->ScheduleDBTask(
     63       FROM_HERE, Bind(&WebDataService::RemoveKeywordImpl, this, id));
     64 }
     65 
     66 void WebDataService::UpdateKeyword(const TemplateURLData& data) {
     67   wdbs_->ScheduleDBTask(
     68       FROM_HERE, Bind(&WebDataService::UpdateKeywordImpl, this, data));
     69 }
     70 
     71 WebDataServiceBase::Handle WebDataService::GetKeywords(
     72     WebDataServiceConsumer* consumer) {
     73   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
     74       Bind(&WebDataService::GetKeywordsImpl, this), consumer);
     75 }
     76 
     77 void WebDataService::SetDefaultSearchProvider(const TemplateURL* url) {
     78   wdbs_->ScheduleDBTask(FROM_HERE,
     79       Bind(&WebDataService::SetDefaultSearchProviderImpl, this,
     80            url ? url->id() : 0));
     81 }
     82 
     83 void WebDataService::SetBuiltinKeywordVersion(int version) {
     84   wdbs_->ScheduleDBTask(FROM_HERE,
     85       Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version));
     86 }
     87 
     88 //////////////////////////////////////////////////////////////////////////////
     89 //
     90 // Web Apps
     91 //
     92 //////////////////////////////////////////////////////////////////////////////
     93 
     94 void WebDataService::SetWebAppImage(const GURL& app_url,
     95                                     const SkBitmap& image) {
     96   wdbs_->ScheduleDBTask(FROM_HERE,
     97       Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image));
     98 }
     99 
    100 void WebDataService::SetWebAppHasAllImages(const GURL& app_url,
    101                                            bool has_all_images) {
    102   wdbs_->ScheduleDBTask(FROM_HERE,
    103       Bind(&WebDataService::SetWebAppHasAllImagesImpl, this, app_url,
    104            has_all_images));
    105 }
    106 
    107 void WebDataService::RemoveWebApp(const GURL& app_url) {
    108   wdbs_->ScheduleDBTask(FROM_HERE,
    109       Bind(&WebDataService::RemoveWebAppImpl, this, app_url));
    110 }
    111 
    112 WebDataServiceBase::Handle WebDataService::GetWebAppImages(
    113     const GURL& app_url, WebDataServiceConsumer* consumer) {
    114   return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
    115       Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer);
    116 }
    117 
    118 ////////////////////////////////////////////////////////////////////////////////
    119 
    120 WebDataService::WebDataService()
    121     : WebDataServiceBase(NULL, ProfileErrorCallback(),
    122           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)) {
    123 }
    124 
    125 WebDataService::~WebDataService() {
    126 }
    127 
    128 ////////////////////////////////////////////////////////////////////////////////
    129 //
    130 // Keywords implementation.
    131 //
    132 ////////////////////////////////////////////////////////////////////////////////
    133 
    134 WebDatabase::State WebDataService::AddKeywordImpl(
    135     const TemplateURLData& data, WebDatabase* db) {
    136   KeywordTable::FromWebDatabase(db)->AddKeyword(data);
    137   return WebDatabase::COMMIT_NEEDED;
    138 }
    139 
    140 WebDatabase::State WebDataService::RemoveKeywordImpl(
    141     TemplateURLID id, WebDatabase* db) {
    142   DCHECK(id);
    143   KeywordTable::FromWebDatabase(db)->RemoveKeyword(id);
    144   return WebDatabase::COMMIT_NEEDED;
    145 }
    146 
    147 WebDatabase::State WebDataService::UpdateKeywordImpl(
    148     const TemplateURLData& data, WebDatabase* db) {
    149   if (!KeywordTable::FromWebDatabase(db)->UpdateKeyword(data)) {
    150     NOTREACHED();
    151     return WebDatabase::COMMIT_NOT_NEEDED;
    152   }
    153  return WebDatabase::COMMIT_NEEDED;
    154 }
    155 
    156 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl(WebDatabase* db) {
    157   WDKeywordsResult result;
    158   KeywordTable::FromWebDatabase(db)->GetKeywords(&result.keywords);
    159   result.default_search_provider_id =
    160       KeywordTable::FromWebDatabase(db)->GetDefaultSearchProviderID();
    161   result.builtin_keyword_version =
    162       KeywordTable::FromWebDatabase(db)->GetBuiltinKeywordVersion();
    163   return scoped_ptr<WDTypedResult>(
    164       new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result));
    165 }
    166 
    167 WebDatabase::State WebDataService::SetDefaultSearchProviderImpl(
    168     TemplateURLID id, WebDatabase* db) {
    169   if (!KeywordTable::FromWebDatabase(db)->SetDefaultSearchProviderID(id)) {
    170     NOTREACHED();
    171     return WebDatabase::COMMIT_NOT_NEEDED;
    172   }
    173   return WebDatabase::COMMIT_NEEDED;
    174 }
    175 
    176 WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl(
    177     int version, WebDatabase* db) {
    178   if (!KeywordTable::FromWebDatabase(db)->SetBuiltinKeywordVersion(version)) {
    179     NOTREACHED();
    180     return WebDatabase::COMMIT_NOT_NEEDED;
    181   }
    182   return WebDatabase::COMMIT_NEEDED;
    183 }
    184 
    185 ////////////////////////////////////////////////////////////////////////////////
    186 //
    187 // Web Apps implementation.
    188 //
    189 ////////////////////////////////////////////////////////////////////////////////
    190 
    191 WebDatabase::State WebDataService::SetWebAppImageImpl(
    192     const GURL& app_url, const SkBitmap& image, WebDatabase* db) {
    193   WebAppsTable::FromWebDatabase(db)->SetWebAppImage(app_url, image);
    194   return WebDatabase::COMMIT_NEEDED;
    195 }
    196 
    197 WebDatabase::State WebDataService::SetWebAppHasAllImagesImpl(
    198     const GURL& app_url, bool has_all_images, WebDatabase* db) {
    199   WebAppsTable::FromWebDatabase(db)->SetWebAppHasAllImages(app_url,
    200                                                            has_all_images);
    201   return WebDatabase::COMMIT_NEEDED;
    202 }
    203 
    204 WebDatabase::State WebDataService::RemoveWebAppImpl(
    205     const GURL& app_url, WebDatabase* db) {
    206   WebAppsTable::FromWebDatabase(db)->RemoveWebApp(app_url);
    207   return WebDatabase::COMMIT_NEEDED;
    208 }
    209 
    210 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl(
    211     const GURL& app_url, WebDatabase* db) {
    212   WDAppImagesResult result;
    213   result.has_all_images =
    214       WebAppsTable::FromWebDatabase(db)->GetWebAppHasAllImages(app_url);
    215   WebAppsTable::FromWebDatabase(db)->GetWebAppImages(app_url, &result.images);
    216   return scoped_ptr<WDTypedResult>(
    217       new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result));
    218 }
    219