Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: components/webdata/common/web_data_service_base.cc

Issue 14103021: Use Observer to notify of WebDB load instead of callbacks (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix WIN builds Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 16 matching lines...) Expand all
27 const ProfileErrorCallback& callback) 27 const ProfileErrorCallback& callback)
28 : wdbs_(wdbs), 28 : wdbs_(wdbs),
29 db_loaded_(false), 29 db_loaded_(false),
30 profile_error_callback_(callback) { 30 profile_error_callback_(callback) {
31 // WebDataService requires DB thread if instantiated. 31 // WebDataService requires DB thread if instantiated.
32 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL) 32 // Set WebDataServiceFactory::GetInstance()->SetTestingFactory(&profile, NULL)
33 // if you do not want to instantiate WebDataService in your test. 33 // if you do not want to instantiate WebDataService in your test.
34 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB)); 34 DCHECK(BrowserThread::IsWellKnownThread(BrowserThread::DB));
35 } 35 }
36 36
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
37 void WebDataServiceBase::ShutdownOnUIThread() { 46 void WebDataServiceBase::ShutdownOnUIThread() {
38 db_loaded_ = false; 47 db_loaded_ = false;
39 BrowserThread::PostTask( 48 BrowserThread::PostTask(
40 BrowserThread::DB, FROM_HERE, 49 BrowserThread::DB, FROM_HERE,
41 base::Bind(&WebDataServiceBase::ShutdownOnDBThread, this)); 50 base::Bind(&WebDataServiceBase::ShutdownOnDBThread, this));
42 } 51 }
43 52
44 void WebDataServiceBase::Init() { 53 void WebDataServiceBase::Init() {
45 DCHECK(wdbs_.get()); 54 DCHECK(wdbs_.get());
46 wdbs_->LoadDatabase(Bind(&WebDataServiceBase::DatabaseInitOnDB, this)); 55 wdbs_->AddObserver(this);
56 wdbs_->LoadDatabase();
47 } 57 }
48 58
49 void WebDataServiceBase::UnloadDatabase() { 59 void WebDataServiceBase::UnloadDatabase() {
50 if (!wdbs_) 60 if (!wdbs_)
51 return; 61 return;
52 wdbs_->UnloadDatabase(); 62 wdbs_->UnloadDatabase();
53 } 63 }
54 64
55 void WebDataServiceBase::ShutdownDatabase() { 65 void WebDataServiceBase::ShutdownDatabase() {
56 if (!wdbs_) 66 if (!wdbs_)
57 return; 67 return;
58 wdbs_->ShutdownDatabase(); 68 wdbs_->ShutdownDatabase();
59 } 69 }
60 70
61 void WebDataServiceBase::CancelRequest(Handle h) { 71 void WebDataServiceBase::CancelRequest(Handle h) {
62 if (!wdbs_) 72 if (!wdbs_)
63 return; 73 return;
64 wdbs_->CancelRequest(h); 74 wdbs_->CancelRequest(h);
65 } 75 }
66 76
67 content::NotificationSource WebDataServiceBase::GetNotificationSource() { 77 content::NotificationSource WebDataServiceBase::GetNotificationSource() {
68 return content::Source<WebDataServiceBase>(this); 78 return content::Source<WebDataServiceBase>(this);
69 } 79 }
70 80
71 bool WebDataServiceBase::IsDatabaseLoaded() { 81 bool WebDataServiceBase::IsDatabaseLoaded() {
72 return db_loaded_; 82 return db_loaded_;
73 } 83 }
74 84
85 void WebDataServiceBase::AddDBObserver(WebDatabaseObserver* observer) {
86 if (!wdbs_)
87 return;
88 wdbs_->AddObserver(observer);
89 }
90
91 void WebDataServiceBase::RemoveDBObserver(WebDatabaseObserver* observer) {
92 if (!wdbs_)
93 return;
94 wdbs_->RemoveObserver(observer);
95 }
96
75 WebDatabase* WebDataServiceBase::GetDatabase() { 97 WebDatabase* WebDataServiceBase::GetDatabase() {
76 if (!wdbs_) 98 if (!wdbs_)
77 return NULL; 99 return NULL;
78 return wdbs_->GetDatabaseOnDB(); 100 return wdbs_->GetDatabaseOnDB();
79 } 101 }
80 102
81 base::SupportsUserData* WebDataServiceBase::GetDBUserData() { 103 base::SupportsUserData* WebDataServiceBase::GetDBUserData() {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
83 if (!db_thread_user_data_) 105 if (!db_thread_user_data_)
84 db_thread_user_data_.reset(new SupportsUserDataAggregatable()); 106 db_thread_user_data_.reset(new SupportsUserDataAggregatable());
85 return db_thread_user_data_.get(); 107 return db_thread_user_data_.get();
86 } 108 }
87 109
88 WebDataServiceBase::~WebDataServiceBase() { 110 WebDataServiceBase::~WebDataServiceBase() {
89 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?"; 111 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?";
90 } 112 }
91 113
92 void WebDataServiceBase::ShutdownOnDBThread() { 114 void WebDataServiceBase::ShutdownOnDBThread() {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
94 db_thread_user_data_.reset(); 116 db_thread_user_data_.reset();
95 } 117 }
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 }
OLDNEW
« no previous file with comments | « components/webdata/common/web_data_service_base.h ('k') | components/webdata/common/web_database_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698