Chromium Code Reviews| 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_port.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" | |
| 8 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 9 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 | 11 |
| 11 namespace extensions { | 12 namespace extensions { |
| 12 | 13 |
| 13 NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process) | 14 NativeMessagePort::NativeMessagePort( |
| 14 : native_process_(native_process) { | 15 base::WeakPtr<MessageService> message_service, |
| 16 int port_id, | |
| 17 scoped_ptr<NativeMessageHost> native_message_host) | |
| 18 : weak_message_service_(message_service), | |
| 19 port_id_(port_id), | |
| 20 native_message_host_(native_message_host.Pass()), | |
| 21 host_task_runner_(native_message_host_->task_runner()), | |
| 22 weak_factory_(this) { | |
| 23 native_message_host_->set_client(weak_factory_.GetWeakPtr()); | |
|
Sergey Ulanov
2014/09/30 20:33:39
See my comment in native_message_process_host.cc.
kelvinp
2014/10/01 06:08:02
Done.
| |
| 15 } | 24 } |
| 16 | 25 |
| 17 NativeMessagePort::~NativeMessagePort() { | 26 NativeMessagePort::~NativeMessagePort() { |
| 18 content::BrowserThread::DeleteSoon( | 27 if (!host_task_runner_->BelongsToCurrentThread()) { |
| 19 content::BrowserThread::IO, FROM_HERE, native_process_); | 28 host_task_runner_->DeleteSoon(FROM_HERE, native_message_host_.release()); |
| 29 } | |
| 20 } | 30 } |
| 21 | 31 |
| 22 void NativeMessagePort::DispatchOnMessage( | 32 void NativeMessagePort::DispatchOnMessage( |
| 23 const Message& message, | 33 const Message& message, |
| 24 int target_port_id) { | 34 int target_port_id) { |
| 25 content::BrowserThread::PostTask( | 35 if (!host_task_runner_->BelongsToCurrentThread()) { |
| 26 content::BrowserThread::IO, FROM_HERE, | 36 host_task_runner_->PostTask( |
| 27 base::Bind(&NativeMessageProcessHost::Send, | 37 FROM_HERE, |
| 28 base::Unretained(native_process_), message.data)); | 38 base::Bind(&NativeMessagePort::DispatchOnMessage, |
| 39 base::Unretained(this), | |
| 40 message, | |
| 41 target_port_id)); | |
| 42 return; | |
| 43 } | |
| 44 native_message_host_->OnMessage(message.data); | |
| 45 } | |
| 46 | |
| 47 void NativeMessagePort::PostMessageFromNativeHost(const std::string& message) { | |
| 48 if (weak_message_service_) { | |
| 49 weak_message_service_->PostMessage( | |
| 50 port_id_, Message(message, false /* user_gesture */)); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void NativeMessagePort::CloseChannel(const std::string& error_message) { | |
| 55 if (weak_message_service_) { | |
| 56 weak_message_service_->CloseChannel(port_id_, error_message); | |
| 57 } | |
| 29 } | 58 } |
| 30 | 59 |
| 31 } // namespace extensions | 60 } // namespace extensions |
| 61 | |
| OLD | NEW |