1 // Copyright (c) 2011 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/common/webblobregistry_impl.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/shared_memory.h"
9 #include "content/common/child_thread.h"
10 #include "content/common/webblob_messages.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebBlobData.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
14 #include "webkit/blob/blob_data.h"
15 #include "webkit/glue/webkit_glue.h"
17 using WebKit::WebBlobData
;
18 using WebKit::WebString
;
21 WebBlobRegistryImpl::WebBlobRegistryImpl(ChildThread
* child_thread
)
22 : child_thread_(child_thread
) {
25 WebBlobRegistryImpl::~WebBlobRegistryImpl() {
28 void WebBlobRegistryImpl::registerBlobURL(
29 const WebURL
& url
, WebBlobData
& data
) {
30 const size_t kLargeThresholdBytes
= 250 * 1024;
31 const size_t kMaxSharedMemoryBytes
= 10 * 1024 * 1024;
33 child_thread_
->Send(new BlobHostMsg_StartBuildingBlob(url
));
35 WebBlobData::Item data_item
;
36 while (data
.itemAt(i
++, data_item
)) {
37 webkit_blob::BlobData::Item item
;
38 switch (data_item
.type
) {
39 case WebBlobData::Item::TypeData
: {
40 // WebBlobData does not allow partial data items.
41 DCHECK(!data_item
.offset
&& data_item
.length
== -1);
42 if (data_item
.data
.size() < kLargeThresholdBytes
) {
43 item
.SetToData(data_item
.data
.data(), data_item
.data
.size());
44 child_thread_
->Send(new BlobHostMsg_AppendBlobDataItem(url
, item
));
46 // We handle larger amounts of data via SharedMemory instead of
47 // writing it directly to the IPC channel.
48 size_t data_size
= data_item
.data
.size();
49 const char* data_ptr
= data_item
.data
.data();
50 size_t shared_memory_size
= std::min(
51 data_size
, kMaxSharedMemoryBytes
);
52 scoped_ptr
<base::SharedMemory
> shared_memory(
53 child_thread_
->AllocateSharedMemory(shared_memory_size
));
54 CHECK(shared_memory
.get());
56 size_t chunk_size
= std::min(data_size
, shared_memory_size
);
57 memcpy(shared_memory
->memory(), data_ptr
, chunk_size
);
58 child_thread_
->Send(new BlobHostMsg_SyncAppendSharedMemory(
59 url
, shared_memory
->handle(), chunk_size
));
60 data_size
-= chunk_size
;
61 data_ptr
+= chunk_size
;
66 case WebBlobData::Item::TypeFile
:
68 webkit_glue::WebStringToFilePath(data_item
.filePath
),
69 static_cast<uint64
>(data_item
.offset
),
70 static_cast<uint64
>(data_item
.length
),
71 base::Time::FromDoubleT(data_item
.expectedModificationTime
));
72 child_thread_
->Send(new BlobHostMsg_AppendBlobDataItem(url
, item
));
74 case WebBlobData::Item::TypeBlob
:
75 if (data_item
.length
) {
78 static_cast<uint64
>(data_item
.offset
),
79 static_cast<uint64
>(data_item
.length
));
81 child_thread_
->Send(new BlobHostMsg_AppendBlobDataItem(url
, item
));
87 child_thread_
->Send(new BlobHostMsg_FinishBuildingBlob(
88 url
, data
.contentType().utf8().data()));
91 void WebBlobRegistryImpl::registerBlobURL(
92 const WebURL
& url
, const WebURL
& src_url
) {
93 child_thread_
->Send(new BlobHostMsg_CloneBlob(url
, src_url
));
96 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL
& url
) {
97 child_thread_
->Send(new BlobHostMsg_RemoveBlob(url
));