Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/child/webparserresourcebridge_impl.h" | |
| 6 | |
| 7 #include "content/child/child_process.h" | |
| 8 #include "content/child/child_thread.h" | |
| 9 #include "content/child/resource_dispatcher.h" | |
| 10 #include "content/child/thread_safe_sender.h" | |
| 11 #include "content/common/resource_messages.h" | |
| 12 #include "ipc/ipc_sync_channel.h" | |
| 13 #include "third_party/WebKit/public/platform/WebThread.h" | |
| 14 #include "webkit/child/webthread_impl.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class ParserResourceMessageFilter : public IPC::ChannelProxy::MessageFilter { | |
| 21 public: | |
| 22 ParserResourceMessageFilter( | |
| 23 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
| 24 base::MessageLoop* main_thread_message_loop, | |
| 25 base::WeakPtr<ResourceDispatcher> resource_dispatcher, | |
| 26 webkit_glue::WebThreadImpl& parser_thread, | |
| 27 base::WeakPtr<WebParserResourceBridgeImpl> parser_bridge, | |
| 28 int request_id); | |
| 29 | |
| 30 // IPC::ChannelProxy::MessageFilter | |
| 31 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE; | |
| 32 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 33 | |
| 34 private: | |
| 35 virtual ~ParserResourceMessageFilter(); | |
| 36 | |
| 37 void OnReceivedData(int request_id, int data_offset, int data_length, | |
| 38 int encoded_data_length); | |
| 39 | |
| 40 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; | |
| 41 base::MessageLoop* main_thread_message_loop_; | |
| 42 // This weakptr can only be dereferenced on the main thread. | |
| 43 base::WeakPtr<ResourceDispatcher> resource_dispatcher_; | |
| 44 webkit_glue::WebThreadImpl& parser_thread_; | |
| 45 // This weakptr can only be dereferenced on the parser thread. | |
| 46 base::WeakPtr<WebParserResourceBridgeImpl> parser_bridge_; | |
| 47 int request_id_; | |
| 48 }; | |
| 49 | |
| 50 ParserResourceMessageFilter::ParserResourceMessageFilter( | |
| 51 const scoped_refptr<base::MessageLoopProxy>& io_message_loop, | |
| 52 base::MessageLoop* main_thread_message_loop, | |
| 53 base::WeakPtr<ResourceDispatcher> resource_dispatcher, | |
| 54 webkit_glue::WebThreadImpl& parser_thread, | |
| 55 base::WeakPtr<WebParserResourceBridgeImpl> parser_bridge, | |
| 56 int request_id) | |
| 57 : io_message_loop_(io_message_loop) | |
| 58 , main_thread_message_loop_(main_thread_message_loop) | |
|
jam
2013/12/17 00:44:40
nit: google style is comma on previous lines
oystein (OOO til 10th of July)
2013/12/17 01:07:27
Done.
| |
| 59 , resource_dispatcher_(resource_dispatcher) | |
| 60 , parser_thread_(parser_thread) | |
| 61 , parser_bridge_(parser_bridge) | |
| 62 , request_id_(request_id) { | |
| 63 DCHECK(main_thread_message_loop != NULL); | |
| 64 } | |
| 65 | |
| 66 void ParserResourceMessageFilter::OnFilterAdded(IPC::Channel* channel) { | |
| 67 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
| 68 | |
| 69 main_thread_message_loop_->PostTask(FROM_HERE, | |
| 70 base::Bind( | |
| 71 &ResourceDispatcher::OnParserResourceMessageFilterAdded, | |
| 72 resource_dispatcher_, | |
| 73 request_id_)); | |
| 74 } | |
| 75 | |
| 76 bool ParserResourceMessageFilter::OnMessageReceived( | |
| 77 const IPC::Message& message) { | |
| 78 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
| 79 | |
| 80 if (message.type() != ResourceMsg_DataReceived::ID) | |
| 81 return false; | |
| 82 | |
| 83 int request_id; | |
| 84 | |
| 85 PickleIterator iter(message); | |
| 86 if (!message.ReadInt(&iter, &request_id)) { | |
| 87 NOTREACHED() << "malformed resource message"; | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 if (request_id == request_id_) { | |
| 92 ResourceMsg_DataReceived::Schema::Param arg; | |
| 93 if (ResourceMsg_DataReceived::Read(&message, &arg)) | |
| 94 OnReceivedData(arg.a, arg.b, arg.c, arg.d); | |
| 95 } | |
| 96 | |
| 97 // Even if the parser thread wants the message (i.e. the request ID matches), | |
| 98 // the main thread currently still expects to get it as well. Each chunk will | |
| 99 // still only be parsed once. | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 ParserResourceMessageFilter::~ParserResourceMessageFilter() { | |
| 104 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
| 105 } | |
| 106 | |
| 107 void ParserResourceMessageFilter::OnReceivedData(int request_id, | |
| 108 int data_offset, | |
| 109 int data_length, | |
| 110 int encoded_data_length) { | |
| 111 DCHECK(io_message_loop_->BelongsToCurrentThread()); | |
| 112 parser_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | |
| 113 &WebParserResourceBridgeImpl::OnReceivedData, | |
| 114 parser_bridge_, | |
| 115 data_offset, data_length, encoded_data_length)); | |
| 116 } | |
| 117 | |
| 118 } // anonymous namespace | |
| 119 | |
| 120 webkit_glue::WebThreadImpl& WebParserResourceBridgeImpl::parser_thread() { | |
| 121 static webkit_glue::WebThreadImpl* thread = NULL; | |
| 122 // Do we care about this leaking on shutdown? | |
|
jam
2013/12/17 00:44:40
no
oystein (OOO til 10th of July)
2013/12/17 01:07:27
Done.
| |
| 123 if (thread == NULL) | |
| 124 thread = new webkit_glue::WebThreadImpl("HTMLParserThread"); | |
| 125 | |
| 126 return *thread; | |
| 127 } | |
| 128 | |
| 129 WebParserResourceBridgeImpl::WebParserResourceBridgeImpl( | |
| 130 int request_id, base::WeakPtr<ResourceDispatcher> resource_dispatcher, | |
| 131 base::SharedMemoryHandle shm_handle, int shm_size) | |
| 132 : peer_(NULL) | |
| 133 , shm_handle_(shm_handle) | |
| 134 , shm_size_(shm_size) | |
|
jam
2013/12/17 00:44:40
ditto
oystein (OOO til 10th of July)
2013/12/17 01:07:27
Done.
| |
| 135 , weak_factory_(this) { | |
| 136 DCHECK(ChildThread::current() != NULL); | |
| 137 | |
| 138 filter_ = new ParserResourceMessageFilter( | |
| 139 ChildProcess::current()->io_message_loop_proxy(), | |
| 140 ChildThread::current()->message_loop(), | |
| 141 resource_dispatcher, | |
| 142 parser_thread(), | |
| 143 weak_factory_.GetWeakPtr(), | |
| 144 request_id); | |
| 145 | |
| 146 ChildThread::current()->channel()->AddFilter(filter_.get()); | |
| 147 } | |
| 148 | |
| 149 WebParserResourceBridgeImpl::~WebParserResourceBridgeImpl() { | |
| 150 DCHECK(ChildThread::current() != NULL); | |
| 151 | |
| 152 // Release it from our locally held member variable before | |
| 153 // posting a message to remove it from the I/O thread, to | |
| 154 // make sure there's no race conditions and it's always destructed | |
| 155 // on the I/O thread. | |
| 156 // Ideally we'd also assert on refcount = 2 here... | |
| 157 IPC::ChannelProxy::MessageFilter* filter = filter_.get(); | |
| 158 filter_ = scoped_refptr<IPC::ChannelProxy::MessageFilter>(); | |
| 159 ChildThread::current()->channel()->RemoveFilter(filter); | |
| 160 // If this fails, we're leaking some shared memory. | |
| 161 DCHECK(shm_handle_ == base::SharedMemory::NULLHandle()); | |
| 162 } | |
| 163 | |
| 164 blink::WebThread* WebParserResourceBridgeImpl::getParserThread() { | |
| 165 return &parser_thread(); | |
| 166 } | |
| 167 | |
| 168 void WebParserResourceBridgeImpl::setPeer( | |
| 169 blink::WebParserResourceBridge::Peer* peer) { | |
| 170 DCHECK(parser_thread().isCurrentThread()); | |
| 171 peer_ = peer; | |
| 172 | |
| 173 if (peer_ == NULL) { | |
|
jam
2013/12/17 00:44:40
nit: if (!peer_). also above and below
oystein (OOO til 10th of July)
2013/12/17 01:07:27
Done.
| |
| 174 // When this happens, the bridge should no longer be called on the | |
| 175 // parser thread as it's about to be destroyed on the main thread. | |
| 176 // Invalidating the weak pointers means no callbacks from the filter | |
| 177 // will happen. | |
| 178 weak_factory_.InvalidateWeakPtrs(); | |
| 179 } else { | |
| 180 CreateSharedMemoryBuffer(); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void WebParserResourceBridgeImpl::OnReceivedData(int data_offset, | |
| 185 int data_length, | |
| 186 int encoded_data_length) { | |
| 187 DCHECK(parser_thread().isCurrentThread()); | |
| 188 DCHECK(shm_buffer_ != NULL); | |
| 189 DCHECK(peer_ != NULL); | |
| 190 | |
| 191 CHECK_GE(shm_size_, data_offset + data_length); | |
| 192 const char* data_ptr = static_cast<char*>(shm_buffer_->memory()); | |
| 193 CHECK(data_ptr); | |
| 194 CHECK(data_ptr + data_offset); | |
| 195 | |
| 196 // TODO: XSS validation and other stuff needs to happen to happen | |
| 197 // here before we pass it to the parser. | |
| 198 peer_->OnReceivedData(data_ptr + data_offset, data_length); | |
| 199 } | |
| 200 | |
| 201 void WebParserResourceBridgeImpl::CreateSharedMemoryBuffer() { | |
| 202 DCHECK(parser_thread().isCurrentThread()); | |
| 203 // This assumes OnSetDataBuffer has been received by the main thread when | |
| 204 // the bridge is created; once the creation is moved to OnReceivedResponse | |
| 205 // rather than the first OnReceiveData, that will no longer always be true | |
| 206 // and the filter will have to listen to OnSetDataBuffer itself and forward | |
| 207 // the handle to the bridge. | |
| 208 DCHECK(base::SharedMemory::IsHandleValid(shm_handle_) || shm_size_ == 0); | |
| 209 | |
| 210 shm_buffer_.reset(new base::SharedMemory(shm_handle_, true)); // read only | |
| 211 shm_handle_ = base::SharedMemory::NULLHandle(); | |
| 212 | |
| 213 bool ok = shm_buffer_->Map(shm_size_); | |
| 214 DCHECK(ok); | |
| 215 } | |
| 216 | |
| 217 } // namespace content | |
| OLD | NEW |