Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / test / url_request / url_request_failed_job.cc
blobb0e2536f42326164d446c1dc070e42940ad87218
1 // Copyright (c) 2012 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_failed_job.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_filter.h"
15 namespace net {
16 namespace {
18 const char kMockHostname[] = "mock.failed.request";
20 // Gets the numeric net error code from URL of the form:
21 // scheme://kMockHostname/error_code. The error code must be a valid
22 // net error code, and not OK or ERR_IO_PENDING.
23 int GetErrorCode(URLRequest* request) {
24 int net_error;
25 std::string path = request->url().path();
26 if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) {
27 CHECK_LT(net_error, 0);
28 CHECK_NE(net_error, ERR_IO_PENDING);
29 return net_error;
31 NOTREACHED();
32 return ERR_UNEXPECTED;
35 GURL GetMockUrl(const std::string& scheme,
36 const std::string& hostname,
37 int net_error) {
38 CHECK_LT(net_error, 0);
39 CHECK_NE(net_error, ERR_IO_PENDING);
40 return GURL(scheme + "://" + hostname + "/" + base::IntToString(net_error));
43 } // namespace
45 URLRequestFailedJob::URLRequestFailedJob(URLRequest* request,
46 NetworkDelegate* network_delegate,
47 int net_error)
48 : URLRequestJob(request, network_delegate),
49 net_error_(net_error),
50 weak_factory_(this) {
53 void URLRequestFailedJob::Start() {
54 base::MessageLoop::current()->PostTask(
55 FROM_HERE,
56 base::Bind(&URLRequestFailedJob::StartAsync, weak_factory_.GetWeakPtr()));
59 // static
60 void URLRequestFailedJob::AddUrlHandler() {
61 return AddUrlHandlerForHostname(kMockHostname);
64 // static
65 void URLRequestFailedJob::AddUrlHandlerForHostname(
66 const std::string& hostname) {
67 // Add |hostname| to URLRequestFilter for HTTP and HTTPS.
68 URLRequestFilter* filter = URLRequestFilter::GetInstance();
69 filter->AddHostnameHandler("http", hostname, URLRequestFailedJob::Factory);
70 filter->AddHostnameHandler("https", hostname, URLRequestFailedJob::Factory);
73 // static
74 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) {
75 return GetMockHttpUrlForHostname(net_error, kMockHostname);
78 // static
79 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) {
80 return GetMockHttpsUrlForHostname(net_error, kMockHostname);
83 // static
84 GURL URLRequestFailedJob::GetMockHttpUrlForHostname(
85 int net_error, const std::string& hostname) {
86 return GetMockUrl("http", hostname, net_error);
89 // static
90 GURL URLRequestFailedJob::GetMockHttpsUrlForHostname(
91 int net_error, const std::string& hostname) {
92 return GetMockUrl("https", hostname, net_error);
95 URLRequestFailedJob::~URLRequestFailedJob() {}
97 // static
98 URLRequestJob* URLRequestFailedJob::Factory(URLRequest* request,
99 NetworkDelegate* network_delegate,
100 const std::string& scheme) {
101 return new URLRequestFailedJob(
102 request, network_delegate, GetErrorCode(request));
105 void URLRequestFailedJob::StartAsync() {
106 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, net_error_));
109 } // namespace net