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