| 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" | |
| 11 #include "base/files/file_enumerator.h" | |
| 12 #include "base/files/file_util.h" | |
| 13 #include "base/guid.h" | 10 #include "base/guid.h" |
| 14 #include "base/metrics/histogram_macros.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/task_runner.h" | |
| 17 #include "base/threading/sequenced_worker_pool.h" | |
| 18 #include "content/public/browser/blob_handle.h" | 11 #include "content/public/browser/blob_handle.h" |
| 19 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 20 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 21 #include "storage/browser/blob/blob_data_builder.h" | 14 #include "storage/browser/blob/blob_data_builder.h" |
| 22 #include "storage/browser/blob/blob_data_handle.h" | 15 #include "storage/browser/blob/blob_data_handle.h" |
| 23 #include "storage/browser/blob/blob_storage_context.h" | 16 #include "storage/browser/blob/blob_storage_context.h" |
| 24 | 17 |
| 25 using base::FilePath; | |
| 26 using base::UserDataAdapter; | 18 using base::UserDataAdapter; |
| 27 using storage::BlobStorageContext; | 19 using storage::BlobStorageContext; |
| 28 | 20 |
| 29 namespace content { | 21 namespace content { |
| 30 | 22 |
| 31 namespace { | 23 namespace { |
| 32 const FilePath::CharType kBlobStorageContextKeyName[] = | |
| 33 FILE_PATH_LITERAL("content_blob_storage_context"); | |
| 34 const FilePath::CharType kBlobStorageParentDirectory[] = | |
| 35 FILE_PATH_LITERAL("blob_storage"); | |
| 36 | 24 |
| 37 // Removes all folders in the parent directory except for the | 25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; |
| 38 // |current_run_dir| folder. If this path is empty, then we delete all folders. | |
| 39 void RemoveOldBlobStorageDirectories(FilePath blob_storage_parent, | |
| 40 const FilePath& current_run_dir) { | |
| 41 if (!base::DirectoryExists(blob_storage_parent)) { | |
| 42 return; | |
| 43 } | |
| 44 base::FileEnumerator enumerator(blob_storage_parent, false /* recursive */, | |
| 45 base::FileEnumerator::DIRECTORIES); | |
| 46 bool success = true; | |
| 47 for (FilePath name = enumerator.Next(); !name.empty(); | |
| 48 name = enumerator.Next()) { | |
| 49 if (current_run_dir.empty() || name != current_run_dir) | |
| 50 success &= base::DeleteFile(name, true /* recursive */); | |
| 51 } | |
| 52 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success); | |
| 53 } | |
| 54 | 26 |
| 55 class BlobHandleImpl : public BlobHandle { | 27 class BlobHandleImpl : public BlobHandle { |
| 56 public: | 28 public: |
| 57 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) | 29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) |
| 58 : handle_(std::move(handle)) {} | 30 : handle_(std::move(handle)) {} |
| 59 | 31 |
| 60 ~BlobHandleImpl() override {} | 32 ~BlobHandleImpl() override {} |
| 61 | 33 |
| 62 std::string GetUUID() override { return handle_->uuid(); } | 34 std::string GetUUID() override { return handle_->uuid(); } |
| 63 | 35 |
| 64 private: | 36 private: |
| 65 std::unique_ptr<storage::BlobDataHandle> handle_; | 37 std::unique_ptr<storage::BlobDataHandle> handle_; |
| 66 }; | 38 }; |
| 67 | 39 |
| 68 } // namespace | 40 } // namespace |
| 69 | 41 |
| 70 ChromeBlobStorageContext::ChromeBlobStorageContext() {} | 42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} |
| 71 | 43 |
| 72 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( | 44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
| 73 BrowserContext* context) { | 45 BrowserContext* context) { |
| 74 if (!context->GetUserData(kBlobStorageContextKeyName)) { | 46 if (!context->GetUserData(kBlobStorageContextKeyName)) { |
| 75 scoped_refptr<ChromeBlobStorageContext> blob = | 47 scoped_refptr<ChromeBlobStorageContext> blob = |
| 76 new ChromeBlobStorageContext(); | 48 new ChromeBlobStorageContext(); |
| 77 context->SetUserData( | 49 context->SetUserData( |
| 78 kBlobStorageContextKeyName, | 50 kBlobStorageContextKeyName, |
| 79 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); | 51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); |
| 80 | |
| 81 // Check first to avoid memory leak in unittests. | 52 // Check first to avoid memory leak in unittests. |
| 82 bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO); | 53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 83 | |
| 84 // Resolve our storage directories. | |
| 85 FilePath blob_storage_parent = | |
| 86 context->GetPath().Append(kBlobStorageParentDirectory); | |
| 87 FilePath blob_storage_dir = blob_storage_parent.Append( | |
| 88 FilePath::FromUTF8Unsafe(base::GenerateGUID())); | |
| 89 | |
| 90 // Only populate the task runner if we're not off the record. This enables | |
| 91 // paging/saving blob data to disk. | |
| 92 scoped_refptr<base::TaskRunner> file_task_runner; | |
| 93 | |
| 94 // If we're not incognito mode, schedule all of our file tasks to enable | |
| 95 // disk on the storage context. | |
| 96 if (!context->IsOffTheRecord() && io_thread_valid) { | |
| 97 file_task_runner = | |
| 98 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 99 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 100 // Removes our old blob directories if they exist. | |
| 101 BrowserThread::PostAfterStartupTask( | |
| 102 FROM_HERE, file_task_runner, | |
| 103 base::Bind(&RemoveOldBlobStorageDirectories, | |
| 104 base::Passed(&blob_storage_parent), blob_storage_dir)); | |
| 105 } | |
| 106 | |
| 107 if (io_thread_valid) { | |
| 108 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
| 109 BrowserThread::IO, FROM_HERE, | 55 BrowserThread::IO, FROM_HERE, |
| 110 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob, | 56 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
| 111 base::Passed(&blob_storage_dir), | |
| 112 base::Passed(&file_task_runner))); | |
| 113 } | 57 } |
| 114 } | 58 } |
| 115 | 59 |
| 116 return UserDataAdapter<ChromeBlobStorageContext>::Get( | 60 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
| 117 context, kBlobStorageContextKeyName); | 61 context, kBlobStorageContextKeyName); |
| 118 } | 62 } |
| 119 | 63 |
| 120 void ChromeBlobStorageContext::InitializeOnIOThread( | 64 void ChromeBlobStorageContext::InitializeOnIOThread() { |
| 121 FilePath blob_storage_dir, | |
| 122 scoped_refptr<base::TaskRunner> file_task_runner) { | |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 context_.reset(new BlobStorageContext(std::move(blob_storage_dir), | 66 context_.reset(new BlobStorageContext()); |
| 125 std::move(file_task_runner))); | |
| 126 } | 67 } |
| 127 | 68 |
| 128 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( | 69 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( |
| 129 const char* data, | 70 const char* data, |
| 130 size_t length) { | 71 size_t length) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 | 73 |
| 133 std::string uuid(base::GenerateGUID()); | 74 std::string uuid(base::GenerateGUID()); |
| 134 storage::BlobDataBuilder blob_data_builder(uuid); | 75 storage::BlobDataBuilder blob_data_builder(uuid); |
| 135 blob_data_builder.AppendData(data, length); | 76 blob_data_builder.AppendData(data, length); |
| 136 | 77 |
| 137 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 78 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
| 138 context_->AddFinishedBlob(&blob_data_builder); | 79 context_->AddFinishedBlob(&blob_data_builder); |
| 139 if (!blob_data_handle) | 80 if (!blob_data_handle) |
| 140 return std::unique_ptr<BlobHandle>(); | 81 return std::unique_ptr<BlobHandle>(); |
| 141 | 82 |
| 142 std::unique_ptr<BlobHandle> blob_handle( | 83 std::unique_ptr<BlobHandle> blob_handle( |
| 143 new BlobHandleImpl(std::move(blob_data_handle))); | 84 new BlobHandleImpl(std::move(blob_data_handle))); |
| 144 return blob_handle; | 85 return blob_handle; |
| 145 } | 86 } |
| 146 | 87 |
| 147 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob( | 88 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateFileBackedBlob( |
| 148 const FilePath& path, | 89 const base::FilePath& path, |
| 149 int64_t offset, | 90 int64_t offset, |
| 150 int64_t size, | 91 int64_t size, |
| 151 const base::Time& expected_modification_time) { | 92 const base::Time& expected_modification_time) { |
| 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 153 | 94 |
| 154 std::string uuid(base::GenerateGUID()); | 95 std::string uuid(base::GenerateGUID()); |
| 155 storage::BlobDataBuilder blob_data_builder(uuid); | 96 storage::BlobDataBuilder blob_data_builder(uuid); |
| 156 blob_data_builder.AppendFile(path, offset, size, expected_modification_time); | 97 blob_data_builder.AppendFile(path, offset, size, expected_modification_time); |
| 157 | 98 |
| 158 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 99 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 170 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { | 111 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
| 171 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | 112 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
| 172 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 113 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 173 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); | 114 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
| 174 return; | 115 return; |
| 175 } | 116 } |
| 176 delete this; | 117 delete this; |
| 177 } | 118 } |
| 178 | 119 |
| 179 } // namespace content | 120 } // namespace content |
| OLD | NEW |