OLD | NEW |
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/common/fileapi/webblobregistry_impl.h" | 5 #include "content/common/fileapi/webblobregistry_impl.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop.h" |
8 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
9 #include "content/common/child_thread.h" | 10 #include "content/common/child_thread.h" |
10 #include "content/common/fileapi/webblob_messages.h" | 11 #include "content/common/fileapi/webblob_messages.h" |
| 12 #include "content/common/thread_safe_sender.h" |
| 13 #include "ipc/ipc_sync_message_filter.h" |
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobData.h" | 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobData.h" |
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" | 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" |
14 #include "webkit/base/file_path_string_conversions.h" | 17 #include "webkit/base/file_path_string_conversions.h" |
15 #include "webkit/blob/blob_data.h" | 18 #include "webkit/blob/blob_data.h" |
16 | 19 |
17 using WebKit::WebBlobData; | 20 using WebKit::WebBlobData; |
18 using WebKit::WebString; | 21 using WebKit::WebString; |
19 using WebKit::WebURL; | 22 using WebKit::WebURL; |
20 | 23 |
21 namespace content { | 24 namespace content { |
22 | 25 |
23 WebBlobRegistryImpl::WebBlobRegistryImpl(ChildThread* child_thread) | 26 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) |
24 : child_thread_(child_thread) { | 27 : sender_(sender) { |
25 } | 28 } |
26 | 29 |
27 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 30 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
28 } | 31 } |
29 | 32 |
30 void WebBlobRegistryImpl::registerBlobURL( | 33 void WebBlobRegistryImpl::registerBlobData( |
31 const WebURL& url, WebBlobData& data) { | 34 const WebString& uuid, const WebBlobData& data) { |
32 const size_t kLargeThresholdBytes = 250 * 1024; | 35 const size_t kLargeThresholdBytes = 250 * 1024; |
33 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; | 36 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
34 | 37 |
35 child_thread_->Send(new BlobHostMsg_StartBuildingBlob(url)); | 38 const std::string uuid_str(uuid.utf8()); |
| 39 |
| 40 sender_->Send(new BlobHostMsg_StartBuildingBlob2(uuid_str)); |
36 size_t i = 0; | 41 size_t i = 0; |
37 WebBlobData::Item data_item; | 42 WebBlobData::Item data_item; |
38 while (data.itemAt(i++, data_item)) { | 43 while (data.itemAt(i++, data_item)) { |
| 44 webkit_blob::BlobData::Item item; |
| 45 switch (data_item.type) { |
| 46 case WebBlobData::Item::TypeData: { |
| 47 // WebBlobData does not allow partial data items. |
| 48 DCHECK(!data_item.offset && data_item.length == -1); |
| 49 if (data_item.data.size() == 0) |
| 50 break; |
| 51 if (data_item.data.size() < kLargeThresholdBytes) { |
| 52 item.SetToBytes(data_item.data.data(), data_item.data.size()); |
| 53 sender_->Send( |
| 54 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
| 55 } else { |
| 56 // We handle larger amounts of data via SharedMemory instead of |
| 57 // writing it directly to the IPC channel. |
| 58 size_t data_size = data_item.data.size(); |
| 59 const char* data_ptr = data_item.data.data(); |
| 60 size_t shared_memory_size = std::min( |
| 61 data_size, kMaxSharedMemoryBytes); |
| 62 scoped_ptr<base::SharedMemory> shared_memory( |
| 63 ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); |
| 64 CHECK(shared_memory.get()); |
| 65 while (data_size) { |
| 66 size_t chunk_size = std::min(data_size, shared_memory_size); |
| 67 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| 68 sender_->Send(new BlobHostMsg_AppendSharedMemory2( |
| 69 uuid_str, shared_memory->handle(), chunk_size)); |
| 70 data_size -= chunk_size; |
| 71 data_ptr += chunk_size; |
| 72 } |
| 73 } |
| 74 break; |
| 75 } |
| 76 case WebBlobData::Item::TypeFile: |
| 77 if (data_item.length) { |
| 78 item.SetToFilePathRange( |
| 79 webkit_base::WebStringToFilePath(data_item.filePath), |
| 80 static_cast<uint64>(data_item.offset), |
| 81 static_cast<uint64>(data_item.length), |
| 82 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 83 sender_->Send( |
| 84 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
| 85 } |
| 86 break; |
| 87 case WebBlobData::Item::TypeBlob: |
| 88 if (data_item.length) { |
| 89 item.SetToBlobRange( |
| 90 data_item.blobUUID.utf8(), |
| 91 static_cast<uint64>(data_item.offset), |
| 92 static_cast<uint64>(data_item.length)); |
| 93 sender_->Send( |
| 94 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
| 95 } |
| 96 break; |
| 97 case WebBlobData::Item::TypeFileSystemURL: |
| 98 if (data_item.length) { |
| 99 // We only support filesystem URL as of now. |
| 100 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); |
| 101 item.SetToFileSystemUrlRange( |
| 102 data_item.fileSystemURL, |
| 103 static_cast<uint64>(data_item.offset), |
| 104 static_cast<uint64>(data_item.length), |
| 105 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 106 sender_->Send( |
| 107 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
| 108 } |
| 109 break; |
| 110 default: |
| 111 NOTREACHED(); |
| 112 } |
| 113 } |
| 114 sender_->Send(new BlobHostMsg_FinishBuildingBlob2( |
| 115 uuid_str, data.contentType().utf8())); |
| 116 } |
| 117 |
| 118 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { |
| 119 sender_->Send(new BlobHostMsg_IncrementBlobRefCount(uuid.utf8())); |
| 120 } |
| 121 |
| 122 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { |
| 123 sender_->Send(new BlobHostMsg_DecrementBlobRefCount(uuid.utf8())); |
| 124 } |
| 125 |
| 126 void WebBlobRegistryImpl::registerPublicBlobURL( |
| 127 const WebURL& url, const WebString& uuid) { |
| 128 sender_->Send(new BlobHostMsg_RegisterPublicBlobURL(url, uuid.utf8())); |
| 129 } |
| 130 |
| 131 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { |
| 132 sender_->Send(new BlobHostMsg_RevokePublicBlobURL(url)); |
| 133 } |
| 134 |
| 135 |
| 136 // DEPRECATED |
| 137 /* |
| 138 void WebBlobRegistryImpl::registerBlobURL( |
| 139 const WebURL& url, WebBlobData& data) { |
| 140 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
| 141 const size_t kLargeThresholdBytes = 250 * 1024; |
| 142 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
| 143 |
| 144 sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
| 145 size_t i = 0; |
| 146 WebBlobData::Item data_item; |
| 147 while (data.itemAt(i++, data_item)) { |
39 webkit_blob::BlobData::Item item; | 148 webkit_blob::BlobData::Item item; |
40 switch (data_item.type) { | 149 switch (data_item.type) { |
41 case WebBlobData::Item::TypeData: { | 150 case WebBlobData::Item::TypeData: { |
42 // WebBlobData does not allow partial data items. | 151 // WebBlobData does not allow partial data items. |
43 DCHECK(!data_item.offset && data_item.length == -1); | 152 DCHECK(!data_item.offset && data_item.length == -1); |
44 if (data_item.data.size() == 0) | 153 if (data_item.data.size() == 0) |
45 break; | 154 break; |
46 if (data_item.data.size() < kLargeThresholdBytes) { | 155 if (data_item.data.size() < kLargeThresholdBytes) { |
47 item.SetToBytes(data_item.data.data(), data_item.data.size()); | 156 item.SetToBytes(data_item.data.data(), data_item.data.size()); |
48 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 157 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
49 } else { | 158 } else { |
50 // We handle larger amounts of data via SharedMemory instead of | 159 // We handle larger amounts of data via SharedMemory instead of |
51 // writing it directly to the IPC channel. | 160 // writing it directly to the IPC channel. |
52 size_t data_size = data_item.data.size(); | 161 size_t data_size = data_item.data.size(); |
53 const char* data_ptr = data_item.data.data(); | 162 const char* data_ptr = data_item.data.data(); |
54 size_t shared_memory_size = std::min( | 163 size_t shared_memory_size = std::min( |
55 data_size, kMaxSharedMemoryBytes); | 164 data_size, kMaxSharedMemoryBytes); |
56 scoped_ptr<base::SharedMemory> shared_memory( | 165 scoped_ptr<base::SharedMemory> shared_memory( |
57 child_thread_->AllocateSharedMemory(shared_memory_size)); | 166 ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); |
58 CHECK(shared_memory.get()); | 167 CHECK(shared_memory.get()); |
59 while (data_size) { | 168 while (data_size) { |
60 size_t chunk_size = std::min(data_size, shared_memory_size); | 169 size_t chunk_size = std::min(data_size, shared_memory_size); |
61 memcpy(shared_memory->memory(), data_ptr, chunk_size); | 170 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
62 child_thread_->Send(new BlobHostMsg_SyncAppendSharedMemory( | 171 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
63 url, shared_memory->handle(), chunk_size)); | 172 url, shared_memory->handle(), chunk_size)); |
64 data_size -= chunk_size; | 173 data_size -= chunk_size; |
65 data_ptr += chunk_size; | 174 data_ptr += chunk_size; |
66 } | 175 } |
67 } | 176 } |
68 break; | 177 break; |
69 } | 178 } |
70 case WebBlobData::Item::TypeFile: | 179 case WebBlobData::Item::TypeFile: |
71 if (data_item.length) { | 180 if (data_item.length) { |
72 item.SetToFilePathRange( | 181 item.SetToFilePathRange( |
73 webkit_base::WebStringToFilePath(data_item.filePath), | 182 webkit_base::WebStringToFilePath(data_item.filePath), |
74 static_cast<uint64>(data_item.offset), | 183 static_cast<uint64>(data_item.offset), |
75 static_cast<uint64>(data_item.length), | 184 static_cast<uint64>(data_item.length), |
76 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 185 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
77 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 186 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
78 } | 187 } |
79 break; | 188 break; |
80 case WebBlobData::Item::TypeBlob: | 189 case WebBlobData::Item::TypeBlob: |
81 if (data_item.length) { | 190 if (data_item.length) { |
82 item.SetToBlobUrlRange( | 191 item.SetToBlobUrlRange( |
83 data_item.blobURL, | 192 data_item.blobURL, |
84 static_cast<uint64>(data_item.offset), | 193 static_cast<uint64>(data_item.offset), |
85 static_cast<uint64>(data_item.length)); | 194 static_cast<uint64>(data_item.length)); |
86 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 195 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
87 } | 196 } |
88 break; | 197 break; |
89 case WebBlobData::Item::TypeURL: | 198 case WebBlobData::Item::TypeURL: |
90 if (data_item.length) { | 199 if (data_item.length) { |
91 // We only support filesystem URL as of now. | 200 // We only support filesystem URL as of now. |
92 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | 201 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); |
93 item.SetToFileSystemUrlRange( | 202 item.SetToFileSystemUrlRange( |
94 data_item.url, | 203 data_item.url, |
95 static_cast<uint64>(data_item.offset), | 204 static_cast<uint64>(data_item.offset), |
96 static_cast<uint64>(data_item.length), | 205 static_cast<uint64>(data_item.length), |
97 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 206 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
98 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 207 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
99 } | 208 } |
100 break; | 209 break; |
101 default: | 210 default: |
102 NOTREACHED(); | 211 NOTREACHED(); |
103 } | 212 } |
104 } | 213 } |
105 child_thread_->Send(new BlobHostMsg_FinishBuildingBlob( | 214 sender_->Send(new BlobHostMsg_FinishBuildingBlob( |
106 url, data.contentType().utf8().data())); | 215 url, data.contentType().utf8().data())); |
107 } | 216 } |
108 | 217 |
109 void WebBlobRegistryImpl::registerBlobURL( | 218 void WebBlobRegistryImpl::registerBlobURL( |
110 const WebURL& url, const WebURL& src_url) { | 219 const WebURL& url, const WebURL& src_url) { |
111 child_thread_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | 220 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
| 221 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); |
112 } | 222 } |
113 | 223 |
114 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 224 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
115 child_thread_->Send(new BlobHostMsg_RemoveBlob(url)); | 225 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
| 226 sender_->Send(new BlobHostMsg_RemoveBlob(url)); |
116 } | 227 } |
| 228 */ |
117 | 229 |
118 } // namespace content | 230 } // namespace content |
OLD | NEW |