| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 5 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| 11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 17 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 18 #include "content/public/browser/blob_handle.h" | 18 #include "content/public/browser/blob_handle.h" |
| 19 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "storage/browser/blob/blob_data_builder.h" | 21 #include "storage/browser/blob/blob_data_builder.h" |
| 22 #include "storage/browser/blob/blob_data_handle.h" | 22 #include "storage/browser/blob/blob_data_handle.h" |
| 23 #include "storage/browser/blob/blob_memory_controller.h" |
| 23 #include "storage/browser/blob/blob_storage_context.h" | 24 #include "storage/browser/blob/blob_storage_context.h" |
| 24 | 25 |
| 25 using base::FilePath; | 26 using base::FilePath; |
| 26 using base::UserDataAdapter; | 27 using base::UserDataAdapter; |
| 27 using storage::BlobStorageContext; | 28 using storage::BlobStorageContext; |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 const FilePath::CharType kBlobStorageContextKeyName[] = | 33 const FilePath::CharType kBlobStorageContextKeyName[] = |
| 33 FILE_PATH_LITERAL("content_blob_storage_context"); | 34 FILE_PATH_LITERAL("content_blob_storage_context"); |
| 34 const FilePath::CharType kBlobStorageParentDirectory[] = | 35 const FilePath::CharType kBlobStorageParentDirectory[] = |
| 35 FILE_PATH_LITERAL("blob_storage"); | 36 FILE_PATH_LITERAL("blob_storage"); |
| 36 | 37 |
| 37 // Removes all folders in the parent directory except for the | 38 // Removes all folders in the parent directory except for the |
| 38 // |current_run_dir| folder. If this path is empty, then we delete all folders. | 39 // |current_run_dir| folder. If this path is empty, then we delete all folders. |
| 39 void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent, | 40 void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent, |
| 40 const FilePath& current_run_dir) { | 41 const FilePath& current_run_dir) { |
| 41 if (!base::DirectoryExists(blob_storage_parent)) { | 42 if (!base::DirectoryExists(blob_storage_parent)) { |
| 42 return; | 43 return; |
| 43 } | 44 } |
| 44 base::FileEnumerator enumerator(blob_storage_parent, false /* recursive */, | 45 base::FileEnumerator enumerator(blob_storage_parent, false /* recursive */, |
| 45 base::FileEnumerator::DIRECTORIES); | 46 base::FileEnumerator::DIRECTORIES); |
| 46 bool success = true; | 47 bool success = true; |
| 48 bool cleanup_needed = false; |
| 47 for (FilePath name = enumerator.Next(); !name.empty(); | 49 for (FilePath name = enumerator.Next(); !name.empty(); |
| 48 name = enumerator.Next()) { | 50 name = enumerator.Next()) { |
| 51 cleanup_needed = true; |
| 49 if (current_run_dir.empty() || name != current_run_dir) | 52 if (current_run_dir.empty() || name != current_run_dir) |
| 50 success &= base::DeleteFile(name, true /* recursive */); | 53 success &= base::DeleteFile(name, true /* recursive */); |
| 51 } | 54 } |
| 52 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success); | 55 if (cleanup_needed) |
| 56 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success); |
| 53 } | 57 } |
| 54 | 58 |
| 55 class BlobHandleImpl : public BlobHandle { | 59 class BlobHandleImpl : public BlobHandle { |
| 56 public: | 60 public: |
| 57 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) | 61 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) |
| 58 : handle_(std::move(handle)) {} | 62 : handle_(std::move(handle)) {} |
| 59 | 63 |
| 60 ~BlobHandleImpl() override {} | 64 ~BlobHandleImpl() override {} |
| 61 | 65 |
| 62 std::string GetUUID() override { return handle_->uuid(); } | 66 std::string GetUUID() override { return handle_->uuid(); } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 return UserDataAdapter<ChromeBlobStorageContext>::Get( | 120 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
| 117 context, kBlobStorageContextKeyName); | 121 context, kBlobStorageContextKeyName); |
| 118 } | 122 } |
| 119 | 123 |
| 120 void ChromeBlobStorageContext::InitializeOnIOThread( | 124 void ChromeBlobStorageContext::InitializeOnIOThread( |
| 121 FilePath blob_storage_dir, | 125 FilePath blob_storage_dir, |
| 122 scoped_refptr<base::TaskRunner> file_task_runner) { | 126 scoped_refptr<base::TaskRunner> file_task_runner) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 context_.reset(new BlobStorageContext(std::move(blob_storage_dir), | 128 context_.reset(new BlobStorageContext(std::move(blob_storage_dir), |
| 125 std::move(file_task_runner))); | 129 std::move(file_task_runner))); |
| 130 // Signal the BlobMemoryController when it's appropriate to calculate its |
| 131 // storage limits. |
| 132 BrowserThread::PostAfterStartupTask( |
| 133 FROM_HERE, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), |
| 134 base::Bind(&storage::BlobMemoryController::CalculateBlobStorageLimits, |
| 135 context_->mutable_memory_controller()->GetWeakPtr())); |
| 126 } | 136 } |
| 127 | 137 |
| 128 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( | 138 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( |
| 129 const char* data, | 139 const char* data, |
| 130 size_t length) { | 140 size_t length) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 | 142 |
| 133 std::string uuid(base::GenerateGUID()); | 143 std::string uuid(base::GenerateGUID()); |
| 134 storage::BlobDataBuilder blob_data_builder(uuid); | 144 storage::BlobDataBuilder blob_data_builder(uuid); |
| 135 blob_data_builder.AppendData(data, length); | 145 blob_data_builder.AppendData(data, length); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { | 180 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
| 171 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | 181 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
| 172 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 182 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 173 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); | 183 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
| 174 return; | 184 return; |
| 175 } | 185 } |
| 176 delete this; | 186 delete this; |
| 177 } | 187 } |
| 178 | 188 |
| 179 } // namespace content | 189 } // namespace content |
| OLD | NEW |