Home | History | Annotate | Download | only in dbus
      1 // Copyright 2014 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 #ifndef CHROMEOS_DBUS_PIPE_READER_H_
      6 #define CHROMEOS_DBUS_PIPE_READER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/files/file.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 
     16 namespace base {
     17 class TaskRunner;
     18 }
     19 
     20 namespace net {
     21 class FileStream;
     22 class IOBufferWithSize;
     23 }
     24 
     25 namespace chromeos {
     26 
     27 // Simple class to encapsulate collecting data from a pipe into a
     28 // string.  To use:
     29 //   - Instantiate the appropriate subclass of PipeReader
     30 //   - Call StartIO() which will create the appropriate FDs.
     31 //   - As data is received, the PipeReader will collect this data
     32 //     as appropriate to the subclass.
     33 //   - When the there is no more data to read, the PipeReader calls
     34 //     |callback|.
     35 class PipeReader {
     36  public:
     37   typedef base::Callback<void(void)> IOCompleteCallback;
     38 
     39   PipeReader(const scoped_refptr<base::TaskRunner>& task_runner,
     40              const IOCompleteCallback& callback);
     41   virtual ~PipeReader();
     42 
     43   // Starts data collection.
     44   // Returns the write end of the pipe if stream was setup correctly.
     45   // On success data will automatically be accumulated into a string that
     46   // can be retrieved with PipeReader::data().  To shutdown collection delete
     47   // the instance and/or use PipeReader::OnDataReady(-1).
     48   base::File StartIO();
     49 
     50   // Called when pipe data are available.  Can also be used to shutdown
     51   // data collection by passing -1 for |byte_count|.
     52   void OnDataReady(int byte_count);
     53 
     54   // Virtual function that subclasses will override in order to deal
     55   // with incoming data.
     56   virtual void AcceptData(const char *data, int length) = 0;
     57 
     58  private:
     59   scoped_ptr<net::FileStream> data_stream_;
     60   scoped_refptr<net::IOBufferWithSize> io_buffer_;
     61   scoped_refptr<base::TaskRunner> task_runner_;
     62   IOCompleteCallback callback_;
     63 
     64   // Note: This should remain the last member so it'll be destroyed and
     65   // invalidate its weak pointers before any other members are destroyed.
     66   base::WeakPtrFactory<PipeReader> weak_ptr_factory_;
     67 
     68   DISALLOW_COPY_AND_ASSIGN(PipeReader);
     69 };
     70 
     71 // PipeReader subclass which accepts incoming data to a string.
     72 class PipeReaderForString : public PipeReader {
     73  public:
     74   PipeReaderForString(const scoped_refptr<base::TaskRunner>& task_runner,
     75                       const IOCompleteCallback& callback);
     76 
     77   virtual void AcceptData(const char *data, int length) OVERRIDE;
     78 
     79   // Destructively returns collected data, by swapping |data_| with |data|.
     80   void GetData(std::string* data);
     81 
     82  private:
     83   std::string data_;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(PipeReaderForString);
     86 };
     87 
     88 }  // namespace chromeos
     89 
     90 #endif  // CHROMEOS_DBUS_PIPE_READER_H_
     91