1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
content / browser / blob_storage / chrome_blob_storage_context.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include <memory>
#include <utility>
#include "base/feature_list.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/supports_user_data.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_runner.h"
#include "base/task/thread_pool.h"
#include "base/uuid.h"
#include "components/file_access/scoped_file_access_delegate.h"
#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/blob_handle.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_features.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_memory_controller.h"
#include "storage/browser/blob/blob_registry_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "storage/browser/blob/blob_url_loader_factory.h"
#include "storage/browser/blob/blob_url_registry.h"
#include "storage/browser/file_system/file_system_context.h"
using base::FilePath;
using base::UserDataAdapter;
using storage::BlobStorageContext;
namespace content {
namespace {
const FilePath::CharType kBlobStorageParentDirectory[] =
FILE_PATH_LITERAL("blob_storage");
// 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(FilePath blob_storage_parent,
const FilePath& current_run_dir) {
if (!base::DirectoryExists(blob_storage_parent)) {
return;
}
base::FileEnumerator enumerator(blob_storage_parent, false /* recursive */,
base::FileEnumerator::DIRECTORIES);
for (FilePath name = enumerator.Next(); !name.empty();
name = enumerator.Next()) {
if (current_run_dir.empty() || name != current_run_dir)
base::DeletePathRecursively(name);
}
}
class BlobHandleImpl : public BlobHandle {
public:
explicit BlobHandleImpl(std::unique_ptr<storage::BlobDataHandle> handle)
: handle_(std::move(handle)) {}
~BlobHandleImpl() override {}
std::string GetUUID() override { return handle_->uuid(); }
mojo::PendingRemote<blink::mojom::Blob> PassBlob() override {
mojo::PendingRemote<blink::mojom::Blob> result;
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(base::IgnoreResult(&storage::BlobImpl::Create),
std::make_unique<storage::BlobDataHandle>(*handle_),
result.InitWithNewPipeAndPassReceiver()));
return result;
}
blink::mojom::SerializedBlobPtr Serialize() override {
return blink::mojom::SerializedBlob::New(
handle_->uuid(), handle_->content_type(), handle_->size(), PassBlob());
}
private:
std::unique_ptr<storage::BlobDataHandle> handle_;
};
} // namespace
ChromeBlobStorageContext::ChromeBlobStorageContext() {}
// static
ChromeBlobStorageContext* ChromeBlobStorageContext::GetFor(
BrowserContext* context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!context->GetUserData(kBlobStorageContextKeyName)) {
scoped_refptr<ChromeBlobStorageContext> blob_storage_context =
new ChromeBlobStorageContext();
context->SetUserData(
kBlobStorageContextKeyName,
std::make_unique<UserDataAdapter<ChromeBlobStorageContext>>(
blob_storage_context.get()));
// Check first to avoid memory leak in unittests.
bool io_thread_valid =
BrowserThread::IsThreadInitialized(BrowserThread::IO);
// Resolve our storage directories.
FilePath blob_storage_parent =
context->GetPath().Append(kBlobStorageParentDirectory);
FilePath blob_storage_dir =
blob_storage_parent.Append(FilePath::FromUTF8Unsafe(
base::Uuid::GenerateRandomV4().AsLowercaseString()));
// Only populate the task runner if we're not off the record. This enables
// paging/saving blob data to disk.
scoped_refptr<base::TaskRunner> file_task_runner;
// 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) {
file_task_runner = base::ThreadPool::CreateTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
// Removes our old blob directories if they exist.
BrowserThread::PostBestEffortTask(
FROM_HERE, file_task_runner,
base::BindOnce(&RemoveOldBlobStorageDirectories,
std::move(blob_storage_parent), blob_storage_dir));
}
if (io_thread_valid) {
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&ChromeBlobStorageContext::InitializeOnIOThread,
blob_storage_context, context->GetPath(),
std::move(blob_storage_dir),
std::move(file_task_runner)));
}
}
return UserDataAdapter<ChromeBlobStorageContext>::Get(
context, kBlobStorageContextKeyName);
}
// static
mojo::PendingRemote<storage::mojom::BlobStorageContext>
ChromeBlobStorageContext::GetRemoteFor(BrowserContext* browser_context) {
DCHECK(browser_context);
mojo::PendingRemote<storage::mojom::BlobStorageContext> remote;
auto receiver = remote.InitWithNewPipeAndPassReceiver();
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
[](scoped_refptr<ChromeBlobStorageContext> blob_storage_context,
mojo::PendingReceiver<storage::mojom::BlobStorageContext>
receiver) {
blob_storage_context->BindMojoContext(std::move(receiver));
},
base::RetainedRef(ChromeBlobStorageContext::GetFor(browser_context)),
std::move(receiver)));
return remote;
}
void ChromeBlobStorageContext::InitializeOnIOThread(
const FilePath& profile_dir,
const FilePath& blob_storage_dir,
scoped_refptr<base::TaskRunner> file_task_runner) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
context_ = std::make_unique<BlobStorageContext>(profile_dir, blob_storage_dir,
std::move(file_task_runner));
// Signal the BlobMemoryController when it's appropriate to calculate its
// storage limits.
content::GetIOThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE,
base::BindOnce(
&storage::BlobMemoryController::CalculateBlobStorageLimits,
context_->mutable_memory_controller()->GetWeakPtr()));
}
storage::BlobStorageContext* ChromeBlobStorageContext::context() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
return context_.get();
}
void ChromeBlobStorageContext::BindMojoContext(
mojo::PendingReceiver<storage::mojom::BlobStorageContext> receiver) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(context_) << "InitializeOnIOThread must be called first";
context_->Bind(std::move(receiver));
}
std::unique_ptr<BlobHandle> ChromeBlobStorageContext::CreateMemoryBackedBlob(
base::span<const uint8_t> data,
const std::string& content_type) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::string uuid(base::Uuid::GenerateRandomV4().AsLowercaseString());
auto blob_data_builder = std::make_unique<storage::BlobDataBuilder>(uuid);
blob_data_builder->set_content_type(content_type);
blob_data_builder->AppendData(data);
std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
context_->AddFinishedBlob(std::move(blob_data_builder));
if (!blob_data_handle)
return nullptr;
std::unique_ptr<BlobHandle> blob_handle(
new BlobHandleImpl(std::move(blob_data_handle)));
return blob_handle;
}
void ChromeBlobStorageContext::CreateFileSystemBlobWithFileAccess(
scoped_refptr<storage::FileSystemContext> file_system_context,
mojo::PendingReceiver<blink::mojom::Blob> blob_receiver,
const storage::FileSystemURL& url,
const std::string& blob_uuid,
const std::string& content_type,
const uint64_t file_size,
const base::Time& file_modification_time,
file_access::ScopedFileAccessDelegate::RequestFilesAccessIOCallback
file_access) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
auto blob_builder = std::make_unique<storage::BlobDataBuilder>(blob_uuid);
if (file_size > 0) {
// Use AppendFileSystemFile here, since we're streaming the file directly
// from the file system backend, and the file thus might not actually be
// backed by a file on disk.
blob_builder->AppendFileSystemFile(
url, 0, file_size, file_modification_time,
std::move(file_system_context), std::move(file_access));
}
blob_builder->set_content_type(content_type);
std::unique_ptr<storage::BlobDataHandle> blob_handle =
context_->AddFinishedBlob(std::move(blob_builder));
// Since the blob we're creating doesn't depend on other blobs, and doesn't
// require blob memory/disk quota, creating the blob can't fail.
DCHECK(!blob_handle->IsBroken());
storage::BlobImpl::Create(std::move(blob_handle), std::move(blob_receiver));
}
void ChromeBlobStorageContext::CreateFileSystemBlob(
scoped_refptr<storage::FileSystemContext> file_system_context,
mojo::PendingReceiver<blink::mojom::Blob> blob_receiver,
const storage::FileSystemURL& url,
const std::string& blob_uuid,
const std::string& content_type,
const uint64_t file_size,
const base::Time& file_modification_time) {
CreateFileSystemBlobWithFileAccess(
file_system_context, std::move(blob_receiver), url, blob_uuid,
content_type, file_size, file_modification_time, base::NullCallback());
}
// static
scoped_refptr<network::SharedURLLoaderFactory>
ChromeBlobStorageContext::URLLoaderFactoryForToken(
StoragePartition* storage_partition,
mojo::PendingRemote<blink::mojom::BlobURLToken> token) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
mojo::PendingRemote<network::mojom::URLLoaderFactory>
blob_url_loader_factory_remote;
storage::BlobURLLoaderFactory::Create(
std::move(token),
static_cast<StoragePartitionImpl*>(storage_partition)
->GetBlobUrlRegistry()
->AsWeakPtr(),
blob_url_loader_factory_remote.InitWithNewPipeAndPassReceiver());
return base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
std::move(blob_url_loader_factory_remote));
}
// static
scoped_refptr<network::SharedURLLoaderFactory>
ChromeBlobStorageContext::URLLoaderFactoryForUrl(
StoragePartition* storage_partition,
const GURL& url) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
mojo::PendingRemote<network::mojom::URLLoaderFactory>
blob_url_loader_factory_remote;
storage::BlobURLLoaderFactory::Create(
static_cast<StoragePartitionImpl*>(storage_partition)
->GetBlobUrlRegistry()
->GetBlobFromUrl(url),
url, blob_url_loader_factory_remote.InitWithNewPipeAndPassReceiver());
return base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
std::move(blob_url_loader_factory_remote));
}
// static
mojo::PendingRemote<blink::mojom::Blob> ChromeBlobStorageContext::GetBlobRemote(
BrowserContext* browser_context,
const std::string& uuid) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
mojo::PendingRemote<blink::mojom::Blob> blob_remote;
GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(
[](scoped_refptr<ChromeBlobStorageContext> context,
mojo::PendingReceiver<blink::mojom::Blob> receiver,
const std::string& uuid) {
auto handle = context->context()->GetBlobDataFromUUID(uuid);
if (handle)
storage::BlobImpl::Create(std::move(handle), std::move(receiver));
},
base::WrapRefCounted(GetFor(browser_context)),
blob_remote.InitWithNewPipeAndPassReceiver(), uuid));
return blob_remote;
}
ChromeBlobStorageContext::~ChromeBlobStorageContext() = default;
storage::BlobStorageContext* GetBlobStorageContext(
ChromeBlobStorageContext* blob_storage_context) {
if (!blob_storage_context)
return nullptr;
return blob_storage_context->context();
}
const char kBlobStorageContextKeyName[] = "content_blob_storage_context";
} // namespace content