File: keyword_web_data_service.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
#define COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__

#include <stddef.h>
#include <stdint.h>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/timer/timer.h"
#include "components/country_codes/country_codes.h"
#include "components/regional_capabilities/regional_capabilities_country_id.h"
#include "components/search_engines/keyword_table.h"
#include "components/search_engines/template_url_id.h"
#include "components/webdata/common/web_data_service_base.h"

namespace base {
class SequencedTaskRunner;
}

class WebDatabaseService;
struct TemplateURLData;

struct WDKeywordsResult {
  WDKeywordsResult();
  WDKeywordsResult(const WDKeywordsResult&);
  WDKeywordsResult& operator=(const WDKeywordsResult&);
  ~WDKeywordsResult();

  KeywordTable::Keywords keywords;

  // Context qualifying the built-in keywords and starter pack engines data.
  struct Metadata {
    // Version number of the most recent prepopulate data that has been merged
    // into the current keyword data.
    int builtin_keyword_data_version = 0;

    // Country associated with the keywords data, stored as a country ID,
    // see `country_codes::CountryId()`.
    std::optional<regional_capabilities::CountryIdHolder>
        builtin_keyword_country;

    // Version number of the most recent starter pack data that has been merged
    // into the current keyword data.
    int starter_pack_version = 0;

    Metadata();
    Metadata(const Metadata&);
    Metadata& operator=(const Metadata&);
    ~Metadata();

    // Whether any metadata associated with the keywords bundle is set.
    bool HasBuiltinKeywordData() const {
      return builtin_keyword_data_version != 0 ||
             builtin_keyword_country.has_value();
    }

    // Whether any metadata associated with the starter pack bundle is set.
    bool HasStarterPackData() const { return starter_pack_version != 0; }
  };
  Metadata metadata;
};

class WebDataServiceConsumer;

class KeywordWebDataService : public WebDataServiceBase {
 public:
  // Instantiate this to turn on batch mode on the provided |service|
  // until the scoper is destroyed.  When batch mode is on, calls to any of the
  // three keyword table modification functions below will result in locally
  // queueing the operation; on setting this back to false, after a short delay,
  // all the modifications will be performed at once.  This is a performance
  // optimization; see comments on KeywordTable::PerformOperations().
  //
  // If multiple scopers are in-scope simultaneously, batch mode will only be
  // exited when all are destroyed.  If |service| is NULL, the object will do
  // nothing.
  class BatchModeScoper {
   public:
    explicit BatchModeScoper(KeywordWebDataService* service);

    BatchModeScoper(const BatchModeScoper&) = delete;
    BatchModeScoper& operator=(const BatchModeScoper&) = delete;

    ~BatchModeScoper();

   private:
    raw_ptr<KeywordWebDataService> service_;
  };

  KeywordWebDataService(
      scoped_refptr<WebDatabaseService> wdbs,
      scoped_refptr<base::SequencedTaskRunner> ui_task_runner);

  KeywordWebDataService(const KeywordWebDataService&) = delete;
  KeywordWebDataService& operator=(const KeywordWebDataService&) = delete;

  // As the database processes requests at a later date, all deletion is done on
  // the background sequence.
  //
  // Many of the keyword related methods do not return a handle. This is because
  // the caller (TemplateURLService) does not need to know when the request is
  // done.

  void AddKeyword(const TemplateURLData& data);
  void RemoveKeyword(TemplateURLID id);
  void UpdateKeyword(const TemplateURLData& data);

  // Fetches the keywords.
  // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
  Handle GetKeywords(WebDataServiceConsumer* consumer);

  // Sets the version of the builtin keyword data.
  void SetBuiltinKeywordDataVersion(int version);

  // Clears the Chrome milestone associated with the builtin keyword data. Used
  // for cleanup.
  void ClearBuiltinKeywordMilestone();

  // Sets the country ID associated with the builtin keyword data.
  void SetBuiltinKeywordCountry(country_codes::CountryId country_id);

  // Sets the version of the starter pack keywords.
  void SetStarterPackKeywordVersion(int version);

  // WebDataServiceBase:
  void ShutdownOnUISequence() override;

 protected:
  ~KeywordWebDataService() override;

 private:
  // Called by the BatchModeScoper (see comments there).
  void AdjustBatchModeLevel(bool entering_batch_mode);

  // Schedules a task to commit any |queued_keyword_operations_| immediately.
  void CommitQueuedOperations();

  size_t batch_mode_level_ = 0;
  KeywordTable::Operations queued_keyword_operations_;
  base::RetainingOneShotTimer timer_;  // Used to commit updates no more often
                                       // than every five seconds.
};

#endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__