Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / keyword_web_data_service.h
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.
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/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"
17
18 namespace base {
19 class SingleThreadTaskRunner;
20 }
21
22 class WebDatabaseService;
23 struct TemplateURLData;
24
25 struct WDKeywordsResult {
26   WDKeywordsResult();
27   WDKeywordsResult(const WDKeywordsResult&);
28   WDKeywordsResult& operator=(const WDKeywordsResult&);
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 = 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;
39 };
40
41 class WebDataServiceConsumer;
42
43 class KeywordWebDataService : public WebDataServiceBase {
44  public:
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().
51   //
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
54   // nothing.
55   class BatchModeScoper {
56    public:
57     explicit BatchModeScoper(KeywordWebDataService* service);
58
59     BatchModeScoper(const BatchModeScoper&) = delete;
60     BatchModeScoper& operator=(const BatchModeScoper&) = delete;
61
62     ~BatchModeScoper();
63
64    private:
65     raw_ptr<KeywordWebDataService> service_;
66   };
67
68   KeywordWebDataService(
69       scoped_refptr<WebDatabaseService> wdbs,
70       scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner);
71
72   KeywordWebDataService(const KeywordWebDataService&) = delete;
73   KeywordWebDataService& operator=(const KeywordWebDataService&) = delete;
74
75   // As the database processes requests at a later date, all deletion is done on
76   // the background sequence.
77   //
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
80   // done.
81
82   void AddKeyword(const TemplateURLData& data);
83   void RemoveKeyword(TemplateURLID id);
84   void UpdateKeyword(const TemplateURLData& data);
85
86   // Fetches the keywords.
87   // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
88   Handle GetKeywords(WebDataServiceConsumer* consumer);
89
90   // Sets the ID of the default search provider.
91   void SetDefaultSearchProviderID(TemplateURLID id);
92
93   // Sets the version of the builtin keywords.
94   void SetBuiltinKeywordVersion(int version);
95
96   // Sets the version of the starter pack keywords.
97   void SetStarterPackKeywordVersion(int version);
98
99   // WebDataServiceBase:
100   void ShutdownOnUISequence() override;
101
102  protected:
103   ~KeywordWebDataService() override;
104
105  private:
106   // Called by the BatchModeScoper (see comments there).
107   void AdjustBatchModeLevel(bool entering_batch_mode);
108
109   // Schedules a task to commit any |queued_keyword_operations_| immediately.
110   void CommitQueuedOperations();
111
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.
116 };
117
118 #endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__