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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_indexed_db_helper.cc

Issue 17518004: Move IndexedDB from WEBKIT_DEPRECATED to dedicated thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: IOS build fix Created 7 years, 6 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 "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h" 5 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browsing_data/browsing_data_helper.h" 16 #include "chrome/browser/browsing_data/browsing_data_helper.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/indexed_db_context.h" 18 #include "content/public/browser/indexed_db_context.h"
19 #include "webkit/browser/database/database_util.h"
20 19
21 using content::BrowserThread; 20 using content::BrowserThread;
22 using content::IndexedDBContext; 21 using content::IndexedDBContext;
23 using content::IndexedDBInfo; 22 using content::IndexedDBInfo;
24 using webkit_database::DatabaseUtil;
25 23
26 namespace { 24 namespace {
27 25
28 class BrowsingDataIndexedDBHelperImpl : public BrowsingDataIndexedDBHelper { 26 class BrowsingDataIndexedDBHelperImpl : public BrowsingDataIndexedDBHelper {
29 public: 27 public:
30 explicit BrowsingDataIndexedDBHelperImpl( 28 explicit BrowsingDataIndexedDBHelperImpl(
31 IndexedDBContext* indexed_db_context); 29 IndexedDBContext* indexed_db_context);
32 30
33 virtual void StartFetching( 31 virtual void StartFetching(
34 const base::Callback<void(const std::list<IndexedDBInfo>&)>& 32 const base::Callback<void(const std::list<IndexedDBInfo>&)>&
35 callback) OVERRIDE; 33 callback) OVERRIDE;
36 virtual void DeleteIndexedDB(const GURL& origin) OVERRIDE; 34 virtual void DeleteIndexedDB(const GURL& origin) OVERRIDE;
37 35
38 private: 36 private:
39 virtual ~BrowsingDataIndexedDBHelperImpl(); 37 virtual ~BrowsingDataIndexedDBHelperImpl();
40 38
41 // Enumerates all indexed database files in the WEBKIT thread. 39 // Enumerates all indexed database files in the IndexedDB thread.
42 void FetchIndexedDBInfoInWebKitThread(); 40 void FetchIndexedDBInfoInIndexedDBThread();
43 // Notifies the completion callback in the UI thread. 41 // Notifies the completion callback in the UI thread.
44 void NotifyInUIThread(); 42 void NotifyInUIThread();
45 // Delete a single indexed database in the WEBKIT thread. 43 // Delete a single indexed database in the IndexedDB thread.
46 void DeleteIndexedDBInWebKitThread(const GURL& origin); 44 void DeleteIndexedDBInIndexedDBThread(const GURL& origin);
47 45
48 scoped_refptr<IndexedDBContext> indexed_db_context_; 46 scoped_refptr<IndexedDBContext> indexed_db_context_;
49 47
50 // Access to |indexed_db_info_| is triggered indirectly via the UI thread and 48 // Access to |indexed_db_info_| is triggered indirectly via the UI thread and
51 // guarded by |is_fetching_|. This means |indexed_db_info_| is only accessed 49 // guarded by |is_fetching_|. This means |indexed_db_info_| is only accessed
52 // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on 50 // while |is_fetching_| is true. The flag |is_fetching_| is only accessed on
53 // the UI thread. 51 // the UI thread.
54 // In the context of this class |indexed_db_info_| is only accessed on the 52 // In the context of this class |indexed_db_info_| is only accessed on the
55 // WEBKIT thread. 53 // context's IndexedDB thread.
56 std::list<IndexedDBInfo> indexed_db_info_; 54 std::list<IndexedDBInfo> indexed_db_info_;
57 55
58 // This only mutates on the UI thread. 56 // This only mutates on the UI thread.
59 base::Callback<void(const std::list<IndexedDBInfo>&)> completion_callback_; 57 base::Callback<void(const std::list<IndexedDBInfo>&)> completion_callback_;
60 58
61 // Indicates whether or not we're currently fetching information: 59 // Indicates whether or not we're currently fetching information:
62 // it's true when StartFetching() is called in the UI thread, and it's reset 60 // it's true when StartFetching() is called in the UI thread, and it's reset
63 // after we notified the callback in the UI thread. 61 // after we notified the callback in the UI thread.
64 // This only mutates on the UI thread. 62 // This only mutates on the UI thread.
65 bool is_fetching_; 63 bool is_fetching_;
(...skipping 12 matching lines...) Expand all
78 } 76 }
79 77
80 void BrowsingDataIndexedDBHelperImpl::StartFetching( 78 void BrowsingDataIndexedDBHelperImpl::StartFetching(
81 const base::Callback<void(const std::list<IndexedDBInfo>&)>& callback) { 79 const base::Callback<void(const std::list<IndexedDBInfo>&)>& callback) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 DCHECK(!is_fetching_); 81 DCHECK(!is_fetching_);
84 DCHECK_EQ(false, callback.is_null()); 82 DCHECK_EQ(false, callback.is_null());
85 83
86 is_fetching_ = true; 84 is_fetching_ = true;
87 completion_callback_ = callback; 85 completion_callback_ = callback;
88 BrowserThread::PostTask( 86 indexed_db_context_->TaskRunner()->PostTask(
89 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, 87 FROM_HERE,
90 base::Bind( 88 base::Bind(
91 &BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread, 89 &BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInIndexedDBThread,
92 this)); 90 this));
93 } 91 }
94 92
95 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDB( 93 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDB(
96 const GURL& origin) { 94 const GURL& origin) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 BrowserThread::PostTask( 96 indexed_db_context_->TaskRunner()->PostTask(
99 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, 97 FROM_HERE,
100 base::Bind( 98 base::Bind(
101 &BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBInWebKitThread, this, 99 &BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBInIndexedDBThread,
100 this,
102 origin)); 101 origin));
103 } 102 }
104 103
105 void BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread() { 104 void BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInIndexedDBThread() {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); 105 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread());
107 std::vector<IndexedDBInfo> origins = indexed_db_context_->GetAllOriginsInfo(); 106 std::vector<IndexedDBInfo> origins = indexed_db_context_->GetAllOriginsInfo();
108 for (std::vector<IndexedDBInfo>::const_iterator iter = origins.begin(); 107 for (std::vector<IndexedDBInfo>::const_iterator iter = origins.begin();
109 iter != origins.end(); ++iter) { 108 iter != origins.end(); ++iter) {
110 const IndexedDBInfo& origin = *iter; 109 const IndexedDBInfo& origin = *iter;
111 if (!BrowsingDataHelper::HasWebScheme(origin.origin_)) 110 if (!BrowsingDataHelper::HasWebScheme(origin.origin_))
112 continue; // Non-websafe state is not considered browsing data. 111 continue; // Non-websafe state is not considered browsing data.
113 112
114 indexed_db_info_.push_back(origin); 113 indexed_db_info_.push_back(origin);
115 } 114 }
116 115
117 BrowserThread::PostTask( 116 BrowserThread::PostTask(
118 BrowserThread::UI, FROM_HERE, 117 BrowserThread::UI, FROM_HERE,
119 base::Bind(&BrowsingDataIndexedDBHelperImpl::NotifyInUIThread, this)); 118 base::Bind(&BrowsingDataIndexedDBHelperImpl::NotifyInUIThread, this));
120 } 119 }
121 120
122 void BrowsingDataIndexedDBHelperImpl::NotifyInUIThread() { 121 void BrowsingDataIndexedDBHelperImpl::NotifyInUIThread() {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
124 DCHECK(is_fetching_); 123 DCHECK(is_fetching_);
125 completion_callback_.Run(indexed_db_info_); 124 completion_callback_.Run(indexed_db_info_);
126 completion_callback_.Reset(); 125 completion_callback_.Reset();
127 is_fetching_ = false; 126 is_fetching_ = false;
128 } 127 }
129 128
130 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBInWebKitThread( 129 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBInIndexedDBThread(
131 const GURL& origin) { 130 const GURL& origin) {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); 131 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread());
133 indexed_db_context_->DeleteForOrigin(origin); 132 indexed_db_context_->DeleteForOrigin(origin);
134 } 133 }
135 134
136 } // namespace 135 } // namespace
137 136
138 137
139 // static 138 // static
140 BrowsingDataIndexedDBHelper* BrowsingDataIndexedDBHelper::Create( 139 BrowsingDataIndexedDBHelper* BrowsingDataIndexedDBHelper::Create(
141 IndexedDBContext* indexed_db_context) { 140 IndexedDBContext* indexed_db_context) {
142 return new BrowsingDataIndexedDBHelperImpl(indexed_db_context); 141 return new BrowsingDataIndexedDBHelperImpl(indexed_db_context);
(...skipping 21 matching lines...) Expand all
164 : is_fetching_(false) { 163 : is_fetching_(false) {
165 } 164 }
166 165
167 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {} 166 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {}
168 167
169 CannedBrowsingDataIndexedDBHelper* CannedBrowsingDataIndexedDBHelper::Clone() { 168 CannedBrowsingDataIndexedDBHelper* CannedBrowsingDataIndexedDBHelper::Clone() {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
171 CannedBrowsingDataIndexedDBHelper* clone = 170 CannedBrowsingDataIndexedDBHelper* clone =
172 new CannedBrowsingDataIndexedDBHelper(); 171 new CannedBrowsingDataIndexedDBHelper();
173 172
174 base::AutoLock auto_lock(lock_);
175 clone->pending_indexed_db_info_ = pending_indexed_db_info_; 173 clone->pending_indexed_db_info_ = pending_indexed_db_info_;
176 clone->indexed_db_info_ = indexed_db_info_; 174 clone->indexed_db_info_ = indexed_db_info_;
177 return clone; 175 return clone;
178 } 176 }
179 177
180 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB( 178 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB(
181 const GURL& origin, const string16& name) { 179 const GURL& origin, const string16& name) {
182 if (!BrowsingDataHelper::HasWebScheme(origin)) 180 if (!BrowsingDataHelper::HasWebScheme(origin))
183 return; // Non-websafe state is not considered browsing data. 181 return; // Non-websafe state is not considered browsing data.
184 182
185 base::AutoLock auto_lock(lock_);
186 pending_indexed_db_info_.insert(PendingIndexedDBInfo(origin, name)); 183 pending_indexed_db_info_.insert(PendingIndexedDBInfo(origin, name));
187 } 184 }
188 185
189 void CannedBrowsingDataIndexedDBHelper::Reset() { 186 void CannedBrowsingDataIndexedDBHelper::Reset() {
190 base::AutoLock auto_lock(lock_);
191 indexed_db_info_.clear(); 187 indexed_db_info_.clear();
192 pending_indexed_db_info_.clear(); 188 pending_indexed_db_info_.clear();
193 } 189 }
194 190
195 bool CannedBrowsingDataIndexedDBHelper::empty() const { 191 bool CannedBrowsingDataIndexedDBHelper::empty() const {
196 base::AutoLock auto_lock(lock_);
197 return indexed_db_info_.empty() && pending_indexed_db_info_.empty(); 192 return indexed_db_info_.empty() && pending_indexed_db_info_.empty();
198 } 193 }
199 194
200 size_t CannedBrowsingDataIndexedDBHelper::GetIndexedDBCount() const { 195 size_t CannedBrowsingDataIndexedDBHelper::GetIndexedDBCount() const {
201 base::AutoLock auto_lock(lock_);
202 return pending_indexed_db_info_.size(); 196 return pending_indexed_db_info_.size();
203 } 197 }
204 198
205 const std::set<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo>& 199 const std::set<CannedBrowsingDataIndexedDBHelper::PendingIndexedDBInfo>&
206 CannedBrowsingDataIndexedDBHelper::GetIndexedDBInfo() const { 200 CannedBrowsingDataIndexedDBHelper::GetIndexedDBInfo() const {
207 base::AutoLock auto_lock(lock_);
208 return pending_indexed_db_info_; 201 return pending_indexed_db_info_;
209 } 202 }
210 203
211 void CannedBrowsingDataIndexedDBHelper::StartFetching( 204 void CannedBrowsingDataIndexedDBHelper::StartFetching(
212 const base::Callback<void(const std::list<IndexedDBInfo>&)>& callback) { 205 const base::Callback<void(const std::list<IndexedDBInfo>&)>& callback) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 DCHECK(!is_fetching_); 207 DCHECK(!is_fetching_);
215 DCHECK_EQ(false, callback.is_null()); 208 DCHECK_EQ(false, callback.is_null());
216 209
217 is_fetching_ = true; 210 is_fetching_ = true;
218 completion_callback_ = callback; 211 completion_callback_ = callback;
219 BrowserThread::PostTask( 212
220 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, 213 // We post a task to emulate async fetching behavior.
221 base::Bind( 214 base::MessageLoop::current()->PostTask(
222 &CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread, 215 FROM_HERE,
223 this)); 216 base::Bind(&CannedBrowsingDataIndexedDBHelper::
217 ConvertPendingInfo,
218 this));
224 } 219 }
225 220
226 void CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread() { 221 void CannedBrowsingDataIndexedDBHelper::ConvertPendingInfo() {
227 base::AutoLock auto_lock(lock_); 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
228 indexed_db_info_.clear(); 223 indexed_db_info_.clear();
229 for (std::set<PendingIndexedDBInfo>::const_iterator 224 for (std::set<PendingIndexedDBInfo>::const_iterator
230 pending_info = pending_indexed_db_info_.begin(); 225 pending_info = pending_indexed_db_info_.begin();
231 pending_info != pending_indexed_db_info_.end(); ++pending_info) { 226 pending_info != pending_indexed_db_info_.end(); ++pending_info) {
232 IndexedDBInfo info(pending_info->origin, 0, base::Time(), base::FilePath()); 227 IndexedDBInfo info(pending_info->origin, 0, base::Time(), base::FilePath());
233 indexed_db_info_.push_back(info); 228 indexed_db_info_.push_back(info);
234 } 229 }
235 230
236 BrowserThread::PostTask(
237 BrowserThread::UI, FROM_HERE,
238 base::Bind(&CannedBrowsingDataIndexedDBHelper::NotifyInUIThread, this));
239 }
240
241 void CannedBrowsingDataIndexedDBHelper::NotifyInUIThread() {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
243 DCHECK(is_fetching_);
244
245 completion_callback_.Run(indexed_db_info_); 231 completion_callback_.Run(indexed_db_info_);
246 completion_callback_.Reset(); 232 completion_callback_.Reset();
247 is_fetching_ = false; 233 is_fetching_ = false;
248 } 234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698