blob: 3c984571f74e7d0a2c4da840cea14fed29600317 [file] [log] [blame]
// 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.
#include <string>
#include "base/callback.h"
#include "base/files/file.h"
#include "base/macros.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 {
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;
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_;
// PipeReader subclass which accepts incoming data to a string.
class PipeReaderForString : public PipeReader {
PipeReaderForString(const scoped_refptr<base::TaskRunner>& task_runner,
const IOCompleteCallback& callback);
void AcceptData(const char* data, int length) override;
// Destructively returns collected data, by swapping |data_| with |data|.
void GetData(std::string* data);
std::string data_;
} // namespace chromeos