Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: ios/chrome/browser/web/error_page_content.mm

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

Powered by Google App Engine
This is Rietveld 408576698