Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / components / search_engines / keyword_web_data_service.h
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
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
7
8 #include <stddef.h>
9 #include <stdint.h>
10
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"
17
18 namespace base {
19 class SingleThreadTaskRunner;
20 }
21
22 class WDTypedResult;
23 class WebDatabaseService;
24 struct TemplateURLData;
25
26 struct WDKeywordsResult {
27   WDKeywordsResult();
28   WDKeywordsResult(const WDKeywordsResult& other);
29   ~WDKeywordsResult();
30
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;
37 };
38
39 class WebDataServiceConsumer;
40
41 class KeywordWebDataService : public WebDataServiceBase {
42  public:
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().
49   //
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
52   // nothing.
53   class BatchModeScoper {
54    public:
55     explicit BatchModeScoper(KeywordWebDataService* service);
56     ~BatchModeScoper();
57
58    private:
59     KeywordWebDataService* service_;
60
61     DISALLOW_COPY_AND_ASSIGN(BatchModeScoper);
62   };
63
64   KeywordWebDataService(
65       scoped_refptr<WebDatabaseService> wdbs,
66       scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
67       const ProfileErrorCallback& callback);
68
69   // As the database processes requests at a later date, all deletion is done on
70   // the background sequence.
71   //
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
74   // done.
75
76   void AddKeyword(const TemplateURLData& data);
77   void RemoveKeyword(TemplateURLID id);
78   void UpdateKeyword(const TemplateURLData& data);
79
80   // Fetches the keywords.
81   // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
82   Handle GetKeywords(WebDataServiceConsumer* consumer);
83
84   // Sets the ID of the default search provider.
85   void SetDefaultSearchProviderID(TemplateURLID id);
86
87   // Sets the version of the builtin keywords.
88   void SetBuiltinKeywordVersion(int version);
89
90  protected:
91   ~KeywordWebDataService() override;
92
93  private:
94   // Called by the BatchModeScoper (see comments there).
95   void AdjustBatchModeLevel(bool entering_batch_mode);
96
97   //////////////////////////////////////////////////////////////////////////////
98   //
99   // The following methods are only invoked on the DB sequence.
100   //
101   //////////////////////////////////////////////////////////////////////////////
102   WebDatabase::State PerformKeywordOperationsImpl(
103       const KeywordTable::Operations& operations,
104       WebDatabase* db);
105   std::unique_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
106   WebDatabase::State SetDefaultSearchProviderIDImpl(TemplateURLID id,
107                                                     WebDatabase* db);
108   WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
109
110   size_t batch_mode_level_;
111   KeywordTable::Operations queued_keyword_operations_;
112
113   DISALLOW_COPY_AND_ASSIGN(KeywordWebDataService);
114 };
115
116 #endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__