Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: content/browser/loader/mojo_stream_writer.cc

Issue 1693563002: PROTOTYPE: PlzNavigate: use a Mojo data pipe to stream navigation data to the renderer. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "content/browser/loader/mojo_stream_writer.h"
6
7 #include "base/bind.h"
8 #include "base/guid.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "content/browser/streams/stream.h"
11 #include "content/browser/streams/stream_registry.h"
12 #include "content/public/browser/resource_controller.h"
13 #include "net/base/io_buffer.h"
14 #include "url/gurl.h"
15 #include "url/url_constants.h"
16
17 namespace content {
18
19 MojoStreamWriter::MojoStreamWriter()
20 : controller_(nullptr), immediate_mode_(false), buffer_(nullptr),
21 buffer_size_(-1) {
22 }
23
24 MojoStreamWriter::~MojoStreamWriter() {
25 if (data_producer_handle_.is_valid())
26 Finalize();
27 }
28
29 void MojoStreamWriter::InitializeStream(
30 mojo::ScopedDataPipeConsumerHandle* data_consumer_handle) {
31 LOG(ERROR) << "MojoStreamWriter@" << this << " InitializeStream";
32 DCHECK(!data_producer_handle_.is_valid());
33
34 // TODO(carlosk): why not have a default constructor for this struct???
35 // The user having to default initialize it is bad.
Ken Rockot(use gerrit already) 2016/02/11 16:40:39 The API at this level (mojo/public/c/system) is sp
carlosk 2016/02/16 15:34:07 Or the one in mojo/public/cpp/system/data_pipe.h c
36 MojoCreateDataPipeOptions options;
37 options.struct_size = sizeof(MojoCreateDataPipeOptions);
38 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
39 options.element_num_bytes = 1;
40 options.capacity_num_bytes = kReadBufSize;
41
42 mojo::DataPipe data_pipe(options);
43 data_producer_handle_ = std::move(data_pipe.producer_handle);
44 *data_consumer_handle = std::move(data_pipe.consumer_handle);
45
46 // TODO(carlosk): hack to comply with the "deferring" interface.
47 buffer_size_ = kReadBufSize;
48 // TODO(carlosk): Should make sure MOJO_WRITE_DATA_FLAG_ALL_OR_NONE is in fact
49 // needed here and in the Begin* calls below.
Ken Rockot(use gerrit already) 2016/02/11 16:40:39 The flag has no effect on Begin* (i.e. two-phase)
carlosk 2016/02/16 15:34:07 This is not what the method documentation for Mojo
50 CHECK_EQ(mojo::BeginWriteDataRaw(
51 data_producer_handle_.get(), &buffer_, &buffer_size_,
52 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), MOJO_RESULT_OK);
Ken Rockot(use gerrit already) 2016/02/11 16:40:39 I haven't dug into the details of how MojoStreamWr
carlosk 2016/02/16 15:34:07 These overridden observer method names are really
53 }
54
55 void MojoStreamWriter::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
56 int* buf_size,
57 int min_size) {
58 LOG(ERROR) << "MojoStreamWriter@" << this << " OnWillRead";
59
60 DCHECK(buf);
61 DCHECK(buf_size);
62 DCHECK_LE(min_size, base::checked_cast<int>(kReadBufSize));
63 *buf = new net::WrappedIOBuffer(static_cast<char*>(buffer_));
64 *buf_size = base::checked_cast<int>(buffer_size_);
65 buffer_ = nullptr;
66 buffer_size_ = -1;
67
68 // void* buffer = nullptr;
69 // uint32_t size = kReadBufSize;
70 // // TODO(carlosk): check if we need and how to handle error conditions. For
71 // // instance, we might need to move the bool* defer argument from
72 // // OnReadCompleted to this method instead for the case when
73 // // MOJO_RESULT_SHOULD_WAIT is returned indicating Mojo can't currently
74 // // allocate a buffer (somewhat similar to stream_->can_add_data()).
75
76 // CHECK_EQ(mojo::BeginWriteDataRaw(
77 // data_producer_handle_.get(), &buffer, &size,
78 // MOJO_WRITE_DATA_FLAG_ALL_OR_NONE), MOJO_RESULT_OK);
79
80 // // TODO(carlosk): Maybe the buffer pointer should still be stored locally t o
81 // // be enable the use of the CHECK in OnReadCompleted that would make sure
82 // // this method was called before that one.
83 // *buf = new net::WrappedIOBuffer(static_cast<char*>(buffer));
84 // *buf_size = base::checked_cast<int>(size);
85 }
86
87 void MojoStreamWriter::OnReadCompleted(int bytes_read, bool* defer) {
88 LOG(ERROR) << "MojoStreamWriter@" << this << " OnReadCompleted";
89 if (!bytes_read)
90 return;
91
92 // We have more data to read.
93 // DCHECK(read_buffer_.get());
94
95 // TODO(carlosk): we might need immediate_mode support for Mojo Data Pipes.
96 CHECK(mojo::EndWriteDataRaw(
97 data_producer_handle_.get(), bytes_read) == MOJO_RESULT_OK);
98
99 // TODO(carlosk): hack to comply with the "deferring" interface.
100 buffer_size_ = kReadBufSize;
101 MojoResult result = mojo::BeginWriteDataRaw(
102 data_producer_handle_.get(), &buffer_, &buffer_size_,
103 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
104
105 if (result == MOJO_RESULT_SHOULD_WAIT) {
106 handle_watcher_.Start(
107 data_producer_handle_.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
108 MOJO_DEADLINE_INDEFINITE,
109 base::Bind(&MojoStreamWriter::OnSpaceAvailable, base::Unretained(this))) ;
110 *defer = true;
111 LOG(ERROR) << "MojoStreamWriter@" << this << " OnReadCompleted - deferring!" ;
112 } else {
113 CHECK_EQ(result, MOJO_RESULT_OK);
114 }
115 }
116
117 void MojoStreamWriter::Finalize() {
118 DCHECK(data_producer_handle_.is_valid());
119 if (buffer_ != nullptr) {
120 CHECK_EQ(mojo::EndWriteDataRaw(data_producer_handle_.get(), 0), MOJO_RESULT_ OK);
121 buffer_size_ = -1;
122 }
123 data_producer_handle_.reset();
124 }
125
126 void MojoStreamWriter::OnSpaceAvailable(MojoResult result) {
127 LOG(ERROR) << "MojoStreamWriter@" << this << " OnSpaceAvailable - resuming!";
128 CHECK(!data_producer_handle_.is_valid() || result == MOJO_RESULT_OK);
129 controller_->Resume();
130 }
131
132 // void MojoStreamWriter::OnClose() {
133 // controller_->Cancel();
134 // }
135
136 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_stream_writer.h ('k') | content/browser/loader/navigation_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698