Index: content/browser/blob_storage/chrome_blob_storage_context.cc |
diff --git a/content/browser/blob_storage/chrome_blob_storage_context.cc b/content/browser/blob_storage/chrome_blob_storage_context.cc |
index 533d3f10e222532aaa763dc26afc2df05a448995..5e751b934d277fe9edbafe87cef061f6e881fb17 100644 |
--- a/content/browser/blob_storage/chrome_blob_storage_context.cc |
+++ b/content/browser/blob_storage/chrome_blob_storage_context.cc |
@@ -7,7 +7,14 @@ |
#include <utility> |
#include "base/bind.h" |
+#include "base/files/file.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_util.h" |
#include "base/guid.h" |
+#include "base/metrics/histogram_macros.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/task_runner.h" |
+#include "base/threading/sequenced_worker_pool.h" |
#include "content/public/browser/blob_handle.h" |
#include "content/public/browser/browser_context.h" |
#include "content/public/browser/browser_thread.h" |
@@ -15,12 +22,33 @@ |
#include "storage/browser/blob/blob_data_handle.h" |
#include "storage/browser/blob/blob_storage_context.h" |
+using base::FilePath; |
using base::UserDataAdapter; |
using storage::BlobStorageContext; |
namespace content { |
namespace { |
+const char kBlobStorageParentDirectory[] = "blob_storage"; |
+ |
+// Removes all folders in the parent directory except for the |
+// |current_run_dir| folder. If this path is empty, then we delete all folders. |
+void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent, |
+ const FilePath& current_run_dir) { |
+ if (!base::DirectoryExists(blob_storage_parent)) { |
+ return; |
+ } |
+ base::FileEnumerator enumerator(blob_storage_parent, false, |
+ base::FileEnumerator::DIRECTORIES); |
+ std::vector<std::string> components; |
+ bool success = true; |
+ for (base::FilePath name = enumerator.Next(); !name.empty(); |
+ name = enumerator.Next()) { |
+ if (current_run_dir.empty() || name != current_run_dir) |
+ success &= base::DeleteFile(name, true); |
+ } |
+ LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success); |
+} |
const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; |
@@ -49,11 +77,40 @@ ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
context->SetUserData( |
kBlobStorageContextKeyName, |
new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); |
- // Check first to avoid memory leak in unittests. |
- if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
+ |
+ // Check to avoid memory leak in unittests. |
+ bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO); |
+ |
+ // Resolve our storage directories. |
+ base::FilePath blob_storage_parent = |
+ context->GetPath().Append(kBlobStorageParentDirectory); |
+ base::FilePath blob_storage_dir = |
+ blob_storage_parent.Append(base::GenerateGUID()); |
+ |
+ // Only populate the task runner if we're not off the record. This enables |
+ // paging/saving blob data to disk. |
+ scoped_refptr<base::TaskRunner> file_task_runner; |
+ |
+ // If we're not incognito mode, schedule all of our file tasks to enable |
+ // disk on the storage context. |
+ if (!context->IsOffTheRecord() && io_thread_valid) { |
+ file_task_runner = |
+ BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
+ base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
+ |
+ // Removes our old blob directories if they exist. |
+ BrowserThread::PostAfterStartupTask( |
+ FROM_HERE, file_task_runner, |
+ base::Bind(&RemoveOldBlobStorageDirectories, |
+ base::Passed(&blob_storage_parent), blob_storage_dir)); |
+ } |
+ |
+ if (io_thread_valid) { |
BrowserThread::PostTask( |
BrowserThread::IO, FROM_HERE, |
- base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
+ base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob, |
+ base::Passed(&blob_storage_dir), |
+ base::Passed(&file_task_runner))); |
} |
} |
@@ -61,9 +118,12 @@ ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
context, kBlobStorageContextKeyName); |
} |
-void ChromeBlobStorageContext::InitializeOnIOThread() { |
+void ChromeBlobStorageContext::InitializeOnIOThread( |
+ base::FilePath blob_storage_dir, |
+ scoped_refptr<base::TaskRunner> file_task_runner) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- context_.reset(new BlobStorageContext()); |
+ context_.reset(new BlobStorageContext(std::move(blob_storage_dir), |
+ std::move(file_task_runner))); |
} |
std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( |