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" | |
jam
2013/02/27 02:46:14
why is this needed?
michaeln
2013/02/27 20:16:18
Done, its not.
| |
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::registerBlobURL( |
31 const WebURL& url, WebBlobData& data) { | 34 const WebURL& url, WebBlobData& data) { |
35 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); | |
32 const size_t kLargeThresholdBytes = 250 * 1024; | 36 const size_t kLargeThresholdBytes = 250 * 1024; |
33 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; | 37 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
34 | 38 |
35 child_thread_->Send(new BlobHostMsg_StartBuildingBlob(url)); | 39 sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
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 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
49 } else { | 53 } else { |
50 // We handle larger amounts of data via SharedMemory instead of | 54 // We handle larger amounts of data via SharedMemory instead of |
51 // writing it directly to the IPC channel. | 55 // writing it directly to the IPC channel. |
52 size_t data_size = data_item.data.size(); | 56 size_t data_size = data_item.data.size(); |
53 const char* data_ptr = data_item.data.data(); | 57 const char* data_ptr = data_item.data.data(); |
54 size_t shared_memory_size = std::min( | 58 size_t shared_memory_size = std::min( |
55 data_size, kMaxSharedMemoryBytes); | 59 data_size, kMaxSharedMemoryBytes); |
56 scoped_ptr<base::SharedMemory> shared_memory( | 60 scoped_ptr<base::SharedMemory> shared_memory( |
57 child_thread_->AllocateSharedMemory(shared_memory_size)); | 61 ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); |
58 CHECK(shared_memory.get()); | 62 CHECK(shared_memory.get()); |
59 while (data_size) { | 63 while (data_size) { |
60 size_t chunk_size = std::min(data_size, shared_memory_size); | 64 size_t chunk_size = std::min(data_size, shared_memory_size); |
61 memcpy(shared_memory->memory(), data_ptr, chunk_size); | 65 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
62 child_thread_->Send(new BlobHostMsg_SyncAppendSharedMemory( | 66 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
63 url, shared_memory->handle(), chunk_size)); | 67 url, shared_memory->handle(), chunk_size)); |
64 data_size -= chunk_size; | 68 data_size -= chunk_size; |
65 data_ptr += chunk_size; | 69 data_ptr += chunk_size; |
66 } | 70 } |
67 } | 71 } |
68 break; | 72 break; |
69 } | 73 } |
70 case WebBlobData::Item::TypeFile: | 74 case WebBlobData::Item::TypeFile: |
71 if (data_item.length) { | 75 if (data_item.length) { |
72 item.SetToFilePathRange( | 76 item.SetToFilePathRange( |
73 webkit_base::WebStringToFilePath(data_item.filePath), | 77 webkit_base::WebStringToFilePath(data_item.filePath), |
74 static_cast<uint64>(data_item.offset), | 78 static_cast<uint64>(data_item.offset), |
75 static_cast<uint64>(data_item.length), | 79 static_cast<uint64>(data_item.length), |
76 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 80 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
77 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 81 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
78 } | 82 } |
79 break; | 83 break; |
80 case WebBlobData::Item::TypeBlob: | 84 case WebBlobData::Item::TypeBlob: |
81 if (data_item.length) { | 85 if (data_item.length) { |
82 item.SetToBlobUrlRange( | 86 item.SetToBlobUrlRange( |
83 data_item.blobURL, | 87 data_item.blobURL, |
84 static_cast<uint64>(data_item.offset), | 88 static_cast<uint64>(data_item.offset), |
85 static_cast<uint64>(data_item.length)); | 89 static_cast<uint64>(data_item.length)); |
86 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 90 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
87 } | 91 } |
88 break; | 92 break; |
89 case WebBlobData::Item::TypeURL: | 93 case WebBlobData::Item::TypeURL: |
90 if (data_item.length) { | 94 if (data_item.length) { |
91 // We only support filesystem URL as of now. | 95 // We only support filesystem URL as of now. |
92 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | 96 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); |
93 item.SetToFileSystemUrlRange( | 97 item.SetToFileSystemUrlRange( |
94 data_item.url, | 98 data_item.url, |
95 static_cast<uint64>(data_item.offset), | 99 static_cast<uint64>(data_item.offset), |
96 static_cast<uint64>(data_item.length), | 100 static_cast<uint64>(data_item.length), |
97 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 101 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
98 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 102 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
99 } | 103 } |
100 break; | 104 break; |
101 default: | 105 default: |
102 NOTREACHED(); | 106 NOTREACHED(); |
103 } | 107 } |
104 } | 108 } |
105 child_thread_->Send(new BlobHostMsg_FinishBuildingBlob( | 109 sender_->Send(new BlobHostMsg_FinishBuildingBlob( |
106 url, data.contentType().utf8().data())); | 110 url, data.contentType().utf8().data())); |
107 } | 111 } |
108 | 112 |
109 void WebBlobRegistryImpl::registerBlobURL( | 113 void WebBlobRegistryImpl::registerBlobURL( |
110 const WebURL& url, const WebURL& src_url) { | 114 const WebURL& url, const WebURL& src_url) { |
111 child_thread_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | 115 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
116 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | |
112 } | 117 } |
113 | 118 |
114 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 119 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
115 child_thread_->Send(new BlobHostMsg_RemoveBlob(url)); | 120 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
121 sender_->Send(new BlobHostMsg_RemoveBlob(url)); | |
116 } | 122 } |
117 | 123 |
118 } // namespace content | 124 } // namespace content |
OLD | NEW |