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_errors_listing_ui.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" |
| 10 #include "content/grit/content_resources.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/common/url_constants.h" |
| 13 #include "net/base/net_errors.h" |
| 14 #include "net/log/net_log_util.h" |
| 15 |
| 16 static const char kDataFile[] = "network-error-data.json"; |
| 17 static const char kErrorCodeField[] = "errorCode"; |
| 18 static const char kErrorCodesDataName[] = "errorCodes"; |
| 19 static const char kErrorIdField[] = "errorId"; |
| 20 static const char kNetworkErrorKey[] = "netError"; |
| 21 |
| 22 namespace content { |
| 23 |
| 24 namespace { |
| 25 |
| 26 scoped_ptr<base::ListValue> GetNetworkErrorData() { |
| 27 scoped_ptr<base::DictionaryValue> error_codes = net::GetNetConstants(); |
| 28 const base::DictionaryValue* net_error_codes_dict = nullptr; |
| 29 |
| 30 for (base::DictionaryValue::Iterator itr(*error_codes); !itr.IsAtEnd(); |
| 31 itr.Advance()) { |
| 32 if (itr.key() == kNetworkErrorKey) { |
| 33 itr.value().GetAsDictionary(&net_error_codes_dict); |
| 34 break; |
| 35 } |
| 36 } |
| 37 |
| 38 scoped_ptr<base::ListValue> error_list(new base::ListValue()); |
| 39 |
| 40 for (base::DictionaryValue::Iterator itr(*net_error_codes_dict); |
| 41 !itr.IsAtEnd(); itr.Advance()) { |
| 42 int error_code; |
| 43 itr.value().GetAsInteger(&error_code); |
| 44 // Exclude the aborted and pending codes as these don't return a page. |
| 45 if (error_code != net::Error::ERR_IO_PENDING && |
| 46 error_code != net::Error::ERR_ABORTED) { |
| 47 base::DictionaryValue* error = new base::DictionaryValue(); |
| 48 error->SetInteger(kErrorIdField, error_code); |
| 49 error->SetString(kErrorCodeField, itr.key()); |
| 50 error_list->Append(error); |
| 51 } |
| 52 } |
| 53 return error_list; |
| 54 } |
| 55 |
| 56 bool HandleRequestCallback(BrowserContext* current_context, |
| 57 const std::string& path, |
| 58 const WebUIDataSource::GotDataCallback& callback) { |
| 59 if (path != kDataFile) |
| 60 return false; |
| 61 |
| 62 base::DictionaryValue data; |
| 63 data.Set(kErrorCodesDataName, GetNetworkErrorData().release()); |
| 64 std::string json_string; |
| 65 base::JSONWriter::Write(data, &json_string); |
| 66 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 67 return true; |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 NetworkErrorsListingUI::NetworkErrorsListingUI(WebUI* web_ui) : |
| 73 WebUIController(web_ui) { |
| 74 // Set up the chrome://network-errors source. |
| 75 WebUIDataSource* html_source = |
| 76 WebUIDataSource::Create(kChromeUINetworkErrorsListingHost); |
| 77 |
| 78 // Add required resources. |
| 79 html_source->SetJsonPath("strings.js"); |
| 80 html_source->AddResourcePath("network_errors_listing.css", |
| 81 IDR_NETWORK_ERROR_LISTING_CSS); |
| 82 html_source->AddResourcePath("network_errors_listing.js", |
| 83 IDR_NETWORK_ERROR_LISTING_JS); |
| 84 html_source->SetDefaultResource(IDR_NETWORK_ERROR_LISTING_HTML); |
| 85 html_source->SetRequestFilter( |
| 86 base::Bind(&HandleRequestCallback, |
| 87 web_ui->GetWebContents()->GetBrowserContext())); |
| 88 |
| 89 BrowserContext* browser_context = |
| 90 web_ui->GetWebContents()->GetBrowserContext(); |
| 91 WebUIDataSource::Add(browser_context, html_source); |
| 92 } |
| 93 |
| 94 } // namespace content |
OLD | NEW |