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