OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "content/browser/plugin_private_storage_helper.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <set> |
| 12 #include <string> |
| 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/compiler_specific.h" |
| 16 #include "base/files/file.h" |
| 17 #include "base/files/file_enumerator.h" |
| 18 #include "base/files/file_path.h" |
| 19 #include "base/location.h" |
| 20 #include "base/logging.h" |
| 21 #include "base/stl_util.h" |
| 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "ppapi/shared_impl/ppapi_constants.h" |
| 25 #include "storage/browser/fileapi/async_file_util.h" |
| 26 #include "storage/browser/fileapi/async_file_util_adapter.h" |
| 27 #include "storage/browser/fileapi/file_system_context.h" |
| 28 #include "storage/browser/fileapi/isolated_context.h" |
| 29 #include "storage/browser/fileapi/obfuscated_file_util.h" |
| 30 #include "storage/common/fileapi/file_system_util.h" |
| 31 |
| 32 namespace content { |
| 33 |
| 34 namespace { |
| 35 |
| 36 std::string StringTypeToString(const base::FilePath::StringType& value) { |
| 37 #if defined(OS_POSIX) |
| 38 return value; |
| 39 #elif defined(OS_WIN) |
| 40 return base::WideToUTF8(value); |
| 41 #endif |
| 42 } |
| 43 |
| 44 // Helper for checking the plugin private data for a specified origin and |
| 45 // plugin for the existance of any file that matches the time range specified. |
| 46 // All of the operations in this class are done on the IO thread. |
| 47 // |
| 48 // This class keeps track of outstanding async requests it generates, and does |
| 49 // not call |callback_| until they all respond (and thus don't need to worry |
| 50 // about lifetime of |this| in the async requests). If the data for the origin |
| 51 // needs to be deleted, it needs to be done on the file task runner, so we |
| 52 // want to ensure that there are no pending requests that may prevent the |
| 53 // date from being deleted. |
| 54 class PluginPrivateDataByOriginChecker { |
| 55 public: |
| 56 PluginPrivateDataByOriginChecker( |
| 57 storage::FileSystemContext* filesystem_context, |
| 58 const GURL& origin, |
| 59 const std::string& plugin_name, |
| 60 const base::Time begin, |
| 61 const base::Time end, |
| 62 const base::Callback<void(bool, const GURL&)>& callback) |
| 63 : filesystem_context_(filesystem_context), |
| 64 origin_(origin), |
| 65 plugin_name_(plugin_name), |
| 66 begin_(begin), |
| 67 end_(end), |
| 68 callback_(callback) { |
| 69 // Create the filesystem ID. |
| 70 fsid_ = storage::IsolatedContext::GetInstance() |
| 71 ->RegisterFileSystemForVirtualPath( |
| 72 storage::kFileSystemTypePluginPrivate, |
| 73 ppapi::kPluginPrivateRootName, base::FilePath()); |
| 74 } |
| 75 ~PluginPrivateDataByOriginChecker() {} |
| 76 |
| 77 // Checks the files contained in the plugin private filesystem for |origin_| |
| 78 // and |plugin_name_| for any file whose last modified time is between |
| 79 // |begin_| and |end_|. |callback_| is called when all actions are complete |
| 80 // with true and the origin if any such file is found, false and empty GURL |
| 81 // otherwise. |
| 82 void CheckFilesOnIOThread(); |
| 83 |
| 84 private: |
| 85 void OnFileSystemOpened(base::File::Error result); |
| 86 void OnDirectoryRead(const std::string& root, |
| 87 base::File::Error result, |
| 88 const storage::AsyncFileUtil::EntryList& file_list, |
| 89 bool has_more); |
| 90 void OnFileInfo(const std::string& file_name, |
| 91 base::File::Error result, |
| 92 const base::File::Info& file_info); |
| 93 |
| 94 // Keeps track of the pending work. When |task_count_| goes to 0 then |
| 95 // |callback_| is called and this helper object is destroyed. |
| 96 void IncrementTaskCount(); |
| 97 void DecrementTaskCount(); |
| 98 |
| 99 // Not owned by this object. Caller is responsible for keeping the |
| 100 // FileSystemContext alive until |callback_| is called. |
| 101 storage::FileSystemContext* filesystem_context_; |
| 102 |
| 103 const GURL origin_; |
| 104 const std::string plugin_name_; |
| 105 const base::Time begin_; |
| 106 const base::Time end_; |
| 107 const base::Callback<void(bool, const GURL&)> callback_; |
| 108 std::string fsid_; |
| 109 int task_count_ = 0; |
| 110 |
| 111 // Keep track if the data for this origin needs to be deleted due to |
| 112 // any file found that has last modified time between |begin_| and |end_|. |
| 113 bool delete_this_origin_data_ = false; |
| 114 }; |
| 115 |
| 116 void PluginPrivateDataByOriginChecker::CheckFilesOnIOThread() { |
| 117 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 118 DCHECK(storage::ValidateIsolatedFileSystemId(fsid_)); |
| 119 |
| 120 IncrementTaskCount(); |
| 121 filesystem_context_->OpenPluginPrivateFileSystem( |
| 122 origin_, storage::kFileSystemTypePluginPrivate, fsid_, plugin_name_, |
| 123 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, |
| 124 base::Bind(&PluginPrivateDataByOriginChecker::OnFileSystemOpened, |
| 125 base::Unretained(this))); |
| 126 } |
| 127 |
| 128 void PluginPrivateDataByOriginChecker::OnFileSystemOpened( |
| 129 base::File::Error result) { |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 131 DVLOG(3) << "Opened filesystem for " << origin_ << ":" << plugin_name_ |
| 132 << ", result: " << result; |
| 133 |
| 134 // If we can't open the directory, we can't delete files so simply return. |
| 135 if (result != base::File::FILE_OK) { |
| 136 DecrementTaskCount(); |
| 137 return; |
| 138 } |
| 139 |
| 140 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil( |
| 141 storage::kFileSystemTypePluginPrivate); |
| 142 std::string root = storage::GetIsolatedFileSystemRootURIString( |
| 143 origin_, fsid_, ppapi::kPluginPrivateRootName); |
| 144 std::unique_ptr<storage::FileSystemOperationContext> operation_context = |
| 145 base::WrapUnique( |
| 146 new storage::FileSystemOperationContext(filesystem_context_)); |
| 147 file_util->ReadDirectory( |
| 148 std::move(operation_context), filesystem_context_->CrackURL(GURL(root)), |
| 149 base::Bind(&PluginPrivateDataByOriginChecker::OnDirectoryRead, |
| 150 base::Unretained(this), root)); |
| 151 } |
| 152 |
| 153 void PluginPrivateDataByOriginChecker::OnDirectoryRead( |
| 154 const std::string& root, |
| 155 base::File::Error result, |
| 156 const storage::AsyncFileUtil::EntryList& file_list, |
| 157 bool has_more) { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 159 DVLOG(3) << __FUNCTION__ << " result: " << result |
| 160 << ", #files: " << file_list.size(); |
| 161 |
| 162 // Quit if there is an error. |
| 163 if (result != base::File::FILE_OK) { |
| 164 DLOG(ERROR) << "Unable to read directory for " << origin_ << ":" |
| 165 << plugin_name_; |
| 166 DecrementTaskCount(); |
| 167 return; |
| 168 } |
| 169 |
| 170 // No error, process the files returned. No need to do this if we have |
| 171 // already decided to delete all the data for this origin. |
| 172 if (!delete_this_origin_data_) { |
| 173 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil( |
| 174 storage::kFileSystemTypePluginPrivate); |
| 175 for (const auto& file : file_list) { |
| 176 DVLOG(3) << __FUNCTION__ << " file: " << file.name; |
| 177 DCHECK(!file.is_directory); // Nested directories not implemented. |
| 178 |
| 179 std::unique_ptr<storage::FileSystemOperationContext> operation_context = |
| 180 base::WrapUnique( |
| 181 new storage::FileSystemOperationContext(filesystem_context_)); |
| 182 storage::FileSystemURL file_url = filesystem_context_->CrackURL( |
| 183 GURL(root + StringTypeToString(file.name))); |
| 184 IncrementTaskCount(); |
| 185 file_util->GetFileInfo( |
| 186 std::move(operation_context), file_url, |
| 187 storage::FileSystemOperation::GET_METADATA_FIELD_SIZE | |
| 188 storage::FileSystemOperation::GET_METADATA_FIELD_LAST_MODIFIED, |
| 189 base::Bind(&PluginPrivateDataByOriginChecker::OnFileInfo, |
| 190 base::Unretained(this), StringTypeToString(file.name))); |
| 191 } |
| 192 } |
| 193 |
| 194 // If there are more files in this directory, wait for the next call. |
| 195 if (has_more) |
| 196 return; |
| 197 |
| 198 DecrementTaskCount(); |
| 199 } |
| 200 |
| 201 void PluginPrivateDataByOriginChecker::OnFileInfo( |
| 202 const std::string& file_name, |
| 203 base::File::Error result, |
| 204 const base::File::Info& file_info) { |
| 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 206 |
| 207 if (result == base::File::FILE_OK) { |
| 208 DVLOG(3) << __FUNCTION__ << " name: " << file_name |
| 209 << ", size: " << file_info.size |
| 210 << ", modified: " << file_info.last_modified; |
| 211 if (file_info.last_modified >= begin_ && file_info.last_modified <= end_) |
| 212 delete_this_origin_data_ = true; |
| 213 } |
| 214 |
| 215 DecrementTaskCount(); |
| 216 } |
| 217 |
| 218 void PluginPrivateDataByOriginChecker::IncrementTaskCount() { |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 220 ++task_count_; |
| 221 } |
| 222 |
| 223 void PluginPrivateDataByOriginChecker::DecrementTaskCount() { |
| 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 225 DCHECK_GT(task_count_, 0); |
| 226 --task_count_; |
| 227 if (task_count_) |
| 228 return; |
| 229 |
| 230 // If there are no more tasks in progress, then run |callback_| on the |
| 231 // proper thread. |
| 232 filesystem_context_->default_file_task_runner()->PostTask( |
| 233 FROM_HERE, base::Bind(callback_, delete_this_origin_data_, origin_)); |
| 234 delete this; |
| 235 } |
| 236 |
| 237 // Helper for deleting the plugin private data. |
| 238 // All of the operations in this class are done on the file task runner. |
| 239 class PluginPrivateDataDeletionHelper { |
| 240 public: |
| 241 PluginPrivateDataDeletionHelper( |
| 242 scoped_refptr<storage::FileSystemContext> filesystem_context, |
| 243 const base::Time begin, |
| 244 const base::Time end, |
| 245 const base::Closure& callback) |
| 246 : filesystem_context_(std::move(filesystem_context)), |
| 247 begin_(begin), |
| 248 end_(end), |
| 249 callback_(callback) {} |
| 250 ~PluginPrivateDataDeletionHelper() {} |
| 251 |
| 252 void CheckOriginsOnFileTaskRunner(const std::set<GURL>& origins); |
| 253 |
| 254 private: |
| 255 // Keeps track of the pending work. When |task_count_| goes to 0 then |
| 256 // |callback_| is called and this helper object is destroyed. |
| 257 void IncrementTaskCount(); |
| 258 void DecrementTaskCount(bool delete_data_for_origin, const GURL& origin); |
| 259 |
| 260 // Keep a reference to FileSystemContext until we are done with it. |
| 261 scoped_refptr<storage::FileSystemContext> filesystem_context_; |
| 262 |
| 263 const base::Time begin_; |
| 264 const base::Time end_; |
| 265 const base::Closure callback_; |
| 266 int task_count_ = 0; |
| 267 }; |
| 268 |
| 269 void PluginPrivateDataDeletionHelper::CheckOriginsOnFileTaskRunner( |
| 270 const std::set<GURL>& origins) { |
| 271 DCHECK(filesystem_context_->default_file_task_runner() |
| 272 ->RunsTasksOnCurrentThread()); |
| 273 IncrementTaskCount(); |
| 274 |
| 275 base::Callback<void(bool, const GURL&)> decrement_callback = |
| 276 base::Bind(&PluginPrivateDataDeletionHelper::DecrementTaskCount, |
| 277 base::Unretained(this)); |
| 278 storage::AsyncFileUtil* async_file_util = |
| 279 filesystem_context_->GetAsyncFileUtil( |
| 280 storage::kFileSystemTypePluginPrivate); |
| 281 storage::ObfuscatedFileUtil* obfuscated_file_util = |
| 282 static_cast<storage::ObfuscatedFileUtil*>( |
| 283 static_cast<storage::AsyncFileUtilAdapter*>(async_file_util) |
| 284 ->sync_file_util()); |
| 285 for (const auto& origin : origins) { |
| 286 // Determine the available plugin private filesystem directories |
| 287 // for this origin. |
| 288 base::File::Error error; |
| 289 base::FilePath path = obfuscated_file_util->GetDirectoryForOriginAndType( |
| 290 origin, "", false, &error); |
| 291 if (error != base::File::FILE_OK) { |
| 292 DLOG(ERROR) << "Unable to read directory for " << origin; |
| 293 continue; |
| 294 } |
| 295 |
| 296 // Currently the plugin private filesystem is only used by Encrypted |
| 297 // Media Content Decryption Modules, which are treated as pepper plugins. |
| 298 // Each CDM gets a directory based on the mimetype (e.g. plugin |
| 299 // application/x-ppapi-widevine-cdm uses directory |
| 300 // application_x-ppapi-widevine-cdm). Enumerate through the set of |
| 301 // directories so that data from any CDM used by this origin is deleted. |
| 302 base::FileEnumerator file_enumerator(path, false, |
| 303 base::FileEnumerator::DIRECTORIES); |
| 304 for (base::FilePath plugin_path = file_enumerator.Next(); |
| 305 !plugin_path.empty(); plugin_path = file_enumerator.Next()) { |
| 306 IncrementTaskCount(); |
| 307 PluginPrivateDataByOriginChecker* helper = |
| 308 new PluginPrivateDataByOriginChecker( |
| 309 filesystem_context_.get(), origin.GetOrigin(), |
| 310 plugin_path.BaseName().MaybeAsASCII(), begin_, end_, |
| 311 decrement_callback); |
| 312 BrowserThread::PostTask( |
| 313 BrowserThread::IO, FROM_HERE, |
| 314 base::Bind(&PluginPrivateDataByOriginChecker::CheckFilesOnIOThread, |
| 315 base::Unretained(helper))); |
| 316 |
| 317 // |helper| will delete itself when it is done. |
| 318 } |
| 319 } |
| 320 |
| 321 // Cancels out the call to IncrementTaskCount() at the start of this method. |
| 322 // If there are no origins specified then this will cause this helper to |
| 323 // be destroyed. |
| 324 DecrementTaskCount(false, GURL()); |
| 325 } |
| 326 |
| 327 void PluginPrivateDataDeletionHelper::IncrementTaskCount() { |
| 328 DCHECK(filesystem_context_->default_file_task_runner() |
| 329 ->RunsTasksOnCurrentThread()); |
| 330 ++task_count_; |
| 331 } |
| 332 |
| 333 void PluginPrivateDataDeletionHelper::DecrementTaskCount( |
| 334 bool delete_data_for_origin, |
| 335 const GURL& origin) { |
| 336 DCHECK(filesystem_context_->default_file_task_runner() |
| 337 ->RunsTasksOnCurrentThread()); |
| 338 |
| 339 // Since the PluginPrivateDataByOriginChecker runs on the IO thread, |
| 340 // delete all the data for |origin| if needed. |
| 341 if (delete_data_for_origin) { |
| 342 DCHECK(!origin.is_empty()); |
| 343 DVLOG(3) << "Deleting plugin data for " << origin; |
| 344 storage::FileSystemBackend* backend = |
| 345 filesystem_context_->GetFileSystemBackend( |
| 346 storage::kFileSystemTypePluginPrivate); |
| 347 storage::FileSystemQuotaUtil* quota_util = backend->GetQuotaUtil(); |
| 348 base::File::Error result = quota_util->DeleteOriginDataOnFileTaskRunner( |
| 349 filesystem_context_.get(), nullptr, origin, |
| 350 storage::kFileSystemTypePluginPrivate); |
| 351 ALLOW_UNUSED_LOCAL(result); |
| 352 DLOG_IF(ERROR, result != base::File::FILE_OK) |
| 353 << "Unable to delete the plugin data for " << origin; |
| 354 } |
| 355 |
| 356 DCHECK_GT(task_count_, 0); |
| 357 --task_count_; |
| 358 if (task_count_) |
| 359 return; |
| 360 |
| 361 // If there are no more tasks in progress, run |callback_| and then |
| 362 // this helper can be deleted. |
| 363 callback_.Run(); |
| 364 delete this; |
| 365 } |
| 366 |
| 367 } // namespace |
| 368 |
| 369 void ClearPluginPrivateDataOnFileTaskRunner( |
| 370 scoped_refptr<storage::FileSystemContext> filesystem_context, |
| 371 const GURL& storage_origin, |
| 372 const base::Time begin, |
| 373 const base::Time end, |
| 374 const base::Closure& callback) { |
| 375 DCHECK(filesystem_context->default_file_task_runner() |
| 376 ->RunsTasksOnCurrentThread()); |
| 377 DVLOG(3) << "Clearing plugin data for origin: " << storage_origin; |
| 378 |
| 379 storage::FileSystemBackend* backend = |
| 380 filesystem_context->GetFileSystemBackend( |
| 381 storage::kFileSystemTypePluginPrivate); |
| 382 storage::FileSystemQuotaUtil* quota_util = backend->GetQuotaUtil(); |
| 383 |
| 384 // Determine the set of origins used. |
| 385 std::set<GURL> origins; |
| 386 quota_util->GetOriginsForTypeOnFileTaskRunner( |
| 387 storage::kFileSystemTypePluginPrivate, &origins); |
| 388 |
| 389 if (origins.empty()) { |
| 390 // No origins, so nothing to do. |
| 391 callback.Run(); |
| 392 return; |
| 393 } |
| 394 |
| 395 // If a specific origin is provided, then check that it is in the list |
| 396 // returned and remove all the other origins. |
| 397 if (!storage_origin.is_empty()) { |
| 398 if (!ContainsKey(origins, storage_origin)) { |
| 399 // Nothing matches, so nothing to do. |
| 400 callback.Run(); |
| 401 return; |
| 402 } |
| 403 |
| 404 // List should only contain the one value that matches. |
| 405 origins.clear(); |
| 406 origins.insert(storage_origin); |
| 407 } |
| 408 |
| 409 PluginPrivateDataDeletionHelper* helper = new PluginPrivateDataDeletionHelper( |
| 410 std::move(filesystem_context), begin, end, callback); |
| 411 helper->CheckOriginsOnFileTaskRunner(origins); |
| 412 // |helper| will delete itself when all origins have been checked. |
| 413 } |
| 414 |
| 415 } // namespace content |
OLD | NEW |