| 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 "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_service.h" | |
| 11 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" | 12 #include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest
.h" |
| 15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" | 13 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h" |
| 16 #include "chrome/common/chrome_version_info.h" | 14 #include "chrome/common/chrome_version_info.h" |
| 17 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/browser/pref_names.h" | |
| 19 #include "extensions/common/constants.h" | 16 #include "extensions/common/constants.h" |
| 20 #include "extensions/common/features/feature.h" | 17 #include "extensions/common/features/feature.h" |
| 21 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
| 22 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 23 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 24 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 25 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 26 | 23 |
| 27 namespace { | 24 namespace { |
| 28 | 25 |
| 29 // Maximum message size in bytes for messages received from Native Messaging | 26 // Maximum message size in bytes for messages received from Native Messaging |
| 30 // hosts. Message size is limited mainly to prevent Chrome from crashing when | 27 // hosts. Message size is limited mainly to prevent Chrome from crashing when |
| 31 // native application misbehaves (e.g. starts writing garbage to the pipe). | 28 // native application misbehaves (e.g. starts writing garbage to the pipe). |
| 32 const size_t kMaximumMessageSize = 1024 * 1024; | 29 const size_t kMaximumMessageSize = 1024 * 1024; |
| 33 | 30 |
| 34 // Message header contains 4-byte integer size of the message. | 31 // Message header contains 4-byte integer size of the message. |
| 35 const size_t kMessageHeaderSize = 4; | 32 const size_t kMessageHeaderSize = 4; |
| 36 | 33 |
| 37 // Size of the buffer to be allocated for each read. | 34 // Size of the buffer to be allocated for each read. |
| 38 const size_t kReadBufferSize = 4096; | 35 const size_t kReadBufferSize = 4096; |
| 39 | 36 |
| 40 const char kFailedToStartError[] = "Failed to start native messaging host."; | |
| 41 const char kInvalidNameError[] = | |
| 42 "Invalid native messaging host name specified."; | |
| 43 const char kNativeHostExited[] = "Native host has exited."; | |
| 44 const char kNotFoundError[] = "Specified native messaging host not found."; | |
| 45 const char kForbiddenError[] = | |
| 46 "Access to the specified native messaging host is forbidden."; | |
| 47 const char kHostInputOuputError[] = | |
| 48 "Error when communicating with the native messaging host."; | |
| 49 | |
| 50 } // namespace | 37 } // namespace |
| 51 | 38 |
| 52 namespace extensions { | 39 namespace extensions { |
| 53 | 40 |
| 54 // static | |
| 55 NativeMessageProcessHost::PolicyPermission | |
| 56 NativeMessageProcessHost::IsHostAllowed(const PrefService* pref_service, | |
| 57 const std::string& native_host_name) { | |
| 58 NativeMessageProcessHost::PolicyPermission allow_result = ALLOW_ALL; | |
| 59 if (pref_service->IsManagedPreference( | |
| 60 pref_names::kNativeMessagingUserLevelHosts)) { | |
| 61 if (!pref_service->GetBoolean(pref_names::kNativeMessagingUserLevelHosts)) | |
| 62 allow_result = ALLOW_SYSTEM_ONLY; | |
| 63 } | |
| 64 | |
| 65 // All native messaging hosts are allowed if there is no blacklist. | |
| 66 if (!pref_service->IsManagedPreference(pref_names::kNativeMessagingBlacklist)) | |
| 67 return allow_result; | |
| 68 const base::ListValue* blacklist = | |
| 69 pref_service->GetList(pref_names::kNativeMessagingBlacklist); | |
| 70 if (!blacklist) | |
| 71 return allow_result; | |
| 72 | |
| 73 // Check if the name or the wildcard is in the blacklist. | |
| 74 base::StringValue name_value(native_host_name); | |
| 75 base::StringValue wildcard_value("*"); | |
| 76 if (blacklist->Find(name_value) == blacklist->end() && | |
| 77 blacklist->Find(wildcard_value) == blacklist->end()) { | |
| 78 return allow_result; | |
| 79 } | |
| 80 | |
| 81 // The native messaging host is blacklisted. Check the whitelist. | |
| 82 if (pref_service->IsManagedPreference( | |
| 83 pref_names::kNativeMessagingWhitelist)) { | |
| 84 const base::ListValue* whitelist = | |
| 85 pref_service->GetList(pref_names::kNativeMessagingWhitelist); | |
| 86 if (whitelist && whitelist->Find(name_value) != whitelist->end()) | |
| 87 return allow_result; | |
| 88 } | |
| 89 | |
| 90 return DISALLOW; | |
| 91 } | |
| 92 | |
| 93 NativeMessageProcessHost::NativeMessageProcessHost( | 41 NativeMessageProcessHost::NativeMessageProcessHost( |
| 94 base::WeakPtr<Client> weak_client_ui, | |
| 95 const std::string& source_extension_id, | 42 const std::string& source_extension_id, |
| 96 const std::string& native_host_name, | 43 const std::string& native_host_name, |
| 97 int destination_port, | |
| 98 scoped_ptr<NativeProcessLauncher> launcher) | 44 scoped_ptr<NativeProcessLauncher> launcher) |
| 99 : weak_client_ui_(weak_client_ui), | 45 : source_extension_id_(source_extension_id), |
| 100 source_extension_id_(source_extension_id), | |
| 101 native_host_name_(native_host_name), | 46 native_host_name_(native_host_name), |
| 102 destination_port_(destination_port), | |
| 103 launcher_(launcher.Pass()), | 47 launcher_(launcher.Pass()), |
| 104 closed_(false), | 48 closed_(false), |
| 105 process_handle_(base::kNullProcessHandle), | 49 process_handle_(base::kNullProcessHandle), |
| 106 #if defined(OS_POSIX) | 50 #if defined(OS_POSIX) |
| 107 read_file_(-1), | 51 read_file_(-1), |
| 108 #endif | 52 #endif |
| 109 read_pending_(false), | 53 read_pending_(false), |
| 110 write_pending_(false) { | 54 write_pending_(false) { |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 112 | 56 |
| 57 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( |
| 58 content::BrowserThread::IO); |
| 113 // It's safe to use base::Unretained() here because NativeMessagePort always | 59 // It's safe to use base::Unretained() here because NativeMessagePort always |
| 114 // deletes us on the IO thread. | 60 // deletes us on the IO thread. |
| 115 content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, | 61 task_runner_->PostTask( |
| 62 FROM_HERE, |
| 116 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, | 63 base::Bind(&NativeMessageProcessHost::LaunchHostProcess, |
| 117 base::Unretained(this))); | 64 base::Unretained(this))); |
| 118 } | 65 } |
| 119 | 66 |
| 120 NativeMessageProcessHost::~NativeMessageProcessHost() { | 67 NativeMessageProcessHost::~NativeMessageProcessHost() { |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 68 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 122 Close(std::string()); | |
| 123 } | 69 } |
| 124 | 70 |
| 125 // static | 71 // static |
| 126 scoped_ptr<NativeMessageProcessHost> NativeMessageProcessHost::Create( | 72 scoped_ptr<NativeMessageHost> NativeMessageHost::Create( |
| 127 gfx::NativeView native_view, | 73 gfx::NativeView native_view, |
| 128 base::WeakPtr<Client> weak_client_ui, | |
| 129 const std::string& source_extension_id, | 74 const std::string& source_extension_id, |
| 130 const std::string& native_host_name, | 75 const std::string& native_host_name, |
| 131 int destination_port, | 76 bool allow_user_level, |
| 132 bool allow_user_level) { | 77 std::string* error_message) { |
| 133 return CreateWithLauncher(weak_client_ui, source_extension_id, | 78 return NativeMessageProcessHost::CreateWithLauncher( |
| 134 native_host_name, destination_port, | 79 source_extension_id, |
| 135 NativeProcessLauncher::CreateDefault( | 80 native_host_name, |
| 136 allow_user_level, native_view)); | 81 NativeProcessLauncher::CreateDefault(allow_user_level, native_view)); |
| 137 } | 82 } |
| 138 | 83 |
| 139 // static | 84 // static |
| 140 scoped_ptr<NativeMessageProcessHost> | 85 scoped_ptr<NativeMessageHost> NativeMessageProcessHost::CreateWithLauncher( |
| 141 NativeMessageProcessHost::CreateWithLauncher( | |
| 142 base::WeakPtr<Client> weak_client_ui, | |
| 143 const std::string& source_extension_id, | 86 const std::string& source_extension_id, |
| 144 const std::string& native_host_name, | 87 const std::string& native_host_name, |
| 145 int destination_port, | |
| 146 scoped_ptr<NativeProcessLauncher> launcher) { | 88 scoped_ptr<NativeProcessLauncher> launcher) { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 | 90 |
| 149 scoped_ptr<NativeMessageProcessHost> process(new NativeMessageProcessHost( | 91 scoped_ptr<NativeMessageHost> process( |
| 150 weak_client_ui, source_extension_id, native_host_name, | 92 new NativeMessageProcessHost(source_extension_id, |
| 151 destination_port, launcher.Pass())); | 93 native_host_name, |
| 94 launcher.Pass())); |
| 152 | 95 |
| 153 return process.Pass(); | 96 return process.Pass(); |
| 154 } | 97 } |
| 155 | 98 |
| 156 void NativeMessageProcessHost::LaunchHostProcess() { | 99 void NativeMessageProcessHost::LaunchHostProcess() { |
| 157 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 100 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 158 | 101 |
| 159 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); | 102 GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_); |
| 160 launcher_->Launch(origin, native_host_name_, | 103 launcher_->Launch(origin, native_host_name_, |
| 161 base::Bind(&NativeMessageProcessHost::OnHostProcessLaunched, | 104 base::Bind(&NativeMessageProcessHost::OnHostProcessLaunched, |
| 162 base::Unretained(this))); | 105 base::Unretained(this))); |
| 163 } | 106 } |
| 164 | 107 |
| 165 void NativeMessageProcessHost::OnHostProcessLaunched( | 108 void NativeMessageProcessHost::OnHostProcessLaunched( |
| 166 NativeProcessLauncher::LaunchResult result, | 109 NativeProcessLauncher::LaunchResult result, |
| 167 base::ProcessHandle process_handle, | 110 base::ProcessHandle process_handle, |
| 168 base::File read_file, | 111 base::File read_file, |
| 169 base::File write_file) { | 112 base::File write_file) { |
| 170 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 113 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 171 | 114 |
| 172 switch (result) { | 115 switch (result) { |
| 173 case NativeProcessLauncher::RESULT_INVALID_NAME: | 116 case NativeProcessLauncher::RESULT_INVALID_NAME: |
| 174 Close(kInvalidNameError); | 117 Close(kInvalidNameError); |
| 175 return; | 118 return; |
| 176 case NativeProcessLauncher::RESULT_NOT_FOUND: | 119 case NativeProcessLauncher::RESULT_NOT_FOUND: |
| 177 Close(kNotFoundError); | 120 Close(kNotFoundError); |
| 178 return; | 121 return; |
| 179 case NativeProcessLauncher::RESULT_FORBIDDEN: | 122 case NativeProcessLauncher::RESULT_FORBIDDEN: |
| 180 Close(kForbiddenError); | 123 Close(kForbiddenError); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 197 GetTaskRunnerWithShutdownBehavior( | 140 GetTaskRunnerWithShutdownBehavior( |
| 198 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | 141 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
| 199 | 142 |
| 200 read_stream_.reset(new net::FileStream(read_file.Pass(), task_runner)); | 143 read_stream_.reset(new net::FileStream(read_file.Pass(), task_runner)); |
| 201 write_stream_.reset(new net::FileStream(write_file.Pass(), task_runner)); | 144 write_stream_.reset(new net::FileStream(write_file.Pass(), task_runner)); |
| 202 | 145 |
| 203 WaitRead(); | 146 WaitRead(); |
| 204 DoWrite(); | 147 DoWrite(); |
| 205 } | 148 } |
| 206 | 149 |
| 207 void NativeMessageProcessHost::Send(const std::string& json) { | 150 void NativeMessageProcessHost::OnMessage(const std::string& json) { |
| 208 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 151 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 209 | 152 |
| 210 if (closed_) | 153 if (closed_) |
| 211 return; | 154 return; |
| 212 | 155 |
| 213 // Allocate new buffer for the message. | 156 // Allocate new buffer for the message. |
| 214 scoped_refptr<net::IOBufferWithSize> buffer = | 157 scoped_refptr<net::IOBufferWithSize> buffer = |
| 215 new net::IOBufferWithSize(json.size() + kMessageHeaderSize); | 158 new net::IOBufferWithSize(json.size() + kMessageHeaderSize); |
| 216 | 159 |
| 217 // Copy size and content of the message to the buffer. | 160 // Copy size and content of the message to the buffer. |
| 218 COMPILE_ASSERT(sizeof(uint32) == kMessageHeaderSize, incorrect_header_size); | 161 COMPILE_ASSERT(sizeof(uint32) == kMessageHeaderSize, incorrect_header_size); |
| 219 *reinterpret_cast<uint32*>(buffer->data()) = json.size(); | 162 *reinterpret_cast<uint32*>(buffer->data()) = json.size(); |
| 220 memcpy(buffer->data() + kMessageHeaderSize, json.data(), json.size()); | 163 memcpy(buffer->data() + kMessageHeaderSize, json.data(), json.size()); |
| 221 | 164 |
| 222 // Push new message to the write queue. | 165 // Push new message to the write queue. |
| 223 write_queue_.push(buffer); | 166 write_queue_.push(buffer); |
| 224 | 167 |
| 225 // Send() may be called before the host process is started. In that case the | 168 // Send() may be called before the host process is started. In that case the |
| 226 // message will be written when OnHostProcessLaunched() is called. If it's | 169 // message will be written when OnHostProcessLaunched() is called. If it's |
| 227 // already started then write the message now. | 170 // already started then write the message now. |
| 228 if (write_stream_) | 171 if (write_stream_) |
| 229 DoWrite(); | 172 DoWrite(); |
| 230 } | 173 } |
| 231 | 174 |
| 175 void NativeMessageProcessHost::Start(Client* client) { |
| 176 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 177 client_ = client; |
| 178 } |
| 179 |
| 180 scoped_refptr<base::SingleThreadTaskRunner> |
| 181 NativeMessageProcessHost::task_runner() const { |
| 182 return task_runner_; |
| 183 } |
| 184 |
| 232 #if defined(OS_POSIX) | 185 #if defined(OS_POSIX) |
| 233 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { | 186 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { |
| 234 DCHECK_EQ(fd, read_file_); | 187 DCHECK_EQ(fd, read_file_); |
| 235 DoRead(); | 188 DoRead(); |
| 236 } | 189 } |
| 237 | 190 |
| 238 void NativeMessageProcessHost::OnFileCanWriteWithoutBlocking(int fd) { | 191 void NativeMessageProcessHost::OnFileCanWriteWithoutBlocking(int fd) { |
| 239 NOTREACHED(); | 192 NOTREACHED(); |
| 240 } | 193 } |
| 241 #endif // !defined(OS_POSIX) | 194 #endif // !defined(OS_POSIX) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 257 #if defined(OS_POSIX) | 210 #if defined(OS_POSIX) |
| 258 base::MessageLoopForIO::current()->WatchFileDescriptor( | 211 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 259 read_file_, false /* persistent */, | 212 read_file_, false /* persistent */, |
| 260 base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); | 213 base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); |
| 261 #else // defined(OS_POSIX) | 214 #else // defined(OS_POSIX) |
| 262 DoRead(); | 215 DoRead(); |
| 263 #endif // defined(!OS_POSIX) | 216 #endif // defined(!OS_POSIX) |
| 264 } | 217 } |
| 265 | 218 |
| 266 void NativeMessageProcessHost::DoRead() { | 219 void NativeMessageProcessHost::DoRead() { |
| 267 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 220 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 268 | 221 |
| 269 while (!closed_ && !read_pending_) { | 222 while (!closed_ && !read_pending_) { |
| 270 read_buffer_ = new net::IOBuffer(kReadBufferSize); | 223 read_buffer_ = new net::IOBuffer(kReadBufferSize); |
| 271 int result = read_stream_->Read( | 224 int result = read_stream_->Read( |
| 272 read_buffer_.get(), | 225 read_buffer_.get(), |
| 273 kReadBufferSize, | 226 kReadBufferSize, |
| 274 base::Bind(&NativeMessageProcessHost::OnRead, base::Unretained(this))); | 227 base::Bind(&NativeMessageProcessHost::OnRead, base::Unretained(this))); |
| 275 HandleReadResult(result); | 228 HandleReadResult(result); |
| 276 } | 229 } |
| 277 } | 230 } |
| 278 | 231 |
| 279 void NativeMessageProcessHost::OnRead(int result) { | 232 void NativeMessageProcessHost::OnRead(int result) { |
| 280 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 233 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 281 DCHECK(read_pending_); | 234 DCHECK(read_pending_); |
| 282 read_pending_ = false; | 235 read_pending_ = false; |
| 283 | 236 |
| 284 HandleReadResult(result); | 237 HandleReadResult(result); |
| 285 WaitRead(); | 238 WaitRead(); |
| 286 } | 239 } |
| 287 | 240 |
| 288 void NativeMessageProcessHost::HandleReadResult(int result) { | 241 void NativeMessageProcessHost::HandleReadResult(int result) { |
| 289 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 242 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 290 | 243 |
| 291 if (closed_) | 244 if (closed_) |
| 292 return; | 245 return; |
| 293 | 246 |
| 294 if (result > 0) { | 247 if (result > 0) { |
| 295 ProcessIncomingData(read_buffer_->data(), result); | 248 ProcessIncomingData(read_buffer_->data(), result); |
| 296 } else if (result == net::ERR_IO_PENDING) { | 249 } else if (result == net::ERR_IO_PENDING) { |
| 297 read_pending_ = true; | 250 read_pending_ = true; |
| 298 } else if (result == 0 || result == net::ERR_CONNECTION_RESET) { | 251 } else if (result == 0 || result == net::ERR_CONNECTION_RESET) { |
| 299 // On Windows we get net::ERR_CONNECTION_RESET for a broken pipe, while on | 252 // On Windows we get net::ERR_CONNECTION_RESET for a broken pipe, while on |
| 300 // Posix read() returns 0 in that case. | 253 // Posix read() returns 0 in that case. |
| 301 Close(kNativeHostExited); | 254 Close(kNativeHostExited); |
| 302 } else { | 255 } else { |
| 303 LOG(ERROR) << "Error when reading from Native Messaging host: " << result; | 256 LOG(ERROR) << "Error when reading from Native Messaging host: " << result; |
| 304 Close(kHostInputOuputError); | 257 Close(kHostInputOuputError); |
| 305 } | 258 } |
| 306 } | 259 } |
| 307 | 260 |
| 308 void NativeMessageProcessHost::ProcessIncomingData( | 261 void NativeMessageProcessHost::ProcessIncomingData( |
| 309 const char* data, int data_size) { | 262 const char* data, int data_size) { |
| 310 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 263 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 311 | 264 |
| 312 incoming_data_.append(data, data_size); | 265 incoming_data_.append(data, data_size); |
| 313 | 266 |
| 314 while (true) { | 267 while (true) { |
| 315 if (incoming_data_.size() < kMessageHeaderSize) | 268 if (incoming_data_.size() < kMessageHeaderSize) |
| 316 return; | 269 return; |
| 317 | 270 |
| 318 size_t message_size = | 271 size_t message_size = |
| 319 *reinterpret_cast<const uint32*>(incoming_data_.data()); | 272 *reinterpret_cast<const uint32*>(incoming_data_.data()); |
| 320 | 273 |
| 321 if (message_size > kMaximumMessageSize) { | 274 if (message_size > kMaximumMessageSize) { |
| 322 LOG(ERROR) << "Native Messaging host tried sending a message that is " | 275 LOG(ERROR) << "Native Messaging host tried sending a message that is " |
| 323 << message_size << " bytes long."; | 276 << message_size << " bytes long."; |
| 324 Close(kHostInputOuputError); | 277 Close(kHostInputOuputError); |
| 325 return; | 278 return; |
| 326 } | 279 } |
| 327 | 280 |
| 328 if (incoming_data_.size() < message_size + kMessageHeaderSize) | 281 if (incoming_data_.size() < message_size + kMessageHeaderSize) |
| 329 return; | 282 return; |
| 330 | 283 |
| 331 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 284 client_->PostMessageFromNativeHost( |
| 332 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, | 285 incoming_data_.substr(kMessageHeaderSize, message_size)); |
| 333 destination_port_, | |
| 334 incoming_data_.substr(kMessageHeaderSize, message_size))); | |
| 335 | 286 |
| 336 incoming_data_.erase(0, kMessageHeaderSize + message_size); | 287 incoming_data_.erase(0, kMessageHeaderSize + message_size); |
| 337 } | 288 } |
| 338 } | 289 } |
| 339 | 290 |
| 340 void NativeMessageProcessHost::DoWrite() { | 291 void NativeMessageProcessHost::DoWrite() { |
| 341 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 292 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 342 | 293 |
| 343 while (!write_pending_ && !closed_) { | 294 while (!write_pending_ && !closed_) { |
| 344 if (!current_write_buffer_.get() || | 295 if (!current_write_buffer_.get() || |
| 345 !current_write_buffer_->BytesRemaining()) { | 296 !current_write_buffer_->BytesRemaining()) { |
| 346 if (write_queue_.empty()) | 297 if (write_queue_.empty()) |
| 347 return; | 298 return; |
| 348 current_write_buffer_ = new net::DrainableIOBuffer( | 299 current_write_buffer_ = new net::DrainableIOBuffer( |
| 349 write_queue_.front().get(), write_queue_.front()->size()); | 300 write_queue_.front().get(), write_queue_.front()->size()); |
| 350 write_queue_.pop(); | 301 write_queue_.pop(); |
| 351 } | 302 } |
| 352 | 303 |
| 353 int result = | 304 int result = |
| 354 write_stream_->Write(current_write_buffer_.get(), | 305 write_stream_->Write(current_write_buffer_.get(), |
| 355 current_write_buffer_->BytesRemaining(), | 306 current_write_buffer_->BytesRemaining(), |
| 356 base::Bind(&NativeMessageProcessHost::OnWritten, | 307 base::Bind(&NativeMessageProcessHost::OnWritten, |
| 357 base::Unretained(this))); | 308 base::Unretained(this))); |
| 358 HandleWriteResult(result); | 309 HandleWriteResult(result); |
| 359 } | 310 } |
| 360 } | 311 } |
| 361 | 312 |
| 362 void NativeMessageProcessHost::HandleWriteResult(int result) { | 313 void NativeMessageProcessHost::HandleWriteResult(int result) { |
| 363 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 314 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 364 | 315 |
| 365 if (result <= 0) { | 316 if (result <= 0) { |
| 366 if (result == net::ERR_IO_PENDING) { | 317 if (result == net::ERR_IO_PENDING) { |
| 367 write_pending_ = true; | 318 write_pending_ = true; |
| 368 } else { | 319 } else { |
| 369 LOG(ERROR) << "Error when writing to Native Messaging host: " << result; | 320 LOG(ERROR) << "Error when writing to Native Messaging host: " << result; |
| 370 Close(kHostInputOuputError); | 321 Close(kHostInputOuputError); |
| 371 } | 322 } |
| 372 return; | 323 return; |
| 373 } | 324 } |
| 374 | 325 |
| 375 current_write_buffer_->DidConsume(result); | 326 current_write_buffer_->DidConsume(result); |
| 376 } | 327 } |
| 377 | 328 |
| 378 void NativeMessageProcessHost::OnWritten(int result) { | 329 void NativeMessageProcessHost::OnWritten(int result) { |
| 379 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 330 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 380 | 331 |
| 381 DCHECK(write_pending_); | 332 DCHECK(write_pending_); |
| 382 write_pending_ = false; | 333 write_pending_ = false; |
| 383 | 334 |
| 384 HandleWriteResult(result); | 335 HandleWriteResult(result); |
| 385 DoWrite(); | 336 DoWrite(); |
| 386 } | 337 } |
| 387 | 338 |
| 388 void NativeMessageProcessHost::Close(const std::string& error_message) { | 339 void NativeMessageProcessHost::Close(const std::string& error_message) { |
| 389 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 340 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 390 | 341 |
| 391 if (!closed_) { | 342 if (!closed_) { |
| 392 closed_ = true; | 343 closed_ = true; |
| 393 read_stream_.reset(); | 344 read_stream_.reset(); |
| 394 write_stream_.reset(); | 345 write_stream_.reset(); |
| 395 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | 346 client_->CloseChannel(error_message); |
| 396 base::Bind(&Client::CloseChannel, weak_client_ui_, | |
| 397 destination_port_, error_message)); | |
| 398 } | 347 } |
| 399 | 348 |
| 400 if (process_handle_ != base::kNullProcessHandle) { | 349 if (process_handle_ != base::kNullProcessHandle) { |
| 401 // Kill the host process if necessary to make sure we don't leave zombies. | 350 // Kill the host process if necessary to make sure we don't leave zombies. |
| 402 // On OSX base::EnsureProcessTerminated() may block, so we have to post a | 351 // On OSX base::EnsureProcessTerminated() may block, so we have to post a |
| 403 // task on the blocking pool. | 352 // task on the blocking pool. |
| 404 #if defined(OS_MACOSX) | 353 #if defined(OS_MACOSX) |
| 405 content::BrowserThread::PostBlockingPoolTask( | 354 content::BrowserThread::PostBlockingPoolTask( |
| 406 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); | 355 FROM_HERE, base::Bind(&base::EnsureProcessTerminated, process_handle_)); |
| 407 #else | 356 #else |
| 408 base::EnsureProcessTerminated(process_handle_); | 357 base::EnsureProcessTerminated(process_handle_); |
| 409 #endif | 358 #endif |
| 410 process_handle_ = base::kNullProcessHandle; | 359 process_handle_ = base::kNullProcessHandle; |
| 411 } | 360 } |
| 412 } | 361 } |
| 413 | 362 |
| 414 } // namespace extensions | 363 } // namespace extensions |
| OLD | NEW |