1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/timer/timer.h"
14 #include "components/search_engines/keyword_table.h"
15 #include "components/search_engines/template_url_id.h"
16 #include "components/webdata/common/web_data_service_base.h"
19 class SingleThreadTaskRunner;
22 class WebDatabaseService;
23 struct TemplateURLData;
25 struct WDKeywordsResult {
27 WDKeywordsResult(const WDKeywordsResult&);
28 WDKeywordsResult& operator=(const WDKeywordsResult&);
31 KeywordTable::Keywords keywords;
32 // Identifies the ID of the TemplateURL that is the default search. A value of
33 // 0 indicates there is no default search provider.
34 int64_t default_search_provider_id = 0;
35 // Version of the built-in keywords and starter pack engines. A value of 0
36 // indicates a first run.
37 int builtin_keyword_version = 0;
38 int starter_pack_version = 0;
41 class WebDataServiceConsumer;
43 class KeywordWebDataService : public WebDataServiceBase {
45 // Instantiate this to turn on batch mode on the provided |service|
46 // until the scoper is destroyed. When batch mode is on, calls to any of the
47 // three keyword table modification functions below will result in locally
48 // queueing the operation; on setting this back to false, after a short delay,
49 // all the modifications will be performed at once. This is a performance
50 // optimization; see comments on KeywordTable::PerformOperations().
52 // If multiple scopers are in-scope simultaneously, batch mode will only be
53 // exited when all are destroyed. If |service| is NULL, the object will do
55 class BatchModeScoper {
57 explicit BatchModeScoper(KeywordWebDataService* service);
59 BatchModeScoper(const BatchModeScoper&) = delete;
60 BatchModeScoper& operator=(const BatchModeScoper&) = delete;
65 raw_ptr<KeywordWebDataService> service_;
68 KeywordWebDataService(
69 scoped_refptr<WebDatabaseService> wdbs,
70 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
72 KeywordWebDataService(const KeywordWebDataService&) = delete;
73 KeywordWebDataService& operator=(const KeywordWebDataService&) = delete;
75 // As the database processes requests at a later date, all deletion is done on
76 // the background sequence.
78 // Many of the keyword related methods do not return a handle. This is because
79 // the caller (TemplateURLService) does not need to know when the request is
82 void AddKeyword(const TemplateURLData& data);
83 void RemoveKeyword(TemplateURLID id);
84 void UpdateKeyword(const TemplateURLData& data);
86 // Fetches the keywords.
87 // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
88 Handle GetKeywords(WebDataServiceConsumer* consumer);
90 // Sets the ID of the default search provider.
91 void SetDefaultSearchProviderID(TemplateURLID id);
93 // Sets the version of the builtin keywords.
94 void SetBuiltinKeywordVersion(int version);
96 // Sets the version of the starter pack keywords.
97 void SetStarterPackKeywordVersion(int version);
99 // WebDataServiceBase:
100 void ShutdownOnUISequence() override;
103 ~KeywordWebDataService() override;
106 // Called by the BatchModeScoper (see comments there).
107 void AdjustBatchModeLevel(bool entering_batch_mode);
109 // Schedules a task to commit any |queued_keyword_operations_| immediately.
110 void CommitQueuedOperations();
112 size_t batch_mode_level_ = 0;
113 KeywordTable::Operations queued_keyword_operations_;
114 base::RetainingOneShotTimer timer_; // Used to commit updates no more often
115 // than every five seconds.
118 #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__