Get foreground tab on Android
[chromium-blink-merge.git] / content / test / net / url_request_failed_job.cc
blob7bb8d2f7819be103006b26d3a60a8e4a096cc9eb
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 "content/test/net/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 content {
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 net::OK or net::ERR_IO_PENDING.
23 int GetErrorCode(net::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, net::ERR_IO_PENDING);
29 return net_error;
31 NOTREACHED();
32 return net::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, net::ERR_IO_PENDING);
40 return GURL(scheme + "://" + hostname + "/" + base::IntToString(net_error));
43 } // namespace
45 URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request,
46 net::NetworkDelegate* network_delegate,
47 int net_error)
48 : net::URLRequestJob(request, network_delegate),
49 net_error_(net_error),
50 weak_factory_(this) {}
52 URLRequestFailedJob::~URLRequestFailedJob() {}
54 void URLRequestFailedJob::Start() {
55 base::MessageLoop::current()->PostTask(
56 FROM_HERE,
57 base::Bind(&URLRequestFailedJob::StartAsync, weak_factory_.GetWeakPtr()));
60 // static
61 void URLRequestFailedJob::AddUrlHandler() {
62 return AddUrlHandlerForHostname(kMockHostname);
65 // static
66 void URLRequestFailedJob::AddUrlHandlerForHostname(
67 const std::string& hostname) {
68 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
69 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
70 filter->AddHostnameHandler("http", hostname, URLRequestFailedJob::Factory);
71 filter->AddHostnameHandler("https", hostname, URLRequestFailedJob::Factory);
74 // static
75 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) {
76 return GetMockHttpUrlForHostname(net_error, kMockHostname);
79 // static
80 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) {
81 return GetMockHttpsUrlForHostname(net_error, kMockHostname);
84 // static
85 GURL URLRequestFailedJob::GetMockHttpUrlForHostname(
86 int net_error, const std::string& hostname) {
87 return GetMockUrl("http", hostname, net_error);
90 // static
91 GURL URLRequestFailedJob::GetMockHttpsUrlForHostname(
92 int net_error, const std::string& hostname) {
93 return GetMockUrl("https", hostname, net_error);
96 // static
97 net::URLRequestJob* URLRequestFailedJob::Factory(
98 net::URLRequest* request,
99 net::NetworkDelegate* network_delegate,
100 const std::string& scheme) {
101 return new URLRequestFailedJob(
102 request, network_delegate, GetErrorCode(request));
105 void URLRequestFailedJob::StartAsync() {
106 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
107 net_error_));
110 } // namespace content