Chromium Code Reviews| 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..cf129a628ab0ca7861634f4b4929ca3df7c6422a |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/messaging/native_message_process_host_posix.cc |
| @@ -0,0 +1,70 @@ |
| +// 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/command_line.h" |
| +#include "base/file_path.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/logging.h" |
| +#include "base/path_service.h" |
| +#include "base/pickle.h" |
| +#include "base/process_util.h" |
| +#include "base/values.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/common/result_codes.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)) { |
| + LOG(ERROR) << "Bad Read"; |
|
Matt Perry
2012/08/22 00:02:42
ReadMessage already spits out a LOG message on fai
eaugusti
2012/08/31 23:47:13
Done.
|
| + return; |
| + } |
| + |
| + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&Client::PostMessageFromNativeProcess, weak_client_, |
| + 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 |