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 ShutdownDatabase(); |
| 44 } |
| 45 |
| 46 void WebDataServiceBase::Init(const base::FilePath& path) { |
| 47 wdbs_.reset(new WebDatabaseService(path)); |
| 48 wdbs_->LoadDatabase(Bind(&WebDataServiceBase::DatabaseInitOnDB, this)); |
| 49 } |
| 50 |
| 51 void WebDataServiceBase::UnloadDatabase() { |
| 52 if (!wdbs_) |
| 53 return; |
| 54 wdbs_->UnloadDatabase(); |
| 55 } |
| 56 |
| 57 void WebDataServiceBase::ShutdownDatabase() { |
| 58 if (!wdbs_) |
| 59 return; |
| 60 wdbs_->ShutdownDatabase(); |
| 61 } |
| 62 |
| 63 void WebDataServiceBase::CancelRequest(Handle h) { |
| 64 if (!wdbs_) |
| 65 return; |
| 66 wdbs_->CancelRequest(h); |
| 67 } |
| 68 |
| 69 content::NotificationSource WebDataServiceBase::GetNotificationSource() { |
| 70 return content::Source<WebDataServiceBase>(this); |
| 71 } |
| 72 |
| 73 bool WebDataServiceBase::IsDatabaseLoaded() { |
| 74 return db_loaded_; |
| 75 } |
| 76 |
| 77 WebDatabase* WebDataServiceBase::GetDatabase() { |
| 78 if (!wdbs_) |
| 79 return NULL; |
| 80 return wdbs_->GetDatabaseOnDB(); |
| 81 } |
| 82 |
| 83 WebDataServiceBase::~WebDataServiceBase() { |
| 84 wdbs_.reset(); |
| 85 } |
| 86 |
| 87 //////////////////////////////////////////////////////////////////////////////// |
| 88 // |
| 89 // The following methods are executed on the DB thread. |
| 90 // |
| 91 //////////////////////////////////////////////////////////////////////////////// |
| 92 |
| 93 void WebDataServiceBase::DBInitFailed(sql::InitStatus sql_status) { |
| 94 if (!profile_error_callback_.is_null()) |
| 95 profile_error_callback_.Run(sql_status); |
| 96 } |
| 97 |
| 98 void WebDataServiceBase::NotifyDatabaseLoadedOnUIThread() { |
| 99 db_loaded_ = true; |
| 100 // Notify that the database has been initialized. |
| 101 content::NotificationService::current()->Notify( |
| 102 chrome::NOTIFICATION_WEB_DATABASE_LOADED, |
| 103 content::Source<WebDataServiceBase>(this), |
| 104 content::NotificationService::NoDetails()); |
| 105 } |
| 106 |
| 107 void WebDataServiceBase::DatabaseInitOnDB(sql::InitStatus status) { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 109 if (status == sql::INIT_OK) { |
| 110 BrowserThread::PostTask( |
| 111 BrowserThread::UI, FROM_HERE, |
| 112 base::Bind(&WebDataServiceBase::NotifyDatabaseLoadedOnUIThread, this)); |
| 113 } else { |
| 114 BrowserThread::PostTask( |
| 115 BrowserThread::UI, FROM_HERE, |
| 116 base::Bind(&WebDataServiceBase::DBInitFailed, this, status)); |
| 117 } |
| 118 } |
| 119 |
OLD | NEW |