keyword_web_data_service.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2014 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. #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
  5. #define COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/timer/timer.h"
  11. #include "components/search_engines/keyword_table.h"
  12. #include "components/search_engines/template_url_id.h"
  13. #include "components/webdata/common/web_data_service_base.h"
  14. namespace base {
  15. class SingleThreadTaskRunner;
  16. }
  17. class WebDatabaseService;
  18. struct TemplateURLData;
  19. struct WDKeywordsResult {
  20. WDKeywordsResult();
  21. WDKeywordsResult(const WDKeywordsResult&);
  22. WDKeywordsResult& operator=(const WDKeywordsResult&);
  23. ~WDKeywordsResult();
  24. KeywordTable::Keywords keywords;
  25. // Identifies the ID of the TemplateURL that is the default search. A value of
  26. // 0 indicates there is no default search provider.
  27. int64_t default_search_provider_id = 0;
  28. // Version of the built-in keywords and starter pack engines. A value of 0
  29. // indicates a first run.
  30. int builtin_keyword_version = 0;
  31. int starter_pack_version = 0;
  32. };
  33. class WebDataServiceConsumer;
  34. class KeywordWebDataService : public WebDataServiceBase {
  35. public:
  36. // Instantiate this to turn on batch mode on the provided |service|
  37. // until the scoper is destroyed. When batch mode is on, calls to any of the
  38. // three keyword table modification functions below will result in locally
  39. // queueing the operation; on setting this back to false, after a short delay,
  40. // all the modifications will be performed at once. This is a performance
  41. // optimization; see comments on KeywordTable::PerformOperations().
  42. //
  43. // If multiple scopers are in-scope simultaneously, batch mode will only be
  44. // exited when all are destroyed. If |service| is NULL, the object will do
  45. // nothing.
  46. class BatchModeScoper {
  47. public:
  48. explicit BatchModeScoper(KeywordWebDataService* service);
  49. BatchModeScoper(const BatchModeScoper&) = delete;
  50. BatchModeScoper& operator=(const BatchModeScoper&) = delete;
  51. ~BatchModeScoper();
  52. private:
  53. raw_ptr<KeywordWebDataService> service_;
  54. };
  55. KeywordWebDataService(
  56. scoped_refptr<WebDatabaseService> wdbs,
  57. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
  58. KeywordWebDataService(const KeywordWebDataService&) = delete;
  59. KeywordWebDataService& operator=(const KeywordWebDataService&) = delete;
  60. // As the database processes requests at a later date, all deletion is done on
  61. // the background sequence.
  62. //
  63. // Many of the keyword related methods do not return a handle. This is because
  64. // the caller (TemplateURLService) does not need to know when the request is
  65. // done.
  66. void AddKeyword(const TemplateURLData& data);
  67. void RemoveKeyword(TemplateURLID id);
  68. void UpdateKeyword(const TemplateURLData& data);
  69. // Fetches the keywords.
  70. // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
  71. Handle GetKeywords(WebDataServiceConsumer* consumer);
  72. // Sets the ID of the default search provider.
  73. void SetDefaultSearchProviderID(TemplateURLID id);
  74. // Sets the version of the builtin keywords.
  75. void SetBuiltinKeywordVersion(int version);
  76. // Sets the version of the starter pack keywords.
  77. void SetStarterPackKeywordVersion(int version);
  78. // WebDataServiceBase:
  79. void ShutdownOnUISequence() override;
  80. protected:
  81. ~KeywordWebDataService() override;
  82. private:
  83. // Called by the BatchModeScoper (see comments there).
  84. void AdjustBatchModeLevel(bool entering_batch_mode);
  85. // Schedules a task to commit any |queued_keyword_operations_| immediately.
  86. void CommitQueuedOperations();
  87. size_t batch_mode_level_ = 0;
  88. KeywordTable::Operations queued_keyword_operations_;
  89. base::RetainingOneShotTimer timer_; // Used to commit updates no more often
  90. // than every five seconds.
  91. };
  92. #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__