Chromium Code Reviews| 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/ios/ns_error_util.h" |
| 10 #include "base/mac/scoped_nsobject.h" | |
|
Eugene But (OOO till 7-30)
2017/02/17 18:37:54
nit: s/include/import
Olivier
2017/02/20 08:47:00
Done.
| |
| 10 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "components/error_page/common/error_page_params.h" | 13 #include "components/error_page/common/error_page_params.h" |
| 13 #include "components/error_page/common/localized_error.h" | 14 #include "components/error_page/common/localized_error.h" |
| 14 #include "components/grit/components_resources.h" | 15 #include "components/grit/components_resources.h" |
| 15 #include "ios/chrome/browser/application_context.h" | 16 #include "ios/chrome/browser/application_context.h" |
| 16 #include "ios/web/public/referrer.h" | 17 #include "ios/web/public/referrer.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/page_transition_types.h" | 19 #include "ui/base/page_transition_types.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/base/webui/jstemplate_builder.h" | 21 #include "ui/base/webui/jstemplate_builder.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 24 #pragma mark - ErrorPageGenerator | |
| 25 | |
| 26 @interface ErrorPageGenerator : NSObject<HtmlGenerator> | |
|
Eugene But (OOO till 7-30)
2017/02/17 18:37:54
Please add comments to class and initializer. F.e.
Olivier
2017/02/20 08:47:00
Done.
| |
| 27 - (instancetype)initWithError:(NSError*)error | |
| 28 isPost:(BOOL)isPost | |
| 29 isIncognito:(BOOL)isIncognito NS_DESIGNATED_INITIALIZER; | |
| 30 | |
| 31 - (instancetype)init NS_UNAVAILABLE; | |
| 32 @end | |
| 33 | |
| 34 @implementation ErrorPageGenerator { | |
| 35 // Stores the HTML generated from the NSError in the initializer. | |
| 36 base::scoped_nsobject<NSString> html_; | |
| 37 } | |
| 38 | |
| 39 - (instancetype)initWithError:(NSError*)error | |
| 40 isPost:(BOOL)isPost | |
| 41 isIncognito:(BOOL)isIncognito { | |
| 42 self = [super init]; | |
| 43 if (self) { | |
| 44 NSString* badURLString = | |
|
Eugene But (OOO till 7-30)
2017/02/17 18:37:54
Optional nit. This would fit 80 symbols:
NSString
Olivier
2017/02/20 08:47:00
Done.
| |
| 45 [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]; | |
| 46 NSError* originalError = base::ios::GetFinalUnderlyingErrorFromError(error); | |
| 47 NSString* errorDomain = nil; | |
| 48 int errorCode = 0; | |
| 49 | |
| 50 if (originalError) { | |
| 51 errorDomain = [originalError domain]; | |
| 52 errorCode = [originalError code]; | |
| 53 } else { | |
| 54 errorDomain = [error domain]; | |
| 55 errorCode = [error code]; | |
| 56 } | |
| 57 DCHECK(errorCode != 0); | |
| 58 | |
| 59 base::DictionaryValue errorStrings; | |
| 60 error_page::LocalizedError::GetStrings( | |
| 61 errorCode, base::SysNSStringToUTF8(errorDomain), | |
| 62 GURL(base::SysNSStringToUTF16(badURLString)), isPost, false, false, | |
| 63 isIncognito, GetApplicationContext()->GetApplicationLocale(), nullptr, | |
| 64 &errorStrings); | |
| 65 | |
| 66 ui::ScaleFactor scaleFactor = | |
| 67 ResourceBundle::GetSharedInstance().GetMaxScaleFactor(); | |
| 68 | |
| 69 const base::StringPiece templateHTML( | |
| 70 ResourceBundle::GetSharedInstance().GetRawDataResourceForScale( | |
| 71 IDR_NET_ERROR_HTML, scaleFactor)); | |
| 72 if (templateHTML.empty()) | |
| 73 NOTREACHED() << "unable to load template. ID: " << IDR_NET_ERROR_HTML; | |
| 74 std::string errorHTML = webui::GetTemplatesHtml( | |
| 75 templateHTML, &errorStrings, "t" /* IDR_NET_ERROR_HTML root id */); | |
| 76 html_.reset([base::SysUTF8ToNSString(errorHTML) retain]); | |
| 77 } | |
| 78 return self; | |
| 79 } | |
| 80 | |
|
Eugene But (OOO till 7-30)
2017/02/17 18:37:54
Optional nit: pragma mark HtmlGenerator
Olivier
2017/02/20 08:47:00
Done.
| |
| 81 - (void)generateHtml:(HtmlCallback)callback { | |
| 82 callback(html_.get()); | |
| 83 } | |
| 84 | |
| 85 @end | |
| 86 | |
| 87 #pragma mark - ErrorPageContent | |
| 88 | |
| 23 @implementation ErrorPageContent | 89 @implementation ErrorPageContent |
| 24 | 90 |
| 25 #pragma mark - | 91 #pragma mark - |
| 26 #pragma mark CRWNativeContent methods | 92 #pragma mark CRWNativeContent methods |
| 27 | 93 |
| 28 // Override StaticHtmlNativeContent method to always force error pages to | 94 // Override StaticHtmlNativeContent method to always force error pages to |
| 29 // try loading the URL again, because the error might have been fixed. | 95 // try loading the URL again, because the error might have been fixed. |
| 30 - (void)reload { | 96 - (void)reload { |
| 31 // Because we don't have the original page transition at this point, just | 97 // 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. | 98 // use PAGE_TRANSITION_TYPED. We can revisit if this causes problems. |
| 33 [super loadURL:[self url] | 99 [super loadURL:[self url] |
| 34 referrer:web::Referrer() | 100 referrer:web::Referrer() |
| 35 transition:ui::PAGE_TRANSITION_TYPED | 101 transition:ui::PAGE_TRANSITION_TYPED |
| 36 rendererInitiated:YES]; | 102 rendererInitiated:YES]; |
| 37 } | 103 } |
| 38 | 104 |
| 39 - (void)generateHtml:(HtmlCallback)callback { | |
| 40 callback(html_.get()); | |
| 41 } | |
| 42 | |
| 43 - (id)initWithLoader:(id<UrlLoader>)loader | 105 - (id)initWithLoader:(id<UrlLoader>)loader |
| 44 browserState:(web::BrowserState*)browserState | 106 browserState:(web::BrowserState*)browserState |
| 45 url:(const GURL&)url | 107 url:(const GURL&)url |
| 46 error:(NSError*)error | 108 error:(NSError*)error |
| 47 isPost:(BOOL)isPost | 109 isPost:(BOOL)isPost |
| 48 isIncognito:(BOOL)isIncognito { | 110 isIncognito:(BOOL)isIncognito { |
| 49 NSString* badURLString = | 111 ErrorPageGenerator* generator = |
| 50 [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]; | 112 [[[ErrorPageGenerator alloc] initWithError:error |
| 51 NSError* originalError = base::ios::GetFinalUnderlyingErrorFromError(error); | 113 isPost:isPost |
| 52 NSString* errorDomain = nil; | 114 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 | 115 |
| 83 base::scoped_nsobject<StaticHtmlViewController> HTMLViewController( | 116 base::scoped_nsobject<StaticHtmlViewController> HTMLViewController( |
| 84 [[StaticHtmlViewController alloc] initWithGenerator:self | 117 [[StaticHtmlViewController alloc] initWithGenerator:generator |
| 85 browserState:browserState]); | 118 browserState:browserState]); |
| 86 return [super initWithLoader:loader | 119 return [super initWithLoader:loader |
| 87 staticHTMLViewController:HTMLViewController | 120 staticHTMLViewController:HTMLViewController |
| 88 URL:url]; | 121 URL:url]; |
| 89 } | 122 } |
| 90 | 123 |
| 91 @end | 124 @end |
| OLD | NEW |