1 // Copyright 2018 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_DATA_PIPE_BYTES_CONSUMER_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_DATA_PIPE_BYTES_CONSUMER_H_ 7 8 #include <memory> 9 10 #include "base/memory/scoped_refptr.h" 11 #include "mojo/public/cpp/system/data_pipe.h" 12 #include "mojo/public/cpp/system/simple_watcher.h" 13 #include "third_party/blink/renderer/platform/heap/handle.h" 14 #include "third_party/blink/renderer/platform/loader/fetch/bytes_consumer.h" 15 #include "third_party/blink/renderer/platform/platform_export.h" 16 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" 17 18 namespace base { 19 class SingleThreadTaskRunner; 20 } // namespace base 21 22 namespace blink { 23 24 // An adapter for mojo::DataPipe. As mojo::DataPipe lacks signals completion and 25 // error signals, we define another interface, CompletionNotifier, for the 26 // signals. 27 class PLATFORM_EXPORT DataPipeBytesConsumer final : public BytesConsumer { 28 USING_PRE_FINALIZER(DataPipeBytesConsumer, Dispose); 29 30 public: 31 class PLATFORM_EXPORT CompletionNotifier final 32 : public GarbageCollected<CompletionNotifier> { 33 public: CompletionNotifier(DataPipeBytesConsumer * bytes_consumer)34 explicit CompletionNotifier(DataPipeBytesConsumer* bytes_consumer) 35 : bytes_consumer_(bytes_consumer) {} 36 37 // One of these methods must be called to signal the end of the data 38 // stream. We cannot assume that the end of the pipe completes the 39 // stream successfully since errors can occur after the last byte is 40 // written into the pipe. 41 void SignalComplete(); 42 void SignalError(const BytesConsumer::Error& error); 43 void Trace(Visitor*); 44 45 private: 46 const WeakMember<DataPipeBytesConsumer> bytes_consumer_; 47 }; 48 49 DataPipeBytesConsumer(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 50 mojo::ScopedDataPipeConsumerHandle, 51 CompletionNotifier** notifier); 52 ~DataPipeBytesConsumer() override; 53 54 Result BeginRead(const char** buffer, size_t* available) override; 55 Result EndRead(size_t read_size) override; 56 mojo::ScopedDataPipeConsumerHandle DrainAsDataPipe() override; 57 void SetClient(BytesConsumer::Client*) override; 58 void ClearClient() override; 59 60 void Cancel() override; 61 PublicState GetPublicState() const override; GetError()62 Error GetError() const override { 63 DCHECK(state_ == InternalState::kErrored); 64 return error_; 65 } DebugName()66 String DebugName() const override { return "DataPipeBytesConsumer"; } 67 68 void Trace(Visitor*) override; 69 70 private: 71 bool IsReadableOrWaiting() const; 72 void MaybeClose(); 73 void SetError(const Error& error); 74 void Notify(MojoResult); 75 void ClearDataPipe(); 76 void SignalComplete(); 77 void SignalError(const Error& error); 78 void Dispose(); 79 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 81 mojo::ScopedDataPipeConsumerHandle data_pipe_; 82 mojo::SimpleWatcher watcher_; 83 Member<BytesConsumer::Client> client_; 84 InternalState state_ = InternalState::kWaiting; 85 Error error_; 86 bool is_in_two_phase_read_ = false; 87 bool has_pending_notification_ = false; 88 bool has_pending_complete_ = false; 89 bool has_pending_error_ = false; 90 bool completion_signaled_ = false; 91 }; 92 93 } // namespace blink 94 95 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_DATA_PIPE_BYTES_CONSUMER_H_ 96