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/webdata/web_database_service.h" |
| 13 #include "chrome/common/chrome_constants.h" |
| 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #ifdef DEBUG |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #endif |
| 18 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_source.h" |
| 21 |
| 22 //////////////////////////////////////////////////////////////////////////////// |
| 23 // |
| 24 // WebDataServiceBase implementation. |
| 25 // |
| 26 //////////////////////////////////////////////////////////////////////////////// |
| 27 |
| 28 using base::Bind; |
| 29 using base::Time; |
| 30 using content::BrowserThread; |
| 31 |
| 32 WebDataServiceBase::WebDataServiceBase(const ProfileErrorCallback& callback) |
| 33 : db_loaded_(false), |
| 34 profile_error_callback_(callback) { |
| 35 // WebDataService requires DB thread if instantiated. |
| 36 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) |
| 37 // if you do not want to instantiate WebDataService in your test. |
| 38 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); |
| 39 } |
| 40 |
| 41 void WebDataServiceBase::ShutdownOnUIThread() { |
| 42 db_loaded_ = false; |
| 43 BrowserThread::PostTask( |
| 44 BrowserThread::DB, FROM_HERE, |
| 45 base::Bind(&WebDataServiceBase::ShutdownOnDBThread, this)); |
| 46 ShutdownDatabase(); |
| 47 } |
| 48 |
| 49 void WebDataServiceBase::Init(const base::FilePath& path) { |
| 50 wdbs_.reset(new WebDatabaseService(path)); |
| 51 wdbs_->LoadDatabase(Bind(&WebDataServiceBase::DatabaseInitOnDB, this)); |
| 52 } |
| 53 |
| 54 void WebDataServiceBase::UnloadDatabase() { |
| 55 if (!wdbs_) |
| 56 return; |
| 57 wdbs_->UnloadDatabase(); |
| 58 } |
| 59 |
| 60 void WebDataServiceBase::ShutdownDatabase() { |
| 61 if (!wdbs_) |
| 62 return; |
| 63 wdbs_->ShutdownDatabase(); |
| 64 } |
| 65 |
| 66 void WebDataServiceBase::CancelRequest(Handle h) { |
| 67 if (!wdbs_) |
| 68 return; |
| 69 wdbs_->CancelRequest(h); |
| 70 } |
| 71 |
| 72 content::NotificationSource WebDataServiceBase::GetNotificationSource() { |
| 73 return content::Source<WebDataServiceBase>(this); |
| 74 } |
| 75 |
| 76 bool WebDataServiceBase::IsDatabaseLoaded() { |
| 77 return db_loaded_; |
| 78 } |
| 79 |
| 80 WebDatabase* WebDataServiceBase::GetDatabase() { |
| 81 if (!wdbs_) |
| 82 return NULL; |
| 83 return wdbs_->GetDatabaseOnDB(); |
| 84 } |
| 85 |
| 86 base::SupportsUserData* WebDataServiceBase::GetDBUserData() { |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 88 if (!db_thread_user_data_) |
| 89 db_thread_user_data_.reset(new SupportsUserDataAggregatable()); |
| 90 return db_thread_user_data_.get(); |
| 91 } |
| 92 |
| 93 WebDataServiceBase::~WebDataServiceBase() { |
| 94 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?"; |
| 95 wdbs_.reset(); |
| 96 } |
| 97 |
| 98 void WebDataServiceBase::ShutdownOnDBThread() { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 100 db_thread_user_data_.reset(); |
| 101 } |
| 102 |
| 103 //////////////////////////////////////////////////////////////////////////////// |
| 104 // |
| 105 // The following methods are executed on the DB thread. |
| 106 // |
| 107 //////////////////////////////////////////////////////////////////////////////// |
| 108 |
| 109 void WebDataServiceBase::DBInitFailed(sql::InitStatus sql_status) { |
| 110 if (!profile_error_callback_.is_null()) |
| 111 profile_error_callback_.Run(sql_status); |
| 112 } |
| 113 |
| 114 void WebDataServiceBase::NotifyDatabaseLoadedOnUIThread() { |
| 115 db_loaded_ = true; |
| 116 // Notify that the database has been initialized. |
| 117 content::NotificationService::current()->Notify( |
| 118 chrome::NOTIFICATION_WEB_DATABASE_LOADED, |
| 119 content::Source<WebDataServiceBase>(this), |
| 120 content::NotificationService::NoDetails()); |
| 121 } |
| 122 |
| 123 void WebDataServiceBase::DatabaseInitOnDB(sql::InitStatus status) { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 125 if (status == sql::INIT_OK) { |
| 126 BrowserThread::PostTask( |
| 127 BrowserThread::UI, FROM_HERE, |
| 128 base::Bind(&WebDataServiceBase::NotifyDatabaseLoadedOnUIThread, this)); |
| 129 } else { |
| 130 BrowserThread::PostTask( |
| 131 BrowserThread::UI, FROM_HERE, |
| 132 base::Bind(&WebDataServiceBase::DBInitFailed, this, status)); |
| 133 } |
| 134 } |
| 135 |
OLD | NEW |