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_file_system_helper.h" | 5 #include "chrome/browser/browsing_data_file_system_helper.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
14 #include "content/browser/in_process_webkit/webkit_context.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
18 #include "webkit/fileapi/file_system_quota_util.h" | 14 #include "webkit/fileapi/file_system_quota_util.h" |
19 #include "webkit/fileapi/file_system_context.h" | 15 #include "webkit/fileapi/file_system_context.h" |
20 #include "webkit/fileapi/file_system_types.h" | 16 #include "webkit/fileapi/file_system_types.h" |
21 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 17 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
22 #include "webkit/glue/webkit_glue.h" | |
23 | |
24 using WebKit::WebSecurityOrigin; | |
25 | 18 |
26 namespace { | 19 namespace { |
27 | 20 |
| 21 // An implementation of the BrowsingDataFileSystemHelper interface that pulls |
| 22 // data from a given |profile| and returns a list of FileSystemInfo items to a |
| 23 // client. |
28 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper { | 24 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper { |
29 public: | 25 public: |
| 26 // BrowsingDataFileSystemHelper implementation |
30 explicit BrowsingDataFileSystemHelperImpl(Profile* profile); | 27 explicit BrowsingDataFileSystemHelperImpl(Profile* profile); |
31 | |
32 virtual void StartFetching( | 28 virtual void StartFetching( |
33 Callback1<const std::vector<FileSystemInfo>& >::Type* callback); | 29 Callback1<const std::vector<FileSystemInfo>& >::Type* callback); |
34 virtual void CancelNotification(); | 30 virtual void CancelNotification(); |
35 virtual void DeleteFileSystemOrigin(const GURL& origin); | 31 virtual void DeleteFileSystemOrigin(const GURL& origin); |
36 | 32 |
37 private: | 33 private: |
38 virtual ~BrowsingDataFileSystemHelperImpl(); | 34 virtual ~BrowsingDataFileSystemHelperImpl(); |
39 | 35 |
40 // Enumerates all filesystem files in the FILE thread. | 36 // Enumerates all filesystem files, storing the resulting list into |
| 37 // file_system_file_ for later use. This must be called on the FILE thread. |
41 void FetchFileSystemInfoInFileThread(); | 38 void FetchFileSystemInfoInFileThread(); |
42 // Notifies the completion callback in the UI thread. | 39 |
43 void NotifyInUIThread(); | 40 // Triggers the success callback as the end of a StartFetching workflow. This |
44 // Delete data for an origin on the FILE thread. | 41 // must be called on the UI thread. |
| 42 void NotifyOnUIThread(); |
| 43 |
| 44 // Deletes all file systems associated with |origin|. This must be called on |
| 45 // the FILE thread. |
45 void DeleteFileSystemOriginInFileThread(const GURL& origin); | 46 void DeleteFileSystemOriginInFileThread(const GURL& origin); |
46 | 47 |
| 48 // We don't own the Profile object. Clients are responsible for destroying the |
| 49 // object when it's no longer used. |
47 Profile* profile_; | 50 Profile* profile_; |
48 | 51 |
49 // This only mutates in the FILE thread. | 52 // Holds the current list of file systems returned to the client after |
| 53 // StartFetching is called. This only mutates in the FILE thread. |
50 std::vector<FileSystemInfo> file_system_info_; | 54 std::vector<FileSystemInfo> file_system_info_; |
51 | 55 |
52 // This only mutates on the UI thread. | 56 // Holds the callback passed in at the beginning of the StartFetching workflow |
| 57 // so that it can be triggered via NotifyOnUIThread. This only mutates on the |
| 58 // UI thread. |
53 scoped_ptr<Callback1<const std::vector<FileSystemInfo>& >::Type > | 59 scoped_ptr<Callback1<const std::vector<FileSystemInfo>& >::Type > |
54 completion_callback_; | 60 completion_callback_; |
55 | 61 |
56 // Indicates whether or not we're currently fetching information: | 62 // Indicates whether or not we're currently fetching information: set to true |
57 // it's true when StartFetching() is called in the UI thread, and it's reset | 63 // when StartFetching is called on the UI thread, and reset to false when |
58 // after we notified the callback in the UI thread. | 64 // NotifyOnUIThread triggers the success callback. |
59 // This only mutates on the UI thread. | 65 // This property only mutates on the UI thread. |
60 bool is_fetching_; | 66 bool is_fetching_; |
61 | 67 |
62 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl); | 68 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl); |
63 }; | 69 }; |
64 | 70 |
65 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl( | 71 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl( |
66 Profile* profile) | 72 Profile* profile) |
67 : profile_(profile), | 73 : profile_(profile), |
68 is_fetching_(false) { | 74 is_fetching_(false) { |
69 DCHECK(profile_); | 75 DCHECK(profile_); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 &BrowsingDataFileSystemHelperImpl:: | 108 &BrowsingDataFileSystemHelperImpl:: |
103 DeleteFileSystemOriginInFileThread, | 109 DeleteFileSystemOriginInFileThread, |
104 origin)); | 110 origin)); |
105 } | 111 } |
106 | 112 |
107 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() { | 113 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() { |
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
109 scoped_ptr<fileapi::SandboxMountPointProvider::OriginEnumerator> | 115 scoped_ptr<fileapi::SandboxMountPointProvider::OriginEnumerator> |
110 origin_enumerator(profile_->GetFileSystemContext()->path_manager()-> | 116 origin_enumerator(profile_->GetFileSystemContext()->path_manager()-> |
111 sandbox_provider()->CreateOriginEnumerator()); | 117 sandbox_provider()->CreateOriginEnumerator()); |
112 // We don't own this pointer; deleting it would be a bad idea. | 118 |
| 119 // We don't own this pointer; it's a magic singleton generated by the |
| 120 // profile's FileSystemContext. Deleting it would be a bad idea. |
113 fileapi::FileSystemQuotaUtil* quota_util = profile_-> | 121 fileapi::FileSystemQuotaUtil* quota_util = profile_-> |
114 GetFileSystemContext()->GetQuotaUtil(fileapi::kFileSystemTypeTemporary); | 122 GetFileSystemContext()->GetQuotaUtil(fileapi::kFileSystemTypeTemporary); |
115 | 123 |
116 GURL current; | 124 GURL current; |
117 while (!(current = origin_enumerator->Next()).is_empty()) { | 125 while (!(current = origin_enumerator->Next()).is_empty()) { |
118 if (current.SchemeIs(chrome::kExtensionScheme)) { | 126 if (current.SchemeIs(chrome::kExtensionScheme)) { |
119 // Extension state is not considered browsing data. | 127 // Extension state is not considered browsing data. |
120 continue; | 128 continue; |
121 } | 129 } |
| 130 // We can call these synchronous methods as we've already verified that |
| 131 // we're running on the FILE thread. |
122 int64 persistent_usage = quota_util->GetOriginUsageOnFileThread(current, | 132 int64 persistent_usage = quota_util->GetOriginUsageOnFileThread(current, |
123 fileapi::kFileSystemTypePersistent); | 133 fileapi::kFileSystemTypePersistent); |
124 int64 temporary_usage = quota_util->GetOriginUsageOnFileThread(current, | 134 int64 temporary_usage = quota_util->GetOriginUsageOnFileThread(current, |
125 fileapi::kFileSystemTypeTemporary); | 135 fileapi::kFileSystemTypeTemporary); |
126 file_system_info_.push_back( | 136 file_system_info_.push_back( |
127 FileSystemInfo( | 137 FileSystemInfo( |
128 current, | 138 current, |
129 origin_enumerator->HasFileSystemType( | 139 origin_enumerator->HasFileSystemType( |
130 fileapi::kFileSystemTypePersistent), | 140 fileapi::kFileSystemTypePersistent), |
131 origin_enumerator->HasFileSystemType( | 141 origin_enumerator->HasFileSystemType( |
132 fileapi::kFileSystemTypeTemporary), | 142 fileapi::kFileSystemTypeTemporary), |
133 persistent_usage, | 143 persistent_usage, |
134 temporary_usage)); | 144 temporary_usage)); |
135 } | 145 } |
136 | 146 |
137 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
138 BrowserThread::UI, FROM_HERE, | 148 BrowserThread::UI, FROM_HERE, |
139 NewRunnableMethod( | 149 NewRunnableMethod( |
140 this, &BrowsingDataFileSystemHelperImpl::NotifyInUIThread)); | 150 this, &BrowsingDataFileSystemHelperImpl::NotifyOnUIThread)); |
141 } | 151 } |
142 | 152 |
143 void BrowsingDataFileSystemHelperImpl::NotifyInUIThread() { | 153 void BrowsingDataFileSystemHelperImpl::NotifyOnUIThread() { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 DCHECK(is_fetching_); | 155 DCHECK(is_fetching_); |
146 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | 156 // completion_callback_ mutates only in the UI thread, so we're safe to test |
147 // test it here. | 157 // it here. |
148 if (completion_callback_ != NULL) { | 158 if (completion_callback_ != NULL) { |
149 completion_callback_->Run(file_system_info_); | 159 completion_callback_->Run(file_system_info_); |
150 completion_callback_.reset(); | 160 completion_callback_.reset(); |
151 } | 161 } |
152 is_fetching_ = false; | 162 is_fetching_ = false; |
153 } | 163 } |
154 | 164 |
155 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread( | 165 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread( |
156 const GURL& origin) { | 166 const GURL& origin) { |
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
(...skipping 17 matching lines...) Expand all Loading... |
175 | 185 |
176 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {} | 186 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {} |
177 | 187 |
178 // static | 188 // static |
179 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create( | 189 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create( |
180 Profile* profile) { | 190 Profile* profile) { |
181 return new BrowsingDataFileSystemHelperImpl(profile); | 191 return new BrowsingDataFileSystemHelperImpl(profile); |
182 } | 192 } |
183 | 193 |
184 CannedBrowsingDataFileSystemHelper:: | 194 CannedBrowsingDataFileSystemHelper:: |
185 PendingFileSystemInfo::PendingFileSystemInfo() { | 195 PendingFileSystemInfo::PendingFileSystemInfo() { |
186 } | 196 } |
187 | 197 |
188 CannedBrowsingDataFileSystemHelper:: | 198 CannedBrowsingDataFileSystemHelper:: |
189 PendingFileSystemInfo::PendingFileSystemInfo(const GURL& origin, | 199 PendingFileSystemInfo::PendingFileSystemInfo( |
190 const fileapi::FileSystemType type, | 200 const GURL& origin, |
191 int64 size) | 201 const fileapi::FileSystemType type, |
| 202 int64 size) |
192 : origin(origin), | 203 : origin(origin), |
193 type(type), | 204 type(type), |
194 size(size) { | 205 size(size) { |
195 } | 206 } |
196 | 207 |
197 CannedBrowsingDataFileSystemHelper:: | 208 CannedBrowsingDataFileSystemHelper:: |
198 PendingFileSystemInfo::~PendingFileSystemInfo() { | 209 PendingFileSystemInfo::~PendingFileSystemInfo() { |
199 } | 210 } |
200 | 211 |
201 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper( | 212 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper( |
202 Profile* profile) | 213 Profile* /* profile */) |
203 : profile_(profile), | 214 : is_fetching_(false) { |
204 is_fetching_(false) { | 215 } |
205 DCHECK(profile); | 216 |
| 217 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper() |
| 218 : is_fetching_(false) { |
206 } | 219 } |
207 | 220 |
208 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {} | 221 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {} |
209 | 222 |
210 CannedBrowsingDataFileSystemHelper* | 223 CannedBrowsingDataFileSystemHelper* |
211 CannedBrowsingDataFileSystemHelper::Clone() { | 224 CannedBrowsingDataFileSystemHelper::Clone() { |
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
213 CannedBrowsingDataFileSystemHelper* clone = | 226 CannedBrowsingDataFileSystemHelper* clone = |
214 new CannedBrowsingDataFileSystemHelper(profile_); | 227 new CannedBrowsingDataFileSystemHelper(); |
215 | |
216 clone->pending_file_system_info_ = pending_file_system_info_; | 228 clone->pending_file_system_info_ = pending_file_system_info_; |
217 clone->file_system_info_ = file_system_info_; | 229 clone->file_system_info_ = file_system_info_; |
218 return clone; | 230 return clone; |
219 } | 231 } |
220 | 232 |
221 void CannedBrowsingDataFileSystemHelper::AddFileSystem( | 233 void CannedBrowsingDataFileSystemHelper::AddFileSystem( |
222 const GURL& origin, const fileapi::FileSystemType type, const int64 size) { | 234 const GURL& origin, const fileapi::FileSystemType type, const int64 size) { |
223 pending_file_system_info_.push_back( | 235 pending_file_system_info_.push_back( |
224 PendingFileSystemInfo(origin, type, size)); | 236 PendingFileSystemInfo(origin, type, size)); |
225 } | 237 } |
226 | 238 |
227 void CannedBrowsingDataFileSystemHelper::Reset() { | 239 void CannedBrowsingDataFileSystemHelper::Reset() { |
228 file_system_info_.clear(); | 240 file_system_info_.clear(); |
229 pending_file_system_info_.clear(); | 241 pending_file_system_info_.clear(); |
230 } | 242 } |
231 | 243 |
232 bool CannedBrowsingDataFileSystemHelper::empty() const { | 244 bool CannedBrowsingDataFileSystemHelper::empty() const { |
233 return file_system_info_.empty() && pending_file_system_info_.empty(); | 245 return file_system_info_.empty() && pending_file_system_info_.empty(); |
234 } | 246 } |
235 | 247 |
236 void CannedBrowsingDataFileSystemHelper::StartFetching( | 248 void CannedBrowsingDataFileSystemHelper::StartFetching( |
237 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) { | 249 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) { |
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 DCHECK(!is_fetching_); | 251 DCHECK(!is_fetching_); |
240 DCHECK(callback); | 252 DCHECK(callback); |
241 is_fetching_ = true; | 253 is_fetching_ = true; |
242 completion_callback_.reset(callback); | 254 completion_callback_.reset(callback); |
243 | 255 |
244 for (std::vector<PendingFileSystemInfo>::const_iterator | 256 for (std::vector<PendingFileSystemInfo>::const_iterator |
245 info = pending_file_system_info_.begin(); | 257 info = pending_file_system_info_.begin(); |
246 info != pending_file_system_info_.end(); ++info) { | 258 info != pending_file_system_info_.end(); |
| 259 ++info) { |
247 bool duplicate = false; | 260 bool duplicate = false; |
248 for (std::vector<FileSystemInfo>::iterator | 261 for (std::vector<FileSystemInfo>::iterator |
249 file_system = file_system_info_.begin(); | 262 file_system = file_system_info_.begin(); |
250 file_system != file_system_info_.end(); | 263 file_system != file_system_info_.end(); |
251 ++file_system) { | 264 ++file_system) { |
252 if (file_system->origin == info->origin) { | 265 if (file_system->origin == info->origin) { |
253 if (info->type == fileapi::kFileSystemTypePersistent) { | 266 if (info->type == fileapi::kFileSystemTypePersistent) { |
254 file_system->has_persistent = true; | 267 file_system->has_persistent = true; |
255 file_system->usage_persistent = info->size; | 268 file_system->usage_persistent = info->size; |
256 } else { | 269 } else { |
257 file_system->has_temporary = true; | 270 file_system->has_temporary = true; |
258 file_system->usage_temporary = info->size; | 271 file_system->usage_temporary = info->size; |
259 } | 272 } |
260 duplicate = true; | 273 duplicate = true; |
261 break; | 274 break; |
262 } | 275 } |
263 } | 276 } |
264 if (duplicate) | 277 if (duplicate) |
265 continue; | 278 continue; |
266 | 279 |
267 file_system_info_.push_back(FileSystemInfo( | 280 file_system_info_.push_back(FileSystemInfo( |
268 info->origin, | 281 info->origin, |
269 (info->type == fileapi::kFileSystemTypePersistent), | 282 (info->type == fileapi::kFileSystemTypePersistent), |
270 (info->type == fileapi::kFileSystemTypeTemporary), | 283 (info->type == fileapi::kFileSystemTypeTemporary), |
271 (info->type == fileapi::kFileSystemTypePersistent) ? info->size : 0, | 284 (info->type == fileapi::kFileSystemTypePersistent) ? info->size : 0, |
272 (info->type == fileapi::kFileSystemTypeTemporary) ? info->size : 0)); | 285 (info->type == fileapi::kFileSystemTypeTemporary) ? info->size : 0)); |
273 } | 286 } |
274 pending_file_system_info_.clear(); | 287 pending_file_system_info_.clear(); |
275 | 288 |
276 // MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this, | |
277 // &CannedBrowsingDataFileSystemHelper::Notify)); | |
278 | |
279 BrowserThread::PostTask( | 289 BrowserThread::PostTask( |
280 BrowserThread::UI, FROM_HERE, | 290 BrowserThread::UI, FROM_HERE, |
281 NewRunnableMethod( | 291 NewRunnableMethod( |
282 this, &CannedBrowsingDataFileSystemHelper::Notify)); | 292 this, &CannedBrowsingDataFileSystemHelper::NotifyOnUIThread)); |
283 } | 293 } |
284 | 294 |
285 void CannedBrowsingDataFileSystemHelper::Notify() { | 295 void CannedBrowsingDataFileSystemHelper::NotifyOnUIThread() { |
286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
287 DCHECK(is_fetching_); | 297 DCHECK(is_fetching_); |
288 if (completion_callback_ != NULL) { | 298 if (completion_callback_ != NULL) { |
289 completion_callback_->Run(file_system_info_); | 299 completion_callback_->Run(file_system_info_); |
290 completion_callback_.reset(); | 300 completion_callback_.reset(); |
291 } | 301 } |
292 is_fetching_ = false; | 302 is_fetching_ = false; |
293 } | 303 } |
294 | 304 |
295 void CannedBrowsingDataFileSystemHelper::CancelNotification() { | 305 void CannedBrowsingDataFileSystemHelper::CancelNotification() { |
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
297 completion_callback_.reset(); | 307 completion_callback_.reset(); |
298 } | 308 } |
OLD | NEW |