1 // Copyright 2015 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.
5 #include "components/web_view/url_request_cloneable.h"
7 #include "base/logging.h"
8 #include "mojo/common/data_pipe_utils.h"
12 URLRequestCloneable::URLRequestCloneable(mojo::URLRequestPtr original_request
)
13 : url_(original_request
->url
),
14 method_(original_request
->method
),
15 headers_(original_request
->headers
.Pass()),
16 response_body_buffer_size_(original_request
->response_body_buffer_size
),
17 auto_follow_redirects_(original_request
->auto_follow_redirects
),
18 bypass_cache_(original_request
->bypass_cache
),
19 original_body_null_(original_request
->body
.is_null()),
20 body_(original_request
->body
.size()) {
21 // TODO(erg): Maybe we can do some sort of async copy here?
22 for (size_t i
= 0; i
< original_request
->body
.size(); ++i
) {
23 mojo::common::BlockingCopyToString(original_request
->body
[i
].Pass(),
28 URLRequestCloneable::~URLRequestCloneable() {}
30 mojo::URLRequestPtr
URLRequestCloneable::Clone() const {
31 mojo::URLRequestPtr request
= mojo::URLRequest::New();
33 request
->method
= method_
;
34 request
->headers
= headers_
.Clone();
35 request
->response_body_buffer_size
= response_body_buffer_size_
;
36 request
->auto_follow_redirects
= auto_follow_redirects_
;
37 request
->bypass_cache
= bypass_cache_
;
39 if (!original_body_null_
) {
41 mojo::Array
<mojo::ScopedDataPipeConsumerHandle
>(body_
.size());
42 for (size_t i
= 0; i
< body_
.size(); ++i
) {
43 uint32_t num_bytes
= static_cast<uint32_t>(body_
[i
].size());
44 MojoCreateDataPipeOptions options
;
45 options
.struct_size
= sizeof(MojoCreateDataPipeOptions
);
46 options
.flags
= MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE
;
47 options
.element_num_bytes
= 1;
48 options
.capacity_num_bytes
= num_bytes
;
49 mojo::DataPipe
data_pipe(options
);
50 request
->body
[i
] = data_pipe
.consumer_handle
.Pass();
51 WriteDataRaw(data_pipe
.producer_handle
.get(), body_
[i
].data(), &num_bytes
,
52 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE
);
53 DCHECK_EQ(num_bytes
, body_
[i
].size());
57 return request
.Pass();
60 } // namespace web_view