Chromium Code Reviews| 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/command_line.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/json/json_writer.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "base/path_service.h" | |
| 15 #include "base/pickle.h" | |
| 16 #include "base/process_util.h" | |
| 17 #include "base/values.h" | |
| 18 #include "chrome/common/chrome_paths.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/common/result_codes.h" | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 void NativeMessageProcessHost::ReadNowForTesting() { | |
| 25 OnFileCanReadWithoutBlocking(read_file_); | |
| 26 } | |
| 27 | |
| 28 void NativeMessageProcessHost::InitIO() { | |
| 29 // Always watch the read end. | |
| 30 MessageLoopForIO::current()->WatchFileDescriptor(read_file_, | |
| 31 true, /* persistent */ | |
| 32 MessageLoopForIO::WATCH_READ, | |
| 33 &read_watcher_, | |
| 34 this); | |
| 35 } | |
| 36 | |
| 37 void NativeMessageProcessHost::OnFileCanReadWithoutBlocking(int fd) { | |
| 38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 39 | |
| 40 // Make sure that the fd given to us is the same one we started with. | |
| 41 CHECK_EQ(fd, read_file_); | |
| 42 | |
| 43 // If this is a sendMessage request, stop trying to read after the first | |
| 44 // message. | |
| 45 if (is_send_message_) | |
| 46 read_watcher_.StopWatchingFileDescriptor(); | |
| 47 | |
| 48 MessageType type; | |
| 49 std::string message; | |
| 50 if (!ReadMessage(&type, &message)) { | |
| 51 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.
| |
| 52 return; | |
| 53 } | |
| 54 | |
| 55 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
| 56 base::Bind(&Client::PostMessageFromNativeProcess, weak_client_, | |
| 57 destination_port_, message)); | |
| 58 } | |
| 59 | |
| 60 bool NativeMessageProcessHost::WriteData(FileHandle file, 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, char* data, | |
| 66 size_t bytes_to_read) { | |
| 67 return file_util::ReadFromFD(file, data, bytes_to_read); | |
| 68 } | |
| 69 | |
| 70 } // namespace extensions | |
| OLD | NEW |