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"
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"
17 WDKeywordsResult::WDKeywordsResult()
18 : default_search_provider_id(0),
19 builtin_keyword_version(0) {
22 WDKeywordsResult::~WDKeywordsResult() {}
24 KeywordWebDataService::BatchModeScoper::BatchModeScoper(
25 KeywordWebDataService
* service
)
28 service_
->AdjustBatchModeLevel(true);
31 KeywordWebDataService::BatchModeScoper::~BatchModeScoper() {
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
));
48 AdjustBatchModeLevel(true);
50 AdjustBatchModeLevel(false);
54 void KeywordWebDataService::RemoveKeyword(TemplateURLID id
) {
55 if (batch_mode_level_
) {
58 queued_keyword_operations_
.push_back(
59 KeywordTable::Operation(KeywordTable::REMOVE
, data
));
61 AdjustBatchModeLevel(true);
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
));
72 AdjustBatchModeLevel(true);
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(
87 Bind(&KeywordWebDataService::SetDefaultSearchProviderIDImpl
, this, id
));
90 void KeywordWebDataService::SetBuiltinKeywordVersion(int version
) {
91 wdbs_
->ScheduleDBTask(
93 Bind(&KeywordWebDataService::SetBuiltinKeywordVersionImpl
,
97 KeywordWebDataService::~KeywordWebDataService() {
98 DCHECK(!batch_mode_level_
);
101 void KeywordWebDataService::AdjustBatchModeLevel(bool entering_batch_mode
) {
102 if (entering_batch_mode
) {
105 DCHECK(batch_mode_level_
);
107 if (!batch_mode_level_
&& !queued_keyword_operations_
.empty()) {
108 wdbs_
->ScheduleDBTask(
110 Bind(&KeywordWebDataService::PerformKeywordOperationsImpl
, this,
111 queued_keyword_operations_
));
112 queued_keyword_operations_
.clear();
117 WebDatabase::State
KeywordWebDataService::PerformKeywordOperationsImpl(
118 const KeywordTable::Operations
& operations
,
120 return KeywordTable::FromWebDatabase(db
)->PerformOperations(operations
) ?
121 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;
124 scoped_ptr
<WDTypedResult
> KeywordWebDataService::GetKeywordsImpl(
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(
141 return KeywordTable::FromWebDatabase(db
)->SetDefaultSearchProviderID(id
) ?
142 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;
145 WebDatabase::State
KeywordWebDataService::SetBuiltinKeywordVersionImpl(
148 return KeywordTable::FromWebDatabase(db
)->SetBuiltinKeywordVersion(version
) ?
149 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;