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 // Chromium settings and storage represent user-selected preferences and
      6 // information and MUST not be extracted, overwritten or modified except
      7 // through Chromium defined APIs.
      8 
      9 #ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__
     10 #define CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__
     11 
     12 #include <map>
     13 #include <string>
     14 #include <vector>
     15 
     16 #include "base/callback_forward.h"
     17 #include "base/files/file_path.h"
     18 #include "base/location.h"
     19 #include "base/memory/ref_counted.h"
     20 #include "base/sequenced_task_runner_helpers.h"
     21 #include "chrome/browser/search_engines/template_url.h"
     22 #include "chrome/browser/search_engines/template_url_id.h"
     23 #include "chrome/browser/webdata/keyword_table.h"
     24 #include "components/webdata/common/web_data_results.h"
     25 #include "components/webdata/common/web_data_service_base.h"
     26 #include "components/webdata/common/web_data_service_consumer.h"
     27 #include "components/webdata/common/web_database.h"
     28 
     29 struct DefaultWebIntentService;
     30 class GURL;
     31 #if defined(OS_WIN)
     32 struct IE7PasswordInfo;
     33 #endif
     34 class Profile;
     35 class SkBitmap;
     36 class WebDatabaseService;
     37 
     38 namespace base {
     39 class Thread;
     40 }
     41 
     42 namespace content {
     43 class BrowserContext;
     44 }
     45 
     46 namespace webkit_glue {
     47 struct WebIntentServiceData;
     48 }
     49 
     50 ////////////////////////////////////////////////////////////////////////////////
     51 //
     52 // WebDataService is a generic data repository for meta data associated with
     53 // web pages. All data is retrieved and archived in an asynchronous way.
     54 //
     55 // All requests return a handle. The handle can be used to cancel the request.
     56 //
     57 ////////////////////////////////////////////////////////////////////////////////
     58 
     59 
     60 ////////////////////////////////////////////////////////////////////////////////
     61 //
     62 // WebDataService results
     63 //
     64 ////////////////////////////////////////////////////////////////////////////////
     65 
     66 typedef base::Callback<scoped_ptr<WDTypedResult>(void)> ResultTask;
     67 
     68 // Result from GetWebAppImages.
     69 struct WDAppImagesResult {
     70   WDAppImagesResult();
     71   ~WDAppImagesResult();
     72 
     73   // True if SetWebAppHasAllImages(true) was invoked.
     74   bool has_all_images;
     75 
     76   // The images, may be empty.
     77   std::vector<SkBitmap> images;
     78 };
     79 
     80 struct WDKeywordsResult {
     81   WDKeywordsResult();
     82   ~WDKeywordsResult();
     83 
     84   KeywordTable::Keywords keywords;
     85   // Identifies the ID of the TemplateURL that is the default search. A value of
     86   // 0 indicates there is no default search provider.
     87   int64 default_search_provider_id;
     88   // Version of the built-in keywords. A value of 0 indicates a first run.
     89   int builtin_keyword_version;
     90 };
     91 
     92 class WebDataServiceConsumer;
     93 
     94 class WebDataService : public WebDataServiceBase {
     95  public:
     96   // Retrieve a WebDataService for the given context.
     97   static scoped_refptr<WebDataService> FromBrowserContext(
     98       content::BrowserContext* context);
     99 
    100   WebDataService(scoped_refptr<WebDatabaseService> wdbs,
    101                  const ProfileErrorCallback& callback);
    102 
    103   //////////////////////////////////////////////////////////////////////////////
    104   //
    105   // Keywords
    106   //
    107   //////////////////////////////////////////////////////////////////////////////
    108 
    109   // As the database processes requests at a later date, all deletion is
    110   // done on the background thread.
    111   //
    112   // Many of the keyword related methods do not return a handle. This is because
    113   // the caller (TemplateURLService) does not need to know when the request is
    114   // done.
    115 
    116   void AddKeyword(const TemplateURLData& data);
    117   void RemoveKeyword(TemplateURLID id);
    118   void UpdateKeyword(const TemplateURLData& data);
    119 
    120   // Fetches the keywords.
    121   // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
    122   Handle GetKeywords(WebDataServiceConsumer* consumer);
    123 
    124   // Sets the keywords used for the default search provider.
    125   void SetDefaultSearchProvider(const TemplateURL* url);
    126 
    127   // Sets the version of the builtin keywords.
    128   void SetBuiltinKeywordVersion(int version);
    129 
    130   //////////////////////////////////////////////////////////////////////////////
    131   //
    132   // Web Apps
    133   //
    134   //////////////////////////////////////////////////////////////////////////////
    135 
    136   // Sets the image for the specified web app. A web app can have any number of
    137   // images, but only one at a particular size. If there was an image for the
    138   // web app at the size of the given image it is replaced.
    139   void SetWebAppImage(const GURL& app_url, const SkBitmap& image);
    140 
    141   // Sets whether all the images have been downloaded for the specified web app.
    142   void SetWebAppHasAllImages(const GURL& app_url, bool has_all_images);
    143 
    144   // Removes all images for the specified web app.
    145   void RemoveWebApp(const GURL& app_url);
    146 
    147   // Fetches the images and whether all images have been downloaded for the
    148   // specified web app.
    149   Handle GetWebAppImages(const GURL& app_url, WebDataServiceConsumer* consumer);
    150 
    151 #if defined(OS_WIN)
    152   //////////////////////////////////////////////////////////////////////////////
    153   //
    154   // IE7/8 Password Access (used by PasswordStoreWin - do not use elsewhere)
    155   //
    156   //////////////////////////////////////////////////////////////////////////////
    157 
    158   // Adds |info| to the list of imported passwords from ie7/ie8.
    159   void AddIE7Login(const IE7PasswordInfo& info);
    160 
    161   // Removes |info| from the list of imported passwords from ie7/ie8.
    162   void RemoveIE7Login(const IE7PasswordInfo& info);
    163 
    164   // Get the login matching the information in |info|. |consumer| will be
    165   // notified when the request is done. The result is of type
    166   // WDResult<IE7PasswordInfo>.
    167   // If there is no match, the fields of the IE7PasswordInfo will be empty.
    168   Handle GetIE7Login(const IE7PasswordInfo& info,
    169                      WebDataServiceConsumer* consumer);
    170 #endif  // defined(OS_WIN)
    171 
    172  protected:
    173   // For unit tests, passes a null callback.
    174   WebDataService();
    175 
    176   virtual ~WebDataService();
    177 
    178  private:
    179   //////////////////////////////////////////////////////////////////////////////
    180   //
    181   // The following methods are only invoked on the DB thread.
    182   //
    183   //////////////////////////////////////////////////////////////////////////////
    184 
    185   //////////////////////////////////////////////////////////////////////////////
    186   //
    187   // Keywords.
    188   //
    189   //////////////////////////////////////////////////////////////////////////////
    190   WebDatabase::State AddKeywordImpl(
    191       const TemplateURLData& data, WebDatabase* db);
    192   WebDatabase::State RemoveKeywordImpl(
    193       TemplateURLID id, WebDatabase* db);
    194   WebDatabase::State UpdateKeywordImpl(
    195       const TemplateURLData& data, WebDatabase* db);
    196   scoped_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
    197   WebDatabase::State SetDefaultSearchProviderImpl(
    198       TemplateURLID r, WebDatabase* db);
    199   WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
    200 
    201   //////////////////////////////////////////////////////////////////////////////
    202   //
    203   // Web Apps.
    204   //
    205   //////////////////////////////////////////////////////////////////////////////
    206 
    207   WebDatabase::State SetWebAppImageImpl(const GURL& app_url,
    208       const SkBitmap& image, WebDatabase* db);
    209   WebDatabase::State SetWebAppHasAllImagesImpl(const GURL& app_url,
    210       bool has_all_images, WebDatabase* db);
    211   WebDatabase::State RemoveWebAppImpl(const GURL& app_url, WebDatabase* db);
    212   scoped_ptr<WDTypedResult> GetWebAppImagesImpl(
    213       const GURL& app_url, WebDatabase* db);
    214 
    215 #if defined(ENABLE_WEB_INTENTS)
    216   //////////////////////////////////////////////////////////////////////////////
    217   //
    218   // Web Intents.
    219   //
    220   //////////////////////////////////////////////////////////////////////////////
    221   WebDatabase::State AddWebIntentServiceImpl(
    222       const webkit_glue::WebIntentServiceData& service);
    223   WebDatabase::State RemoveWebIntentServiceImpl(
    224       const webkit_glue::WebIntentServiceData& service);
    225   scoped_ptr<WDTypedResult> GetWebIntentServicesImpl(
    226       const base::string16& action);
    227   scoped_ptr<WDTypedResult> GetWebIntentServicesForURLImpl(
    228       const base::string16& service_url);
    229   scoped_ptr<WDTypedResult> GetAllWebIntentServicesImpl();
    230   WebDatabase::State AddDefaultWebIntentServiceImpl(
    231       const DefaultWebIntentService& service);
    232   WebDatabase::State RemoveDefaultWebIntentServiceImpl(
    233       const DefaultWebIntentService& service);
    234   WebDatabase::State RemoveWebIntentServiceDefaultsImpl(
    235       const GURL& service_url);
    236   scoped_ptr<WDTypedResult> GetDefaultWebIntentServicesForActionImpl(
    237       const base::string16& action);
    238   scoped_ptr<WDTypedResult> GetAllDefaultWebIntentServicesImpl();
    239 #endif
    240 
    241 #if defined(OS_WIN)
    242   //////////////////////////////////////////////////////////////////////////////
    243   //
    244   // Password manager.
    245   //
    246   //////////////////////////////////////////////////////////////////////////////
    247   WebDatabase::State AddIE7LoginImpl(
    248       const IE7PasswordInfo& info, WebDatabase* db);
    249   WebDatabase::State RemoveIE7LoginImpl(
    250       const IE7PasswordInfo& info, WebDatabase* db);
    251   scoped_ptr<WDTypedResult> GetIE7LoginImpl(
    252       const IE7PasswordInfo& info, WebDatabase* db);
    253 #endif  // defined(OS_WIN)
    254 
    255   DISALLOW_COPY_AND_ASSIGN(WebDataService);
    256 };
    257 
    258 #endif  // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__
    259