• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/webdata/keyword_table.h"
23 #include "components/search_engines/template_url_id.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   // Instantiate this to turn on keyword batch mode on the provided |service|
97   // until the scoper is destroyed.  When batch mode is on, calls to any of the
98   // three keyword table modification functions below will result in locally
99   // queueing the operation; on setting this back to false, all the
100   // modifications will be performed at once.  This is a performance
101   // optimization; see comments on KeywordTable::PerformOperations().
102   //
103   // If multiple scopers are in-scope simultaneously, batch mode will only be
104   // exited when all are destroyed.  If |service| is NULL, the object will do
105   // nothing.
106   class KeywordBatchModeScoper {
107    public:
108     explicit KeywordBatchModeScoper(WebDataService* service);
109     ~KeywordBatchModeScoper();
110 
111    private:
112     WebDataService* service_;
113 
114     DISALLOW_COPY_AND_ASSIGN(KeywordBatchModeScoper);
115   };
116 
117   // Retrieve a WebDataService for the given context.
118   static scoped_refptr<WebDataService> FromBrowserContext(
119       content::BrowserContext* context);
120 
121   WebDataService(scoped_refptr<WebDatabaseService> wdbs,
122                  const ProfileErrorCallback& callback);
123 
124   //////////////////////////////////////////////////////////////////////////////
125   //
126   // Keywords
127   //
128   //////////////////////////////////////////////////////////////////////////////
129 
130   // As the database processes requests at a later date, all deletion is
131   // done on the background thread.
132   //
133   // Many of the keyword related methods do not return a handle. This is because
134   // the caller (TemplateURLService) does not need to know when the request is
135   // done.
136 
137   void AddKeyword(const TemplateURLData& data);
138   void RemoveKeyword(TemplateURLID id);
139   void UpdateKeyword(const TemplateURLData& data);
140 
141   // Fetches the keywords.
142   // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
143   Handle GetKeywords(WebDataServiceConsumer* consumer);
144 
145   // Sets the ID of the default search provider.
146   void SetDefaultSearchProviderID(TemplateURLID id);
147 
148   // Sets the version of the builtin keywords.
149   void SetBuiltinKeywordVersion(int version);
150 
151   //////////////////////////////////////////////////////////////////////////////
152   //
153   // Web Apps
154   //
155   //////////////////////////////////////////////////////////////////////////////
156 
157   // Sets the image for the specified web app. A web app can have any number of
158   // images, but only one at a particular size. If there was an image for the
159   // web app at the size of the given image it is replaced.
160   void SetWebAppImage(const GURL& app_url, const SkBitmap& image);
161 
162   // Sets whether all the images have been downloaded for the specified web app.
163   void SetWebAppHasAllImages(const GURL& app_url, bool has_all_images);
164 
165   // Removes all images for the specified web app.
166   void RemoveWebApp(const GURL& app_url);
167 
168   // Fetches the images and whether all images have been downloaded for the
169   // specified web app.
170   Handle GetWebAppImages(const GURL& app_url, WebDataServiceConsumer* consumer);
171 
172 #if defined(OS_WIN)
173   //////////////////////////////////////////////////////////////////////////////
174   //
175   // IE7/8 Password Access (used by PasswordStoreWin - do not use elsewhere)
176   //
177   //////////////////////////////////////////////////////////////////////////////
178 
179   // Adds |info| to the list of imported passwords from ie7/ie8.
180   void AddIE7Login(const IE7PasswordInfo& info);
181 
182   // Removes |info| from the list of imported passwords from ie7/ie8.
183   void RemoveIE7Login(const IE7PasswordInfo& info);
184 
185   // Get the login matching the information in |info|. |consumer| will be
186   // notified when the request is done. The result is of type
187   // WDResult<IE7PasswordInfo>.
188   // If there is no match, the fields of the IE7PasswordInfo will be empty.
189   Handle GetIE7Login(const IE7PasswordInfo& info,
190                      WebDataServiceConsumer* consumer);
191 #endif  // defined(OS_WIN)
192 
193  protected:
194   // For unit tests, passes a null callback.
195   WebDataService();
196 
197   virtual ~WebDataService();
198 
199  private:
200   // Called by the KeywordBatchModeScoper (see comments there).
201   void AdjustKeywordBatchModeLevel(bool entering_batch_mode);
202 
203   //////////////////////////////////////////////////////////////////////////////
204   //
205   // The following methods are only invoked on the DB thread.
206   //
207   //////////////////////////////////////////////////////////////////////////////
208 
209   //////////////////////////////////////////////////////////////////////////////
210   //
211   // Keywords.
212   //
213   //////////////////////////////////////////////////////////////////////////////
214   WebDatabase::State PerformKeywordOperationsImpl(
215       const KeywordTable::Operations& operations,
216       WebDatabase* db);
217   scoped_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
218   WebDatabase::State SetDefaultSearchProviderIDImpl(TemplateURLID id,
219                                                     WebDatabase* db);
220   WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
221 
222   //////////////////////////////////////////////////////////////////////////////
223   //
224   // Web Apps.
225   //
226   //////////////////////////////////////////////////////////////////////////////
227 
228   WebDatabase::State SetWebAppImageImpl(const GURL& app_url,
229       const SkBitmap& image, WebDatabase* db);
230   WebDatabase::State SetWebAppHasAllImagesImpl(const GURL& app_url,
231       bool has_all_images, WebDatabase* db);
232   WebDatabase::State RemoveWebAppImpl(const GURL& app_url, WebDatabase* db);
233   scoped_ptr<WDTypedResult> GetWebAppImagesImpl(
234       const GURL& app_url, WebDatabase* db);
235 
236 #if defined(ENABLE_WEB_INTENTS)
237   //////////////////////////////////////////////////////////////////////////////
238   //
239   // Web Intents.
240   //
241   //////////////////////////////////////////////////////////////////////////////
242   WebDatabase::State AddWebIntentServiceImpl(
243       const webkit_glue::WebIntentServiceData& service);
244   WebDatabase::State RemoveWebIntentServiceImpl(
245       const webkit_glue::WebIntentServiceData& service);
246   scoped_ptr<WDTypedResult> GetWebIntentServicesImpl(
247       const base::string16& action);
248   scoped_ptr<WDTypedResult> GetWebIntentServicesForURLImpl(
249       const base::string16& service_url);
250   scoped_ptr<WDTypedResult> GetAllWebIntentServicesImpl();
251   WebDatabase::State AddDefaultWebIntentServiceImpl(
252       const DefaultWebIntentService& service);
253   WebDatabase::State RemoveDefaultWebIntentServiceImpl(
254       const DefaultWebIntentService& service);
255   WebDatabase::State RemoveWebIntentServiceDefaultsImpl(
256       const GURL& service_url);
257   scoped_ptr<WDTypedResult> GetDefaultWebIntentServicesForActionImpl(
258       const base::string16& action);
259   scoped_ptr<WDTypedResult> GetAllDefaultWebIntentServicesImpl();
260 #endif
261 
262 #if defined(OS_WIN)
263   //////////////////////////////////////////////////////////////////////////////
264   //
265   // Password manager.
266   //
267   //////////////////////////////////////////////////////////////////////////////
268   WebDatabase::State AddIE7LoginImpl(
269       const IE7PasswordInfo& info, WebDatabase* db);
270   WebDatabase::State RemoveIE7LoginImpl(
271       const IE7PasswordInfo& info, WebDatabase* db);
272   scoped_ptr<WDTypedResult> GetIE7LoginImpl(
273       const IE7PasswordInfo& info, WebDatabase* db);
274 #endif  // defined(OS_WIN)
275 
276   size_t keyword_batch_mode_level_;
277   KeywordTable::Operations queued_keyword_operations_;
278 
279   DISALLOW_COPY_AND_ASSIGN(WebDataService);
280 };
281 
282 #endif  // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__
283