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_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 completion_callback_(NULL), | |
|
kinuko
2011/05/25 10:24:41
nit: no need to explicitly initialize it to NULL
Mike West
2011/05/25 12:00:01
Done.
| |
| 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; | |
| 111 | |
| 112 origin_enumerator.reset(profile_->GetFileSystemContext()->path_manager()-> | |
| 113 sandbox_provider()->CreateOriginEnumerator()); | |
|
kinuko
2011/05/25 10:24:41
nit: probably this can be done when you initialize
Mike West
2011/05/25 12:00:01
Done.
| |
| 114 | |
| 115 GURL current; | |
| 116 while (!(current = origin_enumerator->Next()).is_empty()) { | |
| 117 if (current.SchemeIs(chrome::kExtensionScheme)) { | |
| 118 // Extension state is not considered browsing data. | |
| 119 continue; | |
| 120 } | |
| 121 file_system_info_.push_back( | |
| 122 FileSystemInfo( | |
| 123 current, | |
| 124 origin_enumerator->HasFileSystemType( | |
| 125 fileapi::kFileSystemTypePersistent), | |
| 126 origin_enumerator->HasFileSystemType( | |
| 127 fileapi::kFileSystemTypeTemporary))); | |
| 128 } | |
| 129 | |
| 130 BrowserThread::PostTask( | |
| 131 BrowserThread::UI, FROM_HERE, | |
| 132 NewRunnableMethod( | |
| 133 this, &BrowsingDataFileSystemHelperImpl::NotifyInUIThread)); | |
| 134 } | |
| 135 | |
| 136 void BrowsingDataFileSystemHelperImpl::NotifyInUIThread() { | |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 138 DCHECK(is_fetching_); | |
| 139 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | |
| 140 // test it here. | |
| 141 if (completion_callback_ != NULL) { | |
| 142 completion_callback_->Run(file_system_info_); | |
| 143 completion_callback_.reset(); | |
| 144 } | |
| 145 is_fetching_ = false; | |
| 146 } | |
| 147 | |
| 148 void BrowsingDataFileSystemHelperImpl::DeleteFileSystemOriginInFileThread( | |
| 149 const GURL& origin) { | |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 151 fileapi::FileSystemContext* context = profile_->GetFileSystemContext(); | |
|
kinuko
2011/05/25 10:24:41
scoped_refptr<FileSystemContext> ? (Looks like it
Mike West
2011/05/25 12:00:01
Done.
| |
| 152 context->DeleteDataForOriginOnFileThread(origin); | |
| 153 } | |
| 154 | |
| 155 } // namespace | |
| 156 | |
| 157 BrowsingDataFileSystemHelper::FileSystemInfo::FileSystemInfo( | |
| 158 const GURL& origin, | |
| 159 bool has_persistent, | |
| 160 bool has_temporary) | |
| 161 : origin(origin), | |
| 162 has_persistent(has_persistent), | |
| 163 has_temporary(has_temporary) { | |
| 164 } | |
| 165 | |
| 166 BrowsingDataFileSystemHelper::FileSystemInfo::~FileSystemInfo() {} | |
| 167 | |
| 168 // static | |
| 169 BrowsingDataFileSystemHelper* BrowsingDataFileSystemHelper::Create( | |
| 170 Profile* profile) { | |
| 171 return new BrowsingDataFileSystemHelperImpl(profile); | |
| 172 } | |
| 173 | |
| 174 CannedBrowsingDataFileSystemHelper:: | |
| 175 PendingFileSystemInfo::PendingFileSystemInfo() { | |
| 176 } | |
| 177 | |
| 178 CannedBrowsingDataFileSystemHelper:: | |
| 179 PendingFileSystemInfo::PendingFileSystemInfo(const GURL& origin, | |
| 180 const fileapi::FileSystemType type) | |
| 181 : origin(origin), | |
| 182 type(type) { | |
| 183 } | |
| 184 | |
| 185 CannedBrowsingDataFileSystemHelper:: | |
| 186 PendingFileSystemInfo::~PendingFileSystemInfo() { | |
| 187 } | |
| 188 | |
| 189 CannedBrowsingDataFileSystemHelper::CannedBrowsingDataFileSystemHelper( | |
| 190 Profile* profile) | |
| 191 : profile_(profile), | |
| 192 is_fetching_(false) { | |
| 193 DCHECK(profile); | |
| 194 } | |
| 195 | |
| 196 CannedBrowsingDataFileSystemHelper::~CannedBrowsingDataFileSystemHelper() {} | |
| 197 | |
| 198 CannedBrowsingDataFileSystemHelper* | |
| 199 CannedBrowsingDataFileSystemHelper::Clone() { | |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 201 CannedBrowsingDataFileSystemHelper* clone = | |
| 202 new CannedBrowsingDataFileSystemHelper(profile_); | |
| 203 | |
| 204 base::AutoLock auto_lock(lock_); | |
| 205 clone->pending_file_system_info_ = pending_file_system_info_; | |
| 206 clone->file_system_info_ = file_system_info_; | |
| 207 return clone; | |
| 208 } | |
| 209 | |
| 210 void CannedBrowsingDataFileSystemHelper::AddFileSystem( | |
| 211 const GURL& origin, const fileapi::FileSystemType type) { | |
| 212 base::AutoLock auto_lock(lock_); | |
| 213 pending_file_system_info_.push_back(PendingFileSystemInfo(origin, type)); | |
| 214 } | |
| 215 | |
| 216 void CannedBrowsingDataFileSystemHelper::Reset() { | |
| 217 base::AutoLock auto_lock(lock_); | |
| 218 file_system_info_.clear(); | |
| 219 pending_file_system_info_.clear(); | |
| 220 } | |
| 221 | |
| 222 bool CannedBrowsingDataFileSystemHelper::empty() const { | |
| 223 base::AutoLock auto_lock(lock_); | |
| 224 return file_system_info_.empty() && pending_file_system_info_.empty(); | |
| 225 } | |
| 226 | |
| 227 void CannedBrowsingDataFileSystemHelper::StartFetching( | |
| 228 Callback1<const std::vector<FileSystemInfo>& >::Type* callback) { | |
| 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 230 DCHECK(!is_fetching_); | |
| 231 DCHECK(callback); | |
| 232 is_fetching_ = true; | |
| 233 completion_callback_.reset(callback); | |
| 234 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( | |
| 235 this, | |
| 236 &CannedBrowsingDataFileSystemHelper::ConvertPendingInfo)); | |
| 237 } | |
| 238 | |
| 239 void CannedBrowsingDataFileSystemHelper::ConvertPendingInfo() { | |
| 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 241 base::AutoLock auto_lock(lock_); | |
| 242 for (std::vector<PendingFileSystemInfo>::const_iterator | |
| 243 info = pending_file_system_info_.begin(); | |
| 244 info != pending_file_system_info_.end(); ++info) { | |
| 245 bool duplicate = false; | |
| 246 for (std::vector<FileSystemInfo>::iterator | |
| 247 file_system = file_system_info_.begin(); | |
| 248 file_system != file_system_info_.end(); | |
| 249 ++file_system) { | |
| 250 if (file_system->origin == info->origin) { | |
| 251 if (info->type == fileapi::kFileSystemTypePersistent) { | |
| 252 file_system->has_persistent = true; | |
| 253 } else { | |
| 254 file_system->has_temporary = true; | |
| 255 } | |
| 256 duplicate = true; | |
| 257 break; | |
| 258 } | |
| 259 } | |
| 260 if (duplicate) | |
| 261 continue; | |
| 262 | |
| 263 file_system_info_.push_back(FileSystemInfo( | |
| 264 info->origin, | |
| 265 (info->type == fileapi::kFileSystemTypePersistent), | |
| 266 (info->type == fileapi::kFileSystemTypeTemporary))); | |
| 267 } | |
| 268 pending_file_system_info_.clear(); | |
| 269 | |
| 270 BrowserThread::PostTask( | |
| 271 BrowserThread::UI, FROM_HERE, | |
| 272 NewRunnableMethod( | |
| 273 this, &CannedBrowsingDataFileSystemHelper::Notify)); | |
| 274 } | |
| 275 | |
| 276 void CannedBrowsingDataFileSystemHelper::Notify() { | |
| 277 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 278 DCHECK(is_fetching_); | |
| 279 // Note: completion_callback_ mutates only in the UI thread, so it's safe to | |
| 280 // test it here. | |
| 281 if (completion_callback_ != NULL) { | |
| 282 completion_callback_->Run(file_system_info_); | |
| 283 completion_callback_.reset(); | |
| 284 } | |
| 285 is_fetching_ = false; | |
| 286 } | |
| OLD | NEW |