OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_database_service.h" | 5 #include "components/webdata/common/web_database_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "components/webdata/common/web_data_request_manager.h" | 9 #include "components/webdata/common/web_data_request_manager.h" |
10 #include "components/webdata/common/web_data_results.h" | 10 #include "components/webdata/common/web_data_results.h" |
11 #include "components/webdata/common/web_data_service_backend.h" | |
12 #include "components/webdata/common/web_data_service_consumer.h" | 11 #include "components/webdata/common/web_data_service_consumer.h" |
| 12 #include "components/webdata/common/web_database_backend.h" |
13 | 13 |
14 using base::Bind; | 14 using base::Bind; |
15 using base::FilePath; | 15 using base::FilePath; |
16 | 16 |
17 // Receives messages from the backend on the DB thread, posts them to | 17 // Receives messages from the backend on the DB thread, posts them to |
18 // WebDatabaseService on the UI thread. | 18 // WebDatabaseService on the UI thread. |
19 class WebDatabaseService::BackendDelegate : | 19 class WebDatabaseService::BackendDelegate |
20 public WebDataServiceBackend::Delegate { | 20 : public WebDatabaseBackend::Delegate { |
21 public: | 21 public: |
22 BackendDelegate( | 22 BackendDelegate( |
23 const base::WeakPtr<WebDatabaseService>& web_database_service) | 23 const base::WeakPtr<WebDatabaseService>& web_database_service) |
24 : web_database_service_(web_database_service), | 24 : web_database_service_(web_database_service), |
25 callback_thread_(base::MessageLoopProxy::current()) { | 25 callback_thread_(base::MessageLoopProxy::current()) { |
26 } | 26 } |
27 | 27 |
28 void DBLoaded(sql::InitStatus status) override { | 28 void DBLoaded(sql::InitStatus status) override { |
29 callback_thread_->PostTask( | 29 callback_thread_->PostTask( |
30 FROM_HERE, | 30 FROM_HERE, |
(...skipping 18 matching lines...) Expand all Loading... |
49 // WebDatabaseService should be instantiated on UI thread. | 49 // WebDatabaseService should be instantiated on UI thread. |
50 DCHECK(ui_thread->BelongsToCurrentThread()); | 50 DCHECK(ui_thread->BelongsToCurrentThread()); |
51 // WebDatabaseService requires DB thread if instantiated. | 51 // WebDatabaseService requires DB thread if instantiated. |
52 DCHECK(db_thread.get()); | 52 DCHECK(db_thread.get()); |
53 } | 53 } |
54 | 54 |
55 WebDatabaseService::~WebDatabaseService() { | 55 WebDatabaseService::~WebDatabaseService() { |
56 } | 56 } |
57 | 57 |
58 void WebDatabaseService::AddTable(scoped_ptr<WebDatabaseTable> table) { | 58 void WebDatabaseService::AddTable(scoped_ptr<WebDatabaseTable> table) { |
59 if (!wds_backend_.get()) { | 59 if (!web_db_backend_.get()) { |
60 wds_backend_ = new WebDataServiceBackend( | 60 web_db_backend_ = new WebDatabaseBackend( |
61 path_, new BackendDelegate(weak_ptr_factory_.GetWeakPtr()), | 61 path_, new BackendDelegate(weak_ptr_factory_.GetWeakPtr()), db_thread_); |
62 db_thread_); | |
63 } | 62 } |
64 wds_backend_->AddTable(table.Pass()); | 63 web_db_backend_->AddTable(table.Pass()); |
65 } | 64 } |
66 | 65 |
67 void WebDatabaseService::LoadDatabase() { | 66 void WebDatabaseService::LoadDatabase() { |
68 DCHECK(wds_backend_.get()); | 67 DCHECK(web_db_backend_.get()); |
69 db_thread_->PostTask( | 68 db_thread_->PostTask( |
70 FROM_HERE, | 69 FROM_HERE, Bind(&WebDatabaseBackend::InitDatabase, web_db_backend_)); |
71 Bind(&WebDataServiceBackend::InitDatabase, wds_backend_)); | |
72 } | 70 } |
73 | 71 |
74 void WebDatabaseService::ShutdownDatabase() { | 72 void WebDatabaseService::ShutdownDatabase() { |
75 db_loaded_ = false; | 73 db_loaded_ = false; |
76 loaded_callbacks_.clear(); | 74 loaded_callbacks_.clear(); |
77 error_callbacks_.clear(); | 75 error_callbacks_.clear(); |
78 weak_ptr_factory_.InvalidateWeakPtrs(); | 76 weak_ptr_factory_.InvalidateWeakPtrs(); |
79 if (!wds_backend_.get()) | 77 if (!web_db_backend_.get()) |
80 return; | 78 return; |
81 db_thread_->PostTask( | 79 db_thread_->PostTask( |
82 FROM_HERE, Bind(&WebDataServiceBackend::ShutdownDatabase, wds_backend_)); | 80 FROM_HERE, Bind(&WebDatabaseBackend::ShutdownDatabase, web_db_backend_)); |
83 } | 81 } |
84 | 82 |
85 WebDatabase* WebDatabaseService::GetDatabaseOnDB() const { | 83 WebDatabase* WebDatabaseService::GetDatabaseOnDB() const { |
86 DCHECK(db_thread_->BelongsToCurrentThread()); | 84 DCHECK(db_thread_->BelongsToCurrentThread()); |
87 return wds_backend_.get() ? wds_backend_->database() : NULL; | 85 return web_db_backend_.get() ? web_db_backend_->database() : NULL; |
88 } | 86 } |
89 | 87 |
90 scoped_refptr<WebDataServiceBackend> WebDatabaseService::GetBackend() const { | 88 scoped_refptr<WebDatabaseBackend> WebDatabaseService::GetBackend() const { |
91 return wds_backend_; | 89 return web_db_backend_; |
92 } | 90 } |
93 | 91 |
94 void WebDatabaseService::ScheduleDBTask( | 92 void WebDatabaseService::ScheduleDBTask( |
95 const tracked_objects::Location& from_here, | 93 const tracked_objects::Location& from_here, |
96 const WriteTask& task) { | 94 const WriteTask& task) { |
97 DCHECK(wds_backend_.get()); | 95 DCHECK(web_db_backend_.get()); |
98 scoped_ptr<WebDataRequest> request( | 96 scoped_ptr<WebDataRequest> request( |
99 new WebDataRequest(NULL, wds_backend_->request_manager().get())); | 97 new WebDataRequest(NULL, web_db_backend_->request_manager().get())); |
100 db_thread_->PostTask(from_here, | 98 db_thread_->PostTask( |
101 Bind(&WebDataServiceBackend::DBWriteTaskWrapper, | 99 from_here, Bind(&WebDatabaseBackend::DBWriteTaskWrapper, web_db_backend_, |
102 wds_backend_, task, base::Passed(&request))); | 100 task, base::Passed(&request))); |
103 } | 101 } |
104 | 102 |
105 WebDataServiceBase::Handle WebDatabaseService::ScheduleDBTaskWithResult( | 103 WebDataServiceBase::Handle WebDatabaseService::ScheduleDBTaskWithResult( |
106 const tracked_objects::Location& from_here, | 104 const tracked_objects::Location& from_here, |
107 const ReadTask& task, | 105 const ReadTask& task, |
108 WebDataServiceConsumer* consumer) { | 106 WebDataServiceConsumer* consumer) { |
109 DCHECK(consumer); | 107 DCHECK(consumer); |
110 DCHECK(wds_backend_.get()); | 108 DCHECK(web_db_backend_.get()); |
111 scoped_ptr<WebDataRequest> request( | 109 scoped_ptr<WebDataRequest> request( |
112 new WebDataRequest(consumer, wds_backend_->request_manager().get())); | 110 new WebDataRequest(consumer, web_db_backend_->request_manager().get())); |
113 WebDataServiceBase::Handle handle = request->GetHandle(); | 111 WebDataServiceBase::Handle handle = request->GetHandle(); |
114 db_thread_->PostTask(from_here, | 112 db_thread_->PostTask( |
115 Bind(&WebDataServiceBackend::DBReadTaskWrapper, | 113 from_here, Bind(&WebDatabaseBackend::DBReadTaskWrapper, web_db_backend_, |
116 wds_backend_, task, base::Passed(&request))); | 114 task, base::Passed(&request))); |
117 return handle; | 115 return handle; |
118 } | 116 } |
119 | 117 |
120 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { | 118 void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { |
121 if (!wds_backend_.get()) | 119 if (!web_db_backend_.get()) |
122 return; | 120 return; |
123 wds_backend_->request_manager()->CancelRequest(h); | 121 web_db_backend_->request_manager()->CancelRequest(h); |
124 } | 122 } |
125 | 123 |
126 void WebDatabaseService::RegisterDBLoadedCallback( | 124 void WebDatabaseService::RegisterDBLoadedCallback( |
127 const DBLoadedCallback& callback) { | 125 const DBLoadedCallback& callback) { |
128 loaded_callbacks_.push_back(callback); | 126 loaded_callbacks_.push_back(callback); |
129 } | 127 } |
130 | 128 |
131 void WebDatabaseService::RegisterDBErrorCallback( | 129 void WebDatabaseService::RegisterDBErrorCallback( |
132 const DBLoadErrorCallback& callback) { | 130 const DBLoadErrorCallback& callback) { |
133 error_callbacks_.push_back(callback); | 131 error_callbacks_.push_back(callback); |
(...skipping 12 matching lines...) Expand all Loading... |
146 } else { | 144 } else { |
147 // Notify that the database load failed. | 145 // Notify that the database load failed. |
148 for (size_t i = 0; i < error_callbacks_.size(); i++) { | 146 for (size_t i = 0; i < error_callbacks_.size(); i++) { |
149 if (!error_callbacks_[i].is_null()) | 147 if (!error_callbacks_[i].is_null()) |
150 error_callbacks_[i].Run(status); | 148 error_callbacks_[i].Run(status); |
151 } | 149 } |
152 | 150 |
153 error_callbacks_.clear(); | 151 error_callbacks_.clear(); |
154 } | 152 } |
155 } | 153 } |
OLD | NEW |