| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/web/error_page_content.h" | 5 #import "ios/chrome/browser/web/error_page_content.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #import "base/ios/ns_error_util.h" | 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #import "ios/chrome/browser/web/error_page_generator.h" |
| 11 #include "base/values.h" | |
| 12 #include "components/error_page/common/error_page_params.h" | |
| 13 #include "components/error_page/common/localized_error.h" | |
| 14 #include "components/grit/components_resources.h" | |
| 15 #include "ios/chrome/browser/application_context.h" | |
| 16 #include "ios/web/public/referrer.h" | 11 #include "ios/web/public/referrer.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 #include "ui/base/page_transition_types.h" | 12 #include "ui/base/page_transition_types.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | |
| 20 #include "ui/base/webui/jstemplate_builder.h" | |
| 21 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 22 | 14 |
| 23 @implementation ErrorPageContent | 15 @implementation ErrorPageContent |
| 24 | 16 |
| 25 #pragma mark - | 17 #pragma mark - |
| 26 #pragma mark CRWNativeContent methods | 18 #pragma mark CRWNativeContent methods |
| 27 | 19 |
| 28 // Override StaticHtmlNativeContent method to always force error pages to | 20 // Override StaticHtmlNativeContent method to always force error pages to |
| 29 // try loading the URL again, because the error might have been fixed. | 21 // try loading the URL again, because the error might have been fixed. |
| 30 - (void)reload { | 22 - (void)reload { |
| 31 // Because we don't have the original page transition at this point, just | 23 // Because we don't have the original page transition at this point, just |
| 32 // use PAGE_TRANSITION_TYPED. We can revisit if this causes problems. | 24 // use PAGE_TRANSITION_TYPED. We can revisit if this causes problems. |
| 33 [super loadURL:[self url] | 25 [super loadURL:[self url] |
| 34 referrer:web::Referrer() | 26 referrer:web::Referrer() |
| 35 transition:ui::PAGE_TRANSITION_TYPED | 27 transition:ui::PAGE_TRANSITION_TYPED |
| 36 rendererInitiated:YES]; | 28 rendererInitiated:YES]; |
| 37 } | 29 } |
| 38 | 30 |
| 39 - (void)generateHtml:(HtmlCallback)callback { | |
| 40 callback(html_.get()); | |
| 41 } | |
| 42 | |
| 43 - (id)initWithLoader:(id<UrlLoader>)loader | 31 - (id)initWithLoader:(id<UrlLoader>)loader |
| 44 browserState:(web::BrowserState*)browserState | 32 browserState:(web::BrowserState*)browserState |
| 45 url:(const GURL&)url | 33 url:(const GURL&)url |
| 46 error:(NSError*)error | 34 error:(NSError*)error |
| 47 isPost:(BOOL)isPost | 35 isPost:(BOOL)isPost |
| 48 isIncognito:(BOOL)isIncognito { | 36 isIncognito:(BOOL)isIncognito { |
| 49 NSString* badURLString = | 37 ErrorPageGenerator* generator = |
| 50 [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]; | 38 [[[ErrorPageGenerator alloc] initWithError:error |
| 51 NSError* originalError = base::ios::GetFinalUnderlyingErrorFromError(error); | 39 isPost:isPost |
| 52 NSString* errorDomain = nil; | 40 isIncognito:isIncognito] autorelease]; |
| 53 int errorCode = 0; | |
| 54 | |
| 55 if (originalError) { | |
| 56 errorDomain = [originalError domain]; | |
| 57 errorCode = [originalError code]; | |
| 58 } else { | |
| 59 errorDomain = [error domain]; | |
| 60 errorCode = [error code]; | |
| 61 } | |
| 62 DCHECK(errorCode != 0); | |
| 63 | |
| 64 base::DictionaryValue errorStrings; | |
| 65 error_page::LocalizedError::GetStrings( | |
| 66 errorCode, base::SysNSStringToUTF8(errorDomain), | |
| 67 GURL(base::SysNSStringToUTF16(badURLString)), isPost, false, false, | |
| 68 isIncognito, GetApplicationContext()->GetApplicationLocale(), nullptr, | |
| 69 &errorStrings); | |
| 70 | |
| 71 ui::ScaleFactor scaleFactor = | |
| 72 ResourceBundle::GetSharedInstance().GetMaxScaleFactor(); | |
| 73 | |
| 74 const base::StringPiece templateHTML( | |
| 75 ResourceBundle::GetSharedInstance().GetRawDataResourceForScale( | |
| 76 IDR_NET_ERROR_HTML, scaleFactor)); | |
| 77 if (templateHTML.empty()) | |
| 78 NOTREACHED() << "unable to load template. ID: " << IDR_NET_ERROR_HTML; | |
| 79 std::string errorHTML = webui::GetTemplatesHtml( | |
| 80 templateHTML, &errorStrings, "t" /* IDR_NET_ERROR_HTML root id */); | |
| 81 html_.reset([base::SysUTF8ToNSString(errorHTML) retain]); | |
| 82 | 41 |
| 83 base::scoped_nsobject<StaticHtmlViewController> HTMLViewController( | 42 base::scoped_nsobject<StaticHtmlViewController> HTMLViewController( |
| 84 [[StaticHtmlViewController alloc] initWithGenerator:self | 43 [[StaticHtmlViewController alloc] initWithGenerator:generator |
| 85 browserState:browserState]); | 44 browserState:browserState]); |
| 86 return [super initWithLoader:loader | 45 return [super initWithLoader:loader |
| 87 staticHTMLViewController:HTMLViewController | 46 staticHTMLViewController:HTMLViewController |
| 88 URL:url]; | 47 URL:url]; |
| 89 } | 48 } |
| 90 | 49 |
| 91 @end | 50 @end |
| OLD | NEW |