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 "components/search_engines/keyword_table.h"
9 #include "components/search_engines/template_url_data.h"
10 #include "components/webdata/common/web_data_results.h"
11 #include "components/webdata/common/web_database_service.h"
15 WDKeywordsResult::WDKeywordsResult()
16 : default_search_provider_id(0),
17 builtin_keyword_version(0) {
20 WDKeywordsResult::~WDKeywordsResult() {}
22 KeywordWebDataService::BatchModeScoper::BatchModeScoper(
23 KeywordWebDataService
* service
)
26 service_
->AdjustBatchModeLevel(true);
29 KeywordWebDataService::BatchModeScoper::~BatchModeScoper() {
31 service_
->AdjustBatchModeLevel(false);
34 KeywordWebDataService::KeywordWebDataService(
35 scoped_refptr
<WebDatabaseService
> wdbs
,
36 scoped_refptr
<base::MessageLoopProxy
> ui_thread
,
37 const ProfileErrorCallback
& callback
)
38 : WebDataServiceBase(wdbs
, callback
, ui_thread
),
39 batch_mode_level_(0) {
42 void KeywordWebDataService::AddKeyword(const TemplateURLData
& data
) {
43 if (batch_mode_level_
) {
44 queued_keyword_operations_
.push_back(
45 KeywordTable::Operation(KeywordTable::ADD
, data
));
47 AdjustBatchModeLevel(true);
49 AdjustBatchModeLevel(false);
53 void KeywordWebDataService::RemoveKeyword(TemplateURLID id
) {
54 if (batch_mode_level_
) {
57 queued_keyword_operations_
.push_back(
58 KeywordTable::Operation(KeywordTable::REMOVE
, data
));
60 AdjustBatchModeLevel(true);
62 AdjustBatchModeLevel(false);
66 void KeywordWebDataService::UpdateKeyword(const TemplateURLData
& data
) {
67 if (batch_mode_level_
) {
68 queued_keyword_operations_
.push_back(
69 KeywordTable::Operation(KeywordTable::UPDATE
, data
));
71 AdjustBatchModeLevel(true);
73 AdjustBatchModeLevel(false);
77 WebDataServiceBase::Handle
KeywordWebDataService::GetKeywords(
78 WebDataServiceConsumer
* consumer
) {
79 return wdbs_
->ScheduleDBTaskWithResult(
80 FROM_HERE
, Bind(&KeywordWebDataService::GetKeywordsImpl
, this), consumer
);
83 void KeywordWebDataService::SetDefaultSearchProviderID(TemplateURLID id
) {
84 wdbs_
->ScheduleDBTask(
86 Bind(&KeywordWebDataService::SetDefaultSearchProviderIDImpl
, this, id
));
89 void KeywordWebDataService::SetBuiltinKeywordVersion(int version
) {
90 wdbs_
->ScheduleDBTask(
92 Bind(&KeywordWebDataService::SetBuiltinKeywordVersionImpl
,
96 KeywordWebDataService::~KeywordWebDataService() {
97 DCHECK(!batch_mode_level_
);
100 void KeywordWebDataService::AdjustBatchModeLevel(bool entering_batch_mode
) {
101 if (entering_batch_mode
) {
104 DCHECK(batch_mode_level_
);
106 if (!batch_mode_level_
&& !queued_keyword_operations_
.empty()) {
107 wdbs_
->ScheduleDBTask(
109 Bind(&KeywordWebDataService::PerformKeywordOperationsImpl
, this,
110 queued_keyword_operations_
));
111 queued_keyword_operations_
.clear();
116 WebDatabase::State
KeywordWebDataService::PerformKeywordOperationsImpl(
117 const KeywordTable::Operations
& operations
,
119 return KeywordTable::FromWebDatabase(db
)->PerformOperations(operations
) ?
120 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;
123 scoped_ptr
<WDTypedResult
> KeywordWebDataService::GetKeywordsImpl(
125 scoped_ptr
<WDTypedResult
> result_ptr
;
126 WDKeywordsResult result
;
127 if (KeywordTable::FromWebDatabase(db
)->GetKeywords(&result
.keywords
)) {
128 result
.default_search_provider_id
=
129 KeywordTable::FromWebDatabase(db
)->GetDefaultSearchProviderID();
130 result
.builtin_keyword_version
=
131 KeywordTable::FromWebDatabase(db
)->GetBuiltinKeywordVersion();
132 result_ptr
.reset(new WDResult
<WDKeywordsResult
>(KEYWORDS_RESULT
, result
));
134 return result_ptr
.Pass();
137 WebDatabase::State
KeywordWebDataService::SetDefaultSearchProviderIDImpl(
140 return KeywordTable::FromWebDatabase(db
)->SetDefaultSearchProviderID(id
) ?
141 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;
144 WebDatabase::State
KeywordWebDataService::SetBuiltinKeywordVersionImpl(
147 return KeywordTable::FromWebDatabase(db
)->SetBuiltinKeywordVersion(version
) ?
148 WebDatabase::COMMIT_NEEDED
: WebDatabase::COMMIT_NOT_NEEDED
;