summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/pipe_reader.h
blob: 851eda6c10b6638dce1efb643304122782fa218c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_PIPE_READER_H_
#define CHROMEOS_DBUS_PIPE_READER_H_

#include <string>

#include "base/callback.h"
#include "base/files/file.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"

namespace base {
class TaskRunner;
}

namespace net {
class FileStream;
class IOBufferWithSize;
}

namespace chromeos {

// Simple class to encapsulate collecting data from a pipe into a
// string.  To use:
//   - Instantiate the appropriate subclass of PipeReader
//   - Call StartIO() which will create the appropriate FDs.
//   - As data is received, the PipeReader will collect this data
//     as appropriate to the subclass.
//   - When the there is no more data to read, the PipeReader calls
//     |callback|.
class PipeReader {
 public:
  typedef base::Callback<void(void)> IOCompleteCallback;

  PipeReader(const scoped_refptr<base::TaskRunner>& task_runner,
             const IOCompleteCallback& callback);
  virtual ~PipeReader();

  // Starts data collection.
  // Returns the write end of the pipe if stream was setup correctly.
  // On success data will automatically be accumulated into a string that
  // can be retrieved with PipeReader::data().  To shutdown collection delete
  // the instance and/or use PipeReader::OnDataReady(-1).
  base::File StartIO();

  // Called when pipe data are available.  Can also be used to shutdown
  // data collection by passing -1 for |byte_count|.
  void OnDataReady(int byte_count);

  // Virtual function that subclasses will override in order to deal
  // with incoming data.
  virtual void AcceptData(const char *data, int length) = 0;

 private:
  scoped_ptr<net::FileStream> data_stream_;
  scoped_refptr<net::IOBufferWithSize> io_buffer_;
  scoped_refptr<base::TaskRunner> task_runner_;
  IOCompleteCallback callback_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<PipeReader> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(PipeReader);
};

// PipeReader subclass which accepts incoming data to a string.
class PipeReaderForString : public PipeReader {
 public:
  PipeReaderForString(const scoped_refptr<base::TaskRunner>& task_runner,
                      const IOCompleteCallback& callback);

  virtual void AcceptData(const char *data, int length) override;

  // Destructively returns collected data, by swapping |data_| with |data|.
  void GetData(std::string* data);

 private:
  std::string data_;

  DISALLOW_COPY_AND_ASSIGN(PipeReaderForString);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_PIPE_READER_H_