Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: content/browser/blob_storage/chrome_blob_storage_context.cc

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added back transport controller test, small cleanups Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/guid.h" 10 #include "base/guid.h"
11 #include "base/files/file.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/task_runner.h"
16 #include "base/single_thread_task_runner.h"
11 #include "content/public/browser/blob_handle.h" 17 #include "content/public/browser/blob_handle.h"
12 #include "content/public/browser/browser_context.h" 18 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
14 #include "storage/browser/blob/blob_data_builder.h" 20 #include "storage/browser/blob/blob_data_builder.h"
15 #include "storage/browser/blob/blob_data_handle.h" 21 #include "storage/browser/blob/blob_data_handle.h"
16 #include "storage/browser/blob/blob_storage_context.h" 22 #include "storage/browser/blob/blob_storage_context.h"
17 23
24 using base::FilePath;
18 using base::UserDataAdapter; 25 using base::UserDataAdapter;
19 using storage::BlobStorageContext; 26 using storage::BlobStorageContext;
20 27
21 namespace content { 28 namespace content {
22 29
23 namespace { 30 namespace {
31 const char kBlobStorageParentDirectory[] = "blob_storage";
32
33 void CreateBlobDirectory(const FilePath& blob_storage_dir,
34 scoped_refptr<base::TaskRunner> io_runner,
35 const base::Closure& done) {
36 base::File::Error error;
37 bool success = base::CreateDirectoryAndGetError(blob_storage_dir, &error);
38 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.DirectorySuccess", success);
39 if (!success) {
40 LOCAL_HISTOGRAM_ENUMERATION("Storage.Blob.DirectorySuccess.Error", -error,
41 -base::File::FILE_ERROR_MAX);
42 } else {
43 io_runner->PostTask(FROM_HERE, done);
44 }
45 }
46
47 // Removes all folders in the parent directory except for the
48 // |current_run_dir| folder. If this path is empty, then we delete all folders.
49 void RemoveOldBlobStorageDirectories(const FilePath& blob_storage_parent,
50 const FilePath& current_run_dir) {
51 if (!base::DirectoryExists(blob_storage_parent)) {
52 return;
53 }
54 base::FileEnumerator enumerator(blob_storage_parent, false,
55 base::FileEnumerator::DIRECTORIES);
56 std::vector<std::string> components;
57 bool success = true;
58 for (base::FilePath name = enumerator.Next(); !name.empty();
59 name = enumerator.Next()) {
60 if (current_run_dir.empty() || name != current_run_dir)
61 success &= base::DeleteFile(name, true);
62 }
63 LOCAL_HISTOGRAM_BOOLEAN("Storage.Blob.CleanupSuccess", success);
64 }
24 65
25 const char kBlobStorageContextKeyName[] = "content_blob_storage_context"; 66 const char kBlobStorageContextKeyName[] = "content_blob_storage_context";
26 67
27 class BlobHandleImpl : public BlobHandle { 68 class BlobHandleImpl : public BlobHandle {
28 public: 69 public:
29 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle) 70 explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle)
30 : handle_(std::move(handle)) {} 71 : handle_(std::move(handle)) {}
31 72
32 ~BlobHandleImpl() override {} 73 ~BlobHandleImpl() override {}
33 74
34 std::string GetUUID() override { return handle_->uuid(); } 75 std::string GetUUID() override { return handle_->uuid(); }
35 76
36 private: 77 private:
37 std::unique_ptr<storage::BlobDataHandle> handle_; 78 std::unique_ptr<storage::BlobDataHandle> handle_;
38 }; 79 };
39 80
40 } // namespace 81 } // namespace
41 82
42 ChromeBlobStorageContext::ChromeBlobStorageContext() {} 83 ChromeBlobStorageContext::ChromeBlobStorageContext() {}
43 84
44 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor( 85 ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor(
45 BrowserContext* context) { 86 BrowserContext* context) {
46 if (!context->GetUserData(kBlobStorageContextKeyName)) { 87 if (!context->GetUserData(kBlobStorageContextKeyName)) {
47 scoped_refptr<ChromeBlobStorageContext> blob = 88 scoped_refptr<ChromeBlobStorageContext> blob =
48 new ChromeBlobStorageContext(); 89 new ChromeBlobStorageContext();
49 context->SetUserData( 90 context->SetUserData(
50 kBlobStorageContextKeyName, 91 kBlobStorageContextKeyName,
51 new UserDataAdapter<ChromeBlobStorageContext>(blob.get())); 92 new UserDataAdapter<ChromeBlobStorageContext>(blob.get()));
93
52 // Check first to avoid memory leak in unittests. 94 // Check first to avoid memory leak in unittests.
53 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { 95 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
96 if (io_thread_valid) {
54 BrowserThread::PostTask( 97 BrowserThread::PostTask(
55 BrowserThread::IO, FROM_HERE, 98 BrowserThread::IO, FROM_HERE,
56 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob)); 99 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread, blob));
57 } 100 }
101
102 // If we're not incognito mode, schedule all of our file tasks to enable
103 // disk on the storage context.
104 if (!context->IsOffTheRecord() && io_thread_valid &&
105 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
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::GetMessageLoopProxyForThread(BrowserThread::IO);
114 scoped_refptr<base::SingleThreadTaskRunner> file_runner =
115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
116
117 // This creates our directory and calls |OnBlobDirectoryCreated| if we're
118 // successful.
119 BrowserThread::PostAfterStartupTask(
michaeln 2016/07/07 20:05:21 Can creation/deletion of the active directory be m
120 FROM_HERE, file_runner,
121 base::Bind(
122 &CreateBlobDirectory, blob_storage_dir, io_runner,
123 base::Bind(&ChromeBlobStorageContext::OnBlobDirectoryCreated,
124 blob, blob_storage_dir)));
125 // Removes our old blob directories if they exist.
126 BrowserThread::PostAfterStartupTask(
127 FROM_HERE, file_runner,
128 base::Bind(&RemoveOldBlobStorageDirectories, blob_storage_parent,
129 blob_storage_dir));
130 }
58 } 131 }
59 132
60 return UserDataAdapter<ChromeBlobStorageContext>::Get( 133 return UserDataAdapter<ChromeBlobStorageContext>::Get(
61 context, kBlobStorageContextKeyName); 134 context, kBlobStorageContextKeyName);
62 } 135 }
63 136
64 void ChromeBlobStorageContext::InitializeOnIOThread() { 137 void ChromeBlobStorageContext::InitializeOnIOThread() {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); 138 DCHECK_CURRENTLY_ON(BrowserThread::IO);
66 context_.reset(new BlobStorageContext()); 139 context_.reset(new BlobStorageContext());
140 if (!blob_storage_dir_.empty()) {
141 // This happens if we create our blob storage folder before this method is
142 // called, so we enable disk here.
143 context_->EnableDisk(
144 blob_storage_dir_,
145 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
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 25 matching lines...) Expand all
102 return std::unique_ptr<BlobHandle>(); 182 return std::unique_ptr<BlobHandle>();
103 183
104 std::unique_ptr<BlobHandle> blob_handle( 184 std::unique_ptr<BlobHandle> blob_handle(
105 new BlobHandleImpl(std::move(blob_data_handle))); 185 new BlobHandleImpl(std::move(blob_data_handle)));
106 return blob_handle; 186 return blob_handle;
107 } 187 }
108 188
109 ChromeBlobStorageContext::~ChromeBlobStorageContext() {} 189 ChromeBlobStorageContext::~ChromeBlobStorageContext() {}
110 190
111 void ChromeBlobStorageContext::DeleteOnCorrectThread() const { 191 void ChromeBlobStorageContext::DeleteOnCorrectThread() const {
192 // Schedule a task to delete all blob directories.
193 if (!blob_storage_dir_.empty()) {
194 BrowserThread::PostTask(
195 BrowserThread::FILE, FROM_HERE,
196 base::Bind(&RemoveOldBlobStorageDirectories,
197 blob_storage_dir_.DirName(), base::FilePath()));
198 }
112 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) && 199 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO) &&
113 !BrowserThread::CurrentlyOn(BrowserThread::IO)) { 200 !BrowserThread::CurrentlyOn(BrowserThread::IO)) {
114 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); 201 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this);
115 return; 202 return;
116 } 203 }
117 delete this; 204 delete this;
118 } 205 }
119 206
207 void ChromeBlobStorageContext::OnBlobDirectoryCreated(
208 const base::FilePath& blob_storage_directory) {
209 LOG(ERROR) << "Blob directory successfully created "
210 << blob_storage_directory.value();
211 DCHECK(blob_storage_dir_.empty());
212 blob_storage_dir_ = blob_storage_directory;
213 if (context_) {
214 context_->EnableDisk(
215 blob_storage_dir_,
216 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
217 }
218 }
219
120 } // namespace content 220 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698