blob: d711c743a6e11a589b15c18bca3525c8087d53f7 [file] [log] [blame]
[email protected]16dd6e22012-03-01 19:08:201// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]22339b12010-08-27 18:29:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b76b556d2013-05-29 03:09:435#include "content/common_child/webblobregistry_impl.h"
[email protected]22339b12010-08-27 18:29:246
[email protected]3b63f8f42011-03-28 01:54:157#include "base/memory/ref_counted.h"
[email protected]b180a0bb2013-03-06 00:36:108#include "base/message_loop.h"
[email protected]a3c71e82011-10-17 23:00:519#include "base/shared_memory.h"
10#include "content/common/child_thread.h"
[email protected]16dd6e22012-03-01 19:08:2011#include "content/common/fileapi/webblob_messages.h"
[email protected]b180a0bb2013-03-06 00:36:1012#include "content/common/thread_safe_sender.h"
[email protected]5c30b5e02013-05-30 03:46:0813#include "third_party/WebKit/public/platform/WebBlobData.h"
14#include "third_party/WebKit/public/platform/WebString.h"
15#include "third_party/WebKit/public/platform/WebURL.h"
[email protected]bb0e79472012-10-23 04:36:3416#include "webkit/base/file_path_string_conversions.h"
[email protected]b76b556d2013-05-29 03:09:4317#include "webkit/common/blob/blob_data.h"
[email protected]22339b12010-08-27 18:29:2418
19using WebKit::WebBlobData;
[email protected]22339b12010-08-27 18:29:2420using WebKit::WebString;
21using WebKit::WebURL;
22
[email protected]eb398192012-10-22 20:16:1923namespace content {
24
[email protected]b180a0bb2013-03-06 00:36:1025WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender)
26 : sender_(sender) {
[email protected]22339b12010-08-27 18:29:2427}
28
29WebBlobRegistryImpl::~WebBlobRegistryImpl() {
30}
31
32void WebBlobRegistryImpl::registerBlobURL(
33 const WebURL& url, WebBlobData& data) {
[email protected]dd32b1272013-05-04 14:17:1134 DCHECK(ChildThread::current()->message_loop() ==
35 base::MessageLoop::current());
[email protected]a3c71e82011-10-17 23:00:5136 const size_t kLargeThresholdBytes = 250 * 1024;
37 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024;
38
[email protected]b180a0bb2013-03-06 00:36:1039 sender_->Send(new BlobHostMsg_StartBuildingBlob(url));
[email protected]a3c71e82011-10-17 23:00:5140 size_t i = 0;
41 WebBlobData::Item data_item;
42 while (data.itemAt(i++, data_item)) {
43 webkit_blob::BlobData::Item item;
44 switch (data_item.type) {
45 case WebBlobData::Item::TypeData: {
46 // WebBlobData does not allow partial data items.
47 DCHECK(!data_item.offset && data_item.length == -1);
[email protected]40c889e2012-05-24 15:40:1248 if (data_item.data.size() == 0)
49 break;
[email protected]a3c71e82011-10-17 23:00:5150 if (data_item.data.size() < kLargeThresholdBytes) {
[email protected]9ff2b2292012-08-23 14:32:4351 item.SetToBytes(data_item.data.data(), data_item.data.size());
[email protected]b180a0bb2013-03-06 00:36:1052 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item));
[email protected]a3c71e82011-10-17 23:00:5153 } else {
54 // We handle larger amounts of data via SharedMemory instead of
55 // writing it directly to the IPC channel.
56 size_t data_size = data_item.data.size();
57 const char* data_ptr = data_item.data.data();
58 size_t shared_memory_size = std::min(
59 data_size, kMaxSharedMemoryBytes);
60 scoped_ptr<base::SharedMemory> shared_memory(
[email protected]b180a0bb2013-03-06 00:36:1061 ChildThread::AllocateSharedMemory(shared_memory_size, sender_));
[email protected]a3c71e82011-10-17 23:00:5162 CHECK(shared_memory.get());
63 while (data_size) {
64 size_t chunk_size = std::min(data_size, shared_memory_size);
65 memcpy(shared_memory->memory(), data_ptr, chunk_size);
[email protected]b180a0bb2013-03-06 00:36:1066 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory(
[email protected]a3c71e82011-10-17 23:00:5167 url, shared_memory->handle(), chunk_size));
68 data_size -= chunk_size;
69 data_ptr += chunk_size;
70 }
71 }
72 break;
73 }
74 case WebBlobData::Item::TypeFile:
[email protected]40c889e2012-05-24 15:40:1275 if (data_item.length) {
[email protected]9ff2b2292012-08-23 14:32:4376 item.SetToFilePathRange(
[email protected]bb0e79472012-10-23 04:36:3477 webkit_base::WebStringToFilePath(data_item.filePath),
[email protected]40c889e2012-05-24 15:40:1278 static_cast<uint64>(data_item.offset),
79 static_cast<uint64>(data_item.length),
80 base::Time::FromDoubleT(data_item.expectedModificationTime));
[email protected]b180a0bb2013-03-06 00:36:1081 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item));
[email protected]40c889e2012-05-24 15:40:1282 }
[email protected]a3c71e82011-10-17 23:00:5183 break;
84 case WebBlobData::Item::TypeBlob:
85 if (data_item.length) {
[email protected]9ff2b2292012-08-23 14:32:4386 item.SetToBlobUrlRange(
[email protected]a3c71e82011-10-17 23:00:5187 data_item.blobURL,
88 static_cast<uint64>(data_item.offset),
89 static_cast<uint64>(data_item.length));
[email protected]b180a0bb2013-03-06 00:36:1090 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item));
[email protected]a3c71e82011-10-17 23:00:5191 }
[email protected]a3c71e82011-10-17 23:00:5192 break;
[email protected]2cd21072012-08-28 10:19:4593 case WebBlobData::Item::TypeURL:
94 if (data_item.length) {
95 // We only support filesystem URL as of now.
96 DCHECK(GURL(data_item.url).SchemeIsFileSystem());
97 item.SetToFileSystemUrlRange(
98 data_item.url,
99 static_cast<uint64>(data_item.offset),
100 static_cast<uint64>(data_item.length),
101 base::Time::FromDoubleT(data_item.expectedModificationTime));
[email protected]b180a0bb2013-03-06 00:36:10102 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item));
[email protected]2cd21072012-08-28 10:19:45103 }
104 break;
[email protected]a3c71e82011-10-17 23:00:51105 default:
106 NOTREACHED();
107 }
108 }
[email protected]b180a0bb2013-03-06 00:36:10109 sender_->Send(new BlobHostMsg_FinishBuildingBlob(
[email protected]a3c71e82011-10-17 23:00:51110 url, data.contentType().utf8().data()));
[email protected]22339b12010-08-27 18:29:24111}
112
113void WebBlobRegistryImpl::registerBlobURL(
114 const WebURL& url, const WebURL& src_url) {
[email protected]dd32b1272013-05-04 14:17:11115 DCHECK(ChildThread::current()->message_loop() ==
116 base::MessageLoop::current());
[email protected]b180a0bb2013-03-06 00:36:10117 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url));
[email protected]22339b12010-08-27 18:29:24118}
119
120void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) {
[email protected]dd32b1272013-05-04 14:17:11121 DCHECK(ChildThread::current()->message_loop() ==
122 base::MessageLoop::current());
[email protected]b180a0bb2013-03-06 00:36:10123 sender_->Send(new BlobHostMsg_RemoveBlob(url));
[email protected]22339b12010-08-27 18:29:24124}
[email protected]eb398192012-10-22 20:16:19125
126} // namespace content