| 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" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "base/threading/thread.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | |
| 14 #include "chrome/browser/search_engines/template_url.h" | 7 #include "chrome/browser/search_engines/template_url.h" |
| 15 #include "chrome/browser/ui/profile_error_dialog.h" | |
| 16 #include "chrome/browser/webdata/autocomplete_syncable_service.h" | 8 #include "chrome/browser/webdata/autocomplete_syncable_service.h" |
| 17 #include "chrome/browser/webdata/autofill_change.h" | 9 #include "chrome/browser/webdata/autofill_change.h" |
| 18 #include "chrome/browser/webdata/autofill_entry.h" | 10 #include "chrome/browser/webdata/autofill_entry.h" |
| 19 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" | 11 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
| 20 #include "chrome/browser/webdata/autofill_table.h" | 12 #include "chrome/browser/webdata/autofill_table.h" |
| 21 #include "chrome/browser/webdata/keyword_table.h" | 13 #include "chrome/browser/webdata/keyword_table.h" |
| 22 #include "chrome/browser/webdata/logins_table.h" | 14 #include "chrome/browser/webdata/logins_table.h" |
| 23 #include "chrome/browser/webdata/token_service_table.h" | 15 #include "chrome/browser/webdata/token_service_table.h" |
| 24 #include "chrome/browser/webdata/web_apps_table.h" | 16 #include "chrome/browser/webdata/web_apps_table.h" |
| 25 #include "chrome/browser/webdata/web_database.h" | 17 #include "chrome/browser/webdata/web_database_service.h" |
| 26 #include "chrome/browser/webdata/web_intents_table.h" | 18 #include "chrome/browser/webdata/web_intents_table.h" |
| 27 #include "chrome/common/chrome_constants.h" | |
| 28 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "components/autofill/browser/autofill_country.h" | 20 #include "components/autofill/browser/autofill_country.h" |
| 30 #include "components/autofill/browser/autofill_profile.h" | 21 #include "components/autofill/browser/autofill_profile.h" |
| 31 #include "components/autofill/browser/credit_card.h" | 22 #include "components/autofill/browser/credit_card.h" |
| 32 #include "components/autofill/common/form_field_data.h" | 23 #include "components/autofill/common/form_field_data.h" |
| 33 #ifdef DEBUG | |
| 34 #include "content/public/browser/browser_thread.h" | |
| 35 #endif | |
| 36 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
| 37 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| 39 #include "grit/chromium_strings.h" | |
| 40 #include "grit/generated_resources.h" | |
| 41 #include "third_party/skia/include/core/SkBitmap.h" | 27 #include "third_party/skia/include/core/SkBitmap.h" |
| 42 | 28 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 29 //////////////////////////////////////////////////////////////////////////////// |
| 44 // | 30 // |
| 45 // WebDataService implementation. | 31 // WebDataService implementation. |
| 46 // | 32 // |
| 47 //////////////////////////////////////////////////////////////////////////////// | 33 //////////////////////////////////////////////////////////////////////////////// |
| 48 | 34 |
| 49 using base::Bind; | 35 using base::Bind; |
| 50 using base::Time; | 36 using base::Time; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 71 WDAppImagesResult::~WDAppImagesResult() {} | 57 WDAppImagesResult::~WDAppImagesResult() {} |
| 72 | 58 |
| 73 WDKeywordsResult::WDKeywordsResult() | 59 WDKeywordsResult::WDKeywordsResult() |
| 74 : default_search_provider_id(0), | 60 : default_search_provider_id(0), |
| 75 builtin_keyword_version(0) { | 61 builtin_keyword_version(0) { |
| 76 } | 62 } |
| 77 | 63 |
| 78 WDKeywordsResult::~WDKeywordsResult() {} | 64 WDKeywordsResult::~WDKeywordsResult() {} |
| 79 | 65 |
| 80 WebDataService::WebDataService() | 66 WebDataService::WebDataService() |
| 81 : is_running_(false), | 67 : autocomplete_syncable_service_(NULL), |
| 82 db_(NULL), | 68 autofill_profile_syncable_service_(NULL) { |
| 83 request_manager_(new WebDataRequestManager()), | |
| 84 app_locale_(AutofillCountry::ApplicationLocale()), | |
| 85 autocomplete_syncable_service_(NULL), | |
| 86 autofill_profile_syncable_service_(NULL), | |
| 87 failed_init_(false), | |
| 88 should_commit_(false), | |
| 89 main_loop_(MessageLoop::current()) { | |
| 90 // WebDataService requires DB thread if instantiated. | |
| 91 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) | |
| 92 // if you do not want to instantiate WebDataService in your test. | |
| 93 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); | |
| 94 } | 69 } |
| 95 | 70 |
| 96 // static | 71 // static |
| 97 void WebDataService::NotifyOfMultipleAutofillChanges( | 72 void WebDataService::NotifyOfMultipleAutofillChanges( |
| 98 WebDataService* web_data_service) { | 73 WebDataService* web_data_service) { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 100 | 75 |
| 101 if (!web_data_service) | 76 if (!web_data_service) |
| 102 return; | 77 return; |
| 103 | 78 |
| 104 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 105 BrowserThread::UI, FROM_HERE, | 80 BrowserThread::UI, FROM_HERE, |
| 106 Bind(&NotifyOfMultipleAutofillChangesTask, | 81 Bind(&NotifyOfMultipleAutofillChangesTask, |
| 107 make_scoped_refptr(web_data_service))); | 82 make_scoped_refptr(web_data_service))); |
| 108 } | 83 } |
| 109 | 84 |
| 110 void WebDataService::ShutdownOnUIThread() { | 85 void WebDataService::ShutdownOnUIThread() { |
| 111 ScheduleTask(FROM_HERE, | 86 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 112 Bind(&WebDataService::ShutdownSyncableServices, this)); | 87 Bind(&WebDataService::ShutdownSyncableServices, this)); |
| 113 UnloadDatabase(); | 88 WebDataServiceBase::ShutdownOnUIThread(); |
| 114 } | 89 } |
| 115 | 90 |
| 116 bool WebDataService::Init(const base::FilePath& profile_path) { | 91 void WebDataService::Init(const base::FilePath& path) { |
| 117 base::FilePath path = profile_path; | 92 WebDataServiceBase::Init(path); |
| 118 path = path.Append(chrome::kWebDataFilename); | 93 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 119 return InitWithPath(path); | 94 Bind(&WebDataService::InitializeSyncableServices, this)); |
| 120 } | |
| 121 | |
| 122 bool WebDataService::IsRunning() const { | |
| 123 return is_running_; | |
| 124 } | |
| 125 | |
| 126 void WebDataService::UnloadDatabase() { | |
| 127 ScheduleTask(FROM_HERE, Bind(&WebDataService::ShutdownDatabase, this)); | |
| 128 } | |
| 129 | |
| 130 void WebDataService::CancelRequest(Handle h) { | |
| 131 request_manager_->CancelRequest(h); | |
| 132 } | |
| 133 | |
| 134 content::NotificationSource WebDataService::GetNotificationSource() { | |
| 135 return content::Source<WebDataService>(this); | |
| 136 } | |
| 137 | |
| 138 bool WebDataService::IsDatabaseLoaded() { | |
| 139 return db_ != NULL; | |
| 140 } | |
| 141 | |
| 142 WebDatabase* WebDataService::GetDatabase() { | |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 144 return db_; | |
| 145 } | 95 } |
| 146 | 96 |
| 147 ////////////////////////////////////////////////////////////////////////////// | 97 ////////////////////////////////////////////////////////////////////////////// |
| 148 // | 98 // |
| 149 // Keywords. | 99 // Keywords. |
| 150 // | 100 // |
| 151 ////////////////////////////////////////////////////////////////////////////// | 101 ////////////////////////////////////////////////////////////////////////////// |
| 152 | 102 |
| 153 void WebDataService::AddKeyword(const TemplateURLData& data) { | 103 void WebDataService::AddKeyword(const TemplateURLData& data) { |
| 154 ScheduleDBTask( | 104 wdbs_->ScheduleDBTask( |
| 155 FROM_HERE, Bind(&WebDataService::AddKeywordImpl, this, data)); | 105 FROM_HERE, Bind(&WebDataService::AddKeywordImpl, this, data)); |
| 156 } | 106 } |
| 157 | 107 |
| 158 void WebDataService::RemoveKeyword(TemplateURLID id) { | 108 void WebDataService::RemoveKeyword(TemplateURLID id) { |
| 159 ScheduleDBTask( | 109 wdbs_->ScheduleDBTask( |
| 160 FROM_HERE, Bind(&WebDataService::RemoveKeywordImpl, this, id)); | 110 FROM_HERE, Bind(&WebDataService::RemoveKeywordImpl, this, id)); |
| 161 } | 111 } |
| 162 | 112 |
| 163 void WebDataService::UpdateKeyword(const TemplateURLData& data) { | 113 void WebDataService::UpdateKeyword(const TemplateURLData& data) { |
| 164 ScheduleDBTask( | 114 wdbs_->ScheduleDBTask( |
| 165 FROM_HERE, Bind(&WebDataService::UpdateKeywordImpl, this, data)); | 115 FROM_HERE, Bind(&WebDataService::UpdateKeywordImpl, this, data)); |
| 166 } | 116 } |
| 167 | 117 |
| 168 WebDataService::Handle WebDataService::GetKeywords( | 118 WebDataService::Handle WebDataService::GetKeywords( |
| 169 WebDataServiceConsumer* consumer) { | 119 WebDataServiceConsumer* consumer) { |
| 170 return ScheduleDBTaskWithResult(FROM_HERE, | 120 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 171 Bind(&WebDataService::GetKeywordsImpl, this), consumer); | 121 Bind(&WebDataService::GetKeywordsImpl, this), consumer); |
| 172 } | 122 } |
| 173 | 123 |
| 174 void WebDataService::SetDefaultSearchProvider(const TemplateURL* url) { | 124 void WebDataService::SetDefaultSearchProvider(const TemplateURL* url) { |
| 175 ScheduleDBTask(FROM_HERE, | 125 wdbs_->ScheduleDBTask(FROM_HERE, |
| 176 Bind(&WebDataService::SetDefaultSearchProviderImpl, this, | 126 Bind(&WebDataService::SetDefaultSearchProviderImpl, this, |
| 177 url ? url->id() : 0)); | 127 url ? url->id() : 0)); |
| 178 } | 128 } |
| 179 | 129 |
| 180 void WebDataService::SetBuiltinKeywordVersion(int version) { | 130 void WebDataService::SetBuiltinKeywordVersion(int version) { |
| 181 ScheduleDBTask(FROM_HERE, | 131 wdbs_->ScheduleDBTask(FROM_HERE, |
| 182 Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version)); | 132 Bind(&WebDataService::SetBuiltinKeywordVersionImpl, this, version)); |
| 183 } | 133 } |
| 184 | 134 |
| 185 ////////////////////////////////////////////////////////////////////////////// | 135 ////////////////////////////////////////////////////////////////////////////// |
| 186 // | 136 // |
| 187 // Web Apps | 137 // Web Apps |
| 188 // | 138 // |
| 189 ////////////////////////////////////////////////////////////////////////////// | 139 ////////////////////////////////////////////////////////////////////////////// |
| 190 | 140 |
| 191 void WebDataService::SetWebAppImage(const GURL& app_url, | 141 void WebDataService::SetWebAppImage(const GURL& app_url, |
| 192 const SkBitmap& image) { | 142 const SkBitmap& image) { |
| 193 ScheduleDBTask(FROM_HERE, | 143 wdbs_->ScheduleDBTask(FROM_HERE, |
| 194 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image)); | 144 Bind(&WebDataService::SetWebAppImageImpl, this, app_url, image)); |
| 195 } | 145 } |
| 196 | 146 |
| 197 void WebDataService::SetWebAppHasAllImages(const GURL& app_url, | 147 void WebDataService::SetWebAppHasAllImages(const GURL& app_url, |
| 198 bool has_all_images) { | 148 bool has_all_images) { |
| 199 ScheduleDBTask(FROM_HERE, | 149 wdbs_->ScheduleDBTask(FROM_HERE, |
| 200 Bind(&WebDataService::SetWebAppHasAllImagesImpl, this, app_url, | 150 Bind(&WebDataService::SetWebAppHasAllImagesImpl, this, app_url, |
| 201 has_all_images)); | 151 has_all_images)); |
| 202 } | 152 } |
| 203 | 153 |
| 204 void WebDataService::RemoveWebApp(const GURL& app_url) { | 154 void WebDataService::RemoveWebApp(const GURL& app_url) { |
| 205 ScheduleDBTask(FROM_HERE, | 155 wdbs_->ScheduleDBTask(FROM_HERE, |
| 206 Bind(&WebDataService::RemoveWebAppImpl, this, app_url)); | 156 Bind(&WebDataService::RemoveWebAppImpl, this, app_url)); |
| 207 } | 157 } |
| 208 | 158 |
| 209 WebDataService::Handle WebDataService::GetWebAppImages( | 159 WebDataService::Handle WebDataService::GetWebAppImages( |
| 210 const GURL& app_url, | 160 const GURL& app_url, WebDataServiceConsumer* consumer) { |
| 211 WebDataServiceConsumer* consumer) { | 161 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 212 return ScheduleDBTaskWithResult(FROM_HERE, | |
| 213 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer); | 162 Bind(&WebDataService::GetWebAppImagesImpl, this, app_url), consumer); |
| 214 } | 163 } |
| 215 | 164 |
| 216 //////////////////////////////////////////////////////////////////////////////// | 165 //////////////////////////////////////////////////////////////////////////////// |
| 217 // | 166 // |
| 218 // Token Service | 167 // Token Service |
| 219 // | 168 // |
| 220 //////////////////////////////////////////////////////////////////////////////// | 169 //////////////////////////////////////////////////////////////////////////////// |
| 221 | 170 |
| 222 void WebDataService::SetTokenForService(const std::string& service, | 171 void WebDataService::SetTokenForService(const std::string& service, |
| 223 const std::string& token) { | 172 const std::string& token) { |
| 224 ScheduleDBTask(FROM_HERE, | 173 wdbs_->ScheduleDBTask(FROM_HERE, |
| 225 Bind(&WebDataService::SetTokenForServiceImpl, this, service, token)); | 174 Bind(&WebDataService::SetTokenForServiceImpl, this, service, token)); |
| 226 } | 175 } |
| 227 | 176 |
| 228 void WebDataService::RemoveAllTokens() { | 177 void WebDataService::RemoveAllTokens() { |
| 229 ScheduleDBTask(FROM_HERE, Bind(&WebDataService::RemoveAllTokensImpl, this)); | 178 wdbs_->ScheduleDBTask(FROM_HERE, |
| 179 Bind(&WebDataService::RemoveAllTokensImpl, this)); |
| 230 } | 180 } |
| 231 | 181 |
| 232 // Null on failure. Success is WDResult<std::string> | 182 // Null on failure. Success is WDResult<std::string> |
| 233 WebDataService::Handle WebDataService::GetAllTokens( | 183 WebDataService::Handle WebDataService::GetAllTokens( |
| 234 WebDataServiceConsumer* consumer) { | 184 WebDataServiceConsumer* consumer) { |
| 235 return ScheduleDBTaskWithResult(FROM_HERE, | 185 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 236 Bind(&WebDataService::GetAllTokensImpl, this), consumer); | 186 Bind(&WebDataService::GetAllTokensImpl, this), consumer); |
| 237 } | 187 } |
| 238 | 188 |
| 239 //////////////////////////////////////////////////////////////////////////////// | 189 //////////////////////////////////////////////////////////////////////////////// |
| 240 // | 190 // |
| 241 // Autofill. | 191 // Autofill. |
| 242 // | 192 // |
| 243 //////////////////////////////////////////////////////////////////////////////// | 193 //////////////////////////////////////////////////////////////////////////////// |
| 244 | 194 |
| 245 void WebDataService::AddFormFields( | 195 void WebDataService::AddFormFields( |
| 246 const std::vector<FormFieldData>& fields) { | 196 const std::vector<FormFieldData>& fields) { |
| 247 ScheduleDBTask(FROM_HERE, | 197 wdbs_->ScheduleDBTask(FROM_HERE, |
| 248 Bind(&WebDataService::AddFormElementsImpl, this, fields)); | 198 Bind(&WebDataService::AddFormElementsImpl, this, fields)); |
| 249 } | 199 } |
| 250 | 200 |
| 251 WebDataService::Handle WebDataService::GetFormValuesForElementName( | 201 WebDataService::Handle WebDataService::GetFormValuesForElementName( |
| 252 const string16& name, const string16& prefix, int limit, | 202 const string16& name, const string16& prefix, int limit, |
| 253 WebDataServiceConsumer* consumer) { | 203 WebDataServiceConsumer* consumer) { |
| 254 return ScheduleDBTaskWithResult(FROM_HERE, | 204 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 255 Bind(&WebDataService::GetFormValuesForElementNameImpl, | 205 Bind(&WebDataService::GetFormValuesForElementNameImpl, |
| 256 this, name, prefix, limit), | 206 this, name, prefix, limit), consumer); |
| 257 consumer); | |
| 258 } | 207 } |
| 259 | 208 |
| 260 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin, | 209 void WebDataService::RemoveFormElementsAddedBetween(const Time& delete_begin, |
| 261 const Time& delete_end) { | 210 const Time& delete_end) { |
| 262 ScheduleDBTask(FROM_HERE, | 211 wdbs_->ScheduleDBTask(FROM_HERE, |
| 263 Bind(&WebDataService::RemoveFormElementsAddedBetweenImpl, | 212 Bind(&WebDataService::RemoveFormElementsAddedBetweenImpl, |
| 264 this, delete_begin, delete_end)); | 213 this, delete_begin, delete_end)); |
| 265 } | 214 } |
| 266 | 215 |
| 267 void WebDataService::RemoveExpiredFormElements() { | 216 void WebDataService::RemoveExpiredFormElements() { |
| 268 ScheduleDBTask(FROM_HERE, | 217 wdbs_->ScheduleDBTask(FROM_HERE, |
| 269 Bind(&WebDataService::RemoveExpiredFormElementsImpl, this)); | 218 Bind(&WebDataService::RemoveExpiredFormElementsImpl, this)); |
| 270 } | 219 } |
| 271 | 220 |
| 272 void WebDataService::RemoveFormValueForElementName( | 221 void WebDataService::RemoveFormValueForElementName( |
| 273 const string16& name, const string16& value) { | 222 const string16& name, const string16& value) { |
| 274 ScheduleDBTask(FROM_HERE, | 223 wdbs_->ScheduleDBTask(FROM_HERE, |
| 275 Bind(&WebDataService::RemoveFormValueForElementNameImpl, | 224 Bind(&WebDataService::RemoveFormValueForElementNameImpl, |
| 276 this, name, value)); | 225 this, name, value)); |
| 277 } | 226 } |
| 278 | 227 |
| 279 void WebDataService::AddAutofillProfile(const AutofillProfile& profile) { | 228 void WebDataService::AddAutofillProfile(const AutofillProfile& profile) { |
| 280 ScheduleDBTask(FROM_HERE, | 229 wdbs_->ScheduleDBTask(FROM_HERE, |
| 281 Bind(&WebDataService::AddAutofillProfileImpl, this, profile)); | 230 Bind(&WebDataService::AddAutofillProfileImpl, this, profile)); |
| 282 } | 231 } |
| 283 | 232 |
| 284 void WebDataService::UpdateAutofillProfile(const AutofillProfile& profile) { | 233 void WebDataService::UpdateAutofillProfile(const AutofillProfile& profile) { |
| 285 ScheduleDBTask(FROM_HERE, | 234 wdbs_->ScheduleDBTask(FROM_HERE, |
| 286 Bind(&WebDataService::UpdateAutofillProfileImpl, this, profile)); | 235 Bind(&WebDataService::UpdateAutofillProfileImpl, this, profile)); |
| 287 } | 236 } |
| 288 | 237 |
| 289 void WebDataService::RemoveAutofillProfile(const std::string& guid) { | 238 void WebDataService::RemoveAutofillProfile(const std::string& guid) { |
| 290 ScheduleDBTask(FROM_HERE, | 239 wdbs_->ScheduleDBTask(FROM_HERE, |
| 291 Bind(&WebDataService::RemoveAutofillProfileImpl, this, guid)); | 240 Bind(&WebDataService::RemoveAutofillProfileImpl, this, guid)); |
| 292 } | 241 } |
| 293 | 242 |
| 294 WebDataService::Handle WebDataService::GetAutofillProfiles( | 243 WebDataService::Handle WebDataService::GetAutofillProfiles( |
| 295 WebDataServiceConsumer* consumer) { | 244 WebDataServiceConsumer* consumer) { |
| 296 return ScheduleDBTaskWithResult(FROM_HERE, | 245 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 297 Bind(&WebDataService::GetAutofillProfilesImpl, this), consumer); | 246 Bind(&WebDataService::GetAutofillProfilesImpl, this), consumer); |
| 298 } | 247 } |
| 299 | 248 |
| 300 void WebDataService::AddCreditCard(const CreditCard& credit_card) { | 249 void WebDataService::AddCreditCard(const CreditCard& credit_card) { |
| 301 ScheduleDBTask(FROM_HERE, | 250 wdbs_->ScheduleDBTask(FROM_HERE, |
| 302 Bind(&WebDataService::AddCreditCardImpl, this, credit_card)); | 251 Bind(&WebDataService::AddCreditCardImpl, this, credit_card)); |
| 303 } | 252 } |
| 304 | 253 |
| 305 void WebDataService::UpdateCreditCard(const CreditCard& credit_card) { | 254 void WebDataService::UpdateCreditCard(const CreditCard& credit_card) { |
| 306 ScheduleDBTask(FROM_HERE, | 255 wdbs_->ScheduleDBTask(FROM_HERE, |
| 307 Bind(&WebDataService::UpdateCreditCardImpl, this, credit_card)); | 256 Bind(&WebDataService::UpdateCreditCardImpl, this, credit_card)); |
| 308 } | 257 } |
| 309 | 258 |
| 310 void WebDataService::RemoveCreditCard(const std::string& guid) { | 259 void WebDataService::RemoveCreditCard(const std::string& guid) { |
| 311 ScheduleDBTask(FROM_HERE, | 260 wdbs_->ScheduleDBTask(FROM_HERE, |
| 312 Bind(&WebDataService::RemoveCreditCardImpl, this, guid)); | 261 Bind(&WebDataService::RemoveCreditCardImpl, this, guid)); |
| 313 } | 262 } |
| 314 | 263 |
| 315 WebDataService::Handle WebDataService::GetCreditCards( | 264 WebDataService::Handle WebDataService::GetCreditCards( |
| 316 WebDataServiceConsumer* consumer) { | 265 WebDataServiceConsumer* consumer) { |
| 317 return ScheduleDBTaskWithResult(FROM_HERE, | 266 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 318 Bind(&WebDataService::GetCreditCardsImpl, this), consumer); | 267 Bind(&WebDataService::GetCreditCardsImpl, this), consumer); |
| 319 } | 268 } |
| 320 | 269 |
| 321 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetween( | 270 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetween( |
| 322 const Time& delete_begin, | 271 const Time& delete_begin, |
| 323 const Time& delete_end) { | 272 const Time& delete_end) { |
| 324 ScheduleDBTask(FROM_HERE, Bind( | 273 wdbs_->ScheduleDBTask(FROM_HERE, Bind( |
| 325 &WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl, | 274 &WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl, |
| 326 this, delete_begin, delete_end)); | 275 this, delete_begin, delete_end)); |
| 327 } | 276 } |
| 328 | 277 |
| 329 WebDataService::~WebDataService() { | 278 WebDataService::~WebDataService() { |
| 330 if (is_running_ && db_) { | 279 DCHECK(!autocomplete_syncable_service_); |
| 331 NOTREACHED() << "WebDataService dtor called without Shutdown"; | 280 DCHECK(!autofill_profile_syncable_service_); |
| 332 } | |
| 333 } | |
| 334 | |
| 335 bool WebDataService::InitWithPath(const base::FilePath& path) { | |
| 336 path_ = path; | |
| 337 is_running_ = true; | |
| 338 | |
| 339 ScheduleTask(FROM_HERE, | |
| 340 Bind(&WebDataService::InitializeDatabaseIfNecessary, this)); | |
| 341 ScheduleTask(FROM_HERE, | |
| 342 Bind(&WebDataService::InitializeSyncableServices, this)); | |
| 343 return true; | |
| 344 } | 281 } |
| 345 | 282 |
| 346 //////////////////////////////////////////////////////////////////////////////// | 283 //////////////////////////////////////////////////////////////////////////////// |
| 347 // | 284 // |
| 348 // The following methods are executed in Chrome_WebDataThread. | 285 // The following methods are executed on the DB thread. |
| 349 // | 286 // |
| 350 //////////////////////////////////////////////////////////////////////////////// | 287 //////////////////////////////////////////////////////////////////////////////// |
| 351 | 288 |
| 352 void WebDataService::DBInitFailed(sql::InitStatus init_status) { | |
| 353 ShowProfileErrorDialog( | |
| 354 (init_status == sql::INIT_FAILURE) ? | |
| 355 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR); | |
| 356 } | |
| 357 | |
| 358 void WebDataService::InitializeDatabaseIfNecessary() { | |
| 359 if (db_ || failed_init_ || path_.empty()) | |
| 360 return; | |
| 361 | |
| 362 // In the rare case where the db fails to initialize a dialog may get shown | |
| 363 // that blocks the caller, yet allows other messages through. For this reason | |
| 364 // we only set db_ to the created database if creation is successful. That | |
| 365 // way other methods won't do anything as db_ is still NULL. | |
| 366 WebDatabase* db = new WebDatabase(); | |
| 367 sql::InitStatus init_status = db->Init(path_, app_locale_); | |
| 368 if (init_status != sql::INIT_OK) { | |
| 369 LOG(ERROR) << "Cannot initialize the web database: " << init_status; | |
| 370 failed_init_ = true; | |
| 371 delete db; | |
| 372 if (main_loop_) { | |
| 373 main_loop_->PostTask( | |
| 374 FROM_HERE, | |
| 375 base::Bind(&WebDataService::DBInitFailed, this, init_status)); | |
| 376 } | |
| 377 return; | |
| 378 } | |
| 379 | |
| 380 BrowserThread::PostTask( | |
| 381 BrowserThread::UI, FROM_HERE, | |
| 382 base::Bind(&WebDataService::NotifyDatabaseLoadedOnUIThread, this)); | |
| 383 | |
| 384 db_ = db; | |
| 385 db_->BeginTransaction(); | |
| 386 } | |
| 387 | |
| 388 void WebDataService::InitializeSyncableServices() { | 289 void WebDataService::InitializeSyncableServices() { |
| 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 390 DCHECK(!autocomplete_syncable_service_); | 291 DCHECK(!autocomplete_syncable_service_); |
| 391 DCHECK(!autofill_profile_syncable_service_); | 292 DCHECK(!autofill_profile_syncable_service_); |
| 392 | 293 |
| 393 autocomplete_syncable_service_ = new AutocompleteSyncableService(this); | 294 autocomplete_syncable_service_ = new AutocompleteSyncableService(this); |
| 394 autofill_profile_syncable_service_ = new AutofillProfileSyncableService(this); | 295 autofill_profile_syncable_service_ = new AutofillProfileSyncableService(this); |
| 395 } | 296 } |
| 396 | 297 |
| 397 void WebDataService::NotifyDatabaseLoadedOnUIThread() { | |
| 398 // Notify that the database has been initialized. | |
| 399 content::NotificationService::current()->Notify( | |
| 400 chrome::NOTIFICATION_WEB_DATABASE_LOADED, | |
| 401 content::Source<WebDataService>(this), | |
| 402 content::NotificationService::NoDetails()); | |
| 403 } | |
| 404 | |
| 405 void WebDataService::ShutdownDatabase() { | |
| 406 should_commit_ = false; | |
| 407 | |
| 408 if (db_) { | |
| 409 db_->CommitTransaction(); | |
| 410 delete db_; | |
| 411 db_ = NULL; | |
| 412 } | |
| 413 } | |
| 414 | |
| 415 void WebDataService::ShutdownSyncableServices() { | 298 void WebDataService::ShutdownSyncableServices() { |
| 416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 417 | 300 |
| 418 delete autocomplete_syncable_service_; | 301 delete autocomplete_syncable_service_; |
| 419 autocomplete_syncable_service_ = NULL; | 302 autocomplete_syncable_service_ = NULL; |
| 420 delete autofill_profile_syncable_service_; | 303 delete autofill_profile_syncable_service_; |
| 421 autofill_profile_syncable_service_ = NULL; | 304 autofill_profile_syncable_service_ = NULL; |
| 422 } | 305 } |
| 423 | 306 |
| 424 void WebDataService::Commit() { | |
| 425 if (should_commit_) { | |
| 426 should_commit_ = false; | |
| 427 | |
| 428 if (db_) { | |
| 429 db_->CommitTransaction(); | |
| 430 db_->BeginTransaction(); | |
| 431 } | |
| 432 } | |
| 433 } | |
| 434 | |
| 435 void WebDataService::ScheduleTask(const tracked_objects::Location& from_here, | |
| 436 const base::Closure& task) { | |
| 437 if (is_running_) | |
| 438 BrowserThread::PostTask(BrowserThread::DB, from_here, task); | |
| 439 else | |
| 440 NOTREACHED() << "Task scheduled after Shutdown()"; | |
| 441 } | |
| 442 | |
| 443 void WebDataService::ScheduleDBTask( | |
| 444 const tracked_objects::Location& from_here, | |
| 445 const base::Closure& task) { | |
| 446 scoped_ptr<WebDataRequest> request( | |
| 447 new WebDataRequest(NULL, request_manager_.get())); | |
| 448 if (is_running_) { | |
| 449 BrowserThread::PostTask(BrowserThread::DB, from_here, | |
| 450 base::Bind(&WebDataService::DBTaskWrapper, this, task, | |
| 451 base::Passed(&request))); | |
| 452 } else { | |
| 453 NOTREACHED() << "Task scheduled after Shutdown()"; | |
| 454 } | |
| 455 } | |
| 456 | |
| 457 WebDataService::Handle WebDataService::ScheduleDBTaskWithResult( | |
| 458 const tracked_objects::Location& from_here, | |
| 459 const ResultTask& task, | |
| 460 WebDataServiceConsumer* consumer) { | |
| 461 DCHECK(consumer); | |
| 462 scoped_ptr<WebDataRequest> request( | |
| 463 new WebDataRequest(consumer, request_manager_.get())); | |
| 464 WebDataService::Handle handle = request->GetHandle(); | |
| 465 if (is_running_) { | |
| 466 BrowserThread::PostTask(BrowserThread::DB, from_here, | |
| 467 base::Bind(&WebDataService::DBResultTaskWrapper, this, task, | |
| 468 base::Passed(&request))); | |
| 469 } else { | |
| 470 NOTREACHED() << "Task scheduled after Shutdown()"; | |
| 471 } | |
| 472 return handle; | |
| 473 } | |
| 474 | |
| 475 void WebDataService::DBTaskWrapper(const base::Closure& task, | |
| 476 scoped_ptr<WebDataRequest> request) { | |
| 477 InitializeDatabaseIfNecessary(); | |
| 478 if (db_ && !request->IsCancelled()) { | |
| 479 task.Run(); | |
| 480 } | |
| 481 request_manager_->RequestCompleted(request.Pass()); | |
| 482 } | |
| 483 | |
| 484 void WebDataService::DBResultTaskWrapper(const ResultTask& task, | |
| 485 scoped_ptr<WebDataRequest> request) { | |
| 486 InitializeDatabaseIfNecessary(); | |
| 487 if (db_ && !request->IsCancelled()) { | |
| 488 request->SetResult(task.Run()); | |
| 489 } | |
| 490 request_manager_->RequestCompleted(request.Pass()); | |
| 491 } | |
| 492 | |
| 493 void WebDataService::ScheduleCommit() { | |
| 494 if (should_commit_ == false) { | |
| 495 should_commit_ = true; | |
| 496 ScheduleTask(FROM_HERE, Bind(&WebDataService::Commit, this)); | |
| 497 } | |
| 498 } | |
| 499 | |
| 500 //////////////////////////////////////////////////////////////////////////////// | 307 //////////////////////////////////////////////////////////////////////////////// |
| 501 // | 308 // |
| 502 // Keywords implementation. | 309 // Keywords implementation. |
| 503 // | 310 // |
| 504 //////////////////////////////////////////////////////////////////////////////// | 311 //////////////////////////////////////////////////////////////////////////////// |
| 505 | 312 |
| 506 void WebDataService::AddKeywordImpl(const TemplateURLData& data) { | 313 WebDatabase::State WebDataService::AddKeywordImpl( |
| 507 db_->GetKeywordTable()->AddKeyword(data); | 314 const TemplateURLData& data, WebDatabase* db) { |
| 508 ScheduleCommit(); | 315 db->GetKeywordTable()->AddKeyword(data); |
| 316 return WebDatabase::COMMIT_NEEDED; |
| 509 } | 317 } |
| 510 | 318 |
| 511 void WebDataService::RemoveKeywordImpl(TemplateURLID id) { | 319 WebDatabase::State WebDataService::RemoveKeywordImpl( |
| 320 TemplateURLID id, WebDatabase* db) { |
| 512 DCHECK(id); | 321 DCHECK(id); |
| 513 db_->GetKeywordTable()->RemoveKeyword(id); | 322 db->GetKeywordTable()->RemoveKeyword(id); |
| 514 ScheduleCommit(); | 323 return WebDatabase::COMMIT_NEEDED; |
| 515 } | 324 } |
| 516 | 325 |
| 517 void WebDataService::UpdateKeywordImpl(const TemplateURLData& data) { | 326 WebDatabase::State WebDataService::UpdateKeywordImpl( |
| 518 if (!db_->GetKeywordTable()->UpdateKeyword(data)) { | 327 const TemplateURLData& data, WebDatabase* db) { |
| 328 if (!db->GetKeywordTable()->UpdateKeyword(data)) { |
| 519 NOTREACHED(); | 329 NOTREACHED(); |
| 520 return; | 330 return WebDatabase::COMMIT_NOT_NEEDED; |
| 521 } | 331 } |
| 522 ScheduleCommit(); | 332 return WebDatabase::COMMIT_NEEDED; |
| 523 } | 333 } |
| 524 | 334 |
| 525 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl() { | 335 scoped_ptr<WDTypedResult> WebDataService::GetKeywordsImpl(WebDatabase* db) { |
| 526 WDKeywordsResult result; | 336 WDKeywordsResult result; |
| 527 db_->GetKeywordTable()->GetKeywords(&result.keywords); | 337 db->GetKeywordTable()->GetKeywords(&result.keywords); |
| 528 result.default_search_provider_id = | 338 result.default_search_provider_id = |
| 529 db_->GetKeywordTable()->GetDefaultSearchProviderID(); | 339 db->GetKeywordTable()->GetDefaultSearchProviderID(); |
| 530 result.builtin_keyword_version = | 340 result.builtin_keyword_version = |
| 531 db_->GetKeywordTable()->GetBuiltinKeywordVersion(); | 341 db->GetKeywordTable()->GetBuiltinKeywordVersion(); |
| 532 return scoped_ptr<WDTypedResult>( | 342 return scoped_ptr<WDTypedResult>( |
| 533 new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result)); | 343 new WDResult<WDKeywordsResult>(KEYWORDS_RESULT, result)); |
| 534 } | 344 } |
| 535 | 345 |
| 536 void WebDataService::SetDefaultSearchProviderImpl(TemplateURLID id) { | 346 WebDatabase::State WebDataService::SetDefaultSearchProviderImpl( |
| 537 if (!db_->GetKeywordTable()->SetDefaultSearchProviderID(id)) { | 347 TemplateURLID id, WebDatabase* db) { |
| 348 if (!db->GetKeywordTable()->SetDefaultSearchProviderID(id)) { |
| 538 NOTREACHED(); | 349 NOTREACHED(); |
| 539 return; | 350 return WebDatabase::COMMIT_NOT_NEEDED; |
| 540 } | 351 } |
| 541 ScheduleCommit(); | 352 return WebDatabase::COMMIT_NEEDED; |
| 542 } | 353 } |
| 543 | 354 |
| 544 void WebDataService::SetBuiltinKeywordVersionImpl(int version) { | 355 WebDatabase::State WebDataService::SetBuiltinKeywordVersionImpl( |
| 545 if (!db_->GetKeywordTable()->SetBuiltinKeywordVersion(version)) { | 356 int version, WebDatabase* db) { |
| 357 if (!db->GetKeywordTable()->SetBuiltinKeywordVersion(version)) { |
| 546 NOTREACHED(); | 358 NOTREACHED(); |
| 547 return; | 359 return WebDatabase::COMMIT_NOT_NEEDED; |
| 548 } | 360 } |
| 549 ScheduleCommit(); | 361 return WebDatabase::COMMIT_NEEDED; |
| 550 } | 362 } |
| 551 | 363 |
| 552 //////////////////////////////////////////////////////////////////////////////// | 364 //////////////////////////////////////////////////////////////////////////////// |
| 553 // | 365 // |
| 554 // Web Apps implementation. | 366 // Web Apps implementation. |
| 555 // | 367 // |
| 556 //////////////////////////////////////////////////////////////////////////////// | 368 //////////////////////////////////////////////////////////////////////////////// |
| 557 | 369 |
| 558 void WebDataService::SetWebAppImageImpl( | 370 WebDatabase::State WebDataService::SetWebAppImageImpl( |
| 559 const GURL& app_url, const SkBitmap& image) { | 371 const GURL& app_url, const SkBitmap& image, WebDatabase* db) { |
| 560 db_->GetWebAppsTable()->SetWebAppImage(app_url, image); | 372 db->GetWebAppsTable()->SetWebAppImage(app_url, image); |
| 561 ScheduleCommit(); | 373 return WebDatabase::COMMIT_NEEDED; |
| 562 } | 374 } |
| 563 | 375 |
| 564 void WebDataService::SetWebAppHasAllImagesImpl( | 376 WebDatabase::State WebDataService::SetWebAppHasAllImagesImpl( |
| 565 const GURL& app_url, bool has_all_images) { | 377 const GURL& app_url, bool has_all_images, WebDatabase* db) { |
| 566 db_->GetWebAppsTable()->SetWebAppHasAllImages(app_url, has_all_images); | 378 db->GetWebAppsTable()-> |
| 567 ScheduleCommit(); | 379 SetWebAppHasAllImages(app_url, has_all_images); |
| 380 return WebDatabase::COMMIT_NEEDED; |
| 568 } | 381 } |
| 569 | 382 |
| 570 void WebDataService::RemoveWebAppImpl(const GURL& app_url) { | 383 WebDatabase::State WebDataService::RemoveWebAppImpl( |
| 571 db_->GetWebAppsTable()->RemoveWebApp(app_url); | 384 const GURL& app_url, WebDatabase* db) { |
| 572 ScheduleCommit(); | 385 db->GetWebAppsTable()->RemoveWebApp(app_url); |
| 386 return WebDatabase::COMMIT_NEEDED; |
| 573 } | 387 } |
| 574 | 388 |
| 575 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( | 389 scoped_ptr<WDTypedResult> WebDataService::GetWebAppImagesImpl( |
| 576 const GURL& app_url) { | 390 const GURL& app_url, WebDatabase* db) { |
| 577 WDAppImagesResult result; | 391 WDAppImagesResult result; |
| 578 result.has_all_images = | 392 result.has_all_images = db->GetWebAppsTable()->GetWebAppHasAllImages(app_url); |
| 579 db_->GetWebAppsTable()->GetWebAppHasAllImages(app_url); | 393 db->GetWebAppsTable()->GetWebAppImages(app_url, &result.images); |
| 580 db_->GetWebAppsTable()->GetWebAppImages(app_url, &result.images); | |
| 581 return scoped_ptr<WDTypedResult>( | 394 return scoped_ptr<WDTypedResult>( |
| 582 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); | 395 new WDResult<WDAppImagesResult>(WEB_APP_IMAGES, result)); |
| 583 } | 396 } |
| 584 | 397 |
| 585 //////////////////////////////////////////////////////////////////////////////// | 398 //////////////////////////////////////////////////////////////////////////////// |
| 586 // | 399 // |
| 587 // Token Service implementation. | 400 // Token Service implementation. |
| 588 // | 401 // |
| 589 //////////////////////////////////////////////////////////////////////////////// | 402 //////////////////////////////////////////////////////////////////////////////// |
| 590 | 403 |
| 591 void WebDataService::RemoveAllTokensImpl() { | 404 WebDatabase::State WebDataService::RemoveAllTokensImpl(WebDatabase* db) { |
| 592 if (db_->GetTokenServiceTable()->RemoveAllTokens()) { | 405 if (db->GetTokenServiceTable()->RemoveAllTokens()) { |
| 593 ScheduleCommit(); | 406 return WebDatabase::COMMIT_NEEDED; |
| 594 } | 407 } |
| 408 return WebDatabase::COMMIT_NOT_NEEDED; |
| 595 } | 409 } |
| 596 | 410 |
| 597 void WebDataService::SetTokenForServiceImpl(const std::string& service, | 411 WebDatabase::State WebDataService::SetTokenForServiceImpl( |
| 598 const std::string& token) { | 412 const std::string& service, const std::string& token, WebDatabase* db) { |
| 599 if (db_->GetTokenServiceTable()->SetTokenForService(service, token)) { | 413 if (db->GetTokenServiceTable()->SetTokenForService(service, token)) { |
| 600 ScheduleCommit(); | 414 return WebDatabase::COMMIT_NEEDED; |
| 601 } | 415 } |
| 416 return WebDatabase::COMMIT_NOT_NEEDED; |
| 602 } | 417 } |
| 603 | 418 |
| 604 scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl() { | 419 scoped_ptr<WDTypedResult> WebDataService::GetAllTokensImpl(WebDatabase* db) { |
| 605 std::map<std::string, std::string> map; | 420 std::map<std::string, std::string> map; |
| 606 db_->GetTokenServiceTable()->GetAllTokens(&map); | 421 db->GetTokenServiceTable()->GetAllTokens(&map); |
| 607 return scoped_ptr<WDTypedResult>( | 422 return scoped_ptr<WDTypedResult>( |
| 608 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map)); | 423 new WDResult<std::map<std::string, std::string> >(TOKEN_RESULT, map)); |
| 609 } | 424 } |
| 610 | 425 |
| 611 //////////////////////////////////////////////////////////////////////////////// | 426 //////////////////////////////////////////////////////////////////////////////// |
| 612 // | 427 // |
| 613 // Autofill implementation. | 428 // Autofill implementation. |
| 614 // | 429 // |
| 615 //////////////////////////////////////////////////////////////////////////////// | 430 //////////////////////////////////////////////////////////////////////////////// |
| 616 | 431 |
| 617 void WebDataService::AddFormElementsImpl( | 432 WebDatabase::State WebDataService::AddFormElementsImpl( |
| 618 const std::vector<FormFieldData>& fields) { | 433 const std::vector<FormFieldData>& fields, WebDatabase* db) { |
| 619 AutofillChangeList changes; | 434 AutofillChangeList changes; |
| 620 if (!db_->GetAutofillTable()->AddFormFieldValues(fields, &changes)) { | 435 if (!db->GetAutofillTable()->AddFormFieldValues(fields, &changes)) { |
| 621 NOTREACHED(); | 436 NOTREACHED(); |
| 622 return; | 437 return WebDatabase::COMMIT_NOT_NEEDED; |
| 623 } | 438 } |
| 624 ScheduleCommit(); | |
| 625 | 439 |
| 626 // Post the notifications including the list of affected keys. | 440 // Post the notifications including the list of affected keys. |
| 627 // This is sent here so that work resulting from this notification will be | 441 // This is sent here so that work resulting from this notification will be |
| 628 // done on the DB thread, and not the UI thread. | 442 // done on the DB thread, and not the UI thread. |
| 629 content::NotificationService::current()->Notify( | 443 content::NotificationService::current()->Notify( |
| 630 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | 444 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 631 content::Source<WebDataService>(this), | 445 content::Source<WebDataService>(this), |
| 632 content::Details<AutofillChangeList>(&changes)); | 446 content::Details<AutofillChangeList>(&changes)); |
| 447 |
| 448 return WebDatabase::COMMIT_NEEDED; |
| 633 } | 449 } |
| 634 | 450 |
| 635 scoped_ptr<WDTypedResult> WebDataService::GetFormValuesForElementNameImpl( | 451 scoped_ptr<WDTypedResult> WebDataService::GetFormValuesForElementNameImpl( |
| 636 const string16& name, const string16& prefix, int limit) { | 452 const string16& name, const string16& prefix, int limit, WebDatabase* db) { |
| 637 std::vector<string16> values; | 453 std::vector<string16> values; |
| 638 db_->GetAutofillTable()->GetFormValuesForElementName( | 454 db->GetAutofillTable()->GetFormValuesForElementName( |
| 639 name, prefix, &values, limit); | 455 name, prefix, &values, limit); |
| 640 return scoped_ptr<WDTypedResult>( | 456 return scoped_ptr<WDTypedResult>( |
| 641 new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values)); | 457 new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values)); |
| 642 } | 458 } |
| 643 | 459 |
| 644 void WebDataService::RemoveFormElementsAddedBetweenImpl( | 460 WebDatabase::State WebDataService::RemoveFormElementsAddedBetweenImpl( |
| 645 const base::Time& delete_begin, const base::Time& delete_end) { | 461 const base::Time& delete_begin, const base::Time& delete_end, |
| 462 WebDatabase* db) { |
| 646 AutofillChangeList changes; | 463 AutofillChangeList changes; |
| 647 if (db_->GetAutofillTable()->RemoveFormElementsAddedBetween( | 464 |
| 465 if (db->GetAutofillTable()->RemoveFormElementsAddedBetween( |
| 648 delete_begin, delete_end, &changes)) { | 466 delete_begin, delete_end, &changes)) { |
| 649 if (!changes.empty()) { | 467 if (!changes.empty()) { |
| 650 // Post the notifications including the list of affected keys. | 468 // Post the notifications including the list of affected keys. |
| 651 // This is sent here so that work resulting from this notification | 469 // This is sent here so that work resulting from this notification |
| 652 // will be done on the DB thread, and not the UI thread. | 470 // will be done on the DB thread, and not the UI thread. |
| 653 content::NotificationService::current()->Notify( | 471 content::NotificationService::current()->Notify( |
| 654 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | 472 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 655 content::Source<WebDataService>(this), | 473 content::Source<WebDataService>(this), |
| 656 content::Details<AutofillChangeList>(&changes)); | 474 content::Details<AutofillChangeList>(&changes)); |
| 657 } | 475 } |
| 658 ScheduleCommit(); | 476 return WebDatabase::COMMIT_NEEDED; |
| 659 } | 477 } |
| 478 return WebDatabase::COMMIT_NOT_NEEDED; |
| 660 } | 479 } |
| 661 | 480 |
| 662 void WebDataService::RemoveExpiredFormElementsImpl() { | 481 WebDatabase::State WebDataService::RemoveExpiredFormElementsImpl( |
| 482 WebDatabase* db) { |
| 663 AutofillChangeList changes; | 483 AutofillChangeList changes; |
| 664 | 484 |
| 665 if (db_->GetAutofillTable()->RemoveExpiredFormElements(&changes)) { | 485 if (db->GetAutofillTable()->RemoveExpiredFormElements(&changes)) { |
| 666 if (!changes.empty()) { | 486 if (!changes.empty()) { |
| 667 // Post the notifications including the list of affected keys. | 487 // Post the notifications including the list of affected keys. |
| 668 // This is sent here so that work resulting from this notification | 488 // This is sent here so that work resulting from this notification |
| 669 // will be done on the DB thread, and not the UI thread. | 489 // will be done on the DB thread, and not the UI thread. |
| 670 content::NotificationService::current()->Notify( | 490 content::NotificationService::current()->Notify( |
| 671 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | 491 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 672 content::Source<WebDataService>(this), | 492 content::Source<WebDataService>(this), |
| 673 content::Details<AutofillChangeList>(&changes)); | 493 content::Details<AutofillChangeList>(&changes)); |
| 674 } | 494 } |
| 675 ScheduleCommit(); | 495 return WebDatabase::COMMIT_NEEDED; |
| 676 } | 496 } |
| 497 return WebDatabase::COMMIT_NOT_NEEDED; |
| 677 } | 498 } |
| 678 | 499 |
| 679 void WebDataService::RemoveFormValueForElementNameImpl( | 500 WebDatabase::State WebDataService::RemoveFormValueForElementNameImpl( |
| 680 const string16& name, const string16& value) { | 501 const string16& name, const string16& value, WebDatabase* db) { |
| 681 | 502 |
| 682 if (db_->GetAutofillTable()->RemoveFormElement(name, value)) { | 503 if (db->GetAutofillTable()->RemoveFormElement(name, value)) { |
| 683 AutofillChangeList changes; | 504 AutofillChangeList changes; |
| 684 changes.push_back(AutofillChange(AutofillChange::REMOVE, | 505 changes.push_back(AutofillChange(AutofillChange::REMOVE, |
| 685 AutofillKey(name, value))); | 506 AutofillKey(name, value))); |
| 686 ScheduleCommit(); | |
| 687 | 507 |
| 688 // Post the notifications including the list of affected keys. | 508 // Post the notifications including the list of affected keys. |
| 689 content::NotificationService::current()->Notify( | 509 content::NotificationService::current()->Notify( |
| 690 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, | 510 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 691 content::Source<WebDataService>(this), | 511 content::Source<WebDataService>(this), |
| 692 content::Details<AutofillChangeList>(&changes)); | 512 content::Details<AutofillChangeList>(&changes)); |
| 513 |
| 514 return WebDatabase::COMMIT_NEEDED; |
| 693 } | 515 } |
| 516 return WebDatabase::COMMIT_NOT_NEEDED; |
| 694 } | 517 } |
| 695 | 518 |
| 696 void WebDataService::AddAutofillProfileImpl(const AutofillProfile& profile) { | 519 WebDatabase::State WebDataService::AddAutofillProfileImpl( |
| 697 if (!db_->GetAutofillTable()->AddAutofillProfile(profile)) { | 520 const AutofillProfile& profile, WebDatabase* db) { |
| 521 if (!db->GetAutofillTable()->AddAutofillProfile(profile)) { |
| 698 NOTREACHED(); | 522 NOTREACHED(); |
| 699 return; | 523 return WebDatabase::COMMIT_NOT_NEEDED; |
| 700 } | 524 } |
| 701 ScheduleCommit(); | |
| 702 | 525 |
| 703 // Send GUID-based notification. | 526 // Send GUID-based notification. |
| 704 AutofillProfileChange change(AutofillProfileChange::ADD, | 527 AutofillProfileChange change(AutofillProfileChange::ADD, |
| 705 profile.guid(), &profile); | 528 profile.guid(), &profile); |
| 706 content::NotificationService::current()->Notify( | 529 content::NotificationService::current()->Notify( |
| 707 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 530 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 708 content::Source<WebDataService>(this), | 531 content::Source<WebDataService>(this), |
| 709 content::Details<AutofillProfileChange>(&change)); | 532 content::Details<AutofillProfileChange>(&change)); |
| 533 |
| 534 return WebDatabase::COMMIT_NEEDED; |
| 710 } | 535 } |
| 711 | 536 |
| 712 void WebDataService::UpdateAutofillProfileImpl(const AutofillProfile& profile) { | 537 WebDatabase::State WebDataService::UpdateAutofillProfileImpl( |
| 538 const AutofillProfile& profile, WebDatabase* db) { |
| 713 // Only perform the update if the profile exists. It is currently | 539 // Only perform the update if the profile exists. It is currently |
| 714 // valid to try to update a missing profile. We simply drop the write and | 540 // valid to try to update a missing profile. We simply drop the write and |
| 715 // the caller will detect this on the next refresh. | 541 // the caller will detect this on the next refresh. |
| 716 AutofillProfile* original_profile = NULL; | 542 AutofillProfile* original_profile = NULL; |
| 717 if (!db_->GetAutofillTable()->GetAutofillProfile(profile.guid(), | 543 if (!db->GetAutofillTable()->GetAutofillProfile(profile.guid(), |
| 718 &original_profile)) { | 544 &original_profile)) { |
| 719 return; | 545 return WebDatabase::COMMIT_NOT_NEEDED; |
| 720 } | 546 } |
| 721 scoped_ptr<AutofillProfile> scoped_profile(original_profile); | 547 scoped_ptr<AutofillProfile> scoped_profile(original_profile); |
| 722 | 548 |
| 723 if (!db_->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) { | 549 if (!db->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) { |
| 724 NOTREACHED(); | 550 NOTREACHED(); |
| 725 return; | 551 return WebDatabase::COMMIT_NEEDED; |
| 726 } | 552 } |
| 727 ScheduleCommit(); | |
| 728 | 553 |
| 729 // Send GUID-based notification. | 554 // Send GUID-based notification. |
| 730 AutofillProfileChange change(AutofillProfileChange::UPDATE, | 555 AutofillProfileChange change(AutofillProfileChange::UPDATE, |
| 731 profile.guid(), &profile); | 556 profile.guid(), &profile); |
| 732 content::NotificationService::current()->Notify( | 557 content::NotificationService::current()->Notify( |
| 733 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 558 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 734 content::Source<WebDataService>(this), | 559 content::Source<WebDataService>(this), |
| 735 content::Details<AutofillProfileChange>(&change)); | 560 content::Details<AutofillProfileChange>(&change)); |
| 561 |
| 562 return WebDatabase::COMMIT_NEEDED; |
| 736 } | 563 } |
| 737 | 564 |
| 738 void WebDataService::RemoveAutofillProfileImpl(const std::string& guid) { | 565 WebDatabase::State WebDataService::RemoveAutofillProfileImpl( |
| 566 const std::string& guid, WebDatabase* db) { |
| 739 AutofillProfile* profile = NULL; | 567 AutofillProfile* profile = NULL; |
| 740 if (!db_->GetAutofillTable()->GetAutofillProfile(guid, &profile)) { | 568 if (!db->GetAutofillTable()->GetAutofillProfile(guid, &profile)) { |
| 741 NOTREACHED(); | 569 NOTREACHED(); |
| 742 return; | 570 return WebDatabase::COMMIT_NOT_NEEDED; |
| 743 } | 571 } |
| 744 scoped_ptr<AutofillProfile> scoped_profile(profile); | 572 scoped_ptr<AutofillProfile> scoped_profile(profile); |
| 745 | 573 |
| 746 if (!db_->GetAutofillTable()->RemoveAutofillProfile(guid)) { | 574 if (!db->GetAutofillTable()->RemoveAutofillProfile(guid)) { |
| 747 NOTREACHED(); | 575 NOTREACHED(); |
| 748 return; | 576 return WebDatabase::COMMIT_NOT_NEEDED; |
| 749 } | 577 } |
| 750 ScheduleCommit(); | |
| 751 | 578 |
| 752 // Send GUID-based notification. | 579 // Send GUID-based notification. |
| 753 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); | 580 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); |
| 754 content::NotificationService::current()->Notify( | 581 content::NotificationService::current()->Notify( |
| 755 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 582 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 756 content::Source<WebDataService>(this), | 583 content::Source<WebDataService>(this), |
| 757 content::Details<AutofillProfileChange>(&change)); | 584 content::Details<AutofillProfileChange>(&change)); |
| 585 |
| 586 return WebDatabase::COMMIT_NEEDED; |
| 758 } | 587 } |
| 759 | 588 |
| 760 scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl() { | 589 scoped_ptr<WDTypedResult> WebDataService::GetAutofillProfilesImpl( |
| 590 WebDatabase* db) { |
| 761 std::vector<AutofillProfile*> profiles; | 591 std::vector<AutofillProfile*> profiles; |
| 762 db_->GetAutofillTable()->GetAutofillProfiles(&profiles); | 592 db->GetAutofillTable()->GetAutofillProfiles(&profiles); |
| 763 return scoped_ptr<WDTypedResult>( | 593 return scoped_ptr<WDTypedResult>( |
| 764 new WDDestroyableResult<std::vector<AutofillProfile*> >( | 594 new WDDestroyableResult<std::vector<AutofillProfile*> >( |
| 765 AUTOFILL_PROFILES_RESULT, | 595 AUTOFILL_PROFILES_RESULT, |
| 766 profiles, | 596 profiles, |
| 767 base::Bind(&WebDataService::DestroyAutofillProfileResult, | 597 base::Bind(&WebDataService::DestroyAutofillProfileResult, |
| 768 base::Unretained(this)))); | 598 base::Unretained(this)))); |
| 769 } | 599 } |
| 770 | 600 |
| 771 void WebDataService::AddCreditCardImpl(const CreditCard& credit_card) { | 601 WebDatabase::State WebDataService::AddCreditCardImpl( |
| 772 if (!db_->GetAutofillTable()->AddCreditCard(credit_card)) { | 602 const CreditCard& credit_card, WebDatabase* db) { |
| 603 if (!db->GetAutofillTable()->AddCreditCard(credit_card)) { |
| 773 NOTREACHED(); | 604 NOTREACHED(); |
| 774 return; | 605 return WebDatabase::COMMIT_NOT_NEEDED; |
| 775 } | 606 } |
| 776 ScheduleCommit(); | 607 |
| 608 return WebDatabase::COMMIT_NEEDED; |
| 777 } | 609 } |
| 778 | 610 |
| 779 void WebDataService::UpdateCreditCardImpl(const CreditCard& credit_card) { | 611 WebDatabase::State WebDataService::UpdateCreditCardImpl( |
| 612 const CreditCard& credit_card, WebDatabase* db) { |
| 780 // It is currently valid to try to update a missing profile. We simply drop | 613 // It is currently valid to try to update a missing profile. We simply drop |
| 781 // the write and the caller will detect this on the next refresh. | 614 // the write and the caller will detect this on the next refresh. |
| 782 CreditCard* original_credit_card = NULL; | 615 CreditCard* original_credit_card = NULL; |
| 783 if (!db_->GetAutofillTable()->GetCreditCard(credit_card.guid(), | 616 if (!db->GetAutofillTable()->GetCreditCard(credit_card.guid(), |
| 784 &original_credit_card)) { | 617 &original_credit_card)) { |
| 785 return; | 618 return WebDatabase::COMMIT_NOT_NEEDED; |
| 786 } | 619 } |
| 787 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); | 620 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); |
| 788 | 621 |
| 789 if (!db_->GetAutofillTable()->UpdateCreditCard(credit_card)) { | 622 if (!db->GetAutofillTable()->UpdateCreditCard(credit_card)) { |
| 790 NOTREACHED(); | 623 NOTREACHED(); |
| 791 return; | 624 return WebDatabase::COMMIT_NOT_NEEDED; |
| 792 } | 625 } |
| 793 ScheduleCommit(); | 626 return WebDatabase::COMMIT_NEEDED; |
| 794 } | 627 } |
| 795 | 628 |
| 796 void WebDataService::RemoveCreditCardImpl(const std::string& guid) { | 629 WebDatabase::State WebDataService::RemoveCreditCardImpl( |
| 797 if (!db_->GetAutofillTable()->RemoveCreditCard(guid)) { | 630 const std::string& guid, WebDatabase* db) { |
| 631 if (!db->GetAutofillTable()->RemoveCreditCard(guid)) { |
| 798 NOTREACHED(); | 632 NOTREACHED(); |
| 799 return; | 633 return WebDatabase::COMMIT_NOT_NEEDED; |
| 800 } | 634 } |
| 801 ScheduleCommit(); | 635 return WebDatabase::COMMIT_NEEDED; |
| 802 } | 636 } |
| 803 | 637 |
| 804 scoped_ptr<WDTypedResult> WebDataService::GetCreditCardsImpl() { | 638 scoped_ptr<WDTypedResult> WebDataService::GetCreditCardsImpl(WebDatabase* db) { |
| 805 std::vector<CreditCard*> credit_cards; | 639 std::vector<CreditCard*> credit_cards; |
| 806 db_->GetAutofillTable()->GetCreditCards(&credit_cards); | 640 db->GetAutofillTable()->GetCreditCards(&credit_cards); |
| 807 return scoped_ptr<WDTypedResult>( | 641 return scoped_ptr<WDTypedResult>( |
| 808 new WDDestroyableResult<std::vector<CreditCard*> >( | 642 new WDDestroyableResult<std::vector<CreditCard*> >( |
| 809 AUTOFILL_CREDITCARDS_RESULT, | 643 AUTOFILL_CREDITCARDS_RESULT, |
| 810 credit_cards, | 644 credit_cards, |
| 811 base::Bind(&WebDataService::DestroyAutofillCreditCardResult, | 645 base::Bind(&WebDataService::DestroyAutofillCreditCardResult, |
| 812 base::Unretained(this)))); | 646 base::Unretained(this)))); |
| 813 } | 647 } |
| 814 | 648 |
| 815 void WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( | 649 WebDatabase::State |
| 816 const base::Time& delete_begin, const base::Time& delete_end) { | 650 WebDataService::RemoveAutofillProfilesAndCreditCardsModifiedBetweenImpl( |
| 651 const base::Time& delete_begin, const base::Time& delete_end, |
| 652 WebDatabase* db) { |
| 817 std::vector<std::string> profile_guids; | 653 std::vector<std::string> profile_guids; |
| 818 std::vector<std::string> credit_card_guids; | 654 std::vector<std::string> credit_card_guids; |
| 819 if (db_->GetAutofillTable()-> | 655 if (db->GetAutofillTable()-> |
| 820 RemoveAutofillProfilesAndCreditCardsModifiedBetween( | 656 RemoveAutofillProfilesAndCreditCardsModifiedBetween( |
| 821 delete_begin, | 657 delete_begin, |
| 822 delete_end, | 658 delete_end, |
| 823 &profile_guids, | 659 &profile_guids, |
| 824 &credit_card_guids)) { | 660 &credit_card_guids)) { |
| 825 for (std::vector<std::string>::iterator iter = profile_guids.begin(); | 661 for (std::vector<std::string>::iterator iter = profile_guids.begin(); |
| 826 iter != profile_guids.end(); ++iter) { | 662 iter != profile_guids.end(); ++iter) { |
| 827 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, | 663 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, |
| 828 NULL); | 664 NULL); |
| 829 content::NotificationService::current()->Notify( | 665 content::NotificationService::current()->Notify( |
| 830 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, | 666 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 831 content::Source<WebDataService>(this), | 667 content::Source<WebDataService>(this), |
| 832 content::Details<AutofillProfileChange>(&change)); | 668 content::Details<AutofillProfileChange>(&change)); |
| 833 } | 669 } |
| 834 // Note: It is the caller's responsibility to post notifications for any | 670 // Note: It is the caller's responsibility to post notifications for any |
| 835 // changes, e.g. by calling the Refresh() method of PersonalDataManager. | 671 // changes, e.g. by calling the Refresh() method of PersonalDataManager. |
| 836 ScheduleCommit(); | 672 return WebDatabase::COMMIT_NEEDED; |
| 837 } | 673 } |
| 674 return WebDatabase::COMMIT_NOT_NEEDED; |
| 838 } | 675 } |
| 839 | 676 |
| 840 AutofillProfileSyncableService* | 677 AutofillProfileSyncableService* |
| 841 WebDataService::GetAutofillProfileSyncableService() const { | 678 WebDataService::GetAutofillProfileSyncableService() const { |
| 842 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 679 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 843 DCHECK(autofill_profile_syncable_service_); // Make sure we're initialized. | 680 DCHECK(autofill_profile_syncable_service_); // Make sure we're initialized. |
| 844 | 681 |
| 845 return autofill_profile_syncable_service_; | 682 return autofill_profile_syncable_service_; |
| 846 } | 683 } |
| 847 | 684 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 863 | 700 |
| 864 void WebDataService::DestroyAutofillCreditCardResult( | 701 void WebDataService::DestroyAutofillCreditCardResult( |
| 865 const WDTypedResult* result) { | 702 const WDTypedResult* result) { |
| 866 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); | 703 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); |
| 867 const WDResult<std::vector<CreditCard*> >* r = | 704 const WDResult<std::vector<CreditCard*> >* r = |
| 868 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | 705 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); |
| 869 | 706 |
| 870 std::vector<CreditCard*> credit_cards = r->GetValue(); | 707 std::vector<CreditCard*> credit_cards = r->GetValue(); |
| 871 STLDeleteElements(&credit_cards); | 708 STLDeleteElements(&credit_cards); |
| 872 } | 709 } |
| OLD | NEW |