blob: 168c586a82dc0ce7e86ca02557bf1c1ee9a9ffc0 [file] [log] [blame]
[email protected]a504ba72012-01-25 01:42:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8f427982011-12-13 23:40:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3a01162b2013-03-12 20:22:485#ifndef CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_
6#define CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_
[email protected]8f427982011-12-13 23:40:237
avi6e1a22d2015-12-21 03:43:208#include <stddef.h>
9
[email protected]8f427982011-12-13 23:40:2310#include <string>
11
12#include "base/callback.h"
tbarzic6ca50552015-08-10 22:32:4513#include "base/files/file.h"
avi6e1a22d2015-12-21 03:43:2014#include "base/macros.h"
tbarzic6ca50552015-08-10 22:32:4515#include "base/memory/weak_ptr.h"
16#include "base/message_loop/message_loop.h"
[email protected]3a01162b2013-03-12 20:22:4817#include "chromeos/chromeos_export.h"
[email protected]8f427982011-12-13 23:40:2318
[email protected]3a01162b2013-03-12 20:22:4819namespace chromeos {
20
[email protected]8f427982011-12-13 23:40:2321enum ProcessOutputType {
22 PROCESS_OUTPUT_TYPE_OUT,
[email protected]a504ba72012-01-25 01:42:5723 PROCESS_OUTPUT_TYPE_EXIT
[email protected]8f427982011-12-13 23:40:2324};
25
tbarzic170f12c2016-01-08 05:15:2926typedef base::Callback<void(ProcessOutputType,
27 const std::string&,
28 const base::Closure&)> ProcessOutputCallback;
[email protected]8f427982011-12-13 23:40:2329
[email protected]65bbd9d22014-05-15 20:34:3730// Observes output on |out_fd| and invokes |callback| when some output is
31// detected. It assumes UTF8 output.
tbarzic6ca50552015-08-10 22:32:4532class CHROMEOS_EXPORT ProcessOutputWatcher
33 : public base::MessageLoopForIO::Watcher {
[email protected]8f427982011-12-13 23:40:2334 public:
tbarzic6ca50552015-08-10 22:32:4535 ProcessOutputWatcher(int out_fd, const ProcessOutputCallback& callback);
36 ~ProcessOutputWatcher() override;
tbarzic296875c2015-05-14 19:40:3737
[email protected]8f427982011-12-13 23:40:2338 void Start();
39
40 private:
tbarzic6ca50552015-08-10 22:32:4541 // MessageLoopForIO::Watcher overrides:
42 void OnFileCanReadWithoutBlocking(int fd) override;
43 void OnFileCanWriteWithoutBlocking(int fd) override;
[email protected]8f427982011-12-13 23:40:2344
tbarzic6ca50552015-08-10 22:32:4545 // Listens to output from fd passed to the constructor.
[email protected]8f427982011-12-13 23:40:2346 void WatchProcessOutput();
47
tbarzic6ca50552015-08-10 22:32:4548 // Reads data from fd and invokes callback |on_read_callback_| with read data.
49 void ReadFromFd(int fd);
[email protected]8f427982011-12-13 23:40:2350
[email protected]65bbd9d22014-05-15 20:34:3751 // Checks if the read buffer has any trailing incomplete UTF8 characters and
52 // returns the read buffer size without them.
53 size_t OutputSizeWithoutIncompleteUTF8();
54
55 // Processes new |read_buffer_| state and notifies observer about new process
56 // output.
tbarzic170f12c2016-01-08 05:15:2957 void ReportOutput(ProcessOutputType type,
58 size_t new_bytes_count,
59 const base::Closure& callback);
[email protected]65bbd9d22014-05-15 20:34:3760
tbarzic170f12c2016-01-08 05:15:2961 char read_buffer_[4096];
[email protected]65bbd9d22014-05-15 20:34:3762 // Maximum read buffer content size.
63 size_t read_buffer_capacity_;
64 // Current read bufferi content size.
65 size_t read_buffer_size_;
[email protected]8f427982011-12-13 23:40:2366
tbarzic6ca50552015-08-10 22:32:4567 // Contains file descsriptor to which watched process output is written.
68 base::File process_output_file_;
69 base::MessageLoopForIO::FileDescriptorWatcher output_file_watcher_;
[email protected]8f427982011-12-13 23:40:2370
71 // Callback that will be invoked when some output is detected.
72 ProcessOutputCallback on_read_callback_;
73
tbarzic6ca50552015-08-10 22:32:4574 base::WeakPtrFactory<ProcessOutputWatcher> weak_factory_;
75
[email protected]8f427982011-12-13 23:40:2376 DISALLOW_COPY_AND_ASSIGN(ProcessOutputWatcher);
77};
[email protected]3a01162b2013-03-12 20:22:4878
79} // namespace chromeos
80
81#endif // CHROMEOS_PROCESS_PROXY_PROCESS_OUTPUT_WATCHER_H_