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 "content/browser/net/network_error_request_job.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/values.h" |
| 9 #include "net/base/escape.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/log/net_log_util.h" |
| 12 #include "net/url_request/url_request.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 static const char kNetworkErrorKey[] = "netError"; |
| 18 |
| 19 NetworkErrorRequestJob::NetworkErrorRequestJob( |
| 20 net::URLRequest* request, |
| 21 net::NetworkDelegate* network_delegate) |
| 22 : net::URLRequestSimpleJob(request, network_delegate), |
| 23 error_code_(net::Error::OK) { |
| 24 std::basic_string<char> error_code_string = |
| 25 request->url().path().substr(1, std::string::npos); |
| 26 |
| 27 if (!base::StringToInt(error_code_string, &error_code_)) |
| 28 error_code_ = 0; |
| 29 } |
| 30 |
| 31 // Creates a HTML page listing all error codes. |
| 32 void ListAllErrorCodes(std::string* data) { |
| 33 std::string unescaped_query; |
| 34 std::string unescaped_title("Network error codes"); |
| 35 |
| 36 data->append("<!DOCTYPE html>\n<html>\n<head>\n"); |
| 37 data->append( |
| 38 "<meta http-equiv=\"Content-Security-Policy\" " |
| 39 "content=\"object-src 'none'; script-src 'none'\">"); |
| 40 data->append("<title>"); |
| 41 data->append(unescaped_title); |
| 42 data->append("</title>\n"); |
| 43 data->append("<link rel=\"stylesheet\""); |
| 44 data->append("href=\"chrome://resources/css/chrome_shared.css\">\n"); |
| 45 data->append("</head><body style=\"margin:1em\">"); |
| 46 data->append("<h1>"); |
| 47 data->append(unescaped_title); |
| 48 data->append("</h1>"); |
| 49 |
| 50 scoped_ptr<base::DictionaryValue> error_codes = net::GetNetConstants(); |
| 51 const base::DictionaryValue* net_error_codes_dict = nullptr; |
| 52 |
| 53 for (base::DictionaryValue::Iterator itr(*error_codes); !itr.IsAtEnd(); |
| 54 itr.Advance()) { |
| 55 if (itr.key() == kNetworkErrorKey) { |
| 56 itr.value().GetAsDictionary(&net_error_codes_dict); |
| 57 break; |
| 58 } |
| 59 } |
| 60 |
| 61 data->append("<ul>"); |
| 62 for (base::DictionaryValue::Iterator itr(*net_error_codes_dict); |
| 63 !itr.IsAtEnd(); itr.Advance()) { |
| 64 int error_code; |
| 65 itr.value().GetAsInteger(&error_code); |
| 66 data->append("<li><a href=\""); |
| 67 data->append(base::IntToString(error_code)); |
| 68 data->append("\">"); |
| 69 data->append(itr.key()); |
| 70 data->append("</a></li>\n"); |
| 71 } |
| 72 data->append("</ul>"); |
| 73 } |
| 74 |
| 75 int NetworkErrorRequestJob::GetData( |
| 76 std::string* mime_type, |
| 77 std::string* charset, |
| 78 std::string* data, |
| 79 const net::CompletionCallback& callback) const { |
| 80 mime_type->assign("text/html"); |
| 81 charset->assign("UTF8"); |
| 82 |
| 83 if (error_code_ == 0 || error_code_ == net::Error::ERR_IO_PENDING) { |
| 84 ListAllErrorCodes(data); |
| 85 return net::OK; |
| 86 } |
| 87 return error_code_; |
| 88 } |
| 89 |
| 90 } // namespace content |
OLD | NEW |