| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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_STRING_WRITER_H_ |
| 6 #define CHROMEOS_DBUS_PIPE_STRING_WRITER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/files/scoped_file.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "chromeos/chromeos_export.h" |
| 17 |
| 18 namespace base { |
| 19 class TaskRunner; |
| 20 } |
| 21 |
| 22 namespace net { |
| 23 class FileStream; |
| 24 class DrainableIOBuffer; |
| 25 class StringIOBuffer; |
| 26 } |
| 27 |
| 28 namespace chromeos { |
| 29 |
| 30 // Simple class to encapsulate sending data to a pipe from a |
| 31 // string. To use: |
| 32 // - Instantiate the PipeStringWriter with data string. |
| 33 // - Call StartIO() which will create the appropriate FDs. |
| 34 // - As consumer will be ready to read the data, the PipeStringWriter will |
| 35 // write the data into the pipe. |
| 36 // - When all the data was sent, the PipeStringWriter calls |
| 37 // |callback|. |
| 38 class CHROMEOS_EXPORT PipeStringWriter { |
| 39 public: |
| 40 typedef base::Callback<void(int error_code)> IOCompleteCallback; |
| 41 |
| 42 PipeStringWriter(const std::string& data, |
| 43 const scoped_refptr<base::TaskRunner>& task_runner, |
| 44 const IOCompleteCallback& callback); |
| 45 virtual ~PipeStringWriter(); |
| 46 |
| 47 // Starts data collection. |
| 48 // Returns the read end of the pipe if stream was setup correctly. |
| 49 base::ScopedFD StartIO(); |
| 50 |
| 51 private: |
| 52 void OnDataWritten(int byte_count); |
| 53 |
| 54 std::unique_ptr<net::FileStream> data_stream_; |
| 55 scoped_refptr<net::StringIOBuffer> string_io_buffer_; |
| 56 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; |
| 57 scoped_refptr<base::TaskRunner> task_runner_; |
| 58 IOCompleteCallback callback_; |
| 59 |
| 60 // Note: This should remain the last member so it'll be destroyed and |
| 61 // invalidate its weak pointers before any other members are destroyed. |
| 62 base::WeakPtrFactory<PipeStringWriter> weak_ptr_factory_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(PipeStringWriter); |
| 65 }; |
| 66 |
| 67 } // namespace chromeos |
| 68 |
| 69 #endif // CHROMEOS_DBUS_PIPE_STRING_WRITER_H_ |
| OLD | NEW |