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

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

Issue 10818013: Native Messaging! (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added Example Created 8 years, 4 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_process_host_posix.cc
diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc
new file mode 100644
index 0000000000000000000000000000000000000000..00f7e80744a5b0415d7f5113ff66189e7b0f084c
--- /dev/null
+++ b/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc
@@ -0,0 +1,71 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
+
+#include <unistd.h>
+
+#include "base/logging.h"
+#include "base/process_util.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace extensions {
+
+void NativeMessageProcessHost::ReadNowForTesting() {
+ OnFileCanReadWithoutBlocking(read_file_);
+}
+
+void NativeMessageProcessHost::InitIO() {
+ // Always watch the read end.
+ MessageLoopForIO::current()->WatchFileDescriptor(read_file_,
+ true, /* persistent */
+ MessageLoopForIO::WATCH_READ,
+ &read_watcher_,
+ this);
+}
+
+void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+
+ // Make sure that the fd given to us is the same one we started with.
+ CHECK_EQ(fd, read_file_);
+
+ // If this is a sendMessage request, stop trying to read after the first
+ // message.
+ if (is_send_message_)
+ read_watcher_.StopWatchingFileDescriptor();
+
+ MessageType type;
+ std::string message;
+ if (!ReadMessage(&type, &message)) {
+ // A read failed, is the process dead?
+ if (base::GetTerminationStatus(native_process_handle_, NULL) !=
+ base::TERMINATION_STATUS_STILL_RUNNING) {
+ read_watcher_.StopWatchingFileDescriptor();
+ // Notify the message service that the channel should close.
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&Client::CloseChannel, weak_client_ui_,
+ destination_port_, true));
+ }
+ return;
+ }
+
+ content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_,
+ destination_port_, message));
+}
+
+bool NativeMessageProcessHost::WriteData(FileHandle file,
+ const char* data,
+ size_t bytes_to_write) {
+ return file_util::WriteFileDescriptor(file, data, bytes_to_write);
+}
+
+bool NativeMessageProcessHost::ReadData(FileHandle file,
+ char* data,
+ size_t bytes_to_read) {
+ return file_util::ReadFromFD(file, data, bytes_to_read);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698