1 // Copyright (c) 2012 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 "chrome/browser/webdata/web_data_service.h"
8 #include "base/stl_util.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/search_engines/template_url.h"
11 #include "chrome/browser/webdata/keyword_table.h"
12 #include "chrome/browser/webdata/logins_table.h"
13 #include "chrome/browser/webdata/web_apps_table.h"
14 #include "chrome/browser/webdata/web_intents_table.h"
15 #include "components/signin/core/webdata/token_service_table.h"
16 #include "components/webdata/common/web_database_service.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/notification_source.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
23 ////////////////////////////////////////////////////////////////////////////////
25 // WebDataService implementation.
27 ////////////////////////////////////////////////////////////////////////////////
31 using content::BrowserThread
;
33 WDAppImagesResult::WDAppImagesResult() : has_all_images(false) {}
35 WDAppImagesResult::~WDAppImagesResult() {}
37 WDKeywordsResult::WDKeywordsResult()
38 : default_search_provider_id(0),
39 builtin_keyword_version(0) {
42 WDKeywordsResult::~WDKeywordsResult() {}
44 WebDataService::WebDataService(scoped_refptr
<WebDatabaseService
> wdbs
,
45 const ProfileErrorCallback
& callback
)
46 : WebDataServiceBase(wdbs
, callback
,
47 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)) {
50 //////////////////////////////////////////////////////////////////////////////
54 //////////////////////////////////////////////////////////////////////////////
56 void WebDataService::AddKeyword(const TemplateURLData
& data
) {
57 wdbs_
->ScheduleDBTask(
58 FROM_HERE
, Bind(&WebDataService::AddKeywordImpl
, this, data
));
61 void WebDataService::RemoveKeyword(TemplateURLID id
) {
62 wdbs_
->ScheduleDBTask(
63 FROM_HERE
, Bind(&WebDataService::RemoveKeywordImpl
, this, id
));
66 void WebDataService::UpdateKeyword(const TemplateURLData
& data
) {
67 wdbs_
->ScheduleDBTask(
68 FROM_HERE
, Bind(&WebDataService::UpdateKeywordImpl
, this, data
));
71 WebDataServiceBase::Handle
WebDataService::GetKeywords(
72 WebDataServiceConsumer
* consumer
) {
73 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
74 Bind(&WebDataService::GetKeywordsImpl
, this), consumer
);
77 void WebDataService::SetDefaultSearchProvider(const TemplateURL
* url
) {
78 wdbs_
->ScheduleDBTask(FROM_HERE
,
79 Bind(&WebDataService::SetDefaultSearchProviderImpl
, this,
80 url
? url
->id() : 0));
83 void WebDataService::SetBuiltinKeywordVersion(int version
) {
84 wdbs_
->ScheduleDBTask(FROM_HERE
,
85 Bind(&WebDataService::SetBuiltinKeywordVersionImpl
, this, version
));
88 //////////////////////////////////////////////////////////////////////////////
92 //////////////////////////////////////////////////////////////////////////////
94 void WebDataService::SetWebAppImage(const GURL
& app_url
,
95 const SkBitmap
& image
) {
96 wdbs_
->ScheduleDBTask(FROM_HERE
,
97 Bind(&WebDataService::SetWebAppImageImpl
, this, app_url
, image
));
100 void WebDataService::SetWebAppHasAllImages(const GURL
& app_url
,
101 bool has_all_images
) {
102 wdbs_
->ScheduleDBTask(FROM_HERE
,
103 Bind(&WebDataService::SetWebAppHasAllImagesImpl
, this, app_url
,
107 void WebDataService::RemoveWebApp(const GURL
& app_url
) {
108 wdbs_
->ScheduleDBTask(FROM_HERE
,
109 Bind(&WebDataService::RemoveWebAppImpl
, this, app_url
));
112 WebDataServiceBase::Handle
WebDataService::GetWebAppImages(
113 const GURL
& app_url
, WebDataServiceConsumer
* consumer
) {
114 return wdbs_
->ScheduleDBTaskWithResult(FROM_HERE
,
115 Bind(&WebDataService::GetWebAppImagesImpl
, this, app_url
), consumer
);
118 ////////////////////////////////////////////////////////////////////////////////
120 WebDataService::WebDataService()
121 : WebDataServiceBase(NULL
, ProfileErrorCallback(),
122 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
)) {
125 WebDataService::~WebDataService() {
128 ////////////////////////////////////////////////////////////////////////////////
130 // Keywords implementation.
132 ////////////////////////////////////////////////////////////////////////////////
134 WebDatabase::State
WebDataService::AddKeywordImpl(
135 const TemplateURLData
& data
, WebDatabase
* db
) {
136 KeywordTable::FromWebDatabase(db
)->AddKeyword(data
);
137 return WebDatabase::COMMIT_NEEDED
;
140 WebDatabase::State
WebDataService::RemoveKeywordImpl(
141 TemplateURLID id
, WebDatabase
* db
) {
143 KeywordTable::FromWebDatabase(db
)->RemoveKeyword(id
);
144 return WebDatabase::COMMIT_NEEDED
;
147 WebDatabase::State
WebDataService::UpdateKeywordImpl(
148 const TemplateURLData
& data
, WebDatabase
* db
) {
149 if (!KeywordTable::FromWebDatabase(db
)->UpdateKeyword(data
)) {
151 return WebDatabase::COMMIT_NOT_NEEDED
;
153 return WebDatabase::COMMIT_NEEDED
;
156 scoped_ptr
<WDTypedResult
> WebDataService::GetKeywordsImpl(WebDatabase
* db
) {
157 WDKeywordsResult result
;
158 KeywordTable::FromWebDatabase(db
)->GetKeywords(&result
.keywords
);
159 result
.default_search_provider_id
=
160 KeywordTable::FromWebDatabase(db
)->GetDefaultSearchProviderID();
161 result
.builtin_keyword_version
=
162 KeywordTable::FromWebDatabase(db
)->GetBuiltinKeywordVersion();
163 return scoped_ptr
<WDTypedResult
>(
164 new WDResult
<WDKeywordsResult
>(KEYWORDS_RESULT
, result
));
167 WebDatabase::State
WebDataService::SetDefaultSearchProviderImpl(
168 TemplateURLID id
, WebDatabase
* db
) {
169 if (!KeywordTable::FromWebDatabase(db
)->SetDefaultSearchProviderID(id
)) {
171 return WebDatabase::COMMIT_NOT_NEEDED
;
173 return WebDatabase::COMMIT_NEEDED
;
176 WebDatabase::State
WebDataService::SetBuiltinKeywordVersionImpl(
177 int version
, WebDatabase
* db
) {
178 if (!KeywordTable::FromWebDatabase(db
)->SetBuiltinKeywordVersion(version
)) {
180 return WebDatabase::COMMIT_NOT_NEEDED
;
182 return WebDatabase::COMMIT_NEEDED
;
185 ////////////////////////////////////////////////////////////////////////////////
187 // Web Apps implementation.
189 ////////////////////////////////////////////////////////////////////////////////
191 WebDatabase::State
WebDataService::SetWebAppImageImpl(
192 const GURL
& app_url
, const SkBitmap
& image
, WebDatabase
* db
) {
193 WebAppsTable::FromWebDatabase(db
)->SetWebAppImage(app_url
, image
);
194 return WebDatabase::COMMIT_NEEDED
;
197 WebDatabase::State
WebDataService::SetWebAppHasAllImagesImpl(
198 const GURL
& app_url
, bool has_all_images
, WebDatabase
* db
) {
199 WebAppsTable::FromWebDatabase(db
)->SetWebAppHasAllImages(app_url
,
201 return WebDatabase::COMMIT_NEEDED
;
204 WebDatabase::State
WebDataService::RemoveWebAppImpl(
205 const GURL
& app_url
, WebDatabase
* db
) {
206 WebAppsTable::FromWebDatabase(db
)->RemoveWebApp(app_url
);
207 return WebDatabase::COMMIT_NEEDED
;
210 scoped_ptr
<WDTypedResult
> WebDataService::GetWebAppImagesImpl(
211 const GURL
& app_url
, WebDatabase
* db
) {
212 WDAppImagesResult result
;
213 result
.has_all_images
=
214 WebAppsTable::FromWebDatabase(db
)->GetWebAppHasAllImages(app_url
);
215 WebAppsTable::FromWebDatabase(db
)->GetWebAppImages(app_url
, &result
.images
);
216 return scoped_ptr
<WDTypedResult
>(
217 new WDResult
<WDAppImagesResult
>(WEB_APP_IMAGES
, result
));