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

Side by Side Diff: chrome/browser/browsing_data_file_system_helper.cc

Issue 7063020: Adding `browsing_data_filesystem_helper*` as the first step towards content settings UI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Wrong include. Created 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/browsing_data_file_system_helper.h"
6
7 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/profiles/profile.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"
19 #include "webkit/fileapi/file_system_context.h"
20 #include "webkit/fileapi/file_system_types.h"
21 #include "webkit/fileapi/sandbox_mount_point_provider.h"
22 #include "webkit/glue/webkit_glue.h"
23
24 using WebKit::WebSecurityOrigin;
25
26 namespace {
27
28 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper {
29 public:
30 explicit BrowsingDataFileSystemHelperImpl(Profile* profile);
31
32 virtual void StartFetching(
33 Callback1<const std::vector<FileSystemInfo>& >::Type* callback);
34 virtual void CancelNotification();
35 virtual void DeleteFileSystemOrigin(const GURL& origin);
36
37 private:
38 virtual ~BrowsingDataFileSystemHelperImpl();
39
40 // Enumerates all filesystem files in the FILE thread.
41 void FetchFileSystemInfoInFileThread();
42 // Notifies the completion callback in the UI thread.
43 void NotifyInUIThread();
44 // Delete data for an origin on the FILE thread.
45 void DeleteFileSystemOriginInFileThread(const GURL& origin);
46
47 Profile* profile_;
48
49 // This only mutates in the FILE thread.
50 std::vector<FileSystemInfo> file_system_info_;
51
52 // This only mutates on the UI thread.
53 scoped_ptr<Callback1<const std::vector<FileSystemInfo>& >::Type >
54 completion_callback_;
55
56 // Indicates whether or not we're currently fetching information:
57 // it's true when StartFetching() is called in the UI thread, and it's reset
58 // after we notified the callback in the UI thread.
59 // This only mutates on the UI thread.
60 bool is_fetching_;
61
62 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
63 };
64
65 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
66 Profile* profile)
67 : profile_(profile),
68 is_fetching_(false) {
69 DCHECK(profile_);
70 }
71
72 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() {
73 }
74
75 void BrowsingDataFileSystemHelperImpl::StartFetching(
76 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
78 DCHECK(!is_fetching_);
79 DCHECK(callback);
80 is_fetching_ = true;
81 completion_callback_.reset(callback);
82 BrowserThread::PostTask(
83 BrowserThread::FILE, FROM_HERE,
84 NewRunnableMethod(
85 this,
86 &BrowsingDataFileSystemHelperImpl::
87 FetchFileSystemInfoInFileThread));
88 }
89
90 void BrowsingDataFileSystemHelperImpl::CancelNotification() {
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92 completion_callback_.reset(NULL);
93 }
94
95 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin(
96 const GURL& origin) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 BrowserThread::PostTask(
99 BrowserThread::FILE, FROM_HERE,
100 NewRunnableMethod(
101 this,
102 &BrowsingDataFileSystemHelperImpl::
103 DeleteFileSystemOriginInFileThread,
104 origin));
105 }
106
107 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
109 scoped_ptr<fileapi::SandboxMountPointProvider::OriginEnumerator>
110 origin_enumerator(profile_->GetFileSystemContext()->path_manager()->
111 sandbox_provider()->CreateOriginEnumerator());
112
113 // We don't own this pointer; deleting it would be a bad idea.
114 fileapi::FileSystemQuotaUtil* quota_util = profile_->
115 GetFileSystemContext()->GetQuotaUtil(fileapi::kFileSystemTypeTemporary);
116
117 GURL current;
118 while (!(current = origin_enumerator->Next()).is_empty()) {
119 if (current.SchemeIs(chrome::kExtensionScheme)) {
120 // Extension state is not considered browsing data.
121 continue;
122 }
123 int64 persistent_usage = quota_util->GetOriginUsageOnFileThread(current,
124 fileapi::kFileSystemTypePersistent);
125 int64 temporary_usage = quota_util->GetOriginUsageOnFileThread(current,
126 fileapi::kFileSystemTypeTemporary);
127 file_system_info_.push_back(
128 FileSystemInfo(
129 current,
130 origin_enumerator->HasFileSystemType(
131 fileapi::kFileSystemTypePersistent),
132 origin_enumerator->HasFileSystemType(
133 fileapi::kFileSystemTypeTemporary),
134 persistent_usage,
135 temporary_usage));
136 }
137
138 BrowserThread::PostTask(
139 BrowserThread::UI, FROM_HERE,
140 NewRunnableMethod(
141 this, &BrowsingDataFileSystemHelperImpl::NotifyInUIThread));
142 }
143
144 void BrowsingDataFileSystemHelperImpl::NotifyInUIThread() {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 DCHECK(is_fetching_);
147 // Note: completion_callback_ mutates only in the UI thread, so it's safe to
148 // test it here.
149 if (completion_callback_ != NULL) {
150 completion_callback_->Run(file_system_info_);
151 completion_callback_.reset();
152 }
153 is_fetching_ = false;
154 }
155
156 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread(
157 const GURL& origin) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
159 scoped_refptr<fileapi::FileSystemContext>
160 context(profile_->GetFileSystemContext());
161 context->DeleteDataForOriginOnFileThread(origin);
162 }
163
164 } // namespace
165
166 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
167 const GURL& origin,
168 bool has_persistent,
169 bool has_temporary,
170 int64 usage_persistent,
171 int64 usage_temporary)
172 : origin(origin),
173 has_persistent(has_persistent),
174 has_temporary(has_temporary),
175 usage_persistent(usage_persistent),
176 usage_temporary(usage_temporary) {
177 }
178
179 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
180
181 // static
182 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
183 Profile* profile) {
184 return new BrowsingDataFileSystemHelperImpl(profile);
185 }
186
187 CannedBrowsingDataFileSystemHelper::
188 PendingFileSystemInfo::PendingFileSystemInfo() {
189 }
190
191 CannedBrowsingDataFileSystemHelper::
192 PendingFileSystemInfo::PendingFileSystemInfo(const GURL& origin,
193 const fileapi::FileSystemType type,
194 int64 size)
195 : origin(origin),
196 type(type),
197 size(size) {
198 }
199
200 CannedBrowsingDataFileSystemHelper::
201 PendingFileSystemInfo::~PendingFileSystemInfo() {
202 }
203
204 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
205 Profile* profile)
206 : profile_(profile),
207 is_fetching_(false) {
208 DCHECK(profile);
209 }
210
211 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
212
213 CannedBrowsingDataFileSystemHelper*
214 CannedBrowsingDataFileSystemHelper::Clone() {
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 CannedBrowsingDataFileSystemHelper* clone =
217 new CannedBrowsingDataFileSystemHelper(profile_);
218
219 clone->pending_file_system_info_ = pending_file_system_info_;
220 clone->file_system_info_ = file_system_info_;
221 return clone;
222 }
223
224 void CannedBrowsingDataFileSystemHelper::AddFileSystem(
225 const GURL& origin, const fileapi::FileSystemType type, const int64 size) {
226 pending_file_system_info_.push_back(
227 PendingFileSystemInfo(origin, type, size));
228 }
229
230 void CannedBrowsingDataFileSystemHelper::Reset() {
231 file_system_info_.clear();
232 pending_file_system_info_.clear();
233 }
234
235 bool CannedBrowsingDataFileSystemHelper::empty() const {
236 return file_system_info_.empty() && pending_file_system_info_.empty();
237 }
238
239 void CannedBrowsingDataFileSystemHelper::StartFetching(
240 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) {
241 DCHECK(!is_fetching_);
242 DCHECK(callback);
243 is_fetching_ = true;
244 completion_callback_.reset(callback);
245
246 for (std::vector<PendingFileSystemInfo>::const_iterator
247 info = pending_file_system_info_.begin();
248 info != pending_file_system_info_.end(); ++info) {
249 bool duplicate = false;
250 for (std::vector<FileSystemInfo>::iterator
251 file_system = file_system_info_.begin();
252 file_system != file_system_info_.end();
253 ++file_system) {
254 if (file_system->origin == info->origin) {
255 if (info->type == fileapi::kFileSystemTypePersistent) {
256 file_system->has_persistent = true;
257 file_system->usage_persistent = info->size;
258 } else {
259 file_system->has_temporary = true;
260 file_system->usage_temporary = info->size;
261 }
262 duplicate = true;
263 break;
264 }
265 }
266 if (duplicate)
267 continue;
268
269 file_system_info_.push_back(FileSystemInfo(
270 info->origin,
271 (info->type == fileapi::kFileSystemTypePersistent),
272 (info->type == fileapi::kFileSystemTypeTemporary),
273 (info->type == fileapi::kFileSystemTypePersistent) ? info->size : 0,
274 (info->type == fileapi::kFileSystemTypeTemporary) ? info->size : 0));
275 }
276 pending_file_system_info_.clear();
277
278 BrowserThread::PostTask(
279 BrowserThread::UI, FROM_HERE,
michaeln 2011/05/26 17:39:53 maybe use MessageLoop::current or MessageLoopProxy
Mike West 2011/05/26 18:55:48 Uploading this now. Thanks. I'll commit as soon
280 NewRunnableMethod(
281 this, &CannedBrowsingDataFileSystemHelper::Notify));
282 }
283
284 void CannedBrowsingDataFileSystemHelper::Notify() {
285 DCHECK(is_fetching_);
286 if (completion_callback_ != NULL) {
287 completion_callback_->Run(file_system_info_);
288 completion_callback_.reset();
289 }
290 is_fetching_ = false;
291 }
OLDNEW
« no previous file with comments | « chrome/browser/browsing_data_file_system_helper.h ('k') | chrome/browser/browsing_data_file_system_helper_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698