1 // Copyright 2017 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 MOJO_PUBLIC_CPP_SYSTEM_STRING_DATA_PIPE_PRODUCER_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_STRING_DATA_PIPE_PRODUCER_H_ 7 8 #include <string> 9 10 #include "base/callback_forward.h" 11 #include "base/macros.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/strings/string_piece.h" 14 #include "mojo/public/cpp/system/data_pipe.h" 15 #include "mojo/public/cpp/system/simple_watcher.h" 16 #include "mojo/public/cpp/system/system_export.h" 17 18 namespace mojo { 19 20 // Helper class which takes ownership of a ScopedDataPipeProducerHandle and 21 // assumes responsibility for feeding it the contents of a given string. This 22 // takes care of waiting for pipe capacity as needed, and can notify callers 23 // asynchronously when the operation is complete. 24 // 25 // Note that the StringDataPipeProducer must be kept alive until notified of 26 // completion to ensure that all of the string's data is written to the pipe. 27 // Premature destruction may result in partial or total truncation of data made 28 // available to the consumer. 29 class MOJO_CPP_SYSTEM_EXPORT StringDataPipeProducer { 30 public: 31 using CompletionCallback = base::OnceCallback<void(MojoResult result)>; 32 33 // Constructs a new StringDataPipeProducer which will write data to 34 // |producer|. 35 explicit StringDataPipeProducer(ScopedDataPipeProducerHandle producer); 36 ~StringDataPipeProducer(); 37 38 // Describes what happens to the data when an async writing situation occurs 39 // (where the pipe cannot immediately accept all of the data). 40 enum class AsyncWritingMode { 41 // The |data| given to Write() may be invalidated before completion 42 // |callback| is called. The pending |data| is copied and owned by this 43 // class until all bytes are written. 44 STRING_MAY_BE_INVALIDATED_BEFORE_COMPLETION, 45 // The |data| given to Write() stays valid until the completion |callback| 46 // is called. 47 STRING_STAYS_VALID_UNTIL_COMPLETION 48 }; 49 50 // Attempts to eventually write all of |data|. Invokes |callback| 51 // asynchronously when done. Note that |callback| IS allowed to delete this 52 // StringDataPipeProducer. 53 // 54 // If the data cannot be entirely written synchronously, then the |mode| 55 // determines how this class holds the pending data. 56 // 57 // If the write is successful |result| will be |MOJO_RESULT_OK|. Otherwise 58 // (e.g. if the producer detects the consumer is closed and the pipe has no 59 // remaining capacity) |result| will be |MOJO_RESULT_ABORTED|. 60 // 61 // Note that if the StringDataPipeProducer is destroyed before |callback| can 62 // be invoked, |callback| is *never* invoked, and the write will be 63 // permanently interrupted (and the producer handle closed) after making 64 // potentially only partial progress. 65 // 66 // Multiple writes may be performed in sequence (each one after the last 67 // completes), but Write() must not be called before the |callback| for the 68 // previous call to Write() (if any) has returned. 69 void Write(const base::StringPiece& data, 70 AsyncWritingMode mode, 71 CompletionCallback callback); 72 73 private: 74 void InvokeCallback(MojoResult result); 75 void OnProducerHandleReady(MojoResult ready_result, 76 const HandleSignalsState& state); 77 78 ScopedDataPipeProducerHandle producer_; 79 std::string data_; 80 base::StringPiece data_view_; 81 CompletionCallback callback_; 82 SimpleWatcher watcher_; 83 base::WeakPtrFactory<StringDataPipeProducer> weak_factory_; 84 85 DISALLOW_COPY_AND_ASSIGN(StringDataPipeProducer); 86 }; 87 88 } // namespace mojo 89 90 #endif // MOJO_PUBLIC_CPP_SYSTEM_STRING_DATA_PIPE_PRODUCER_H_ 91