| 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_process_host.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
| 6 | 6 |
| 7 #include <unistd.h> | 7 #include <unistd.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 | 12 |
| 13 namespace extensions { | 13 namespace extensions { |
| 14 | 14 |
| 15 void NativeMessageProcessHost::ReadNowForTesting() { | 15 void NativeMessageProcessHost::ReadNowForTesting() { |
| 16 OnFileCanReadWithoutBlocking(read_file_); | 16 OnFileCanReadWithoutBlocking(read_file_); |
| 17 } | 17 } |
| 18 | 18 |
| 19 void NativeMessageProcessHost::InitIO() { | 19 void NativeMessageProcessHost::InitIO() { |
| 20 // Always watch the read end. | 20 // Always watch the read end. |
| 21 MessageLoopForIO::current()->WatchFileDescriptor(read_file_, | 21 MessageLoopForIO::current()->WatchFileDescriptor(read_file_, |
| 22 true, /* persistent */ | 22 true, /* persistent */ |
| 23 MessageLoopForIO::WATCH_READ, | 23 MessageLoopForIO::WATCH_READ, |
| 24 &read_watcher_, | 24 &read_watcher_, |
| 25 this); | 25 this); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void NativeMessageProcessHost::StopIO() { |
| 29 read_watcher_.StopWatchingFileDescriptor(); |
| 30 } |
| 31 |
| 32 bool NativeMessageProcessHost::ReadMessage(MessageType* type, |
| 33 std::string* message) { |
| 34 // Read the type (uint32) and length (uint32). |
| 35 char message_meta_data[8]; |
| 36 if (!file_util::ReadFromFD(read_file_, message_meta_data, 8)) { |
| 37 LOG(ERROR) << "Error reading the message type and length."; |
| 38 return false; |
| 39 } |
| 40 |
| 41 uint32 message_length; |
| 42 if (!VerifyMessageMetaData(message_meta_data, type, &message_length)) |
| 43 return false; |
| 44 |
| 45 message->resize(message_length, '\0'); |
| 46 if (!file_util::ReadFromFD(read_file_, &(*message)[0], message_length)) { |
| 47 LOG(ERROR) << "Error reading the json data."; |
| 48 return false; |
| 49 } |
| 50 |
| 51 return true; |
| 52 } |
| 53 |
| 28 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { | 54 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { |
| 29 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 55 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 30 | 56 |
| 31 // Make sure that the fd given to us is the same one we started with. | 57 // Make sure that the fd given to us is the same one we started with. |
| 32 CHECK_EQ(fd, read_file_); | 58 CHECK_EQ(fd, read_file_); |
| 33 | 59 |
| 34 // If this is a sendMessage request, stop trying to read after the first | 60 // If this is a sendMessage request, stop trying to read after the first |
| 35 // message. | 61 // message. |
| 36 if (is_send_message_) | 62 if (is_send_message_) |
| 37 read_watcher_.StopWatchingFileDescriptor(); | 63 read_watcher_.StopWatchingFileDescriptor(); |
| 38 | 64 |
| 39 MessageType type; | 65 MessageType type; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, | 81 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_ui_, |
| 56 destination_port_, message)); | 82 destination_port_, message)); |
| 57 } | 83 } |
| 58 | 84 |
| 59 bool NativeMessageProcessHost::WriteData(FileHandle file, | 85 bool NativeMessageProcessHost::WriteData(FileHandle file, |
| 60 const char* data, | 86 const char* data, |
| 61 size_t bytes_to_write) { | 87 size_t bytes_to_write) { |
| 62 return file_util::WriteFileDescriptor(file, data, bytes_to_write); | 88 return file_util::WriteFileDescriptor(file, data, bytes_to_write); |
| 63 } | 89 } |
| 64 | 90 |
| 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 | 91 } // namespace extensions |
| OLD | NEW |