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..b1cbdd07b7b46eac96c645222ea15dab27288a8c 100644 |
--- a/content/browser/blob_storage/chrome_blob_storage_context.cc |
+++ b/content/browser/blob_storage/chrome_blob_storage_context.cc |
@@ -8,6 +8,12 @@ |
#include "base/bind.h" |
#include "base/guid.h" |
+#include "base/files/file.h" |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_util.h" |
+#include "base/metrics/histogram_macros.h" |
+#include "base/task_runner.h" |
+#include "base/single_thread_task_runner.h" |
#include "content/public/browser/blob_handle.h" |
#include "content/public/browser/browser_context.h" |
#include "content/public/browser/browser_thread.h" |
@@ -15,12 +21,47 @@ |
#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"; |
+ |
+void CreateBlobDirectory(const FilePath& blob_storage_dir, |
+ scoped_refptr<base::TaskRunner> io_runner, |
+ const base::Closure& done) { |
+ base::File::Error error; |
+ bool success = base::CreateDirectoryAndGetError(blob_storage_dir, &error); |
+ LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.DirectorySuccess", success); |
+ if (!success) { |
+ LOCAL_HISTOGRAM_ENUMERATION("Storage.Blob.DirectorySuccess.Error", -error, |
+ -base::File::FILE_ERROR_MAX); |
+ } else { |
+ io_runner->PostTask(FROM_HERE, done); |
+ } |
+} |
+ |
+// 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(const 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,12 +90,44 @@ ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
context->SetUserData( |
kBlobStorageContextKeyName, |
new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); |
+ |
// Check first to avoid memory leak in unittests. |
- if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
+ bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO); |
michaeln
2016/07/07 20:05:21
Maybe early return for this case to simplify?
I w
|
+ if (io_thread_valid) { |
BrowserThread::PostTask( |
BrowserThread::IO, FROM_HERE, |
base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
} |
+ |
+ // 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 && |
+ BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) { |
+ // Resolve our storage directories. |
+ base::FilePath blob_storage_parent = |
+ context->GetPath().Append(kBlobStorageParentDirectory); |
+ base::FilePath blob_storage_dir = |
+ blob_storage_parent.Append(base::GenerateGUID()); |
+ |
+ scoped_refptr<base::SingleThreadTaskRunner> io_runner = |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
+ scoped_refptr<base::SingleThreadTaskRunner> file_runner = |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
+ |
+ // This creates our directory and calls |OnBlobDirectoryCreated| if we're |
+ // successful. |
+ BrowserThread::PostAfterStartupTask( |
michaeln
2016/07/07 20:05:21
Can creation/deletion of the active directory be m
|
+ FROM_HERE, file_runner, |
+ base::Bind( |
+ &CreateBlobDirectory, blob_storage_dir, io_runner, |
+ base::Bind(&ChromeBlobStorageContext::OnBlobDirectoryCreated, |
+ blob, blob_storage_dir))); |
+ // Removes our old blob directories if they exist. |
+ BrowserThread::PostAfterStartupTask( |
+ FROM_HERE, file_runner, |
+ base::Bind(&RemoveOldBlobStorageDirectories, blob_storage_parent, |
+ blob_storage_dir)); |
+ } |
} |
return UserDataAdapter<ChromeBlobStorageContext>::Get( |
@@ -64,6 +137,13 @@ ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
void ChromeBlobStorageContext::InitializeOnIOThread() { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
context_.reset(new BlobStorageContext()); |
+ if (!blob_storage_dir_.empty()) { |
+ // This happens if we create our blob storage folder before this method is |
+ // called, so we enable disk here. |
+ context_->EnableDisk( |
+ blob_storage_dir_, |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
+ } |
} |
std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( |
@@ -109,6 +189,13 @@ std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob( |
ChromeBlobStorageContext::~ChromeBlobStorageContext() {} |
void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
+ // Schedule a task to delete all blob directories. |
+ if (!blob_storage_dir_.empty()) { |
+ BrowserThread::PostTask( |
+ BrowserThread::FILE, FROM_HERE, |
+ base::Bind(&RemoveOldBlobStorageDirectories, |
+ blob_storage_dir_.DirName(), base::FilePath())); |
+ } |
if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
@@ -117,4 +204,17 @@ void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
delete this; |
} |
+void ChromeBlobStorageContext::OnBlobDirectoryCreated( |
+ const base::FilePath& blob_storage_directory) { |
+ LOG(ERROR) << "Blob directory successfully created " |
+ << blob_storage_directory.value(); |
+ DCHECK(blob_storage_dir_.empty()); |
+ blob_storage_dir_ = blob_storage_directory; |
+ if (context_) { |
+ context_->EnableDisk( |
+ blob_storage_dir_, |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
+ } |
+} |
+ |
} // namespace content |