BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / components / search_engines / keyword_web_data_service.cc
blobe6448d28b9f176a4f9ecda65e99c974df0d23b95
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 #include "components/search_engines/keyword_web_data_service.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "components/search_engines/keyword_table.h"
11 #include "components/search_engines/template_url_data.h"
12 #include "components/webdata/common/web_data_results.h"
13 #include "components/webdata/common/web_database_service.h"
15 using base::Bind;
17 WDKeywordsResult::WDKeywordsResult()
18 : default_search_provider_id(0),
19 builtin_keyword_version(0) {
22 WDKeywordsResult::~WDKeywordsResult() {}
24 KeywordWebDataService::BatchModeScoper::BatchModeScoper(
25 KeywordWebDataService* service)
26 : service_(service) {
27 if (service_)
28 service_->AdjustBatchModeLevel(true);
31 KeywordWebDataService::BatchModeScoper::~BatchModeScoper() {
32 if (service_)
33 service_->AdjustBatchModeLevel(false);
36 KeywordWebDataService::KeywordWebDataService(
37 scoped_refptr<WebDatabaseService> wdbs,
38 scoped_refptr<base::SingleThreadTaskRunner> ui_thread,
39 const ProfileErrorCallback& callback)
40 : WebDataServiceBase(wdbs, callback, ui_thread), batch_mode_level_(0) {
43 void KeywordWebDataService::AddKeyword(const TemplateURLData& data) {
44 if (batch_mode_level_) {
45 queued_keyword_operations_.push_back(
46 KeywordTable::Operation(KeywordTable::ADD, data));
47 } else {
48 AdjustBatchModeLevel(true);
49 AddKeyword(data);
50 AdjustBatchModeLevel(false);
54 void KeywordWebDataService::RemoveKeyword(TemplateURLID id) {
55 if (batch_mode_level_) {
56 TemplateURLData data;
57 data.id = id;
58 queued_keyword_operations_.push_back(
59 KeywordTable::Operation(KeywordTable::REMOVE, data));
60 } else {
61 AdjustBatchModeLevel(true);
62 RemoveKeyword(id);
63 AdjustBatchModeLevel(false);
67 void KeywordWebDataService::UpdateKeyword(const TemplateURLData& data) {
68 if (batch_mode_level_) {
69 queued_keyword_operations_.push_back(
70 KeywordTable::Operation(KeywordTable::UPDATE, data));
71 } else {
72 AdjustBatchModeLevel(true);
73 UpdateKeyword(data);
74 AdjustBatchModeLevel(false);
78 WebDataServiceBase::Handle KeywordWebDataService::GetKeywords(
79 WebDataServiceConsumer* consumer) {
80 return wdbs_->ScheduleDBTaskWithResult(
81 FROM_HERE, Bind(&KeywordWebDataService::GetKeywordsImpl, this), consumer);
84 void KeywordWebDataService::SetDefaultSearchProviderID(TemplateURLID id) {
85 wdbs_->ScheduleDBTask(
86 FROM_HERE,
87 Bind(&KeywordWebDataService::SetDefaultSearchProviderIDImpl, this, id));
90 void KeywordWebDataService::SetBuiltinKeywordVersion(int version) {
91 wdbs_->ScheduleDBTask(
92 FROM_HERE,
93 Bind(&KeywordWebDataService::SetBuiltinKeywordVersionImpl,
94 this, version));
97 KeywordWebDataService::~KeywordWebDataService() {
98 DCHECK(!batch_mode_level_);
101 void KeywordWebDataService::AdjustBatchModeLevel(bool entering_batch_mode) {
102 if (entering_batch_mode) {
103 ++batch_mode_level_;
104 } else {
105 DCHECK(batch_mode_level_);
106 --batch_mode_level_;
107 if (!batch_mode_level_ && !queued_keyword_operations_.empty()) {
108 wdbs_->ScheduleDBTask(
109 FROM_HERE,
110 Bind(&KeywordWebDataService::PerformKeywordOperationsImpl, this,
111 queued_keyword_operations_));
112 queued_keyword_operations_.clear();
117 WebDatabase::State KeywordWebDataService::PerformKeywordOperationsImpl(
118 const KeywordTable::Operations& operations,
119 WebDatabase* db) {
120 return KeywordTable::FromWebDatabase(db)->PerformOperations(operations) ?
121 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED;
124 scoped_ptr<WDTypedResult> KeywordWebDataService::GetKeywordsImpl(
125 WebDatabase* db) {
126 scoped_ptr<WDTypedResult> result_ptr;
127 WDKeywordsResult result;
128 if (KeywordTable::FromWebDatabase(db)->GetKeywords(&result.keywords)) {
129 result.default_search_provider_id =
130 KeywordTable::FromWebDatabase(db)->GetDefaultSearchProviderID();
131 result.builtin_keyword_version =
132 KeywordTable::FromWebDatabase(db)->GetBuiltinKeywordVersion();
133 result_ptr.reset(new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result));
135 return result_ptr.Pass();
138 WebDatabase::State KeywordWebDataService::SetDefaultSearchProviderIDImpl(
139 TemplateURLID id,
140 WebDatabase* db) {
141 return KeywordTable::FromWebDatabase(db)->SetDefaultSearchProviderID(id) ?
142 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED;
145 WebDatabase::State KeywordWebDataService::SetBuiltinKeywordVersionImpl(
146 int version,
147 WebDatabase* db) {
148 return KeywordTable::FromWebDatabase(db)->SetBuiltinKeywordVersion(version) ?
149 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED;