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(ENABLE_WEB_INTENTS)
    152   //////////////////////////////////////////////////////////////////////////////
    153   //
    154   // Web Intents
    155   //
    156   //////////////////////////////////////////////////////////////////////////////
    157 
    158   // Adds a web intent service registration.
    159   void AddWebIntentService(const webkit_glue::WebIntentServiceData& service);
    160 
    161   // Removes a web intent service registration.
    162   void RemoveWebIntentService(const webkit_glue::WebIntentServiceData& service);
    163 
    164   // Get all web intent services registered for the specified |action|.
    165   // |consumer| must not be NULL.
    166   Handle GetWebIntentServicesForAction(const string16& action,
    167                                        WebDataServiceConsumer* consumer);
    168 
    169   // Get all web intent services registered using the specified |service_url|.
    170   // |consumer| must not be NULL.
    171   Handle GetWebIntentServicesForURL(const string16& service_url,
    172                                     WebDataServiceConsumer* consumer);
    173 
    174   // Get all web intent services registered. |consumer| must not be NULL.
    175   Handle GetAllWebIntentServices(WebDataServiceConsumer* consumer);
    176 
    177   // Adds a default web intent service entry.
    178   void AddDefaultWebIntentService(const DefaultWebIntentService& service);
    179 
    180   // Removes a default web intent service entry. Removes entries matching
    181   // the |action|, |type|, and |url_pattern| of |service|.
    182   void RemoveDefaultWebIntentService(const DefaultWebIntentService& service);
    183 
    184   // Removes all default web intent service entries associated with
    185   // |service_url|
    186   void RemoveWebIntentServiceDefaults(const GURL& service_url);
    187 
    188     // Get a list of all web intent service defaults for the given |action|.
    189   // |consumer| must not be null.
    190   Handle GetDefaultWebIntentServicesForAction(const string16& action,
    191                                               WebDataServiceConsumer* consumer);
    192 
    193   // Get a list of all registered web intent service defaults.
    194   // |consumer| must not be null.
    195   Handle GetAllDefaultWebIntentServices(WebDataServiceConsumer* consumer);
    196 #endif
    197 
    198 #if defined(OS_WIN)
    199   //////////////////////////////////////////////////////////////////////////////
    200   //
    201   // IE7/8 Password Access (used by PasswordStoreWin - do not use elsewhere)
    202   //
    203   //////////////////////////////////////////////////////////////////////////////
    204 
    205   // Adds |info| to the list of imported passwords from ie7/ie8.
    206   void AddIE7Login(const IE7PasswordInfo& info);
    207 
    208   // Removes |info| from the list of imported passwords from ie7/ie8.
    209   void RemoveIE7Login(const IE7PasswordInfo& info);
    210 
    211   // Get the login matching the information in |info|. |consumer| will be
    212   // notified when the request is done. The result is of type
    213   // WDResult<IE7PasswordInfo>.
    214   // If there is no match, the fields of the IE7PasswordInfo will be empty.
    215   Handle GetIE7Login(const IE7PasswordInfo& info,
    216                      WebDataServiceConsumer* consumer);
    217 #endif  // defined(OS_WIN)
    218 
    219  protected:
    220   // For unit tests, passes a null callback.
    221   WebDataService();
    222 
    223   virtual ~WebDataService();
    224 
    225  private:
    226   //////////////////////////////////////////////////////////////////////////////
    227   //
    228   // The following methods are only invoked on the DB thread.
    229   //
    230   //////////////////////////////////////////////////////////////////////////////
    231 
    232   //////////////////////////////////////////////////////////////////////////////
    233   //
    234   // Keywords.
    235   //
    236   //////////////////////////////////////////////////////////////////////////////
    237   WebDatabase::State AddKeywordImpl(
    238       const TemplateURLData& data, WebDatabase* db);
    239   WebDatabase::State RemoveKeywordImpl(
    240       TemplateURLID id, WebDatabase* db);
    241   WebDatabase::State UpdateKeywordImpl(
    242       const TemplateURLData& data, WebDatabase* db);
    243   scoped_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
    244   WebDatabase::State SetDefaultSearchProviderImpl(
    245       TemplateURLID r, WebDatabase* db);
    246   WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
    247 
    248   //////////////////////////////////////////////////////////////////////////////
    249   //
    250   // Web Apps.
    251   //
    252   //////////////////////////////////////////////////////////////////////////////
    253 
    254   WebDatabase::State SetWebAppImageImpl(const GURL& app_url,
    255       const SkBitmap& image, WebDatabase* db);
    256   WebDatabase::State SetWebAppHasAllImagesImpl(const GURL& app_url,
    257       bool has_all_images, WebDatabase* db);
    258   WebDatabase::State RemoveWebAppImpl(const GURL& app_url, WebDatabase* db);
    259   scoped_ptr<WDTypedResult> GetWebAppImagesImpl(
    260       const GURL& app_url, WebDatabase* db);
    261 
    262 #if defined(ENABLE_WEB_INTENTS)
    263   //////////////////////////////////////////////////////////////////////////////
    264   //
    265   // Web Intents.
    266   //
    267   //////////////////////////////////////////////////////////////////////////////
    268   WebDatabase::State AddWebIntentServiceImpl(
    269       const webkit_glue::WebIntentServiceData& service);
    270   WebDatabase::State RemoveWebIntentServiceImpl(
    271       const webkit_glue::WebIntentServiceData& service);
    272   scoped_ptr<WDTypedResult> GetWebIntentServicesImpl(const string16& action);
    273   scoped_ptr<WDTypedResult> GetWebIntentServicesForURLImpl(
    274       const string16& service_url);
    275   scoped_ptr<WDTypedResult> GetAllWebIntentServicesImpl();
    276   WebDatabase::State AddDefaultWebIntentServiceImpl(
    277       const DefaultWebIntentService& service);
    278   WebDatabase::State RemoveDefaultWebIntentServiceImpl(
    279       const DefaultWebIntentService& service);
    280   WebDatabase::State RemoveWebIntentServiceDefaultsImpl(
    281       const GURL& service_url);
    282   scoped_ptr<WDTypedResult> GetDefaultWebIntentServicesForActionImpl(
    283       const string16& action);
    284   scoped_ptr<WDTypedResult> GetAllDefaultWebIntentServicesImpl();
    285 #endif
    286 
    287 #if defined(OS_WIN)
    288   //////////////////////////////////////////////////////////////////////////////
    289   //
    290   // Password manager.
    291   //
    292   //////////////////////////////////////////////////////////////////////////////
    293   WebDatabase::State AddIE7LoginImpl(
    294       const IE7PasswordInfo& info, WebDatabase* db);
    295   WebDatabase::State RemoveIE7LoginImpl(
    296       const IE7PasswordInfo& info, WebDatabase* db);
    297   scoped_ptr<WDTypedResult> GetIE7LoginImpl(
    298       const IE7PasswordInfo& info, WebDatabase* db);
    299 #endif  // defined(OS_WIN)
    300 
    301   DISALLOW_COPY_AND_ASSIGN(WebDataService);
    302 };
    303 
    304 #endif  // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__
    305