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 "base/memory/ref_counted.h"
9#include "components/search_engines/keyword_table.h"
10#include "components/search_engines/template_url_id.h"
11#include "components/webdata/common/web_data_service_base.h"
12#include "components/webdata/common/web_database.h"
13
14namespace base {
15class MessageLoopProxy;
16}
17
18class WDTypedResult;
19class WebDatabaseService;
20struct TemplateURLData;
21
22struct WDKeywordsResult {
23  WDKeywordsResult();
24  ~WDKeywordsResult();
25
26  KeywordTable::Keywords keywords;
27  // Identifies the ID of the TemplateURL that is the default search. A value of
28  // 0 indicates there is no default search provider.
29  int64 default_search_provider_id;
30  // Version of the built-in keywords. A value of 0 indicates a first run.
31  int builtin_keyword_version;
32};
33
34class WebDataServiceConsumer;
35
36class KeywordWebDataService : public WebDataServiceBase {
37 public:
38  // Instantiate this to turn on batch mode on the provided |service|
39  // until the scoper is destroyed.  When batch mode is on, calls to any of the
40  // three keyword table modification functions below will result in locally
41  // queueing the operation; on setting this back to false, all the
42  // modifications will be performed at once.  This is a performance
43  // optimization; see comments on KeywordTable::PerformOperations().
44  //
45  // If multiple scopers are in-scope simultaneously, batch mode will only be
46  // exited when all are destroyed.  If |service| is NULL, the object will do
47  // nothing.
48  class BatchModeScoper {
49   public:
50    explicit BatchModeScoper(KeywordWebDataService* service);
51    ~BatchModeScoper();
52
53   private:
54    KeywordWebDataService* service_;
55
56    DISALLOW_COPY_AND_ASSIGN(BatchModeScoper);
57  };
58
59  KeywordWebDataService(scoped_refptr<WebDatabaseService> wdbs,
60                        scoped_refptr<base::MessageLoopProxy> ui_thread,
61                        const ProfileErrorCallback& callback);
62
63  // As the database processes requests at a later date, all deletion is
64  // done on the background thread.
65  //
66  // Many of the keyword related methods do not return a handle. This is because
67  // the caller (TemplateURLService) does not need to know when the request is
68  // done.
69
70  void AddKeyword(const TemplateURLData& data);
71  void RemoveKeyword(TemplateURLID id);
72  void UpdateKeyword(const TemplateURLData& data);
73
74  // Fetches the keywords.
75  // On success, consumer is notified with WDResult<KeywordTable::Keywords>.
76  Handle GetKeywords(WebDataServiceConsumer* consumer);
77
78  // Sets the ID of the default search provider.
79  void SetDefaultSearchProviderID(TemplateURLID id);
80
81  // Sets the version of the builtin keywords.
82  void SetBuiltinKeywordVersion(int version);
83
84 protected:
85  virtual ~KeywordWebDataService();
86
87 private:
88  // Called by the BatchModeScoper (see comments there).
89  void AdjustBatchModeLevel(bool entering_batch_mode);
90
91  //////////////////////////////////////////////////////////////////////////////
92  //
93  // The following methods are only invoked on the DB thread.
94  //
95  //////////////////////////////////////////////////////////////////////////////
96  WebDatabase::State PerformKeywordOperationsImpl(
97      const KeywordTable::Operations& operations,
98      WebDatabase* db);
99  scoped_ptr<WDTypedResult> GetKeywordsImpl(WebDatabase* db);
100  WebDatabase::State SetDefaultSearchProviderIDImpl(TemplateURLID id,
101                                                    WebDatabase* db);
102  WebDatabase::State SetBuiltinKeywordVersionImpl(int version, WebDatabase* db);
103
104  size_t batch_mode_level_;
105  KeywordTable::Operations queued_keyword_operations_;
106
107  DISALLOW_COPY_AND_ASSIGN(KeywordWebDataService);
108};
109
110#endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_WEB_DATA_SERVICE_H__
111