Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4942)

Unified Diff: chrome/browser/extensions/api/messaging/native_message_port.cc

Issue 591463003: Remote Assistance on Chrome OS Part III - NativeMessageHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@native_messaging
Patch Set: Add NativeMessagePort::Core Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/messaging/native_message_port.cc
diff --git a/chrome/browser/extensions/api/messaging/native_message_port.cc b/chrome/browser/extensions/api/messaging/native_message_port.cc
index a0a47d561bbb77cad1e495fcaad3a1a5bb46da41..d8e7932c80b7b97356bb349341bd8b454e406869 100644
--- a/chrome/browser/extensions/api/messaging/native_message_port.cc
+++ b/chrome/browser/extensions/api/messaging/native_message_port.cc
@@ -5,27 +5,121 @@
#include "chrome/browser/extensions/api/messaging/native_message_port.h"
#include "base/bind.h"
+#include "base/single_thread_task_runner.h"
#include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
#include "content/public/browser/browser_thread.h"
namespace extensions {
-NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process)
- : native_process_(native_process) {
+typedef NativeMessageHost::Client Client;
Sergey Ulanov 2014/10/01 23:23:59 Don't need this typedef: Client is used in only on
kelvinp 2014/10/02 03:12:16 Done.
+
+// Handles jumping between the |host_task_runner| and the
+// |message_service_task_runner|.
+// All methods on the host interface should be called on |host_task_runner|.
+// All methods on |weak_port| (that calls into MessageServices) should be called
+// on |message_service_task_runner|.
+class NativeMessagePort::Core : public Client {
+ public:
+ Core(
+ scoped_ptr<NativeMessageHost> host,
+ base::WeakPtr<NativeMessagePort> weak_port,
Sergey Ulanov 2014/10/01 23:23:59 don't need 'weak_' prefix.
kelvinp 2014/10/02 03:12:17 Done.
+ scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_);
+ virtual ~Core();
+
+ void OnMessageFromChrome(const std::string& message);
+
+ // NativeMessageHost::Client implementation.
+ virtual void PostMessageFromNativeHost(const std::string& message) OVERRIDE;
+ virtual void CloseChannel(const std::string& error_message) OVERRIDE;
+
+ private:
+ scoped_ptr<NativeMessageHost> host_;
+ base::WeakPtr<NativeMessagePort> weak_port_;
Sergey Ulanov 2014/10/01 23:23:58 port_
kelvinp 2014/10/02 03:12:17 Done.
+
+ scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_;
+ scoped_refptr<base::SingleThreadTaskRunner> host_task_runner_;
+};
+
+NativeMessagePort::Core::Core(
+ scoped_ptr<NativeMessageHost> host,
+ base::WeakPtr<NativeMessagePort> weak_port,
+ scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner)
+ : host_(host.Pass()),
+ weak_port_(weak_port),
+ message_service_task_runner_(message_service_task_runner),
+ host_task_runner_(host_->task_runner()) {
+ DCHECK(message_service_task_runner_->BelongsToCurrentThread());
+ host_task_runner_->PostTask(FROM_HERE,
+ base::Bind(&NativeMessageHost::set_client,
+ base::Unretained(host_.get()),
+ base::Unretained(this)));
+}
+
+NativeMessagePort::Core::~Core() {
Sergey Ulanov 2014/10/01 23:23:59 This object must always be destroyed on |host_task
kelvinp 2014/10/02 03:12:16 Done.
+ if (!host_task_runner_->BelongsToCurrentThread()) {
+ host_task_runner_->DeleteSoon(FROM_HERE, host_.release());
+ }
+}
+
+void NativeMessagePort::Core::OnMessageFromChrome(const std::string& message) {
+ DCHECK(message_service_task_runner_->BelongsToCurrentThread());
+ host_task_runner_->PostTask(FROM_HERE,
+ base::Bind(&NativeMessageHost::OnMessage,
+ base::Unretained(host_.get()),
+ message));
+}
+
+void NativeMessagePort::Core::PostMessageFromNativeHost(
+ const std::string& message) {
+ DCHECK(host_task_runner_->BelongsToCurrentThread());
+ message_service_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(
+ &NativeMessagePort::PostMessageFromNativeHost, weak_port_, message));
+}
+
+void NativeMessagePort::Core::CloseChannel(const std::string& error_message) {
+ DCHECK(host_task_runner_->BelongsToCurrentThread());
+ message_service_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&NativeMessagePort::CloseChannel, weak_port_, error_message));
+}
+
+NativeMessagePort::NativeMessagePort(
+ base::WeakPtr<MessageService> message_service,
+ int port_id,
+ scoped_ptr<NativeMessageHost> native_message_host)
+ : weak_message_service_(message_service),
+ port_id_(port_id),
+ weak_factory_(this) {
+ core_.reset(new Core(native_message_host.Pass(),
+ weak_factory_.GetWeakPtr(),
+ base::MessageLoopProxy::current()));
}
NativeMessagePort::~NativeMessagePort() {
- content::BrowserThread::DeleteSoon(
- content::BrowserThread::IO, FROM_HERE, native_process_);
}
void NativeMessagePort::DispatchOnMessage(
const Message& message,
int target_port_id) {
- content::BrowserThread::PostTask(
- content::BrowserThread::IO, FROM_HERE,
- base::Bind(&NativeMessageProcessHost::Send,
- base::Unretained(native_process_), message.data));
+ DCHECK(thread_checker_.CalledOnValidThread());
+ core_->OnMessageFromChrome(message.data);
+}
+
+void NativeMessagePort::PostMessageFromNativeHost(const std::string& message) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (weak_message_service_) {
+ weak_message_service_->PostMessage(
+ port_id_, Message(message, false /* user_gesture */));
+ }
+}
+
+void NativeMessagePort::CloseChannel(const std::string& error_message) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (weak_message_service_) {
+ weak_message_service_->CloseChannel(port_id_, error_message);
+ }
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698