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

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: First round of comments Created 7 years, 8 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()) {
dhollowa 2013/04/18 16:21:17 nit: no {} needed
Cait (Slow) 2013/04/19 19:06:22 Done.
43 profile_error_callback_.Run(status);
44 }
45 }
46
37 void WebDataServiceBase::ShutdownOnUIThread() { 47 void WebDataServiceBase::ShutdownOnUIThread() {
38 db_loaded_ = false; 48 db_loaded_ = false;
39 BrowserThread::PostTask( 49 BrowserThread::PostTask(
40 BrowserThread::DB, FROM_HERE, 50 BrowserThread::DB, FROM_HERE,
41 base::Bind(&WebDataServiceBase::ShutdownOnDBThread, this)); 51 base::Bind(&WebDataServiceBase::ShutdownOnDBThread, this));
42 } 52 }
43 53
44 void WebDataServiceBase::Init() { 54 void WebDataServiceBase::Init() {
45 DCHECK(wdbs_.get()); 55 DCHECK(wdbs_.get());
46 wdbs_->LoadDatabase(Bind(&WebDataServiceBase::DatabaseInitOnDB, this)); 56 wdbs_->AddObserver(this);
57 wdbs_->LoadDatabase();
47 } 58 }
48 59
49 void WebDataServiceBase::UnloadDatabase() { 60 void WebDataServiceBase::UnloadDatabase() {
50 if (!wdbs_) 61 if (!wdbs_)
51 return; 62 return;
52 wdbs_->UnloadDatabase(); 63 wdbs_->UnloadDatabase();
53 } 64 }
54 65
55 void WebDataServiceBase::ShutdownDatabase() { 66 void WebDataServiceBase::ShutdownDatabase() {
56 if (!wdbs_) 67 if (!wdbs_)
57 return; 68 return;
58 wdbs_->ShutdownDatabase(); 69 wdbs_->ShutdownDatabase();
59 } 70 }
60 71
61 void WebDataServiceBase::CancelRequest(Handle h) { 72 void WebDataServiceBase::CancelRequest(Handle h) {
62 if (!wdbs_) 73 if (!wdbs_)
63 return; 74 return;
64 wdbs_->CancelRequest(h); 75 wdbs_->CancelRequest(h);
65 } 76 }
66 77
67 content::NotificationSource WebDataServiceBase::GetNotificationSource() { 78 content::NotificationSource WebDataServiceBase::GetNotificationSource() {
68 return content::Source<WebDataServiceBase>(this); 79 return content::Source<WebDataServiceBase>(this);
69 } 80 }
70 81
71 bool WebDataServiceBase::IsDatabaseLoaded() { 82 bool WebDataServiceBase::IsDatabaseLoaded() {
72 return db_loaded_; 83 return db_loaded_;
73 } 84 }
74 85
86 void WebDataServiceBase::AddDBObserver(WebDatabaseObserver* observer) {
87 if (!wdbs_)
88 return;
89 wdbs_->AddObserver(observer);
90 }
91
92 void WebDataServiceBase::RemoveDBObserver(WebDatabaseObserver* observer) {
93 if (!wdbs_)
94 return;
95 wdbs_->RemoveObserver(observer);
96 }
97
75 WebDatabase* WebDataServiceBase::GetDatabase() { 98 WebDatabase* WebDataServiceBase::GetDatabase() {
76 if (!wdbs_) 99 if (!wdbs_)
77 return NULL; 100 return NULL;
78 return wdbs_->GetDatabaseOnDB(); 101 return wdbs_->GetDatabaseOnDB();
79 } 102 }
80 103
81 base::SupportsUserData* WebDataServiceBase::GetDBUserData() { 104 base::SupportsUserData* WebDataServiceBase::GetDBUserData() {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
83 if (!db_thread_user_data_) 106 if (!db_thread_user_data_)
84 db_thread_user_data_.reset(new SupportsUserDataAggregatable()); 107 db_thread_user_data_.reset(new SupportsUserDataAggregatable());
85 return db_thread_user_data_.get(); 108 return db_thread_user_data_.get();
86 } 109 }
87 110
88 WebDataServiceBase::~WebDataServiceBase() { 111 WebDataServiceBase::~WebDataServiceBase() {
89 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?"; 112 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?";
90 } 113 }
91 114
92 void WebDataServiceBase::ShutdownOnDBThread() { 115 void WebDataServiceBase::ShutdownOnDBThread() {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
94 db_thread_user_data_.reset(); 117 db_thread_user_data_.reset();
95 } 118 }
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

Powered by Google App Engine
This is Rietveld 408576698