Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/native_messaging/native_messaging_reader.h" | 5 #include "remoting/host/native_messaging/native_messaging_reader.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <cstdint> |
| 8 | |
| 9 #include <string> | 8 #include <string> |
| 10 #include <utility> | 9 #include <utility> |
| 11 | 10 |
| 12 #include "base/bind.h" | 11 #include "base/bind.h" |
| 13 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 14 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 15 #include "base/location.h" | 14 #include "base/location.h" |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
| 18 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 19 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/thread.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "build/build_config.h" | |
| 23 | |
| 24 #if defined(OS_WIN) | |
| 25 #include <windows.h> | |
| 26 | |
| 27 #include "base/threading/platform_thread.h" | |
| 28 #include "base/win/scoped_handle.h" | |
| 29 #endif // defined(OS_WIN) | |
| 23 | 30 |
| 24 namespace { | 31 namespace { |
| 25 | 32 |
| 26 // uint32_t is specified in the protocol as the type for the message header. | 33 // uint32_t is specified in the protocol as the type for the message header. |
| 27 typedef uint32_t MessageLengthType; | 34 typedef uint32_t MessageLengthType; |
| 28 | 35 |
| 29 const int kMessageHeaderSize = sizeof(MessageLengthType); | 36 const int kMessageHeaderSize = sizeof(MessageLengthType); |
| 30 | 37 |
| 31 // Limit the size of received messages, to avoid excessive memory-allocation in | 38 // Limit the size of received messages, to avoid excessive memory-allocation in |
| 32 // this process, and potential overflow issues when casting to a signed 32-bit | 39 // this process, and potential overflow issues when casting to a signed 32-bit |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 void NativeMessagingReader::Core::NotifyEof() { | 137 void NativeMessagingReader::Core::NotifyEof() { |
| 131 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 138 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 132 caller_task_runner_->PostTask( | 139 caller_task_runner_->PostTask( |
| 133 FROM_HERE, | 140 FROM_HERE, |
| 134 base::Bind(&NativeMessagingReader::InvokeEofCallback, reader_)); | 141 base::Bind(&NativeMessagingReader::InvokeEofCallback, reader_)); |
| 135 } | 142 } |
| 136 | 143 |
| 137 NativeMessagingReader::NativeMessagingReader(base::File file) | 144 NativeMessagingReader::NativeMessagingReader(base::File file) |
| 138 : reader_thread_("Reader"), | 145 : reader_thread_("Reader"), |
| 139 weak_factory_(this) { | 146 weak_factory_(this) { |
| 140 reader_thread_.Start(); | 147 base::Thread::Options options; |
|
Sergey Ulanov
2016/08/09 15:39:58
base::Thread::Options thread_options(base::Message
joedow
2016/08/10 02:49:58
Done.
| |
| 148 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 149 reader_thread_.StartWithOptions(options); | |
| 150 | |
| 141 read_task_runner_ = reader_thread_.task_runner(); | 151 read_task_runner_ = reader_thread_.task_runner(); |
| 142 core_.reset(new Core(std::move(file), base::ThreadTaskRunnerHandle::Get(), | 152 core_.reset(new Core(std::move(file), base::ThreadTaskRunnerHandle::Get(), |
| 143 read_task_runner_, weak_factory_.GetWeakPtr())); | 153 read_task_runner_, weak_factory_.GetWeakPtr())); |
| 144 } | 154 } |
| 145 | 155 |
| 146 NativeMessagingReader::~NativeMessagingReader() { | 156 NativeMessagingReader::~NativeMessagingReader() { |
| 147 read_task_runner_->DeleteSoon(FROM_HERE, core_.release()); | 157 read_task_runner_->DeleteSoon(FROM_HERE, core_.release()); |
| 158 | |
| 159 #if defined(OS_WIN) | |
| 160 base::PlatformThreadId thread_id = reader_thread_.GetThreadId(); | |
| 161 base::win::ScopedHandle thread_handle( | |
| 162 OpenThread(THREAD_TERMINATE, /*bInheritHandle=*/false, thread_id)); | |
| 163 if (!CancelSynchronousIo(thread_handle.Get())) { | |
| 164 // ERROR_NOT_FOUND means there were no pending IO requests so don't treat | |
| 165 // that result as an error. | |
| 166 if (GetLastError() != ERROR_NOT_FOUND) { | |
| 167 PLOG(ERROR) << "CancelSynchronousIo() failed"; | |
| 168 } | |
| 169 } | |
| 170 #endif // defined(OS_WIN) | |
| 148 } | 171 } |
| 149 | 172 |
| 150 void NativeMessagingReader::Start(MessageCallback message_callback, | 173 void NativeMessagingReader::Start(MessageCallback message_callback, |
| 151 base::Closure eof_callback) { | 174 base::Closure eof_callback) { |
| 152 message_callback_ = message_callback; | 175 message_callback_ = message_callback; |
| 153 eof_callback_ = eof_callback; | 176 eof_callback_ = eof_callback; |
| 154 | 177 |
| 155 // base::Unretained is safe since |core_| is only deleted via the | 178 // base::Unretained is safe since |core_| is only deleted via the |
| 156 // DeleteSoon task which is posted from this class's dtor. | 179 // DeleteSoon task which is posted from this class's dtor. |
| 157 read_task_runner_->PostTask( | 180 read_task_runner_->PostTask( |
| 158 FROM_HERE, base::Bind(&NativeMessagingReader::Core::ReadMessage, | 181 FROM_HERE, base::Bind(&NativeMessagingReader::Core::ReadMessage, |
| 159 base::Unretained(core_.get()))); | 182 base::Unretained(core_.get()))); |
| 160 } | 183 } |
| 161 | 184 |
| 162 void NativeMessagingReader::InvokeMessageCallback( | 185 void NativeMessagingReader::InvokeMessageCallback( |
| 163 std::unique_ptr<base::Value> message) { | 186 std::unique_ptr<base::Value> message) { |
| 164 message_callback_.Run(std::move(message)); | 187 message_callback_.Run(std::move(message)); |
| 165 } | 188 } |
| 166 | 189 |
| 167 void NativeMessagingReader::InvokeEofCallback() { | 190 void NativeMessagingReader::InvokeEofCallback() { |
| 168 eof_callback_.Run(); | 191 eof_callback_.Run(); |
| 169 } | 192 } |
| 170 | 193 |
| 171 } // namespace remoting | 194 } // namespace remoting |
| OLD | NEW |