OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_indexed_db_helper.h" | 5 #include "chrome/browser/browsing_data_indexed_db_helper.h" |
6 | 6 |
7 #include "base/callback_old.h" | 7 #include "base/callback_old.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
17 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
18 #include "content/browser/in_process_webkit/webkit_context.h" | 15 #include "content/browser/in_process_webkit/webkit_context.h" |
| 16 #include "webkit/database/database_util.h" |
19 #include "webkit/glue/webkit_glue.h" | 17 #include "webkit/glue/webkit_glue.h" |
20 | 18 |
21 using WebKit::WebSecurityOrigin; | 19 using webkit_database::DatabaseUtil; |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 class BrowsingDataIndexedDBHelperImpl : public BrowsingDataIndexedDBHelper { | 23 class BrowsingDataIndexedDBHelperImpl : public BrowsingDataIndexedDBHelper { |
26 public: | 24 public: |
27 explicit BrowsingDataIndexedDBHelperImpl(Profile* profile); | 25 explicit BrowsingDataIndexedDBHelperImpl(Profile* profile); |
28 | 26 |
29 virtual void StartFetching( | 27 virtual void StartFetching( |
30 Callback1<const std::list<IndexedDBInfo>& >::Type* callback); | 28 Callback1<const std::list<IndexedDBInfo>& >::Type* callback); |
31 virtual void CancelNotification(); | 29 virtual void CancelNotification(); |
32 virtual void DeleteIndexedDBFile(const FilePath& file_path); | 30 virtual void DeleteIndexedDB(const GURL& origin); |
33 | 31 |
34 private: | 32 private: |
35 virtual ~BrowsingDataIndexedDBHelperImpl(); | 33 virtual ~BrowsingDataIndexedDBHelperImpl(); |
36 | 34 |
37 // Enumerates all indexed database files in the WEBKIT thread. | 35 // Enumerates all indexed database files in the WEBKIT thread. |
38 void FetchIndexedDBInfoInWebKitThread(); | 36 void FetchIndexedDBInfoInWebKitThread(); |
39 // Notifies the completion callback in the UI thread. | 37 // Notifies the completion callback in the UI thread. |
40 void NotifyInUIThread(); | 38 void NotifyInUIThread(); |
41 // Delete a single indexed database file in the WEBKIT thread. | 39 // Delete a single indexed database in the WEBKIT thread. |
42 void DeleteIndexedDBFileInWebKitThread(const FilePath& file_path); | 40 void DeleteIndexedDBInWebKitThread(const GURL& origin); |
43 | 41 |
44 Profile* profile_; | 42 scoped_refptr<IndexedDBContext> indexed_db_context_; |
45 | 43 |
46 // This only mutates in the WEBKIT thread. | 44 // This only mutates in the WEBKIT thread. |
47 std::list<IndexedDBInfo> indexed_db_info_; | 45 std::list<IndexedDBInfo> indexed_db_info_; |
48 | 46 |
49 // This only mutates on the UI thread. | 47 // This only mutates on the UI thread. |
50 scoped_ptr<Callback1<const std::list<IndexedDBInfo>& >::Type > | 48 scoped_ptr<Callback1<const std::list<IndexedDBInfo>& >::Type > |
51 completion_callback_; | 49 completion_callback_; |
52 // Indicates whether or not we're currently fetching information: | 50 // Indicates whether or not we're currently fetching information: |
53 // it's true when StartFetching() is called in the UI thread, and it's reset | 51 // it's true when StartFetching() is called in the UI thread, and it's reset |
54 // after we notified the callback in the UI thread. | 52 // after we notified the callback in the UI thread. |
55 // This only mutates on the UI thread. | 53 // This only mutates on the UI thread. |
56 bool is_fetching_; | 54 bool is_fetching_; |
57 | 55 |
58 DISALLOW_COPY_AND_ASSIGN(BrowsingDataIndexedDBHelperImpl); | 56 DISALLOW_COPY_AND_ASSIGN(BrowsingDataIndexedDBHelperImpl); |
59 }; | 57 }; |
60 | 58 |
61 BrowsingDataIndexedDBHelperImpl::BrowsingDataIndexedDBHelperImpl( | 59 BrowsingDataIndexedDBHelperImpl::BrowsingDataIndexedDBHelperImpl( |
62 Profile* profile) | 60 Profile* profile) |
63 : profile_(profile), | 61 : indexed_db_context_(profile->GetWebKitContext()->indexed_db_context()), |
64 completion_callback_(NULL), | 62 completion_callback_(NULL), |
65 is_fetching_(false) { | 63 is_fetching_(false) { |
66 DCHECK(profile_); | 64 DCHECK(indexed_db_context_.get()); |
67 } | 65 } |
68 | 66 |
69 BrowsingDataIndexedDBHelperImpl::~BrowsingDataIndexedDBHelperImpl() { | 67 BrowsingDataIndexedDBHelperImpl::~BrowsingDataIndexedDBHelperImpl() { |
70 } | 68 } |
71 | 69 |
72 void BrowsingDataIndexedDBHelperImpl::StartFetching( | 70 void BrowsingDataIndexedDBHelperImpl::StartFetching( |
73 Callback1<const std::list<IndexedDBInfo>& >::Type* callback) { | 71 Callback1<const std::list<IndexedDBInfo>& >::Type* callback) { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
75 DCHECK(!is_fetching_); | 73 DCHECK(!is_fetching_); |
76 DCHECK(callback); | 74 DCHECK(callback); |
77 is_fetching_ = true; | 75 is_fetching_ = true; |
78 completion_callback_.reset(callback); | 76 completion_callback_.reset(callback); |
79 BrowserThread::PostTask( | 77 BrowserThread::PostTask( |
80 BrowserThread::WEBKIT, FROM_HERE, | 78 BrowserThread::WEBKIT, FROM_HERE, |
81 NewRunnableMethod( | 79 NewRunnableMethod( |
82 this, | 80 this, |
83 &BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread)); | 81 &BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread)); |
84 } | 82 } |
85 | 83 |
86 void BrowsingDataIndexedDBHelperImpl::CancelNotification() { | 84 void BrowsingDataIndexedDBHelperImpl::CancelNotification() { |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
88 completion_callback_.reset(); | 86 completion_callback_.reset(); |
89 } | 87 } |
90 | 88 |
91 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBFile( | 89 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDB( |
92 const FilePath& file_path) { | 90 const GURL& origin) { |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
94 BrowserThread::PostTask( | 92 BrowserThread::PostTask( |
95 BrowserThread::WEBKIT, FROM_HERE, | 93 BrowserThread::WEBKIT, FROM_HERE, |
96 NewRunnableMethod( | 94 NewRunnableMethod( |
97 this, | 95 this, |
98 &BrowsingDataIndexedDBHelperImpl:: | 96 &BrowsingDataIndexedDBHelperImpl:: |
99 DeleteIndexedDBFileInWebKitThread, | 97 DeleteIndexedDBInWebKitThread, |
100 file_path)); | 98 origin)); |
101 } | 99 } |
102 | 100 |
103 void BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread() { | 101 void BrowsingDataIndexedDBHelperImpl::FetchIndexedDBInfoInWebKitThread() { |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
105 file_util::FileEnumerator file_enumerator( | 103 std::vector<GURL> origins; |
106 profile_->GetWebKitContext()->data_path().Append( | 104 indexed_db_context_->GetAllOrigins(&origins); |
107 IndexedDBContext::kIndexedDBDirectory), | 105 for (std::vector<GURL>::const_iterator iter = origins.begin(); |
108 false, file_util::FileEnumerator::DIRECTORIES); | 106 iter != origins.end(); ++iter) { |
109 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); | 107 const GURL& origin = *iter; |
110 file_path = file_enumerator.Next()) { | 108 if (origin.SchemeIs(chrome::kExtensionScheme)) |
111 if (file_path.Extension() == IndexedDBContext::kIndexedDBExtension) { | 109 continue; // Extension state is not considered browsing data. |
112 WebSecurityOrigin web_security_origin = | 110 indexed_db_info_.push_back(IndexedDBInfo( |
113 WebSecurityOrigin::createFromDatabaseIdentifier( | 111 origin, |
114 webkit_glue::FilePathToWebString(file_path.BaseName())); | 112 indexed_db_context_->GetOriginDiskUsage(origin), |
115 if (EqualsASCII(web_security_origin.protocol(), | 113 indexed_db_context_->GetOriginLastModified(origin))); |
116 chrome::kExtensionScheme)) { | |
117 // Extension state is not considered browsing data. | |
118 continue; | |
119 } | |
120 base::PlatformFileInfo file_info; | |
121 bool ret = file_util::GetFileInfo(file_path, &file_info); | |
122 if (ret) { | |
123 indexed_db_info_.push_back(IndexedDBInfo( | |
124 web_security_origin.protocol().utf8(), | |
125 web_security_origin.host().utf8(), | |
126 web_security_origin.port(), | |
127 web_security_origin.databaseIdentifier().utf8(), | |
128 web_security_origin.toString().utf8(), | |
129 file_path, | |
130 file_info.size, | |
131 file_info.last_modified)); | |
132 } | |
133 } | |
134 } | 114 } |
135 | 115 |
136 BrowserThread::PostTask( | 116 BrowserThread::PostTask( |
137 BrowserThread::UI, FROM_HERE, | 117 BrowserThread::UI, FROM_HERE, |
138 NewRunnableMethod( | 118 NewRunnableMethod( |
139 this, &BrowsingDataIndexedDBHelperImpl::NotifyInUIThread)); | 119 this, &BrowsingDataIndexedDBHelperImpl::NotifyInUIThread)); |
140 } | 120 } |
141 | 121 |
142 void BrowsingDataIndexedDBHelperImpl::NotifyInUIThread() { | 122 void BrowsingDataIndexedDBHelperImpl::NotifyInUIThread() { |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
144 DCHECK(is_fetching_); | 124 DCHECK(is_fetching_); |
145 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | 125 // Note: completion_callback_ mutates only in the UI thread, so it's safe to |
146 // test it here. | 126 // test it here. |
147 if (completion_callback_ != NULL) { | 127 if (completion_callback_ != NULL) { |
148 completion_callback_->Run(indexed_db_info_); | 128 completion_callback_->Run(indexed_db_info_); |
149 completion_callback_.reset(); | 129 completion_callback_.reset(); |
150 } | 130 } |
151 is_fetching_ = false; | 131 is_fetching_ = false; |
152 } | 132 } |
153 | 133 |
154 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBFileInWebKitThread( | 134 void BrowsingDataIndexedDBHelperImpl::DeleteIndexedDBInWebKitThread( |
155 const FilePath& file_path) { | 135 const GURL& origin) { |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
157 // TODO(jochen): implement this once it's possible to delete indexed DBs. | 137 indexed_db_context_->DeleteIndexedDBForOrigin(origin); |
158 } | 138 } |
159 | 139 |
160 } // namespace | 140 } // namespace |
161 | 141 |
162 BrowsingDataIndexedDBHelper::IndexedDBInfo::IndexedDBInfo( | 142 BrowsingDataIndexedDBHelper::IndexedDBInfo::IndexedDBInfo( |
163 const std::string& protocol, | 143 const GURL& origin, |
164 const std::string& host, | |
165 unsigned short port, | |
166 const std::string& database_identifier, | |
167 const std::string& origin, | |
168 const FilePath& file_path, | |
169 int64 size, | 144 int64 size, |
170 base::Time last_modified) | 145 base::Time last_modified) |
171 : protocol(protocol), | 146 : origin(origin), |
172 host(host), | |
173 port(port), | |
174 database_identifier(database_identifier), | |
175 origin(origin), | |
176 file_path(file_path), | |
177 size(size), | 147 size(size), |
178 last_modified(last_modified) { | 148 last_modified(last_modified) { |
179 } | 149 } |
180 | 150 |
181 BrowsingDataIndexedDBHelper::IndexedDBInfo::~IndexedDBInfo() {} | 151 BrowsingDataIndexedDBHelper::IndexedDBInfo::~IndexedDBInfo() {} |
182 | 152 |
183 // static | 153 // static |
184 BrowsingDataIndexedDBHelper* BrowsingDataIndexedDBHelper::Create( | 154 BrowsingDataIndexedDBHelper* BrowsingDataIndexedDBHelper::Create( |
185 Profile* profile) { | 155 Profile* profile) { |
186 return new BrowsingDataIndexedDBHelperImpl(profile); | 156 return new BrowsingDataIndexedDBHelperImpl(profile); |
187 } | 157 } |
188 | 158 |
189 CannedBrowsingDataIndexedDBHelper:: | 159 CannedBrowsingDataIndexedDBHelper:: |
190 PendingIndexedDBInfo::PendingIndexedDBInfo() { | 160 PendingIndexedDBInfo::PendingIndexedDBInfo() { |
191 } | 161 } |
192 | 162 |
193 CannedBrowsingDataIndexedDBHelper:: | 163 CannedBrowsingDataIndexedDBHelper:: |
194 PendingIndexedDBInfo::PendingIndexedDBInfo(const GURL& origin, | 164 PendingIndexedDBInfo::PendingIndexedDBInfo(const GURL& origin, |
195 const string16& description) | 165 const string16& description) |
196 : origin(origin), | 166 : origin(origin), |
197 description(description) { | 167 description(description) { |
198 } | 168 } |
199 | 169 |
200 CannedBrowsingDataIndexedDBHelper:: | 170 CannedBrowsingDataIndexedDBHelper:: |
201 PendingIndexedDBInfo::~PendingIndexedDBInfo() { | 171 PendingIndexedDBInfo::~PendingIndexedDBInfo() { |
202 } | 172 } |
203 | 173 |
204 CannedBrowsingDataIndexedDBHelper::CannedBrowsingDataIndexedDBHelper( | 174 CannedBrowsingDataIndexedDBHelper::CannedBrowsingDataIndexedDBHelper() |
205 Profile* profile) | 175 : completion_callback_(NULL), |
206 : profile_(profile), | |
207 completion_callback_(NULL), | |
208 is_fetching_(false) { | 176 is_fetching_(false) { |
209 DCHECK(profile); | |
210 } | 177 } |
211 | 178 |
212 CannedBrowsingDataIndexedDBHelper* CannedBrowsingDataIndexedDBHelper::Clone() { | 179 CannedBrowsingDataIndexedDBHelper* CannedBrowsingDataIndexedDBHelper::Clone() { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
214 CannedBrowsingDataIndexedDBHelper* clone = | 181 CannedBrowsingDataIndexedDBHelper* clone = |
215 new CannedBrowsingDataIndexedDBHelper(profile_); | 182 new CannedBrowsingDataIndexedDBHelper(); |
216 | 183 |
217 base::AutoLock auto_lock(lock_); | 184 base::AutoLock auto_lock(lock_); |
218 clone->pending_indexed_db_info_ = pending_indexed_db_info_; | 185 clone->pending_indexed_db_info_ = pending_indexed_db_info_; |
219 clone->indexed_db_info_ = indexed_db_info_; | 186 clone->indexed_db_info_ = indexed_db_info_; |
220 return clone; | 187 return clone; |
221 } | 188 } |
222 | 189 |
223 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB( | 190 void CannedBrowsingDataIndexedDBHelper::AddIndexedDB( |
224 const GURL& origin, const string16& description) { | 191 const GURL& origin, const string16& description) { |
225 base::AutoLock auto_lock(lock_); | 192 base::AutoLock auto_lock(lock_); |
(...skipping 23 matching lines...) Expand all Loading... |
249 &CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread)); | 216 &CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread)); |
250 } | 217 } |
251 | 218 |
252 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {} | 219 CannedBrowsingDataIndexedDBHelper::~CannedBrowsingDataIndexedDBHelper() {} |
253 | 220 |
254 void CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread() { | 221 void CannedBrowsingDataIndexedDBHelper::ConvertPendingInfoInWebKitThread() { |
255 base::AutoLock auto_lock(lock_); | 222 base::AutoLock auto_lock(lock_); |
256 for (std::list<PendingIndexedDBInfo>::const_iterator | 223 for (std::list<PendingIndexedDBInfo>::const_iterator |
257 info = pending_indexed_db_info_.begin(); | 224 info = pending_indexed_db_info_.begin(); |
258 info != pending_indexed_db_info_.end(); ++info) { | 225 info != pending_indexed_db_info_.end(); ++info) { |
259 WebSecurityOrigin web_security_origin = | |
260 WebSecurityOrigin::createFromString( | |
261 UTF8ToUTF16(info->origin.spec())); | |
262 std::string security_origin(web_security_origin.toString().utf8()); | |
263 | |
264 bool duplicate = false; | 226 bool duplicate = false; |
265 for (std::list<IndexedDBInfo>::iterator | 227 for (std::list<IndexedDBInfo>::iterator |
266 indexed_db = indexed_db_info_.begin(); | 228 indexed_db = indexed_db_info_.begin(); |
267 indexed_db != indexed_db_info_.end(); ++indexed_db) { | 229 indexed_db != indexed_db_info_.end(); ++indexed_db) { |
268 if (indexed_db->origin == security_origin) { | 230 if (indexed_db->origin == info->origin) { |
269 duplicate = true; | 231 duplicate = true; |
270 break; | 232 break; |
271 } | 233 } |
272 } | 234 } |
273 if (duplicate) | 235 if (duplicate) |
274 continue; | 236 continue; |
275 | 237 |
276 indexed_db_info_.push_back(IndexedDBInfo( | 238 indexed_db_info_.push_back(IndexedDBInfo( |
277 web_security_origin.protocol().utf8(), | 239 info->origin, |
278 web_security_origin.host().utf8(), | |
279 web_security_origin.port(), | |
280 web_security_origin.databaseIdentifier().utf8(), | |
281 security_origin, | |
282 profile_->GetWebKitContext()->indexed_db_context()-> | |
283 GetIndexedDBFilePath(web_security_origin.databaseIdentifier()), | |
284 0, | 240 0, |
285 base::Time())); | 241 base::Time())); |
286 } | 242 } |
287 pending_indexed_db_info_.clear(); | 243 pending_indexed_db_info_.clear(); |
288 | 244 |
289 BrowserThread::PostTask( | 245 BrowserThread::PostTask( |
290 BrowserThread::UI, FROM_HERE, | 246 BrowserThread::UI, FROM_HERE, |
291 NewRunnableMethod( | 247 NewRunnableMethod( |
292 this, &CannedBrowsingDataIndexedDBHelper::NotifyInUIThread)); | 248 this, &CannedBrowsingDataIndexedDBHelper::NotifyInUIThread)); |
293 } | 249 } |
294 | 250 |
295 void CannedBrowsingDataIndexedDBHelper::NotifyInUIThread() { | 251 void CannedBrowsingDataIndexedDBHelper::NotifyInUIThread() { |
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
297 DCHECK(is_fetching_); | 253 DCHECK(is_fetching_); |
298 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | 254 // Note: completion_callback_ mutates only in the UI thread, so it's safe to |
299 // test it here. | 255 // test it here. |
300 if (completion_callback_ != NULL) { | 256 if (completion_callback_ != NULL) { |
301 completion_callback_->Run(indexed_db_info_); | 257 completion_callback_->Run(indexed_db_info_); |
302 completion_callback_.reset(); | 258 completion_callback_.reset(); |
303 } | 259 } |
304 is_fetching_ = false; | 260 is_fetching_ = false; |
305 } | 261 } |
306 | 262 |
307 void CannedBrowsingDataIndexedDBHelper::CancelNotification() { | 263 void CannedBrowsingDataIndexedDBHelper::CancelNotification() { |
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
309 completion_callback_.reset(); | 265 completion_callback_.reset(); |
310 } | 266 } |
OLD | NEW |