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 "ipc/ipc_sync_message_filter.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebBlobData.
h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebBlobData.
h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
14 #include "webkit/base/file_path_string_conversions.h" | 16 #include "webkit/base/file_path_string_conversions.h" |
15 #include "webkit/blob/blob_data.h" | 17 #include "webkit/blob/blob_data.h" |
16 | 18 |
17 using WebKit::WebBlobData; | 19 using WebKit::WebBlobData; |
18 using WebKit::WebString; | 20 using WebKit::WebString; |
19 using WebKit::WebURL; | 21 using WebKit::WebURL; |
20 | 22 |
21 namespace content { | 23 namespace content { |
22 | 24 |
23 WebBlobRegistryImpl::WebBlobRegistryImpl(ChildThread* child_thread) | 25 WebBlobRegistryImpl::WebBlobRegistryImpl(ChildThread* child_thread) |
24 : child_thread_(child_thread) { | 26 : child_thread_(child_thread) { |
25 } | 27 } |
26 | 28 |
27 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 29 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
28 } | 30 } |
29 | 31 |
30 void WebBlobRegistryImpl::registerBlobURL( | 32 void WebBlobRegistryImpl::registerBlobData( |
31 const WebURL& url, WebBlobData& data) { | 33 const WebString& uuid, const WebBlobData& data) { |
32 const size_t kLargeThresholdBytes = 250 * 1024; | 34 const size_t kLargeThresholdBytes = 250 * 1024; |
33 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; | 35 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
34 | 36 |
35 child_thread_->Send(new BlobHostMsg_StartBuildingBlob(url)); | 37 const std::string uuid_str(uuid.utf8()); |
| 38 |
| 39 SendFromAnyThread(new BlobHostMsg_StartBuildingBlob2(uuid_str)); |
36 size_t i = 0; | 40 size_t i = 0; |
37 WebBlobData::Item data_item; | 41 WebBlobData::Item data_item; |
38 while (data.itemAt(i++, data_item)) { | 42 while (data.itemAt(i++, data_item)) { |
39 webkit_blob::BlobData::Item item; | 43 webkit_blob::BlobData::Item item; |
40 switch (data_item.type) { | 44 switch (data_item.type) { |
41 case WebBlobData::Item::TypeData: { | 45 case WebBlobData::Item::TypeData: { |
42 // WebBlobData does not allow partial data items. | 46 // WebBlobData does not allow partial data items. |
43 DCHECK(!data_item.offset && data_item.length == -1); | 47 DCHECK(!data_item.offset && data_item.length == -1); |
44 if (data_item.data.size() == 0) | 48 if (data_item.data.size() == 0) |
45 break; | 49 break; |
46 if (data_item.data.size() < kLargeThresholdBytes) { | 50 if (data_item.data.size() < kLargeThresholdBytes) { |
47 item.SetToBytes(data_item.data.data(), data_item.data.size()); | 51 item.SetToBytes(data_item.data.data(), data_item.data.size()); |
48 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 52 SendFromAnyThread( |
| 53 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
49 } else { | 54 } else { |
50 // We handle larger amounts of data via SharedMemory instead of | 55 // We handle larger amounts of data via SharedMemory instead of |
51 // writing it directly to the IPC channel. | 56 // writing it directly to the IPC channel. |
52 size_t data_size = data_item.data.size(); | 57 size_t data_size = data_item.data.size(); |
53 const char* data_ptr = data_item.data.data(); | 58 const char* data_ptr = data_item.data.data(); |
54 size_t shared_memory_size = std::min( | 59 size_t shared_memory_size = std::min( |
55 data_size, kMaxSharedMemoryBytes); | 60 data_size, kMaxSharedMemoryBytes); |
56 scoped_ptr<base::SharedMemory> shared_memory( | 61 scoped_ptr<base::SharedMemory> shared_memory( |
57 child_thread_->AllocateSharedMemory(shared_memory_size)); | 62 child_thread_->AllocateSharedMemory(shared_memory_size)); |
58 CHECK(shared_memory.get()); | 63 CHECK(shared_memory.get()); |
59 while (data_size) { | 64 while (data_size) { |
60 size_t chunk_size = std::min(data_size, shared_memory_size); | 65 size_t chunk_size = std::min(data_size, shared_memory_size); |
61 memcpy(shared_memory->memory(), data_ptr, chunk_size); | 66 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
62 child_thread_->Send(new BlobHostMsg_SyncAppendSharedMemory( | 67 SendFromAnyThread(new BlobHostMsg_AppendSharedMemory2( |
63 url, shared_memory->handle(), chunk_size)); | 68 uuid_str, shared_memory->handle(), chunk_size)); |
64 data_size -= chunk_size; | 69 data_size -= chunk_size; |
65 data_ptr += chunk_size; | 70 data_ptr += chunk_size; |
66 } | 71 } |
67 } | 72 } |
68 break; | 73 break; |
69 } | 74 } |
70 case WebBlobData::Item::TypeFile: | 75 case WebBlobData::Item::TypeFile: |
71 if (data_item.length) { | 76 if (data_item.length) { |
72 item.SetToFilePathRange( | 77 item.SetToFilePathRange( |
73 webkit_base::WebStringToFilePath(data_item.filePath), | 78 webkit_base::WebStringToFilePath(data_item.filePath), |
74 static_cast<uint64>(data_item.offset), | 79 static_cast<uint64>(data_item.offset), |
75 static_cast<uint64>(data_item.length), | 80 static_cast<uint64>(data_item.length), |
76 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 81 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
77 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 82 SendFromAnyThread( |
| 83 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
78 } | 84 } |
79 break; | 85 break; |
80 case WebBlobData::Item::TypeBlob: | 86 case WebBlobData::Item::TypeBlob: |
81 if (data_item.length) { | 87 if (data_item.length) { |
82 item.SetToBlobUrlRange( | 88 item.SetToBlobRange( |
83 data_item.blobURL, | 89 data_item.blobUUID.utf8(), |
84 static_cast<uint64>(data_item.offset), | 90 static_cast<uint64>(data_item.offset), |
85 static_cast<uint64>(data_item.length)); | 91 static_cast<uint64>(data_item.length)); |
86 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 92 SendFromAnyThread( |
| 93 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
87 } | 94 } |
88 break; | 95 break; |
89 case WebBlobData::Item::TypeURL: | 96 case WebBlobData::Item::TypeFileSystemURL: |
90 if (data_item.length) { | 97 if (data_item.length) { |
91 // We only support filesystem URL as of now. | 98 // We only support filesystem URL as of now. |
92 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | 99 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); |
93 item.SetToFileSystemUrlRange( | 100 item.SetToFileSystemUrlRange( |
94 data_item.url, | 101 data_item.fileSystemURL, |
95 static_cast<uint64>(data_item.offset), | 102 static_cast<uint64>(data_item.offset), |
96 static_cast<uint64>(data_item.length), | 103 static_cast<uint64>(data_item.length), |
97 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 104 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
98 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 105 SendFromAnyThread( |
| 106 new BlobHostMsg_AppendBlobDataItem2(uuid_str, item)); |
99 } | 107 } |
100 break; | 108 break; |
101 default: | 109 default: |
102 NOTREACHED(); | 110 NOTREACHED(); |
103 } | 111 } |
104 } | 112 } |
105 child_thread_->Send(new BlobHostMsg_FinishBuildingBlob( | 113 SendFromAnyThread(new BlobHostMsg_FinishBuildingBlob2( |
106 url, data.contentType().utf8().data())); | 114 uuid_str, data.contentType().utf8())); |
107 } | 115 } |
108 | 116 |
109 void WebBlobRegistryImpl::registerBlobURL( | 117 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { |
110 const WebURL& url, const WebURL& src_url) { | 118 SendFromAnyThread(new BlobHostMsg_IncrementBlobRefCount(uuid.utf8())); |
111 child_thread_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | |
112 } | 119 } |
113 | 120 |
114 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 121 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { |
115 child_thread_->Send(new BlobHostMsg_RemoveBlob(url)); | 122 SendFromAnyThread(new BlobHostMsg_DecrementBlobRefCount(uuid.utf8())); |
| 123 } |
| 124 |
| 125 void WebBlobRegistryImpl::registerPublicBlobURL( |
| 126 const WebURL& url, const WebString& uuid) { |
| 127 SendFromAnyThread(new BlobHostMsg_RegisterPublicBlobURL(url, uuid.utf8())); |
| 128 } |
| 129 |
| 130 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { |
| 131 SendFromAnyThread(new BlobHostMsg_RevokePublicBlobURL(url)); |
| 132 } |
| 133 |
| 134 void WebBlobRegistryImpl::SendFromAnyThread(IPC::Message* msg) { |
| 135 ChildThread* child_thread = ChildThread::current(); |
| 136 if (child_thread->message_loop() == MessageLoop::current()) |
| 137 child_thread->Send(msg); |
| 138 else |
| 139 child_thread->sync_message_filter()->Send(msg); |
116 } | 140 } |
117 | 141 |
118 } // namespace content | 142 } // namespace content |
OLD | NEW |