OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 6 |
| 7 #include <unistd.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/process_util.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 |
| 13 namespace extensions { |
| 14 |
| 15 void NativeMessageProcessHost::ReadNowForTesting() { |
| 16 OnFileCanReadWithoutBlocking(read_file_); |
| 17 } |
| 18 |
| 19 void NativeMessageProcessHost::InitIO() { |
| 20 // Always watch the read end. |
| 21 MessageLoopForIO::current()->WatchFileDescriptor(read_file_, |
| 22 true, /* persistent */ |
| 23 MessageLoopForIO::WATCH_READ, |
| 24 &read_watcher_, |
| 25 this); |
| 26 } |
| 27 |
| 28 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { |
| 29 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 30 |
| 31 // Make sure that the fd given to us is the same one we started with. |
| 32 CHECK_EQ(fd, read_file_); |
| 33 |
| 34 // If this is a sendMessage request, stop trying to read after the first |
| 35 // message. |
| 36 if (is_send_message_) |
| 37 read_watcher_.StopWatchingFileDescriptor(); |
| 38 |
| 39 MessageType type; |
| 40 std::string message; |
| 41 if (!ReadMessage(&type, &message)) { |
| 42 // A read failed, is the process dead? |
| 43 if (base::GetTerminationStatus(native_process_handle_, NULL) != |
| 44 base::TERMINATION_STATUS_STILL_RUNNING) { |
| 45 read_watcher_.StopWatchingFileDescriptor(); |
| 46 // Notify the message service that the channel should close. |
| 47 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 48 base::Bind(&Client::CloseChannel, weak_client_ui_, |
| 49 destination_port_, true)); |
| 50 } |
| 51 return; |
| 52 } |
| 53 |
| 54 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 55 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, |
| 56 destination_port_, message)); |
| 57 } |
| 58 |
| 59 bool NativeMessageProcessHost::WriteData(FileHandle file, |
| 60 const char* data, |
| 61 size_t bytes_to_write) { |
| 62 return file_util::WriteFileDescriptor(file, data, bytes_to_write); |
| 63 } |
| 64 |
| 65 bool NativeMessageProcessHost::ReadData(FileHandle file, |
| 66 char* data, |
| 67 size_t bytes_to_read) { |
| 68 return file_util::ReadFromFD(file, data, bytes_to_read); |
| 69 } |
| 70 |
| 71 } // namespace extensions |
OLD | NEW |