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

Side by Side Diff: net/dns/host_resolver_impl_fuzzer.cc

Issue 2116983002: Change HostResolver::Resolve() to take an std::unique_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: http_stream_factory_impl_job_controller_unittest RequestHandle* to unique_ptr Created 4 years, 4 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 20 matching lines...) Expand all
31 }; 31 };
32 32
33 class DnsRequest { 33 class DnsRequest {
34 public: 34 public:
35 DnsRequest(net::HostResolver* host_resolver, 35 DnsRequest(net::HostResolver* host_resolver,
36 net::FuzzedDataProvider* data_provider, 36 net::FuzzedDataProvider* data_provider,
37 std::vector<std::unique_ptr<DnsRequest>>* dns_requests) 37 std::vector<std::unique_ptr<DnsRequest>>* dns_requests)
38 : host_resolver_(host_resolver), 38 : host_resolver_(host_resolver),
39 data_provider_(data_provider), 39 data_provider_(data_provider),
40 dns_requests_(dns_requests), 40 dns_requests_(dns_requests),
41 handle_(nullptr),
42 is_running_(false) {} 41 is_running_(false) {}
43 42
44 ~DnsRequest() { 43 ~DnsRequest() {}
45 if (is_running_)
46 Cancel();
47 }
48 44
49 // Creates and starts a DNS request using fuzzed parameters. If the request 45 // Creates and starts a DNS request using fuzzed parameters. If the request
50 // doesn't complete synchronously, adds it to |dns_requests|. 46 // doesn't complete synchronously, adds it to |dns_requests|.
51 static void CreateRequest( 47 static void CreateRequest(
52 net::HostResolver* host_resolver, 48 net::HostResolver* host_resolver,
53 net::FuzzedDataProvider* data_provider, 49 net::FuzzedDataProvider* data_provider,
54 std::vector<std::unique_ptr<DnsRequest>>* dns_requests) { 50 std::vector<std::unique_ptr<DnsRequest>>* dns_requests) {
55 std::unique_ptr<DnsRequest> dns_request( 51 std::unique_ptr<DnsRequest> dns_request(
56 new DnsRequest(host_resolver, data_provider, dns_requests)); 52 new DnsRequest(host_resolver, data_provider, dns_requests));
57 53
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 auto request = dns_requests->begin() + index; 86 auto request = dns_requests->begin() + index;
91 (*request)->Cancel(); 87 (*request)->Cancel();
92 dns_requests->erase(request); 88 dns_requests->erase(request);
93 } 89 }
94 90
95 private: 91 private:
96 void OnCallback(int result) { 92 void OnCallback(int result) {
97 CHECK_NE(net::ERR_IO_PENDING, result); 93 CHECK_NE(net::ERR_IO_PENDING, result);
98 94
99 is_running_ = false; 95 is_running_ = false;
96 request_.reset();
100 97
101 // Remove |this| from |dns_requests| and take ownership of it, if it wasn't 98 // Remove |this| from |dns_requests| and take ownership of it, if it wasn't
102 // already removed from the vector. It may have been removed if this is in a 99 // already removed from the vector. It may have been removed if this is in a
103 // WaitForRequest call, in which case, do nothing. 100 // WaitForRequest call, in which case, do nothing.
104 std::unique_ptr<DnsRequest> self; 101 std::unique_ptr<DnsRequest> self;
105 for (auto request = dns_requests_->begin(); request != dns_requests_->end(); 102 for (auto request = dns_requests_->begin(); request != dns_requests_->end();
106 ++request) { 103 ++request) {
107 if (request->get() != this) 104 if (request->get() != this)
108 continue; 105 continue;
109 self = std::move(*request); 106 self = std::move(*request);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 145
149 // Decide if should be a cache-only resolution. 146 // Decide if should be a cache-only resolution.
150 if (data_provider_->ConsumeBool()) { 147 if (data_provider_->ConsumeBool()) {
151 return host_resolver_->ResolveFromCache(info, &address_list_, 148 return host_resolver_->ResolveFromCache(info, &address_list_,
152 net::BoundNetLog()); 149 net::BoundNetLog());
153 } 150 }
154 151
155 info.set_allow_cached_response(data_provider_->ConsumeBool()); 152 info.set_allow_cached_response(data_provider_->ConsumeBool());
156 return host_resolver_->Resolve( 153 return host_resolver_->Resolve(
157 info, priority, &address_list_, 154 info, priority, &address_list_,
158 base::Bind(&DnsRequest::OnCallback, base::Unretained(this)), &handle_, 155 base::Bind(&DnsRequest::OnCallback, base::Unretained(this)), &request_,
159 net::BoundNetLog()); 156 net::BoundNetLog());
160 } 157 }
161 158
162 // Waits until the request is done, if it isn't done already. 159 // Waits until the request is done, if it isn't done already.
163 void WaitUntilDone() { 160 void WaitUntilDone() {
164 CHECK(!run_loop_); 161 CHECK(!run_loop_);
165 if (is_running_) { 162 if (is_running_) {
166 run_loop_.reset(new base::RunLoop()); 163 run_loop_.reset(new base::RunLoop());
167 run_loop_->Run(); 164 run_loop_->Run();
168 run_loop_.reset(); 165 run_loop_.reset();
169 CHECK(!is_running_); 166 DCHECK(request_);
170 } 167 }
171 } 168 }
172 169
173 // Cancel the request, if not already completed. Otherwise, does nothing. 170 // Cancel the request, if not already completed. Otherwise, does nothing.
174 void Cancel() { 171 void Cancel() {
175 if (is_running_) 172 request_.reset();
176 host_resolver_->CancelRequest(handle_);
177 is_running_ = false; 173 is_running_ = false;
178 } 174 }
179 175
180 net::HostResolver* host_resolver_; 176 net::HostResolver* host_resolver_;
181 net::FuzzedDataProvider* data_provider_; 177 net::FuzzedDataProvider* data_provider_;
182 std::vector<std::unique_ptr<DnsRequest>>* dns_requests_; 178 std::vector<std::unique_ptr<DnsRequest>>* dns_requests_;
183 179
184 net::HostResolver::RequestHandle handle_; 180 std::unique_ptr<net::HostResolver::Request> request_;
185 net::AddressList address_list_; 181 net::AddressList address_list_;
186 182
187 bool is_running_; 183 bool is_running_;
188 184
189 std::unique_ptr<base::RunLoop> run_loop_; 185 std::unique_ptr<base::RunLoop> run_loop_;
190 186
191 DISALLOW_COPY_AND_ASSIGN(DnsRequest); 187 DISALLOW_COPY_AND_ASSIGN(DnsRequest);
192 }; 188 };
193 189
194 } // namespace 190 } // namespace
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 break; 226 break;
231 } 227 }
232 } 228 }
233 } 229 }
234 230
235 // Clean up any pending tasks, after deleting everything. 231 // Clean up any pending tasks, after deleting everything.
236 base::RunLoop().RunUntilIdle(); 232 base::RunLoop().RunUntilIdle();
237 233
238 return 0; 234 return 0;
239 } 235 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698