OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/web_view/url_request_cloneable.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "mojo/common/data_pipe_utils.h" | |
12 #include "mojo/common/url_type_converters.h" | |
13 | |
14 namespace web_view { | |
15 | |
16 // TODO(erg): In the long run, we might not want to have a stack of | |
17 // URLRequestPtrs, but another type that captures most of the data. When I saw | |
18 // NavigationController the first time, I didn't understand why they made their | |
19 // own datastructure which kept track of everything in a request. The reason is | |
20 // that they have to build requests from multiple different datatypes. | |
21 | |
22 URLRequestCloneable::URLRequestCloneable(mojo::URLRequestPtr original_request) | |
23 : url_(original_request->url), | |
24 method_(original_request->method), | |
25 headers_(std::move(original_request->headers)), | |
26 response_body_buffer_size_(original_request->response_body_buffer_size), | |
27 auto_follow_redirects_(original_request->auto_follow_redirects), | |
28 bypass_cache_(original_request->bypass_cache), | |
29 original_body_null_(original_request->body.is_null()), | |
30 body_(original_request->body.size()), | |
31 originating_time_(base::TimeTicks::FromInternalValue( | |
32 original_request->originating_time_ticks)) { | |
33 // TODO(erg): Maybe we can do some sort of async copy here? | |
34 for (size_t i = 0; i < original_request->body.size(); ++i) { | |
35 mojo::common::BlockingCopyToString(std::move(original_request->body[i]), | |
36 &body_[i]); | |
37 } | |
38 } | |
39 | |
40 URLRequestCloneable::URLRequestCloneable(const GURL& raw_url) | |
41 : url_(mojo::String::From(raw_url)), | |
42 method_("GET"), | |
43 headers_(), | |
44 response_body_buffer_size_(0), | |
45 auto_follow_redirects_(false), | |
46 bypass_cache_(false), | |
47 original_body_null_(true) { | |
48 } | |
49 | |
50 URLRequestCloneable::~URLRequestCloneable() {} | |
51 | |
52 mojo::URLRequestPtr URLRequestCloneable::Clone() const { | |
53 mojo::URLRequestPtr request = mojo::URLRequest::New(); | |
54 request->url = url_; | |
55 request->method = method_; | |
56 request->headers = headers_.Clone(); | |
57 request->response_body_buffer_size = response_body_buffer_size_; | |
58 request->auto_follow_redirects = auto_follow_redirects_; | |
59 request->bypass_cache = bypass_cache_; | |
60 | |
61 if (!original_body_null_) { | |
62 request->body = | |
63 mojo::Array<mojo::ScopedDataPipeConsumerHandle>(body_.size()); | |
64 for (size_t i = 0; i < body_.size(); ++i) { | |
65 uint32_t num_bytes = static_cast<uint32_t>(body_[i].size()); | |
66 MojoCreateDataPipeOptions options; | |
67 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
68 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
69 options.element_num_bytes = 1; | |
70 options.capacity_num_bytes = num_bytes; | |
71 mojo::DataPipe data_pipe(options); | |
72 request->body[i] = std::move(data_pipe.consumer_handle); | |
73 WriteDataRaw(data_pipe.producer_handle.get(), body_[i].data(), &num_bytes, | |
74 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | |
75 DCHECK_EQ(num_bytes, body_[i].size()); | |
76 } | |
77 } | |
78 | |
79 request->originating_time_ticks = originating_time_.ToInternalValue(); | |
80 | |
81 return request; | |
82 } | |
83 | |
84 } // namespace web_view | |
OLD | NEW |