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
3 // found in the LICENSE file.
5 #include "content/browser/fileapi/chrome_blob_storage_context.h"
9 #include "content/public/browser/blob_handle.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "storage/browser/blob/blob_data_builder.h"
13 #include "storage/browser/blob/blob_data_handle.h"
14 #include "storage/browser/blob/blob_storage_context.h"
16 using base::UserDataAdapter
;
17 using storage::BlobStorageContext
;
23 const char kBlobStorageContextKeyName
[] = "content_blob_storage_context";
25 class BlobHandleImpl
: public BlobHandle
{
27 explicit BlobHandleImpl(scoped_ptr
<storage::BlobDataHandle
> handle
)
28 : handle_(handle
.Pass()) {}
30 ~BlobHandleImpl() override
{}
32 std::string
GetUUID() override
{ return handle_
->uuid(); }
35 scoped_ptr
<storage::BlobDataHandle
> handle_
;
40 ChromeBlobStorageContext::ChromeBlobStorageContext() {}
42 ChromeBlobStorageContext
* ChromeBlobStorageContext::GetFor(
43 BrowserContext
* context
) {
44 if (!context
->GetUserData(kBlobStorageContextKeyName
)) {
45 scoped_refptr
<ChromeBlobStorageContext
> blob
=
46 new ChromeBlobStorageContext();
48 kBlobStorageContextKeyName
,
49 new UserDataAdapter
<ChromeBlobStorageContext
>(blob
.get()));
50 // Check first to avoid memory leak in unittests.
51 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO
)) {
52 BrowserThread::PostTask(
53 BrowserThread::IO
, FROM_HERE
,
54 base::Bind(&ChromeBlobStorageContext::InitializeOnIOThread
, blob
));
58 return UserDataAdapter
<ChromeBlobStorageContext
>::Get(
59 context
, kBlobStorageContextKeyName
);
62 void ChromeBlobStorageContext::InitializeOnIOThread() {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
64 context_
.reset(new BlobStorageContext());
67 scoped_ptr
<BlobHandle
> ChromeBlobStorageContext::CreateMemoryBackedBlob(
68 const char* data
, size_t length
) {
69 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
71 std::string
uuid(base::GenerateGUID());
72 storage::BlobDataBuilder
blob_data_builder(uuid
);
73 blob_data_builder
.AppendData(data
, length
);
75 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
=
76 context_
->AddFinishedBlob(&blob_data_builder
);
77 if (!blob_data_handle
)
78 return scoped_ptr
<BlobHandle
>();
80 scoped_ptr
<BlobHandle
> blob_handle(
81 new BlobHandleImpl(blob_data_handle
.Pass()));
82 return blob_handle
.Pass();
85 scoped_ptr
<BlobHandle
> ChromeBlobStorageContext::CreateFileBackedBlob(
86 const base::FilePath
& path
,
89 const base::Time
& expected_modification_time
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
92 std::string
uuid(base::GenerateGUID());
93 storage::BlobDataBuilder
blob_data_builder(uuid
);
94 blob_data_builder
.AppendFile(path
, offset
, size
, expected_modification_time
);
96 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
=
97 context_
->AddFinishedBlob(&blob_data_builder
);
98 if (!blob_data_handle
)
99 return scoped_ptr
<BlobHandle
>();
101 scoped_ptr
<BlobHandle
> blob_handle(
102 new BlobHandleImpl(blob_data_handle
.Pass()));
103 return blob_handle
.Pass();
106 ChromeBlobStorageContext::~ChromeBlobStorageContext() {}
108 void ChromeBlobStorageContext::DeleteOnCorrectThread() const {
109 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO
) &&
110 !BrowserThread::CurrentlyOn(BrowserThread::IO
)) {
111 BrowserThread::DeleteSoon(BrowserThread::IO
, FROM_HERE
, this);
117 } // namespace content