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 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 } | |
|
ericu
2011/05/25 19:36:30
If you got back an origin here, but both HasFileSy
Mike West
2011/05/25 19:50:25
As currently implemented, it would show up as a fi
| |
| 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(!is_fetching_); | |
| 224 DCHECK(callback); | |
| 225 is_fetching_ = true; | |
| 226 | |
| 227 for (std::vector<PendingFileSystemInfo>::const_iterator | |
| 228 info = pending_file_system_info_.begin(); | |
| 229 info != pending_file_system_info_.end(); ++info) { | |
| 230 bool duplicate = false; | |
| 231 for (std::vector<FileSystemInfo>::iterator | |
| 232 file_system = file_system_info_.begin(); | |
| 233 file_system != file_system_info_.end(); | |
| 234 ++file_system) { | |
| 235 if (file_system->origin == info->origin) { | |
| 236 if (info->type == fileapi::kFileSystemTypePersistent) { | |
| 237 file_system->has_persistent = true; | |
| 238 } else { | |
| 239 file_system->has_temporary = true; | |
| 240 } | |
| 241 duplicate = true; | |
| 242 break; | |
| 243 } | |
| 244 } | |
| 245 if (duplicate) | |
| 246 continue; | |
| 247 | |
| 248 file_system_info_.push_back(FileSystemInfo( | |
| 249 info->origin, | |
| 250 (info->type == fileapi::kFileSystemTypePersistent), | |
| 251 (info->type == fileapi::kFileSystemTypeTemporary))); | |
| 252 } | |
| 253 pending_file_system_info_.clear(); | |
| 254 | |
| 255 callback->Run(file_system_info_); | |
|
michaeln
2011/05/25 18:26:34
Do you also need to delete the callback as Browsin
Mike West
2011/05/26 07:20:34
I'm running with this as an async callback; I thin
| |
| 256 is_fetching_ = false; | |
| 257 } | |
| OLD | NEW |