Chromium Code Reviews| 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" | |
| 10 #include "base/guid.h" | 13 #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" | |
| 11 #include "content/public/browser/blob_handle.h" | 18 #include "content/public/browser/blob_handle.h" |
| 12 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 14 #include "storage/browser/blob/blob_data_builder.h" | 21 #include "storage/browser/blob/blob_data_builder.h" |
| 15 #include "storage/browser/blob/blob_data_handle.h" | 22 #include "storage/browser/blob/blob_data_handle.h" |
| 16 #include "storage/browser/blob/blob_storage_context.h" | 23 #include "storage/browser/blob/blob_storage_context.h" |
| 17 | 24 |
| 25 using base::FilePath; | |
| 18 using base::UserDataAdapter; | 26 using base::UserDataAdapter; |
| 19 using storage::BlobStorageContext; | 27 using storage::BlobStorageContext; |
| 20 | 28 |
| 21 namespace content { | 29 namespace content { |
| 22 | 30 |
| 23 namespace { | 31 namespace { |
| 32 const char kBlobStorageParentDirectory[] = "blob_storage"; | |
| 33 | |
| 34 void CreateBlobDirectory(const FilePath& blob_storage_dir, | |
| 35 scoped_refptr<base::TaskRunner> io_runner, | |
| 36 const base::Closure& done) { | |
| 37 base::File::Error error; | |
| 38 bool success = base::CreateDirectoryAndGetError(blob_storage_dir, &error); | |
| 39 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.DirectorySuccess", success); | |
| 40 if (!success) { | |
| 41 LOCAL_HISTOGRAM_ENUMERATION("Storage.Blob.DirectorySuccess.Error", -error, | |
| 42 -base::File::FILE_ERROR_MAX); | |
| 43 } else { | |
| 44 io_runner->PostTask(FROM_HERE, done); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // Removes all folders in the parent directory except for the | |
| 49 // |current_run_dir| folder. If this path is empty, then we delete all folders. | |
| 50 void RemoveOldBlobStorageDirectories(const FilePath& blob_storage_parent, | |
| 51 const FilePath& current_run_dir) { | |
| 52 if (!base::DirectoryExists(blob_storage_parent)) { | |
| 53 return; | |
| 54 } | |
| 55 base::FileEnumerator enumerator(blob_storage_parent, false, | |
| 56 base::FileEnumerator::DIRECTORIES); | |
| 57 std::vector<std::string> components; | |
| 58 bool success = true; | |
| 59 for (base::FilePath name = enumerator.Next(); !name.empty(); | |
| 60 name = enumerator.Next()) { | |
| 61 if (current_run_dir.empty() || name != current_run_dir) | |
| 62 success &= base::DeleteFile(name, true); | |
| 63 } | |
| 64 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success); | |
| 65 } | |
| 24 | 66 |
| 25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; | 67 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; |
| 26 | 68 |
| 27 class BlobHandleImpl : public BlobHandle { | 69 class BlobHandleImpl : public BlobHandle { |
| 28 public: | 70 public: |
| 29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) | 71 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) |
| 30 : handle_(std::move(handle)) {} | 72 : handle_(std::move(handle)) {} |
| 31 | 73 |
| 32 ~BlobHandleImpl() override {} | 74 ~BlobHandleImpl() override {} |
| 33 | 75 |
| 34 std::string GetUUID() override { return handle_->uuid(); } | 76 std::string GetUUID() override { return handle_->uuid(); } |
| 35 | 77 |
| 36 private: | 78 private: |
| 37 std::unique_ptr<storage::BlobDataHandle> handle_; | 79 std::unique_ptr<storage::BlobDataHandle> handle_; |
| 38 }; | 80 }; |
| 39 | 81 |
| 40 } // namespace | 82 } // namespace |
| 41 | 83 |
| 42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} | 84 ChromeBlobStorageContext::ChromeBlobStorageContext() {} |
| 43 | 85 |
| 44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( | 86 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( |
| 45 BrowserContext* context) { | 87 BrowserContext* context) { |
| 46 if (!context->GetUserData(kBlobStorageContextKeyName)) { | 88 if (!context->GetUserData(kBlobStorageContextKeyName)) { |
| 47 scoped_refptr<ChromeBlobStorageContext> blob = | 89 scoped_refptr<ChromeBlobStorageContext> blob = |
| 48 new ChromeBlobStorageContext(); | 90 new ChromeBlobStorageContext(); |
| 49 context->SetUserData( | 91 context->SetUserData( |
| 50 kBlobStorageContextKeyName, | 92 kBlobStorageContextKeyName, |
| 51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); | 93 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); |
| 94 | |
| 52 // Check first to avoid memory leak in unittests. | 95 // Check first to avoid memory leak in unittests. |
| 53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 96 bool io_thread_valid = BrowserThread::IsMessageLoopValid(BrowserThread::IO); |
| 97 if (io_thread_valid) { | |
| 54 BrowserThread::PostTask( | 98 BrowserThread::PostTask( |
| 55 BrowserThread::IO, FROM_HERE, | 99 BrowserThread::IO, FROM_HERE, |
| 56 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); | 100 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); |
| 57 } | 101 } |
| 102 | |
| 103 // If we're not incognito mode, schedule all of our file tasks to enable | |
| 104 // disk on the storage context. | |
| 105 if (!context->IsOffTheRecord() && io_thread_valid) { | |
| 106 // Resolve our storage directories. | |
| 107 base::FilePath blob_storage_parent = | |
| 108 context->GetPath().Append(kBlobStorageParentDirectory); | |
| 109 base::FilePath blob_storage_dir = | |
| 110 blob_storage_parent.Append(base::GenerateGUID()); | |
| 111 | |
| 112 scoped_refptr<base::SingleThreadTaskRunner> io_runner = | |
| 113 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | |
| 114 scoped_refptr<base::TaskRunner> file_runner = | |
| 115 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 116 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 117 | |
| 118 // This creates our directory and calls |OnBlobDirectoryCreated| if | |
| 119 // we're successful. | |
| 120 BrowserThread::PostAfterStartupTask( | |
|
michaeln
2016/08/15 22:44:42
I mentioned this earlier but it got lost in other
| |
| 121 FROM_HERE, file_runner, | |
| 122 base::Bind( | |
| 123 &CreateBlobDirectory, blob_storage_dir, io_runner, | |
| 124 base::Bind(&ChromeBlobStorageContext::OnBlobDirectoryCreated, | |
| 125 blob, blob_storage_dir))); | |
| 126 | |
| 127 // Removes our old blob directories if they exist. | |
| 128 BrowserThread::PostAfterStartupTask( | |
| 129 FROM_HERE, file_runner, | |
| 130 base::Bind(&RemoveOldBlobStorageDirectories, blob_storage_parent, | |
| 131 blob_storage_dir)); | |
| 132 } | |
| 58 } | 133 } |
| 59 | 134 |
| 60 return UserDataAdapter<ChromeBlobStorageContext>::Get( | 135 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
| 61 context, kBlobStorageContextKeyName); | 136 context, kBlobStorageContextKeyName); |
| 62 } | 137 } |
| 63 | 138 |
| 64 void ChromeBlobStorageContext::InitializeOnIOThread() { | 139 void ChromeBlobStorageContext::InitializeOnIOThread() { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 66 context_.reset(new BlobStorageContext()); | 141 context_.reset(new BlobStorageContext()); |
| 142 if (!blob_storage_dir_.empty()) { | |
| 143 // This happens if we create our blob storage folder before this method is | |
| 144 // called, so we enable disk here. | |
| 145 context_->EnableDisk(blob_storage_dir_, BrowserThread::GetBlockingPool()); | |
|
michaeln
2016/08/15 22:44:42
that would also remove the need for the EnableDisk
| |
| 146 } | |
| 67 } | 147 } |
| 68 | 148 |
| 69 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( | 149 std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob( |
| 70 const char* data, | 150 const char* data, |
| 71 size_t length) { | 151 size_t length) { |
| 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 73 | 153 |
| 74 std::string uuid(base::GenerateGUID()); | 154 std::string uuid(base::GenerateGUID()); |
| 75 storage::BlobDataBuilder blob_data_builder(uuid); | 155 storage::BlobDataBuilder blob_data_builder(uuid); |
| 76 blob_data_builder.AppendData(data, length); | 156 blob_data_builder.AppendData(data, length); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 | 190 |
| 111 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { | 191 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { |
| 112 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && | 192 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && |
| 113 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 193 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 114 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); | 194 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
| 115 return; | 195 return; |
| 116 } | 196 } |
| 117 delete this; | 197 delete this; |
| 118 } | 198 } |
| 119 | 199 |
| 200 void ChromeBlobStorageContext::OnBlobDirectoryCreated( | |
| 201 const base::FilePath& blob_storage_directory) { | |
| 202 LOG(ERROR) << "Blob directory successfully created " | |
| 203 << blob_storage_directory.value(); | |
| 204 DCHECK(blob_storage_dir_.empty()); | |
| 205 blob_storage_dir_ = blob_storage_directory; | |
| 206 if (context_) { | |
| 207 scoped_refptr<base::TaskRunner> file_runner = | |
| 208 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 209 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 210 context_->EnableDisk(blob_storage_dir_, file_runner); | |
| 211 } | |
| 212 } | |
| 213 | |
| 120 } // namespace content | 214 } // namespace content |
| OLD | NEW |