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" |
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobData.h" | 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobData.h" |
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/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(ThreadSafeSender* sender) |
24 : child_thread_(child_thread) { | 26 : sender_(sender) { |
25 } | 27 } |
26 | 28 |
27 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 29 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
28 } | 30 } |
29 | 31 |
30 void WebBlobRegistryImpl::registerBlobURL( | 32 void WebBlobRegistryImpl::registerBlobURL( |
31 const WebURL& url, WebBlobData& data) { | 33 const WebURL& url, WebBlobData& data) { |
| 34 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
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 sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
36 size_t i = 0; | 39 size_t i = 0; |
37 WebBlobData::Item data_item; | 40 WebBlobData::Item data_item; |
38 while (data.itemAt(i++, data_item)) { | 41 while (data.itemAt(i++, data_item)) { |
39 webkit_blob::BlobData::Item item; | 42 webkit_blob::BlobData::Item item; |
40 switch (data_item.type) { | 43 switch (data_item.type) { |
41 case WebBlobData::Item::TypeData: { | 44 case WebBlobData::Item::TypeData: { |
42 // WebBlobData does not allow partial data items. | 45 // WebBlobData does not allow partial data items. |
43 DCHECK(!data_item.offset && data_item.length == -1); | 46 DCHECK(!data_item.offset && data_item.length == -1); |
44 if (data_item.data.size() == 0) | 47 if (data_item.data.size() == 0) |
45 break; | 48 break; |
46 if (data_item.data.size() < kLargeThresholdBytes) { | 49 if (data_item.data.size() < kLargeThresholdBytes) { |
47 item.SetToBytes(data_item.data.data(), data_item.data.size()); | 50 item.SetToBytes(data_item.data.data(), data_item.data.size()); |
48 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 51 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
49 } else { | 52 } else { |
50 // We handle larger amounts of data via SharedMemory instead of | 53 // We handle larger amounts of data via SharedMemory instead of |
51 // writing it directly to the IPC channel. | 54 // writing it directly to the IPC channel. |
52 size_t data_size = data_item.data.size(); | 55 size_t data_size = data_item.data.size(); |
53 const char* data_ptr = data_item.data.data(); | 56 const char* data_ptr = data_item.data.data(); |
54 size_t shared_memory_size = std::min( | 57 size_t shared_memory_size = std::min( |
55 data_size, kMaxSharedMemoryBytes); | 58 data_size, kMaxSharedMemoryBytes); |
56 scoped_ptr<base::SharedMemory> shared_memory( | 59 scoped_ptr<base::SharedMemory> shared_memory( |
57 child_thread_->AllocateSharedMemory(shared_memory_size)); | 60 ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); |
58 CHECK(shared_memory.get()); | 61 CHECK(shared_memory.get()); |
59 while (data_size) { | 62 while (data_size) { |
60 size_t chunk_size = std::min(data_size, shared_memory_size); | 63 size_t chunk_size = std::min(data_size, shared_memory_size); |
61 memcpy(shared_memory->memory(), data_ptr, chunk_size); | 64 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
62 child_thread_->Send(new BlobHostMsg_SyncAppendSharedMemory( | 65 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
63 url, shared_memory->handle(), chunk_size)); | 66 url, shared_memory->handle(), chunk_size)); |
64 data_size -= chunk_size; | 67 data_size -= chunk_size; |
65 data_ptr += chunk_size; | 68 data_ptr += chunk_size; |
66 } | 69 } |
67 } | 70 } |
68 break; | 71 break; |
69 } | 72 } |
70 case WebBlobData::Item::TypeFile: | 73 case WebBlobData::Item::TypeFile: |
71 if (data_item.length) { | 74 if (data_item.length) { |
72 item.SetToFilePathRange( | 75 item.SetToFilePathRange( |
73 webkit_base::WebStringToFilePath(data_item.filePath), | 76 webkit_base::WebStringToFilePath(data_item.filePath), |
74 static_cast<uint64>(data_item.offset), | 77 static_cast<uint64>(data_item.offset), |
75 static_cast<uint64>(data_item.length), | 78 static_cast<uint64>(data_item.length), |
76 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 79 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
77 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 80 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
78 } | 81 } |
79 break; | 82 break; |
80 case WebBlobData::Item::TypeBlob: | 83 case WebBlobData::Item::TypeBlob: |
81 if (data_item.length) { | 84 if (data_item.length) { |
82 item.SetToBlobUrlRange( | 85 item.SetToBlobUrlRange( |
83 data_item.blobURL, | 86 data_item.blobURL, |
84 static_cast<uint64>(data_item.offset), | 87 static_cast<uint64>(data_item.offset), |
85 static_cast<uint64>(data_item.length)); | 88 static_cast<uint64>(data_item.length)); |
86 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 89 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
87 } | 90 } |
88 break; | 91 break; |
89 case WebBlobData::Item::TypeURL: | 92 case WebBlobData::Item::TypeURL: |
90 if (data_item.length) { | 93 if (data_item.length) { |
91 // We only support filesystem URL as of now. | 94 // We only support filesystem URL as of now. |
92 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | 95 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); |
93 item.SetToFileSystemUrlRange( | 96 item.SetToFileSystemUrlRange( |
94 data_item.url, | 97 data_item.url, |
95 static_cast<uint64>(data_item.offset), | 98 static_cast<uint64>(data_item.offset), |
96 static_cast<uint64>(data_item.length), | 99 static_cast<uint64>(data_item.length), |
97 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 100 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
98 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | 101 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
99 } | 102 } |
100 break; | 103 break; |
101 default: | 104 default: |
102 NOTREACHED(); | 105 NOTREACHED(); |
103 } | 106 } |
104 } | 107 } |
105 child_thread_->Send(new BlobHostMsg_FinishBuildingBlob( | 108 sender_->Send(new BlobHostMsg_FinishBuildingBlob( |
106 url, data.contentType().utf8().data())); | 109 url, data.contentType().utf8().data())); |
107 } | 110 } |
108 | 111 |
109 void WebBlobRegistryImpl::registerBlobURL( | 112 void WebBlobRegistryImpl::registerBlobURL( |
110 const WebURL& url, const WebURL& src_url) { | 113 const WebURL& url, const WebURL& src_url) { |
111 child_thread_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | 114 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
| 115 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); |
112 } | 116 } |
113 | 117 |
114 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 118 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
115 child_thread_->Send(new BlobHostMsg_RemoveBlob(url)); | 119 DCHECK(ChildThread::current()->message_loop() == MessageLoop::current()); |
| 120 sender_->Send(new BlobHostMsg_RemoveBlob(url)); |
116 } | 121 } |
117 | 122 |
118 } // namespace content | 123 } // namespace content |
OLD | NEW |