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 "chromeos/process_proxy/process_output_watcher.h" | 5 #include "chromeos/process_proxy/process_output_watcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // We want to be sure we will be able to add 0 at the end of the input, so -1. | 60 // We want to be sure we will be able to add 0 at the end of the input, so -1. |
61 read_buffer_capacity_ = arraysize(read_buffer_) - 1; | 61 read_buffer_capacity_ = arraysize(read_buffer_) - 1; |
62 } | 62 } |
63 | 63 |
64 ProcessOutputWatcher::~ProcessOutputWatcher() {} | 64 ProcessOutputWatcher::~ProcessOutputWatcher() {} |
65 | 65 |
66 void ProcessOutputWatcher::Start() { | 66 void ProcessOutputWatcher::Start() { |
67 WatchProcessOutput(); | 67 WatchProcessOutput(); |
68 } | 68 } |
69 | 69 |
70 void ProcessOutputWatcher::OnFileCanReadWithoutBlocking(int fd) { | 70 void ProcessOutputWatcher::OnProcessOutputCanReadWithoutBlocking() { |
71 DCHECK_EQ(process_output_file_.GetPlatformFile(), fd); | 71 output_file_watcher_.reset(); |
72 | 72 ReadFromFd(process_output_file_.GetPlatformFile()); |
73 output_file_watcher_.StopWatchingFileDescriptor(); | |
74 ReadFromFd(fd); | |
75 } | |
76 | |
77 void ProcessOutputWatcher::OnFileCanWriteWithoutBlocking(int fd) { | |
78 NOTREACHED(); | |
79 } | 73 } |
80 | 74 |
81 void ProcessOutputWatcher::WatchProcessOutput() { | 75 void ProcessOutputWatcher::WatchProcessOutput() { |
82 base::MessageLoopForIO::current()->WatchFileDescriptor( | 76 output_file_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
83 process_output_file_.GetPlatformFile(), false, | 77 process_output_file_.GetPlatformFile(), |
84 base::MessageLoopForIO::WATCH_READ, &output_file_watcher_, this); | 78 base::Bind(&ProcessOutputWatcher::OnProcessOutputCanReadWithoutBlocking, |
| 79 base::Unretained(this))); |
85 } | 80 } |
86 | 81 |
87 void ProcessOutputWatcher::ReadFromFd(int fd) { | 82 void ProcessOutputWatcher::ReadFromFd(int fd) { |
88 // We don't want to necessary read pipe until it is empty so we don't starve | 83 // We don't want to necessary read pipe until it is empty so we don't starve |
89 // other streams in case data is written faster than we read it. If there is | 84 // other streams in case data is written faster than we read it. If there is |
90 // more than read_buffer_size_ bytes in pipe, it will be read in the next | 85 // more than read_buffer_size_ bytes in pipe, it will be read in the next |
91 // iteration. | 86 // iteration. |
92 DCHECK_GT(read_buffer_capacity_, read_buffer_size_); | 87 DCHECK_GT(read_buffer_capacity_, read_buffer_size_); |
93 ssize_t bytes_read = | 88 ssize_t bytes_read = |
94 HANDLE_EINTR(read(fd, &read_buffer_[read_buffer_size_], | 89 HANDLE_EINTR(read(fd, &read_buffer_[read_buffer_size_], |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // update the buffer size accordingly. | 158 // update the buffer size accordingly. |
164 if (output_to_report < read_buffer_size_) { | 159 if (output_to_report < read_buffer_size_) { |
165 for (size_t i = output_to_report; i < read_buffer_size_; ++i) { | 160 for (size_t i = output_to_report; i < read_buffer_size_; ++i) { |
166 read_buffer_[i - output_to_report] = read_buffer_[i]; | 161 read_buffer_[i - output_to_report] = read_buffer_[i]; |
167 } | 162 } |
168 } | 163 } |
169 read_buffer_size_ -= output_to_report; | 164 read_buffer_size_ -= output_to_report; |
170 } | 165 } |
171 | 166 |
172 } // namespace chromeos | 167 } // namespace chromeos |
OLD | NEW |