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.
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "components/search_engines/keyword_table.h"
14 #include "components/search_engines/template_url_id.h"
15 #include "components/webdata/common/web_data_service_base.h"
16 #include "components/webdata/common/web_database.h"
19 class SingleThreadTaskRunner;
23 class WebDatabaseService;
24 struct TemplateURLData;
26 struct WDKeywordsResult {
28 WDKeywordsResult(const WDKeywordsResult& other);
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;
35 // Version of the built-in keywords. A value of 0 indicates a first run.
36 int builtin_keyword_version;
39 class WebDataServiceConsumer;
41 class KeywordWebDataService : public WebDataServiceBase {
43 // Instantiate this to turn on batch mode on the provided |service|
44 // until the scoper is destroyed. When batch mode is on, calls to any of the
45 // three keyword table modification functions below will result in locally
46 // queueing the operation; on setting this back to false, all the
47 // modifications will be performed at once. This is a performance
48 // optimization; see comments on KeywordTable::PerformOperations().
50 // If multiple scopers are in-scope simultaneously, batch mode will only be
51 // exited when all are destroyed. If |service| is NULL, the object will do
53 class BatchModeScoper {
55 explicit BatchModeScoper(KeywordWebDataService* service);
59 KeywordWebDataService* service_;
61 DISALLOW_COPY_AND_ASSIGN(BatchModeScoper);
64 KeywordWebDataService(
65 scoped_refptr<WebDatabaseService> wdbs,
66 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
67 const ProfileErrorCallback& callback);
69 // As the database processes requests at a later date, all deletion is done on
70 // the background sequence.
72 // Many of the keyword related methods do not return a handle. This is because
73 // the caller (TemplateURLService) does not need to know when the request is
76 void AddKeyword(const TemplateURLData& data);
77 void RemoveKeyword(TemplateURLID id);
78 void UpdateKeyword(const TemplateURLData& data);
80 // Fetches the keywords.
81 // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
82 Handle GetKeywords(WebDataServiceConsumer* consumer);
84 // Sets the ID of the default search provider.
85 void SetDefaultSearchProviderID(TemplateURLID id);
87 // Sets the version of the builtin keywords.
88 void SetBuiltinKeywordVersion(int version);
91 ~KeywordWebDataService() override;
94 // Called by the BatchModeScoper (see comments there).
95 void AdjustBatchModeLevel(bool entering_batch_mode);
97 //////////////////////////////////////////////////////////////////////////////
99 // The following methods are only invoked on the DB sequence.
101 //////////////////////////////////////////////////////////////////////////////
102 WebDatabase::State PerformKeywordOperationsImpl(
103 const KeywordTable::Operations& operations,
105 std::unique_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
106 WebDatabase::State SetDefaultSearchProviderIDImpl(TemplateURLID id,
108 WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
110 size_t batch_mode_level_;
111 KeywordTable::Operations queued_keyword_operations_;
113 DISALLOW_COPY_AND_ASSIGN(KeywordWebDataService);
116 #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__