Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_filesystem_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> filesystem_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 completion_callback_(NULL), | |
| 68 is_fetching_(false) { | |
| 69 DCHECK(profile_); | |
| 70 } | |
| 71 | |
| 72 BrowsingDataFilesystemHelperImpl::~BrowsingDataFilesystemHelperImpl() { | |
| 73 } | |
| 74 | |
| 75 void BrowsingDataFilesystemHelperImpl::StartFetching( | |
|
ericu
2011/05/24 23:08:15
We generally capitalize the S in FileSystem, and b
Mike West
2011/05/25 09:22:36
Done.
| |
| 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 fileapi::SandboxMountPointProvider::OriginEnumerator* origin_enumerator = | |
|
ericu
2011/05/24 23:08:15
You're leaking the origin_enumerator.
Mike West
2011/05/25 09:22:36
Wrapped in a scoped_ptr, thanks.
| |
| 110 profile_->GetFileSystemContext()->path_manager()->sandbox_provider()-> | |
| 111 CreateOriginEnumerator(); | |
| 112 | |
| 113 GURL current; | |
| 114 while (!(current = origin_enumerator->Next()).is_empty()) { | |
| 115 if (current.SchemeIs(chrome::kExtensionScheme)) { | |
| 116 // Extension state is not considered browsing data. | |
| 117 continue; | |
| 118 } | |
| 119 filesystem_info_.push_back( | |
| 120 FilesystemInfo( | |
|
ericu
2011/05/24 23:08:15
Why bother storing scheme, host, and port, if you'
Mike West
2011/05/25 09:22:36
Fair question. I'm sure it made sense at the time
| |
| 121 current.scheme(), | |
| 122 current.host(), | |
| 123 current.port(), | |
| 124 current, | |
| 125 origin_enumerator->HasFileSystemType( | |
| 126 fileapi::kFileSystemTypePersistent), | |
| 127 origin_enumerator->HasFileSystemType( | |
| 128 fileapi::kFileSystemTypeTemporary))); | |
| 129 } | |
| 130 | |
| 131 BrowserThread::PostTask( | |
| 132 BrowserThread::UI, FROM_HERE, | |
| 133 NewRunnableMethod( | |
| 134 this, &BrowsingDataFilesystemHelperImpl::NotifyInUIThread)); | |
| 135 } | |
| 136 | |
| 137 void BrowsingDataFilesystemHelperImpl::NotifyInUIThread() { | |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 139 DCHECK(is_fetching_); | |
| 140 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | |
| 141 // test it here. | |
| 142 if (completion_callback_ != NULL) { | |
| 143 completion_callback_->Run(filesystem_info_); | |
| 144 completion_callback_.reset(); | |
| 145 } | |
| 146 is_fetching_ = false; | |
| 147 } | |
| 148 | |
| 149 void BrowsingDataFilesystemHelperImpl::DeleteFilesystemOriginInFileThread( | |
| 150 const GURL& origin) { | |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 152 fileapi::FileSystemContext* context = profile_->GetFileSystemContext(); | |
| 153 context->DeleteDataForOriginOnFileThread(origin); | |
| 154 } | |
| 155 | |
| 156 } // namespace | |
| 157 | |
| 158 BrowsingDataFilesystemHelper::FilesystemInfo::FilesystemInfo( | |
| 159 const std::string& protocol, | |
| 160 const std::string& host, | |
| 161 const std::string& port, | |
| 162 const GURL& origin, | |
| 163 bool has_persistent, | |
| 164 bool has_temporary) | |
| 165 : protocol(protocol), | |
| 166 host(host), | |
| 167 port(port), | |
| 168 origin(origin), | |
| 169 has_persistent(has_persistent), | |
| 170 has_temporary(has_temporary) { | |
| 171 } | |
| 172 | |
| 173 BrowsingDataFilesystemHelper::FilesystemInfo::~FilesystemInfo() {} | |
| 174 | |
| 175 // static | |
| 176 BrowsingDataFilesystemHelper* BrowsingDataFilesystemHelper::Create( | |
| 177 Profile* profile) { | |
| 178 return new BrowsingDataFilesystemHelperImpl(profile); | |
| 179 } | |
| 180 | |
| 181 CannedBrowsingDataFilesystemHelper:: | |
| 182 PendingFilesystemInfo::PendingFilesystemInfo() { | |
| 183 } | |
| 184 | |
| 185 CannedBrowsingDataFilesystemHelper:: | |
| 186 PendingFilesystemInfo::PendingFilesystemInfo(const GURL& origin, | |
| 187 const fileapi::FileSystemType type) | |
| 188 : origin(origin), | |
| 189 type(type) { | |
| 190 } | |
| 191 | |
| 192 CannedBrowsingDataFilesystemHelper:: | |
| 193 PendingFilesystemInfo::~PendingFilesystemInfo() { | |
| 194 } | |
| 195 | |
| 196 CannedBrowsingDataFilesystemHelper::CannedBrowsingDataFilesystemHelper( | |
| 197 Profile* profile) | |
| 198 : profile_(profile), | |
| 199 completion_callback_(NULL), | |
| 200 is_fetching_(false) { | |
| 201 DCHECK(profile); | |
| 202 } | |
| 203 | |
| 204 CannedBrowsingDataFilesystemHelper* | |
| 205 CannedBrowsingDataFilesystemHelper::Clone() { | |
| 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 207 CannedBrowsingDataFilesystemHelper* clone = | |
| 208 new CannedBrowsingDataFilesystemHelper(profile_); | |
| 209 | |
| 210 base::AutoLock auto_lock(lock_); | |
| 211 clone->pending_filesystem_info_ = pending_filesystem_info_; | |
| 212 clone->filesystem_info_ = filesystem_info_; | |
| 213 return clone; | |
| 214 } | |
| 215 | |
| 216 void CannedBrowsingDataFilesystemHelper::AddFilesystem( | |
| 217 const GURL& origin, const fileapi::FileSystemType type) { | |
| 218 base::AutoLock auto_lock(lock_); | |
| 219 pending_filesystem_info_.push_back(PendingFilesystemInfo(origin, type)); | |
| 220 } | |
| 221 | |
| 222 void CannedBrowsingDataFilesystemHelper::Reset() { | |
| 223 base::AutoLock auto_lock(lock_); | |
| 224 filesystem_info_.clear(); | |
| 225 pending_filesystem_info_.clear(); | |
| 226 } | |
| 227 | |
| 228 bool CannedBrowsingDataFilesystemHelper::empty() const { | |
| 229 base::AutoLock auto_lock(lock_); | |
| 230 return filesystem_info_.empty() && pending_filesystem_info_.empty(); | |
| 231 } | |
| 232 | |
| 233 void CannedBrowsingDataFilesystemHelper::StartFetching( | |
| 234 Callback1<const std::vector<FilesystemInfo>& >::Type* callback) { | |
| 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 236 DCHECK(!is_fetching_); | |
| 237 DCHECK(callback); | |
| 238 is_fetching_ = true; | |
| 239 completion_callback_.reset(callback); | |
| 240 BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( | |
|
jochen (gone - plz use gerrit)
2011/05/24 19:41:46
as you're not accessing webkit methods (such as se
Mike West
2011/05/25 09:22:36
Ok. I'll drop the hop over to the WEBKIT thread.
| |
| 241 this, | |
| 242 &CannedBrowsingDataFilesystemHelper::ConvertPendingInfoInWebKitThread)); | |
| 243 } | |
| 244 | |
| 245 CannedBrowsingDataFilesystemHelper::~CannedBrowsingDataFilesystemHelper() {} | |
| 246 | |
| 247 void CannedBrowsingDataFilesystemHelper::ConvertPendingInfoInWebKitThread() { | |
| 248 base::AutoLock auto_lock(lock_); | |
| 249 for (std::vector<PendingFilesystemInfo>::const_iterator | |
| 250 info = pending_filesystem_info_.begin(); | |
| 251 info != pending_filesystem_info_.end(); ++info) { | |
| 252 bool duplicate = false; | |
| 253 for (std::vector<FilesystemInfo>::iterator | |
| 254 filesystem = filesystem_info_.begin(); | |
| 255 filesystem != filesystem_info_.end(); | |
| 256 ++filesystem) { | |
| 257 if (filesystem->origin == info->origin) { | |
| 258 if (info->type == fileapi::kFileSystemTypePersistent) { | |
| 259 filesystem->has_persistent = true; | |
| 260 } else { | |
| 261 filesystem->has_temporary = true; | |
| 262 } | |
| 263 duplicate = true; | |
| 264 break; | |
| 265 } | |
| 266 } | |
| 267 if (duplicate) | |
| 268 continue; | |
| 269 | |
| 270 filesystem_info_.push_back(FilesystemInfo( | |
| 271 info->origin.scheme(), | |
| 272 info->origin.host(), | |
| 273 info->origin.port(), | |
| 274 info->origin, | |
| 275 (info->type == fileapi::kFileSystemTypePersistent), | |
| 276 (info->type == fileapi::kFileSystemTypeTemporary))); | |
| 277 } | |
| 278 pending_filesystem_info_.clear(); | |
| 279 | |
| 280 BrowserThread::PostTask( | |
| 281 BrowserThread::UI, FROM_HERE, | |
| 282 NewRunnableMethod( | |
| 283 this, &CannedBrowsingDataFilesystemHelper::NotifyInUIThread)); | |
| 284 } | |
| 285 | |
| 286 void CannedBrowsingDataFilesystemHelper::NotifyInUIThread() { | |
| 287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 288 DCHECK(is_fetching_); | |
| 289 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | |
| 290 // test it here. | |
| 291 if (completion_callback_ != NULL) { | |
| 292 completion_callback_->Run(filesystem_info_); | |
| 293 completion_callback_.reset(); | |
| 294 } | |
| 295 is_fetching_ = false; | |
| 296 } | |
| OLD | NEW |