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

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: Working in Kinuko's feedback. Created 9 years, 7 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_context.h"
19 #include "webkit/fileapi/file_system_types.h"
20 #include "webkit/fileapi/sandbox_mount_point_provider.h"
21 #include "webkit/glue/webkit_glue.h"
22
23 using WebKit::WebSecurityOrigin;
24
25 namespace {
26
27 class BrowsingDataFileSystemHelperImpl : public BrowsingDataFileSystemHelper {
28 public:
29 explicit BrowsingDataFileSystemHelperImpl(Profile* profile);
30
31 virtual void StartFetching(
32 Callback1<const std::vector<FileSystemInfo>& >::Type* callback);
33 virtual void CancelNotification();
34 virtual void DeleteFileSystemOrigin(const GURL& origin);
35
36 private:
37 virtual ~BrowsingDataFileSystemHelperImpl();
38
39 // Enumerates all filesystem files in the FILE thread.
40 void FetchFileSystemInfoInFileThread();
41 // Notifies the completion callback in the UI thread.
42 void NotifyInUIThread();
43 // Delete data for an origin on the FILE thread.
44 void DeleteFileSystemOriginInFileThread(const GURL& origin);
45
46 Profile* profile_;
47
48 // This only mutates in the FILE thread.
49 std::vector<FileSystemInfo> file_system_info_;
50
51 // This only mutates on the UI thread.
52 scoped_ptr<Callback1<const std::vector<FileSystemInfo>& >::Type >
53 completion_callback_;
54
55 // Indicates whether or not we're currently fetching information:
56 // it's true when StartFetching() is called in the UI thread, and it's reset
57 // after we notified the callback in the UI thread.
58 // This only mutates on the UI thread.
59 bool is_fetching_;
60
61 DISALLOW_COPY_AND_ASSIGN(BrowsingDataFileSystemHelperImpl);
62 };
63
64 BrowsingDataFileSystemHelperImpl::BrowsingDataFileSystemHelperImpl(
65 Profile* profile)
66 : profile_(profile),
67 is_fetching_(false) {
68 DCHECK(profile_);
69 }
70
71 BrowsingDataFileSystemHelperImpl::~BrowsingDataFileSystemHelperImpl() {
72 }
73
74 void BrowsingDataFileSystemHelperImpl::StartFetching(
75 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 DCHECK(!is_fetching_);
78 DCHECK(callback);
79 is_fetching_ = true;
80 completion_callback_.reset(callback);
81 BrowserThread::PostTask(
82 BrowserThread::FILE, FROM_HERE,
83 NewRunnableMethod(
84 this,
85 &BrowsingDataFileSystemHelperImpl::
86 FetchFileSystemInfoInFileThread));
87 }
88
89 void BrowsingDataFileSystemHelperImpl::CancelNotification() {
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
91 completion_callback_.reset(NULL);
92 }
93
94 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOrigin(
95 const GURL& origin) {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97 BrowserThread::PostTask(
98 BrowserThread::FILE, FROM_HERE,
99 NewRunnableMethod(
100 this,
101 &BrowsingDataFileSystemHelperImpl::
102 DeleteFileSystemOriginInFileThread,
103 origin));
104 }
105
106 void BrowsingDataFileSystemHelperImpl::FetchFileSystemInfoInFileThread() {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
108 scoped_ptr<fileapi::SandboxMountPointProvider::OriginEnumerator>
109 origin_enumerator(profile_->GetFileSystemContext()->path_manager()->
110 sandbox_provider()->CreateOriginEnumerator());
111
112 GURL current;
113 while (!(current = origin_enumerator->Next()).is_empty()) {
114 if (current.SchemeIs(chrome::kExtensionScheme)) {
115 // Extension state is not considered browsing data.
116 continue;
117 }
118 file_system_info_.push_back(
119 FileSystemInfo(
120 current,
121 origin_enumerator->HasFileSystemType(
122 fileapi::kFileSystemTypePersistent),
123 origin_enumerator->HasFileSystemType(
124 fileapi::kFileSystemTypeTemporary)));
125 }
126
127 BrowserThread::PostTask(
128 BrowserThread::UI, FROM_HERE,
129 NewRunnableMethod(
130 this, &BrowsingDataFileSystemHelperImpl::NotifyInUIThread));
131 }
132
133 void BrowsingDataFileSystemHelperImpl::NotifyInUIThread() {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
135 DCHECK(is_fetching_);
136 // Note: completion_callback_ mutates only in the UI thread, so it's safe to
137 // test it here.
138 if (completion_callback_ != NULL) {
139 completion_callback_->Run(file_system_info_);
140 completion_callback_.reset();
141 }
142 is_fetching_ = false;
143 }
144
145 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread(
146 const GURL& origin) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
148 scoped_refptr<fileapi::FileSystemContext>
149 context(profile_->GetFileSystemContext());
150 context->DeleteDataForOriginOnFileThread(origin);
151 }
152
153 } // namespace
154
155 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo(
156 const GURL& origin,
157 bool has_persistent,
158 bool has_temporary)
159 : origin(origin),
160 has_persistent(has_persistent),
161 has_temporary(has_temporary) {
162 }
163
164 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {}
165
166 // static
167 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create(
168 Profile* profile) {
169 return new BrowsingDataFileSystemHelperImpl(profile);
170 }
171
172 CannedBrowsingDataFileSystemHelper::
173 PendingFileSystemInfo::PendingFileSystemInfo() {
174 }
175
176 CannedBrowsingDataFileSystemHelper::
177 PendingFileSystemInfo::PendingFileSystemInfo(const GURL& origin,
178 const fileapi::FileSystemType type)
179 : origin(origin),
180 type(type) {
181 }
182
183 CannedBrowsingDataFileSystemHelper::
184 PendingFileSystemInfo::~PendingFileSystemInfo() {
185 }
186
187 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper(
188 Profile* profile)
189 : profile_(profile),
190 is_fetching_(false) {
191 DCHECK(profile);
192 }
193
194 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {}
195
196 CannedBrowsingDataFileSystemHelper*
197 CannedBrowsingDataFileSystemHelper::Clone() {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
199 CannedBrowsingDataFileSystemHelper* clone =
200 new CannedBrowsingDataFileSystemHelper(profile_);
201
202 clone->pending_file_system_info_ = pending_file_system_info_;
203 clone->file_system_info_ = file_system_info_;
204 return clone;
205 }
206
207 void CannedBrowsingDataFileSystemHelper::AddFileSystem(
208 const GURL& origin, const fileapi::FileSystemType type) {
209 pending_file_system_info_.push_back(PendingFileSystemInfo(origin, type));
210 }
211
212 void CannedBrowsingDataFileSystemHelper::Reset() {
213 file_system_info_.clear();
214 pending_file_system_info_.clear();
215 }
216
217 bool CannedBrowsingDataFileSystemHelper::empty() const {
218 return file_system_info_.empty() && pending_file_system_info_.empty();
219 }
220
221 void CannedBrowsingDataFileSystemHelper::StartFetching(
222 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
224 DCHECK(!is_fetching_);
225 DCHECK(callback);
226 is_fetching_ = true;
227 completion_callback_.reset(callback);
228 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod(
229 this,
230 &CannedBrowsingDataFileSystemHelper::ConvertPendingInfo));
kinuko 2011/05/25 12:34:58 So what's blocking to drop this PostTask? If the
Mike West 2011/05/25 12:55:10 Nothing blocks dropping this PostTask. I kept it
Mike West 2011/05/25 16:07:28 So, there you go. Broken browser test if I do thi
231 }
232
233 void CannedBrowsingDataFileSystemHelper::ConvertPendingInfo() {
234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
235 for (std::vector<PendingFileSystemInfo>::const_iterator
236 info = pending_file_system_info_.begin();
237 info != pending_file_system_info_.end(); ++info) {
238 bool duplicate = false;
239 for (std::vector<FileSystemInfo>::iterator
240 file_system = file_system_info_.begin();
241 file_system != file_system_info_.end();
242 ++file_system) {
243 if (file_system->origin == info->origin) {
244 if (info->type == fileapi::kFileSystemTypePersistent) {
245 file_system->has_persistent = true;
246 } else {
247 file_system->has_temporary = true;
248 }
249 duplicate = true;
250 break;
251 }
252 }
253 if (duplicate)
254 continue;
255
256 file_system_info_.push_back(FileSystemInfo(
257 info->origin,
258 (info->type == fileapi::kFileSystemTypePersistent),
259 (info->type == fileapi::kFileSystemTypeTemporary)));
260 }
261 pending_file_system_info_.clear();
262
263 BrowserThread::PostTask(
264 BrowserThread::UI, FROM_HERE,
265 NewRunnableMethod(
266 this, &CannedBrowsingDataFileSystemHelper::Notify));
267 }
268
269 void CannedBrowsingDataFileSystemHelper::Notify() {
270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
271 DCHECK(is_fetching_);
272 // Note: completion_callback_ mutates only in the UI thread, so it's safe to
273 // test it here.
274 if (completion_callback_ != NULL) {
275 completion_callback_->Run(file_system_info_);
276 completion_callback_.reset();
277 }
278 is_fetching_ = false;
279 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698