| 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 "components/webdata/common/web_data_service_base.h" | 5 #include "components/webdata/common/web_data_service_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "components/webdata/common/web_database_service.h" | 11 #include "components/webdata/common/web_database_service.h" |
| 12 #ifdef DEBUG | 12 #ifdef DEBUG |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
| 17 // | 17 // |
| 18 // WebDataServiceBase implementation. | 18 // WebDataServiceBase implementation. |
| 19 // | 19 // |
| 20 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
| 21 | 21 |
| 22 using base::Bind; | 22 using base::Bind; |
| 23 using base::Time; | 23 using base::Time; |
| 24 using content::BrowserThread; | 24 using content::BrowserThread; |
| 25 | 25 |
| 26 WebDataServiceBase::WebDataServiceBase(scoped_refptr<WebDatabaseService> wdbs, | 26 WebDataServiceBase::WebDataServiceBase(scoped_refptr<WebDatabaseService> wdbs, |
| 27 const ProfileErrorCallback& callback) | 27 const ProfileErrorCallback& callback) |
| 28 : wdbs_(wdbs), | 28 : wdbs_(wdbs), |
| 29 db_loaded_(false), | |
| 30 profile_error_callback_(callback) { | 29 profile_error_callback_(callback) { |
| 31 // WebDataService requires DB thread if instantiated. | 30 // WebDataService requires DB thread if instantiated. |
| 32 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) | 31 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) |
| 33 // if you do not want to instantiate WebDataService in your test. | 32 // if you do not want to instantiate WebDataService in your test. |
| 34 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); | 33 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void WebDataServiceBase::WebDatabaseLoaded() { | |
| 38 db_loaded_ = true; | |
| 39 } | |
| 40 | |
| 41 void WebDataServiceBase::WebDatabaseLoadFailed(sql::InitStatus status) { | |
| 42 if (!profile_error_callback_.is_null()) | |
| 43 profile_error_callback_.Run(status); | |
| 44 } | |
| 45 | |
| 46 void WebDataServiceBase::ShutdownOnUIThread() { | 36 void WebDataServiceBase::ShutdownOnUIThread() { |
| 47 db_loaded_ = false; | |
| 48 } | 37 } |
| 49 | 38 |
| 50 void WebDataServiceBase::Init() { | 39 void WebDataServiceBase::Init() { |
| 51 DCHECK(wdbs_.get()); | 40 DCHECK(wdbs_.get()); |
| 52 wdbs_->AddObserver(this); | 41 wdbs_->RegisterDBErrorCallback(profile_error_callback_); |
| 53 wdbs_->LoadDatabase(); | 42 wdbs_->LoadDatabase(); |
| 54 } | 43 } |
| 55 | 44 |
| 56 void WebDataServiceBase::UnloadDatabase() { | 45 void WebDataServiceBase::UnloadDatabase() { |
| 57 if (!wdbs_.get()) | 46 if (!wdbs_.get()) |
| 58 return; | 47 return; |
| 59 wdbs_->UnloadDatabase(); | 48 wdbs_->UnloadDatabase(); |
| 60 } | 49 } |
| 61 | 50 |
| 62 void WebDataServiceBase::ShutdownDatabase() { | 51 void WebDataServiceBase::ShutdownDatabase() { |
| 63 if (!wdbs_.get()) | 52 if (!wdbs_.get()) |
| 64 return; | 53 return; |
| 65 wdbs_->ShutdownDatabase(); | 54 wdbs_->ShutdownDatabase(); |
| 66 } | 55 } |
| 67 | 56 |
| 68 void WebDataServiceBase::CancelRequest(Handle h) { | 57 void WebDataServiceBase::CancelRequest(Handle h) { |
| 69 if (!wdbs_.get()) | 58 if (!wdbs_.get()) |
| 70 return; | 59 return; |
| 71 wdbs_->CancelRequest(h); | 60 wdbs_->CancelRequest(h); |
| 72 } | 61 } |
| 73 | 62 |
| 74 content::NotificationSource WebDataServiceBase::GetNotificationSource() { | 63 content::NotificationSource WebDataServiceBase::GetNotificationSource() { |
| 75 return content::Source<WebDataServiceBase>(this); | 64 return content::Source<WebDataServiceBase>(this); |
| 76 } | 65 } |
| 77 | 66 |
| 78 bool WebDataServiceBase::IsDatabaseLoaded() { | 67 bool WebDataServiceBase::IsDatabaseLoaded() { |
| 79 return db_loaded_; | 68 if (!wdbs_) |
| 69 return false; |
| 70 return wdbs_->db_loaded(); |
| 80 } | 71 } |
| 81 | 72 |
| 82 void WebDataServiceBase::AddDBObserver(WebDatabaseObserver* observer) { | 73 void WebDataServiceBase::RegisterDBLoadedCallback( |
| 83 if (!wdbs_.get()) | 74 const base::Callback<void(void)>& callback) { |
| 75 if (!wdbs_) |
| 84 return; | 76 return; |
| 85 wdbs_->AddObserver(observer); | 77 wdbs_->RegisterDBLoadedCallback(callback); |
| 86 } | |
| 87 | |
| 88 void WebDataServiceBase::RemoveDBObserver(WebDatabaseObserver* observer) { | |
| 89 if (!wdbs_.get()) | |
| 90 return; | |
| 91 wdbs_->RemoveObserver(observer); | |
| 92 } | 78 } |
| 93 | 79 |
| 94 WebDatabase* WebDataServiceBase::GetDatabase() { | 80 WebDatabase* WebDataServiceBase::GetDatabase() { |
| 95 if (!wdbs_.get()) | 81 if (!wdbs_.get()) |
| 96 return NULL; | 82 return NULL; |
| 97 return wdbs_->GetDatabaseOnDB(); | 83 return wdbs_->GetDatabaseOnDB(); |
| 98 } | 84 } |
| 99 | 85 |
| 100 WebDataServiceBase::~WebDataServiceBase() { | 86 WebDataServiceBase::~WebDataServiceBase() { |
| 101 } | 87 } |
| OLD | NEW |