OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/api/webdata/web_data_service_base.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" |
| 10 #include "base/threading/thread.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/profile_error_dialog.h" |
| 13 #include "chrome/browser/webdata/web_database_service.h" |
| 14 #include "chrome/common/chrome_constants.h" |
| 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #ifdef DEBUG |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #endif |
| 19 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_source.h" |
| 22 #include "grit/chromium_strings.h" |
| 23 #include "grit/generated_resources.h" |
| 24 |
| 25 //////////////////////////////////////////////////////////////////////////////// |
| 26 // |
| 27 // WebDataServiceBase implementation. |
| 28 // |
| 29 //////////////////////////////////////////////////////////////////////////////// |
| 30 |
| 31 using base::Bind; |
| 32 using base::Time; |
| 33 using content::BrowserThread; |
| 34 |
| 35 WebDataServiceBase::WebDataServiceBase() |
| 36 : db_loaded_(false) { |
| 37 // WebDataService requires DB thread if instantiated. |
| 38 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) |
| 39 // if you do not want to instantiate WebDataService in your test. |
| 40 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); |
| 41 } |
| 42 |
| 43 void WebDataServiceBase::ShutdownOnUIThread() { |
| 44 db_loaded_ = false; |
| 45 ShutdownDatabase(); |
| 46 } |
| 47 |
| 48 void WebDataServiceBase::Init(const base::FilePath& path) { |
| 49 wdbs_.reset(new WebDatabaseService(path)); |
| 50 wdbs_->LoadDatabase(Bind(&WebDataServiceBase::DatabaseInitOnDB, this)); |
| 51 } |
| 52 |
| 53 void WebDataServiceBase::UnloadDatabase() { |
| 54 if (!wdbs_) |
| 55 return; |
| 56 wdbs_->UnloadDatabase(); |
| 57 } |
| 58 |
| 59 void WebDataServiceBase::ShutdownDatabase() { |
| 60 if (!wdbs_) |
| 61 return; |
| 62 wdbs_->ShutdownDatabase(); |
| 63 } |
| 64 |
| 65 void WebDataServiceBase::CancelRequest(Handle h) { |
| 66 if (!wdbs_) |
| 67 return; |
| 68 wdbs_->CancelRequest(h); |
| 69 } |
| 70 |
| 71 content::NotificationSource WebDataServiceBase::GetNotificationSource() { |
| 72 return content::Source<WebDataServiceBase>(this); |
| 73 } |
| 74 |
| 75 bool WebDataServiceBase::IsDatabaseLoaded() { |
| 76 return db_loaded_; |
| 77 } |
| 78 |
| 79 WebDatabase* WebDataServiceBase::GetDatabase() { |
| 80 if (!wdbs_) |
| 81 return NULL; |
| 82 return wdbs_->GetDatabaseOnDB(); |
| 83 } |
| 84 |
| 85 WebDataServiceBase::~WebDataServiceBase() { |
| 86 wdbs_.reset(); |
| 87 } |
| 88 |
| 89 //////////////////////////////////////////////////////////////////////////////// |
| 90 // |
| 91 // The following methods are executed on the DB thread. |
| 92 // |
| 93 //////////////////////////////////////////////////////////////////////////////// |
| 94 |
| 95 void WebDataServiceBase::DBInitFailed(sql::InitStatus sql_status) { |
| 96 ShowProfileErrorDialog( |
| 97 (sql_status == sql::INIT_FAILURE) ? |
| 98 IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR); |
| 99 } |
| 100 |
| 101 void WebDataServiceBase::NotifyDatabaseLoadedOnUIThread() { |
| 102 db_loaded_ = true; |
| 103 // Notify that the database has been initialized. |
| 104 content::NotificationService::current()->Notify( |
| 105 chrome::NOTIFICATION_WEB_DATABASE_LOADED, |
| 106 content::Source<WebDataServiceBase>(this), |
| 107 content::NotificationService::NoDetails()); |
| 108 } |
| 109 |
| 110 void WebDataServiceBase::DatabaseInitOnDB(sql::InitStatus status) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 112 if (status == sql::INIT_OK) { |
| 113 BrowserThread::PostTask( |
| 114 BrowserThread::UI, FROM_HERE, |
| 115 base::Bind(&WebDataServiceBase::NotifyDatabaseLoadedOnUIThread, this)); |
| 116 } else { |
| 117 BrowserThread::PostTask( |
| 118 BrowserThread::UI, FROM_HERE, |
| 119 base::Bind(&WebDataServiceBase::DBInitFailed, this, status)); |
| 120 } |
| 121 } |
| 122 |
OLD | NEW |