Suppress data races in blink::Scheduler
[chromium-blink-merge.git] / net / test / url_request / url_request_mock_data_job.cc
blob54c174c091baa7ce0052291d041a052a8a86adea
1 // Copyright 2014 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 "net/test/url_request/url_request_mock_data_job.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/url_util.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/url_request/url_request_filter.h"
15 namespace net {
16 namespace {
18 const char kMockHostname[] = "mock.data";
20 // Gets the data from URL of the form:
21 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
22 std::string GetDataFromRequest(const net::URLRequest& request) {
23 std::string value;
24 if (!GetValueForKeyInQuery(request.url(), "data", &value))
25 return "default_data";
26 return value;
29 // Gets the numeric repeat count from URL of the form:
30 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
31 int GetRepeatCountFromRequest(const net::URLRequest& request) {
32 std::string value;
33 if (!GetValueForKeyInQuery(request.url(), "repeat", &value))
34 return 1;
36 int repeat_count;
37 if (!base::StringToInt(value, &repeat_count))
38 return 1;
40 DCHECK_GT(repeat_count, 0);
42 return repeat_count;
45 GURL GetMockUrl(const std::string& scheme,
46 const std::string& hostname,
47 const std::string& data,
48 int data_repeat_count) {
49 DCHECK_GT(data_repeat_count, 0);
50 std::string url(scheme + "://" + hostname + "/");
51 url.append("?data=");
52 url.append(data);
53 url.append("&repeat=");
54 url.append(base::IntToString(data_repeat_count));
55 return GURL(url);
58 class MockJobInterceptor : public net::URLRequestInterceptor {
59 public:
60 MockJobInterceptor() {}
61 ~MockJobInterceptor() override {}
63 // net::URLRequestInterceptor implementation
64 net::URLRequestJob* MaybeInterceptRequest(
65 net::URLRequest* request,
66 net::NetworkDelegate* network_delegate) const override {
67 return new URLRequestMockDataJob(request, network_delegate,
68 GetDataFromRequest(*request),
69 GetRepeatCountFromRequest(*request));
72 private:
73 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
76 } // namespace
78 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request,
79 NetworkDelegate* network_delegate,
80 const std::string& data,
81 int data_repeat_count)
82 : URLRequestJob(request, network_delegate),
83 data_offset_(0),
84 weak_factory_(this) {
85 DCHECK_GT(data_repeat_count, 0);
86 for (int i = 0; i < data_repeat_count; ++i) {
87 data_.append(data);
91 void URLRequestMockDataJob::Start() {
92 // Start reading asynchronously so that all error reporting and data
93 // callbacks happen as they would for network requests.
94 base::MessageLoop::current()->PostTask(
95 FROM_HERE, base::Bind(&URLRequestMockDataJob::StartAsync,
96 weak_factory_.GetWeakPtr()));
99 URLRequestMockDataJob::~URLRequestMockDataJob() {
102 bool URLRequestMockDataJob::ReadRawData(IOBuffer* buf,
103 int buf_size,
104 int* bytes_read) {
105 DCHECK(bytes_read);
106 *bytes_read = static_cast<int>(
107 std::min(static_cast<size_t>(buf_size), data_.length() - data_offset_));
108 memcpy(buf->data(), data_.c_str() + data_offset_, *bytes_read);
109 data_offset_ += *bytes_read;
110 return true;
113 void URLRequestMockDataJob::StartAsync() {
114 if (!request_)
115 return;
117 set_expected_content_size(data_.length());
118 NotifyHeadersComplete();
121 // static
122 void URLRequestMockDataJob::AddUrlHandler() {
123 return AddUrlHandlerForHostname(kMockHostname);
126 // static
127 void URLRequestMockDataJob::AddUrlHandlerForHostname(
128 const std::string& hostname) {
129 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
130 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
131 filter->AddHostnameInterceptor("http", hostname,
132 make_scoped_ptr(new MockJobInterceptor()));
133 filter->AddHostnameInterceptor("https", hostname,
134 make_scoped_ptr(new MockJobInterceptor()));
137 // static
138 GURL URLRequestMockDataJob::GetMockHttpUrl(const std::string& data,
139 int repeat_count) {
140 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count);
143 // static
144 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data,
145 int repeat_count) {
146 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count);
149 // static
150 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname(
151 const std::string& hostname,
152 const std::string& data,
153 int repeat_count) {
154 return GetMockUrl("http", hostname, data, repeat_count);
157 // static
158 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname(
159 const std::string& hostname,
160 const std::string& data,
161 int repeat_count) {
162 return GetMockUrl("https", hostname, data, repeat_count);
165 } // namespace net