OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/webdata/web_data_service.h" | 5 #include "chrome/browser/webdata/web_data_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | |
9 #include "chrome/browser/chrome_notification_types.h" | |
10 #include "chrome/browser/webdata/keyword_table.h" | |
11 #include "chrome/browser/webdata/logins_table.h" | 8 #include "chrome/browser/webdata/logins_table.h" |
12 #include "chrome/browser/webdata/web_apps_table.h" | 9 #include "chrome/browser/webdata/web_apps_table.h" |
13 #include "chrome/browser/webdata/web_intents_table.h" | 10 #include "chrome/browser/webdata/web_intents_table.h" |
14 #include "components/search_engines/template_url.h" | 11 #include "components/search_engines/template_url.h" |
15 #include "components/signin/core/browser/webdata/token_service_table.h" | 12 #include "components/signin/core/browser/webdata/token_service_table.h" |
16 #include "components/webdata/common/web_database_service.h" | 13 #include "components/webdata/common/web_database_service.h" |
17 #include "content/public/browser/browser_thread.h" | 14 #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" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
22 | 16 |
23 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
24 // | 18 // |
25 // WebDataService implementation. | 19 // WebDataService implementation. |
26 // | 20 // |
27 //////////////////////////////////////////////////////////////////////////////// | 21 //////////////////////////////////////////////////////////////////////////////// |
28 | 22 |
29 using base::Bind; | 23 using base::Bind; |
30 using base::Time; | |
31 using content::BrowserThread; | 24 using content::BrowserThread; |
32 | 25 |
33 WDAppImagesResult::WDAppImagesResult() : has_all_images(false) {} | 26 WDAppImagesResult::WDAppImagesResult() : has_all_images(false) {} |
34 | 27 |
35 WDAppImagesResult::~WDAppImagesResult() {} | 28 WDAppImagesResult::~WDAppImagesResult() {} |
36 | 29 |
37 WDKeywordsResult::WDKeywordsResult() | |
38 : default_search_provider_id(0), | |
39 builtin_keyword_version(0) { | |
40 } | |
41 | |
42 WDKeywordsResult::~WDKeywordsResult() {} | |
43 | |
44 WebDataService::KeywordBatchModeScoper::KeywordBatchModeScoper( | |
45 WebDataService* service) | |
46 : service_(service) { | |
47 if (service_) | |
48 service_->AdjustKeywordBatchModeLevel(true); | |
49 } | |
50 | |
51 WebDataService::KeywordBatchModeScoper::~KeywordBatchModeScoper() { | |
52 if (service_) | |
53 service_->AdjustKeywordBatchModeLevel(false); | |
54 } | |
55 | |
56 WebDataService::WebDataService(scoped_refptr<WebDatabaseService> wdbs, | 30 WebDataService::WebDataService(scoped_refptr<WebDatabaseService> wdbs, |
57 const ProfileErrorCallback& callback) | 31 const ProfileErrorCallback& callback) |
58 : WebDataServiceBase( | 32 : WebDataServiceBase( |
59 wdbs, callback, | 33 wdbs, callback, |
60 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), | 34 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)) { |
61 keyword_batch_mode_level_(0) { | |
62 } | 35 } |
63 | 36 |
64 ////////////////////////////////////////////////////////////////////////////// | 37 ////////////////////////////////////////////////////////////////////////////// |
65 // | |
66 // Keywords. | |
67 // | |
68 ////////////////////////////////////////////////////////////////////////////// | |
69 | |
70 void WebDataService::AddKeyword(const TemplateURLData& data) { | |
71 if (keyword_batch_mode_level_) { | |
72 queued_keyword_operations_.push_back( | |
73 KeywordTable::Operation(KeywordTable::ADD, data)); | |
74 } else { | |
75 AdjustKeywordBatchModeLevel(true); | |
76 AddKeyword(data); | |
77 AdjustKeywordBatchModeLevel(false); | |
78 } | |
79 } | |
80 | |
81 void WebDataService::RemoveKeyword(TemplateURLID id) { | |
82 if (keyword_batch_mode_level_) { | |
83 TemplateURLData data; | |
84 data.id = id; | |
85 queued_keyword_operations_.push_back( | |
86 KeywordTable::Operation(KeywordTable::REMOVE, data)); | |
87 } else { | |
88 AdjustKeywordBatchModeLevel(true); | |
89 RemoveKeyword(id); | |
90 AdjustKeywordBatchModeLevel(false); | |
91 } | |
92 } | |
93 | |
94 void WebDataService::UpdateKeyword(const TemplateURLData& data) { | |
95 if (keyword_batch_mode_level_) { | |
96 queued_keyword_operations_.push_back( | |
97 KeywordTable::Operation(KeywordTable::UPDATE, data)); | |
98 } else { | |
99 AdjustKeywordBatchModeLevel(true); | |
100 UpdateKeyword(data); | |
101 AdjustKeywordBatchModeLevel(false); | |
102 } | |
103 } | |
104 | |
105 WebDataServiceBase::Handle WebDataService::GetKeywords( | |
106 WebDataServiceConsumer* consumer) { | |
107 return wdbs_->ScheduleDBTaskWithResult( | |
108 FROM_HERE, Bind(&WebDataService::GetKeywordsImpl, this), consumer); | |
109 } | |
110 | |
111 void WebDataService::SetDefaultSearchProviderID(TemplateURLID id) { | |
112 wdbs_->ScheduleDBTask( | |
113 FROM_HERE, | |
114 Bind(&WebDataService::SetDefaultSearchProviderIDImpl, this, id)); | |
115 } | |
116 | |
117 void WebDataService::SetBuiltinKeywordVersion(int version) { | |
118 wdbs_->ScheduleDBTask( | |
119 FROM_HERE, | |
120 Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version)); | |
121 } | |
122 | |
123 ////////////////////////////////////////////////////////////////////////////// | |
124 // | 38 // |
125 // Web Apps | 39 // Web Apps |
126 // | 40 // |
127 ////////////////////////////////////////////////////////////////////////////// | 41 ////////////////////////////////////////////////////////////////////////////// |
128 | 42 |
129 void WebDataService::SetWebAppImage(const GURL& app_url, | 43 void WebDataService::SetWebAppImage(const GURL& app_url, |
130 const SkBitmap& image) { | 44 const SkBitmap& image) { |
131 wdbs_->ScheduleDBTask(FROM_HERE, | 45 wdbs_->ScheduleDBTask(FROM_HERE, |
132 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image)); | 46 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image)); |
133 } | 47 } |
(...skipping 14 matching lines...) Expand all Loading... |
148 const GURL& app_url, WebDataServiceConsumer* consumer) { | 62 const GURL& app_url, WebDataServiceConsumer* consumer) { |
149 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, | 63 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
150 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer); | 64 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer); |
151 } | 65 } |
152 | 66 |
153 //////////////////////////////////////////////////////////////////////////////// | 67 //////////////////////////////////////////////////////////////////////////////// |
154 | 68 |
155 WebDataService::WebDataService() | 69 WebDataService::WebDataService() |
156 : WebDataServiceBase( | 70 : WebDataServiceBase( |
157 NULL, ProfileErrorCallback(), | 71 NULL, ProfileErrorCallback(), |
158 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), | 72 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)) { |
159 keyword_batch_mode_level_(0) { | |
160 } | 73 } |
161 | 74 |
162 WebDataService::~WebDataService() { | 75 WebDataService::~WebDataService() { |
163 DCHECK(!keyword_batch_mode_level_); | |
164 } | |
165 | |
166 void WebDataService::AdjustKeywordBatchModeLevel(bool entering_batch_mode) { | |
167 if (entering_batch_mode) { | |
168 ++keyword_batch_mode_level_; | |
169 } else { | |
170 DCHECK(keyword_batch_mode_level_); | |
171 --keyword_batch_mode_level_; | |
172 if (!keyword_batch_mode_level_ && !queued_keyword_operations_.empty()) { | |
173 wdbs_->ScheduleDBTask( | |
174 FROM_HERE, | |
175 Bind(&WebDataService::PerformKeywordOperationsImpl, this, | |
176 queued_keyword_operations_)); | |
177 queued_keyword_operations_.clear(); | |
178 } | |
179 } | |
180 } | 76 } |
181 | 77 |
182 //////////////////////////////////////////////////////////////////////////////// | 78 //////////////////////////////////////////////////////////////////////////////// |
183 // | |
184 // Keywords implementation. | |
185 // | |
186 //////////////////////////////////////////////////////////////////////////////// | |
187 | |
188 WebDatabase::State WebDataService::PerformKeywordOperationsImpl( | |
189 const KeywordTable::Operations& operations, | |
190 WebDatabase* db) { | |
191 return KeywordTable::FromWebDatabase(db)->PerformOperations(operations) ? | |
192 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED; | |
193 } | |
194 | |
195 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl(WebDatabase* db) { | |
196 scoped_ptr<WDTypedResult> result_ptr; | |
197 WDKeywordsResult result; | |
198 if (KeywordTable::FromWebDatabase(db)->GetKeywords(&result.keywords)) { | |
199 result.default_search_provider_id = | |
200 KeywordTable::FromWebDatabase(db)->GetDefaultSearchProviderID(); | |
201 result.builtin_keyword_version = | |
202 KeywordTable::FromWebDatabase(db)->GetBuiltinKeywordVersion(); | |
203 result_ptr.reset(new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result)); | |
204 } | |
205 return result_ptr.Pass(); | |
206 } | |
207 | |
208 WebDatabase::State WebDataService::SetDefaultSearchProviderIDImpl( | |
209 TemplateURLID id, | |
210 WebDatabase* db) { | |
211 return KeywordTable::FromWebDatabase(db)->SetDefaultSearchProviderID(id) ? | |
212 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED; | |
213 } | |
214 | |
215 WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl( | |
216 int version, | |
217 WebDatabase* db) { | |
218 return KeywordTable::FromWebDatabase(db)->SetBuiltinKeywordVersion(version) ? | |
219 WebDatabase::COMMIT_NEEDED : WebDatabase::COMMIT_NOT_NEEDED; | |
220 } | |
221 | |
222 //////////////////////////////////////////////////////////////////////////////// | |
223 // | 79 // |
224 // Web Apps implementation. | 80 // Web Apps implementation. |
225 // | 81 // |
226 //////////////////////////////////////////////////////////////////////////////// | 82 //////////////////////////////////////////////////////////////////////////////// |
227 | 83 |
228 WebDatabase::State WebDataService::SetWebAppImageImpl( | 84 WebDatabase::State WebDataService::SetWebAppImageImpl( |
229 const GURL& app_url, const SkBitmap& image, WebDatabase* db) { | 85 const GURL& app_url, const SkBitmap& image, WebDatabase* db) { |
230 WebAppsTable::FromWebDatabase(db)->SetWebAppImage(app_url, image); | 86 WebAppsTable::FromWebDatabase(db)->SetWebAppImage(app_url, image); |
231 return WebDatabase::COMMIT_NEEDED; | 87 return WebDatabase::COMMIT_NEEDED; |
232 } | 88 } |
(...skipping 13 matching lines...) Expand all Loading... |
246 | 102 |
247 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( | 103 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( |
248 const GURL& app_url, WebDatabase* db) { | 104 const GURL& app_url, WebDatabase* db) { |
249 WDAppImagesResult result; | 105 WDAppImagesResult result; |
250 result.has_all_images = | 106 result.has_all_images = |
251 WebAppsTable::FromWebDatabase(db)->GetWebAppHasAllImages(app_url); | 107 WebAppsTable::FromWebDatabase(db)->GetWebAppHasAllImages(app_url); |
252 WebAppsTable::FromWebDatabase(db)->GetWebAppImages(app_url, &result.images); | 108 WebAppsTable::FromWebDatabase(db)->GetWebAppImages(app_url, &result.images); |
253 return scoped_ptr<WDTypedResult>( | 109 return scoped_ptr<WDTypedResult>( |
254 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); | 110 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); |
255 } | 111 } |
OLD | NEW |