web_data_service.h revision e5757d9f5d659078c7e68329c5cd2c4b39c38668
1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ 6#define CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ 7#pragma once 8 9#include <map> 10#include <string> 11#include <vector> 12 13#include "app/sql/init_status.h" 14#include "base/file_path.h" 15#include "base/ref_counted.h" 16#include "base/synchronization/lock.h" 17#include "chrome/browser/search_engines/template_url_id.h" 18#ifndef ANDROID 19#include "content/browser/browser_thread.h" 20#endif 21#ifdef ANDROID 22#include "third_party/skia/include/core/SkBitmap.h" 23#include <WebCoreSupport/autofill/FormFieldAndroid.h> 24#else 25#include "webkit/glue/form_field.h" 26#endif 27 28class AutofillChange; 29class AutoFillProfile; 30class CreditCard; 31class GURL; 32#if defined(OS_WIN) 33struct IE7PasswordInfo; 34#endif 35class MessageLoop; 36class SkBitmap; 37class Task; 38class TemplateURL; 39class WebDatabase; 40 41namespace base { 42class Thread; 43} 44 45namespace webkit_glue { 46struct PasswordForm; 47} 48 49//////////////////////////////////////////////////////////////////////////////// 50// 51// WebDataService is a generic data repository for meta data associated with 52// web pages. All data is retrieved and archived in an asynchronous way. 53// 54// All requests return a handle. The handle can be used to cancel the request. 55// 56//////////////////////////////////////////////////////////////////////////////// 57 58 59//////////////////////////////////////////////////////////////////////////////// 60// 61// WebDataService results 62// 63//////////////////////////////////////////////////////////////////////////////// 64 65// 66// Result types 67// 68typedef enum { 69 BOOL_RESULT = 1, // WDResult<bool> 70 KEYWORDS_RESULT, // WDResult<WDKeywordsResult> 71 INT64_RESULT, // WDResult<int64> 72 PASSWORD_RESULT, // WDResult<std::vector<PasswordForm*>> 73#if defined(OS_WIN) 74 PASSWORD_IE7_RESULT, // WDResult<IE7PasswordInfo> 75#endif 76 WEB_APP_IMAGES, // WDResult<WDAppImagesResult> 77 TOKEN_RESULT, // WDResult<std::vector<std::string>> 78 AUTOFILL_VALUE_RESULT, // WDResult<std::vector<string16>> 79 AUTOFILL_CHANGES, // WDResult<std::vector<AutofillChange>> 80 AUTOFILL_PROFILE_RESULT, // WDResult<AutoFillProfile> 81 AUTOFILL_PROFILES_RESULT, // WDResult<std::vector<AutoFillProfile*>> 82 AUTOFILL_CREDITCARD_RESULT, // WDResult<CreditCard> 83 AUTOFILL_CREDITCARDS_RESULT // WDResult<std::vector<CreditCard*>> 84} WDResultType; 85 86typedef std::vector<AutofillChange> AutofillChangeList; 87 88// Result from GetWebAppImages. 89struct WDAppImagesResult { 90 WDAppImagesResult(); 91 ~WDAppImagesResult(); 92 93 // True if SetWebAppHasAllImages(true) was invoked. 94 bool has_all_images; 95 96 // The images, may be empty. 97 std::vector<SkBitmap> images; 98}; 99 100struct WDKeywordsResult { 101 WDKeywordsResult(); 102 ~WDKeywordsResult(); 103 104 std::vector<TemplateURL*> keywords; 105 // Identifies the ID of the TemplateURL that is the default search. A value of 106 // 0 indicates there is no default search provider. 107 int64 default_search_provider_id; 108 // Version of the built-in keywords. A value of 0 indicates a first run. 109 int builtin_keyword_version; 110}; 111 112// 113// The top level class for a result. 114// 115class WDTypedResult { 116 public: 117 virtual ~WDTypedResult() {} 118 119 // Return the result type. 120 WDResultType GetType() const { 121 return type_; 122 } 123 124 protected: 125 explicit WDTypedResult(WDResultType type) : type_(type) { 126 } 127 128 private: 129 WDResultType type_; 130 DISALLOW_COPY_AND_ASSIGN(WDTypedResult); 131}; 132 133// A result containing one specific pointer or literal value. 134template <class T> class WDResult : public WDTypedResult { 135 public: 136 137 WDResult(WDResultType type, const T& v) : WDTypedResult(type), value_(v) { 138 } 139 140 virtual ~WDResult() { 141 } 142 143 // Return a single value result. 144 T GetValue() const { 145 return value_; 146 } 147 148 private: 149 T value_; 150 151 DISALLOW_COPY_AND_ASSIGN(WDResult); 152}; 153 154template <class T> class WDObjectResult : public WDTypedResult { 155 public: 156 explicit WDObjectResult(WDResultType type) : WDTypedResult(type) { 157 } 158 159 T* GetValue() const { 160 return &value_; 161 } 162 163 private: 164 // mutable to keep GetValue() const. 165 mutable T value_; 166 DISALLOW_COPY_AND_ASSIGN(WDObjectResult); 167}; 168 169class WebDataServiceConsumer; 170 171class WebDataService 172 : public base::RefCountedThreadSafe<WebDataService 173#ifndef ANDROID 174 , BrowserThread::DeleteOnUIThread 175#endif 176 > { 177 public: 178 // All requests return an opaque handle of the following type. 179 typedef int Handle; 180 181 ////////////////////////////////////////////////////////////////////////////// 182 // 183 // Internal requests 184 // 185 // Every request is processed using a request object. The object contains 186 // both the request parameters and the results. 187 ////////////////////////////////////////////////////////////////////////////// 188 class WebDataRequest { 189 public: 190 WebDataRequest(WebDataService* service, 191 Handle handle, 192 WebDataServiceConsumer* consumer); 193 194 virtual ~WebDataRequest(); 195 196 Handle GetHandle() const; 197 WebDataServiceConsumer* GetConsumer() const; 198 bool IsCancelled() const; 199 200 // This can be invoked from any thread. From this point we assume that 201 // our consumer_ reference is invalid. 202 void Cancel(); 203 204 // Invoked by the service when this request has been completed. 205 // This will notify the service in whatever thread was used to create this 206 // request. 207 void RequestComplete(); 208 209 // The result is owned by the request. 210 void SetResult(WDTypedResult* r); 211 const WDTypedResult* GetResult() const; 212 213 private: 214 scoped_refptr<WebDataService> service_; 215 MessageLoop* message_loop_; 216 Handle handle_; 217 bool canceled_; 218 WebDataServiceConsumer* consumer_; 219 WDTypedResult* result_; 220 221 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); 222 }; 223 224 // 225 // Internally we use instances of the following template to represent 226 // requests. 227 // 228 template <class T> 229 class GenericRequest : public WebDataRequest { 230 public: 231 GenericRequest(WebDataService* service, 232 Handle handle, 233 WebDataServiceConsumer* consumer, 234 const T& arg) 235 : WebDataRequest(service, handle, consumer), 236 arg_(arg) { 237 } 238 239 virtual ~GenericRequest() { 240 } 241 242 T GetArgument() { 243 return arg_; 244 } 245 246 private: 247 T arg_; 248 }; 249 250 template <class T, class U> 251 class GenericRequest2 : public WebDataRequest { 252 public: 253 GenericRequest2(WebDataService* service, 254 Handle handle, 255 WebDataServiceConsumer* consumer, 256 const T& arg1, 257 const U& arg2) 258 : WebDataRequest(service, handle, consumer), 259 arg1_(arg1), 260 arg2_(arg2) { 261 } 262 263 virtual ~GenericRequest2() { } 264 265 T GetArgument1() { 266 return arg1_; 267 } 268 269 U GetArgument2() { 270 return arg2_; 271 } 272 273 private: 274 T arg1_; 275 U arg2_; 276 }; 277 278 WebDataService(); 279 280 // Initializes the web data service. Returns false on failure 281 // Takes the path of the profile directory as its argument. 282 bool Init(const FilePath& profile_path); 283 284 // Shutdown the web data service. The service can no longer be used after this 285 // call. 286 void Shutdown(); 287 288 // Returns false if Shutdown() has been called. 289 bool IsRunning() const; 290 291 // Unloads the database without actually shutting down the service. This can 292 // be used to temporarily reduce the browser process' memory footprint. 293 void UnloadDatabase(); 294 295 // Cancel any pending request. You need to call this method if your 296 // WebDataServiceConsumer is about to be deleted. 297 void CancelRequest(Handle h); 298 299 virtual bool IsDatabaseLoaded(); 300 virtual WebDatabase* GetDatabase(); 301 302 ////////////////////////////////////////////////////////////////////////////// 303 // 304 // Keywords 305 // 306 ////////////////////////////////////////////////////////////////////////////// 307 308 // As the database processes requests at a later date, all deletion is 309 // done on the background thread. 310 // 311 // Many of the keyword related methods do not return a handle. This is because 312 // the caller (TemplateURLModel) does not need to know when the request is 313 // done. 314 void AddKeyword(const TemplateURL& url); 315 316 void RemoveKeyword(const TemplateURL& url); 317 318 void UpdateKeyword(const TemplateURL& url); 319 320 // Fetches the keywords. 321 // On success, consumer is notified with WDResult<std::vector<TemplateURL*>. 322 Handle GetKeywords(WebDataServiceConsumer* consumer); 323 324 // Sets the keywords used for the default search provider. 325 void SetDefaultSearchProvider(const TemplateURL* url); 326 327 // Sets the version of the builtin keywords. 328 void SetBuiltinKeywordVersion(int version); 329 330 ////////////////////////////////////////////////////////////////////////////// 331 // 332 // Web Apps 333 // 334 ////////////////////////////////////////////////////////////////////////////// 335 336 // Sets the image for the specified web app. A web app can have any number of 337 // images, but only one at a particular size. If there was an image for the 338 // web app at the size of the given image it is replaced. 339 void SetWebAppImage(const GURL& app_url, const SkBitmap& image); 340 341 // Sets whether all the images have been downloaded for the specified web app. 342 void SetWebAppHasAllImages(const GURL& app_url, bool has_all_images); 343 344 // Removes all images for the specified web app. 345 void RemoveWebApp(const GURL& app_url); 346 347 // Fetches the images and whether all images have been downloaded for the 348 // specified web app. 349 Handle GetWebAppImages(const GURL& app_url, WebDataServiceConsumer* consumer); 350 351 ////////////////////////////////////////////////////////////////////////////// 352 // 353 // Token Service 354 // 355 ////////////////////////////////////////////////////////////////////////////// 356 357 // Set a token to use for a specified service. 358 void SetTokenForService(const std::string& service, 359 const std::string& token); 360 361 // Remove all tokens stored in the web database. 362 void RemoveAllTokens(); 363 364 // Null on failure. Success is WDResult<std::vector<std::string> > 365 Handle GetAllTokens(WebDataServiceConsumer* consumer); 366 367 ////////////////////////////////////////////////////////////////////////////// 368 // 369 // Password manager 370 // NOTE: These methods are all deprecated; new clients should use 371 // PasswordStore. These are only still here because Windows is (temporarily) 372 // still using them for its PasswordStore implementation. 373 // 374 ////////////////////////////////////////////////////////////////////////////// 375 376 // Adds |form| to the list of remembered password forms. 377 void AddLogin(const webkit_glue::PasswordForm& form); 378 379 // Updates the remembered password form. 380 void UpdateLogin(const webkit_glue::PasswordForm& form); 381 382 // Removes |form| from the list of remembered password forms. 383 void RemoveLogin(const webkit_glue::PasswordForm& form); 384 385 // Removes all logins created in the specified daterange 386 void RemoveLoginsCreatedBetween(const base::Time& delete_begin, 387 const base::Time& delete_end); 388 389 // Removes all logins created on or after the date passed in. 390 void RemoveLoginsCreatedAfter(const base::Time& delete_begin); 391 392 // Gets a list of password forms that match |form|. 393 // |consumer| will be notified when the request is done. The result is of 394 // type WDResult<std::vector<PasswordForm*>>. 395 // The result will be null on failure. The |consumer| owns all PasswordForm's. 396 Handle GetLogins(const webkit_glue::PasswordForm& form, 397 WebDataServiceConsumer* consumer); 398 399 // Gets the complete list of password forms that have not been blacklisted and 400 // are thus auto-fillable. 401 // |consumer| will be notified when the request is done. The result is of 402 // type WDResult<std::vector<PasswordForm*>>. 403 // The result will be null on failure. The |consumer| owns all PasswordForms. 404 Handle GetAutofillableLogins(WebDataServiceConsumer* consumer); 405 406 // Gets the complete list of password forms that have been blacklisted. 407 // |consumer| will be notified when the request is done. The result is of 408 // type WDResult<std::vector<PasswordForm*>>. 409 // The result will be null on failure. The |consumer| owns all PasswordForm's. 410 Handle GetBlacklistLogins(WebDataServiceConsumer* consumer); 411 412#if defined(OS_WIN) 413 // Adds |info| to the list of imported passwords from ie7/ie8. 414 void AddIE7Login(const IE7PasswordInfo& info); 415 416 // Removes |info| from the list of imported passwords from ie7/ie8. 417 void RemoveIE7Login(const IE7PasswordInfo& info); 418 419 // Get the login matching the information in |info|. |consumer| will be 420 // notified when the request is done. The result is of type 421 // WDResult<IE7PasswordInfo>. 422 // If there is no match, the fields of the IE7PasswordInfo will be empty. 423 Handle GetIE7Login(const IE7PasswordInfo& info, 424 WebDataServiceConsumer* consumer); 425#endif // defined(OS_WIN) 426 427 ////////////////////////////////////////////////////////////////////////////// 428 // 429 // AutoFill. 430 // 431 ////////////////////////////////////////////////////////////////////////////// 432 433 // Schedules a task to add form fields to the web database. 434 virtual void AddFormFields(const std::vector<webkit_glue::FormField>& fields); 435 436 // Initiates the request for a vector of values which have been entered in 437 // form input fields named |name|. The method OnWebDataServiceRequestDone of 438 // |consumer| gets called back when the request is finished, with the vector 439 // included in the argument |result|. 440 Handle GetFormValuesForElementName(const string16& name, 441 const string16& prefix, 442 int limit, 443 WebDataServiceConsumer* consumer); 444 445 // Removes form elements recorded for Autocomplete from the database. 446 void RemoveFormElementsAddedBetween(const base::Time& delete_begin, 447 const base::Time& delete_end); 448 void RemoveFormValueForElementName(const string16& name, 449 const string16& value); 450 451 // Schedules a task to add an AutoFill profile to the web database. 452 void AddAutoFillProfile(const AutoFillProfile& profile); 453 454 // Schedules a task to update an AutoFill profile in the web database. 455 void UpdateAutoFillProfile(const AutoFillProfile& profile); 456 457 // Schedules a task to remove an AutoFill profile from the web database. 458 // |guid| is the identifer of the profile to remove. 459 void RemoveAutoFillProfile(const std::string& guid); 460 461 // Initiates the request for all AutoFill profiles. The method 462 // OnWebDataServiceRequestDone of |consumer| gets called when the request is 463 // finished, with the profiles included in the argument |result|. The 464 // consumer owns the profiles. 465 Handle GetAutoFillProfiles(WebDataServiceConsumer* consumer); 466 467 // Schedules a task to add credit card to the web database. 468 void AddCreditCard(const CreditCard& credit_card); 469 470 // Schedules a task to update credit card in the web database. 471 void UpdateCreditCard(const CreditCard& credit_card); 472 473 // Schedules a task to remove a credit card from the web database. 474 // |guid| is identifer of the credit card to remove. 475 void RemoveCreditCard(const std::string& guid); 476 477 // Initiates the request for all credit cards. The method 478 // OnWebDataServiceRequestDone of |consumer| gets called when the request is 479 // finished, with the credit cards included in the argument |result|. The 480 // consumer owns the credit cards. 481 Handle GetCreditCards(WebDataServiceConsumer* consumer); 482 483 // Removes AutoFill records from the database. 484 void RemoveAutoFillProfilesAndCreditCardsModifiedBetween( 485 const base::Time& delete_begin, 486 const base::Time& delete_end); 487 488 // Testing 489#ifdef UNIT_TEST 490 void set_failed_init(bool value) { failed_init_ = value; } 491#endif 492 493 protected: 494 friend class TemplateURLModelTest; 495 friend class TemplateURLModelTestingProfile; 496 friend class WebDataServiceTest; 497 friend class WebDataRequest; 498 499 virtual ~WebDataService(); 500 501 // This is invoked by the unit test; path is the path of the Web Data file. 502 bool InitWithPath(const FilePath& path); 503 504 // Invoked by request implementations when a request has been processed. 505 void RequestCompleted(Handle h); 506 507 // Register the request as a pending request. 508 void RegisterRequest(WebDataRequest* request); 509 510 ////////////////////////////////////////////////////////////////////////////// 511 // 512 // The following methods are only invoked in the web data service thread. 513 // 514 ////////////////////////////////////////////////////////////////////////////// 515 private: 516#ifndef ANDROID 517 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; 518#endif 519 friend class DeleteTask<WebDataService>; 520 friend class ShutdownTask; 521 522 typedef GenericRequest2<std::vector<const TemplateURL*>, 523 std::vector<TemplateURL*> > SetKeywordsRequest; 524 525 // Invoked on the main thread if initializing the db fails. 526 void DBInitFailed(sql::InitStatus init_status); 527 528 // Initialize the database, if it hasn't already been initialized. 529 void InitializeDatabaseIfNecessary(); 530 531 // The notification method. 532 void NotifyDatabaseLoadedOnUIThread(); 533 534 // Commit any pending transaction and deletes the database. 535 void ShutdownDatabase(); 536 537 // Commit the current transaction and creates a new one. 538 void Commit(); 539 540 // Schedule a task on our worker thread. 541 void ScheduleTask(Task* t); 542 543 // Schedule a commit if one is not already pending. 544 void ScheduleCommit(); 545 546 // Return the next request handle. 547 int GetNextRequestHandle(); 548 549 ////////////////////////////////////////////////////////////////////////////// 550 // 551 // Keywords. 552 // 553 ////////////////////////////////////////////////////////////////////////////// 554 void AddKeywordImpl(GenericRequest<TemplateURL>* request); 555 void RemoveKeywordImpl(GenericRequest<TemplateURLID>* request); 556 void UpdateKeywordImpl(GenericRequest<TemplateURL>* request); 557 void GetKeywordsImpl(WebDataRequest* request); 558 void SetDefaultSearchProviderImpl(GenericRequest<TemplateURLID>* r); 559 void SetBuiltinKeywordVersionImpl(GenericRequest<int>* r); 560 561 ////////////////////////////////////////////////////////////////////////////// 562 // 563 // Web Apps. 564 // 565 ////////////////////////////////////////////////////////////////////////////// 566 void SetWebAppImageImpl(GenericRequest2<GURL, SkBitmap>* request); 567 void SetWebAppHasAllImagesImpl(GenericRequest2<GURL, bool>* request); 568 void RemoveWebAppImpl(GenericRequest<GURL>* request); 569 void GetWebAppImagesImpl(GenericRequest<GURL>* request); 570 571 ////////////////////////////////////////////////////////////////////////////// 572 // 573 // Token Service. 574 // 575 ////////////////////////////////////////////////////////////////////////////// 576 577 void RemoveAllTokensImpl(GenericRequest<std::string>* request); 578 void SetTokenForServiceImpl( 579 GenericRequest2<std::string, std::string>* request); 580 void GetAllTokensImpl(GenericRequest<std::string>* request); 581 582 ////////////////////////////////////////////////////////////////////////////// 583 // 584 // Password manager. 585 // 586 ////////////////////////////////////////////////////////////////////////////// 587 void AddLoginImpl(GenericRequest<webkit_glue::PasswordForm>* request); 588 void UpdateLoginImpl(GenericRequest<webkit_glue::PasswordForm>* request); 589 void RemoveLoginImpl(GenericRequest<webkit_glue::PasswordForm>* request); 590 void RemoveLoginsCreatedBetweenImpl( 591 GenericRequest2<base::Time, base::Time>* request); 592 void GetLoginsImpl(GenericRequest<webkit_glue::PasswordForm>* request); 593 void GetAutofillableLoginsImpl(WebDataRequest* request); 594 void GetBlacklistLoginsImpl(WebDataRequest* request); 595#if defined(OS_WIN) 596 void AddIE7LoginImpl(GenericRequest<IE7PasswordInfo>* request); 597 void RemoveIE7LoginImpl(GenericRequest<IE7PasswordInfo>* request); 598 void GetIE7LoginImpl(GenericRequest<IE7PasswordInfo>* request); 599#endif // defined(OS_WIN) 600 601 ////////////////////////////////////////////////////////////////////////////// 602 // 603 // AutoFill. 604 // 605 ////////////////////////////////////////////////////////////////////////////// 606 void AddFormElementsImpl( 607 GenericRequest<std::vector<webkit_glue::FormField> >* request); 608 void GetFormValuesForElementNameImpl(WebDataRequest* request, 609 const string16& name, const string16& prefix, int limit); 610 void RemoveFormElementsAddedBetweenImpl( 611 GenericRequest2<base::Time, base::Time>* request); 612 void RemoveFormValueForElementNameImpl( 613 GenericRequest2<string16, string16>* request); 614 void AddAutoFillProfileImpl(GenericRequest<AutoFillProfile>* request); 615 void UpdateAutoFillProfileImpl(GenericRequest<AutoFillProfile>* request); 616 void RemoveAutoFillProfileImpl(GenericRequest<std::string>* request); 617 void GetAutoFillProfilesImpl(WebDataRequest* request); 618 void AddCreditCardImpl(GenericRequest<CreditCard>* request); 619 void UpdateCreditCardImpl(GenericRequest<CreditCard>* request); 620 void RemoveCreditCardImpl(GenericRequest<std::string>* request); 621 void GetCreditCardsImpl(WebDataRequest* request); 622 void RemoveAutoFillProfilesAndCreditCardsModifiedBetweenImpl( 623 GenericRequest2<base::Time, base::Time>* request); 624 625 // True once initialization has started. 626 bool is_running_; 627 628 // The path with which to initialize the database. 629 FilePath path_; 630 631 // Our database. 632 WebDatabase* db_; 633 634 // Whether the database failed to initialize. We use this to avoid 635 // continually trying to reinit. 636 bool failed_init_; 637 638 // Whether we should commit the database. 639 bool should_commit_; 640 641 // A lock to protect pending requests and next request handle. 642 base::Lock pending_lock_; 643 644 // Next handle to be used for requests. Incremented for each use. 645 Handle next_request_handle_; 646 647 typedef std::map<Handle, WebDataRequest*> RequestMap; 648 RequestMap pending_requests_; 649 650 // MessageLoop the WebDataService is created on. 651 MessageLoop* main_loop_; 652 653 DISALLOW_COPY_AND_ASSIGN(WebDataService); 654}; 655 656//////////////////////////////////////////////////////////////////////////////// 657// 658// WebDataServiceConsumer. 659// 660// All requests to the web data service are asynchronous. When the request has 661// been performed, the data consumer is notified using the following interface. 662// 663//////////////////////////////////////////////////////////////////////////////// 664 665class WebDataServiceConsumer { 666 public: 667 668 // Called when a request is done. h uniquely identifies the request. 669 // result can be NULL, if no result is expected or if the database could 670 // not be opened. The result object is destroyed after this call. 671 virtual void OnWebDataServiceRequestDone(WebDataService::Handle h, 672 const WDTypedResult* result) = 0; 673 674 protected: 675 virtual ~WebDataServiceConsumer() {} 676}; 677 678#endif // CHROME_BROWSER_WEBDATA_WEB_DATA_SERVICE_H__ 679