OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/static_content/static_html_native_content.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/ui/overscroll_actions/overscroll_actions_controller.
h" |
| 10 #include "ios/chrome/browser/ui/static_content/static_html_view_controller.h" |
| 11 #import "ios/chrome/browser/ui/url_loader.h" |
| 12 #include "ios/web/public/referrer.h" |
| 13 |
| 14 @interface StaticHtmlNativeContent () |
| 15 // Designated initializer. |
| 16 - (instancetype)initWithLoader:(id<UrlLoader>)loader |
| 17 staticHTMLViewController:(StaticHtmlViewController*)HTMLViewController |
| 18 URL:(const GURL&)URL; |
| 19 @end |
| 20 |
| 21 @implementation StaticHtmlNativeContent { |
| 22 // The url of the controller. |
| 23 GURL _URL; |
| 24 // YES is web views are allowed to be created. |
| 25 BOOL _webUsageEnabled; |
| 26 // The static HTML view controller that is used to display the content in |
| 27 // a web view. |
| 28 base::scoped_nsobject<StaticHtmlViewController> _staticHTMLViewController; |
| 29 // Responsible for loading a particular URL. |
| 30 id<UrlLoader> _loader; // weak |
| 31 // The controller handling the overscroll actions. |
| 32 base::scoped_nsobject<OverscrollActionsController> |
| 33 _overscrollActionsController; |
| 34 } |
| 35 |
| 36 #pragma mark - |
| 37 #pragma mark Public |
| 38 |
| 39 - (instancetype)initWithLoader:(id<UrlLoader>)loader |
| 40 staticHTMLViewController:(StaticHtmlViewController*)HTMLViewController |
| 41 URL:(const GURL&)URL { |
| 42 DCHECK(loader); |
| 43 DCHECK(HTMLViewController); |
| 44 // No DCHECK for URL (invalid URL is a valid input). |
| 45 if (self = [super init]) { |
| 46 web::Referrer referrer(URL, web::ReferrerPolicyDefault); |
| 47 [HTMLViewController setLoader:loader referrer:referrer]; |
| 48 _URL = URL; |
| 49 _loader = loader; |
| 50 _staticHTMLViewController.reset([HTMLViewController retain]); |
| 51 } |
| 52 return self; |
| 53 } |
| 54 |
| 55 - (instancetype)initWithResourcePathResource:(NSString*)resourcePath |
| 56 loader:(id<UrlLoader>)loader |
| 57 browserState:(web::BrowserState*)browserState |
| 58 url:(const GURL&)URL { |
| 59 DCHECK(loader); |
| 60 DCHECK(browserState); |
| 61 DCHECK(URL.is_valid()); |
| 62 DCHECK(resourcePath); |
| 63 base::scoped_nsobject<StaticHtmlViewController> HTMLViewController( |
| 64 [[StaticHtmlViewController alloc] initWithResource:resourcePath |
| 65 browserState:browserState]); |
| 66 return [self initWithLoader:loader |
| 67 staticHTMLViewController:HTMLViewController |
| 68 URL:URL]; |
| 69 } |
| 70 |
| 71 - (void)loadURL:(const GURL&)URL |
| 72 referrer:(const web::Referrer&)referrer |
| 73 transition:(ui::PageTransition)transition |
| 74 rendererInitiated:(BOOL)rendererInitiated { |
| 75 [_loader loadURL:URL |
| 76 referrer:referrer |
| 77 transition:transition |
| 78 rendererInitiated:rendererInitiated]; |
| 79 } |
| 80 |
| 81 - (OverscrollActionsController*)overscrollActionsController { |
| 82 return _overscrollActionsController.get(); |
| 83 } |
| 84 |
| 85 - (void)setOverscrollActionsController: |
| 86 (OverscrollActionsController*)controller { |
| 87 _overscrollActionsController.reset([controller retain]); |
| 88 } |
| 89 |
| 90 #pragma mark - |
| 91 #pragma mark CRWNativeContent implementation |
| 92 |
| 93 - (void)close { |
| 94 [_overscrollActionsController invalidate]; |
| 95 } |
| 96 |
| 97 - (void)willUpdateSnapshot { |
| 98 [_overscrollActionsController clear]; |
| 99 } |
| 100 |
| 101 - (const GURL&)url { |
| 102 return _URL; |
| 103 } |
| 104 |
| 105 - (UIView*)view { |
| 106 CHECK(_webUsageEnabled) << "Tried to create a web view when web usage was" |
| 107 << " disabled!"; |
| 108 return [_staticHTMLViewController webView]; |
| 109 } |
| 110 |
| 111 - (NSString*)title { |
| 112 return [_staticHTMLViewController title]; |
| 113 } |
| 114 |
| 115 - (void)reload { |
| 116 [_staticHTMLViewController reload]; |
| 117 } |
| 118 |
| 119 - (void)handleLowMemory { |
| 120 [_staticHTMLViewController handleLowMemory]; |
| 121 } |
| 122 |
| 123 - (BOOL)isViewAlive { |
| 124 return [_staticHTMLViewController isViewAlive]; |
| 125 } |
| 126 |
| 127 - (void)executeJavaScript:(NSString*)script |
| 128 completionHandler:(web::JavaScriptResultBlock)handler { |
| 129 [_staticHTMLViewController executeJavaScript:script |
| 130 completionHandler:handler]; |
| 131 } |
| 132 |
| 133 - (void)setScrollEnabled:(BOOL)enabled { |
| 134 [_staticHTMLViewController setScrollEnabled:enabled]; |
| 135 } |
| 136 |
| 137 - (void)setWebUsageEnabled:(BOOL)webUsageEnabled { |
| 138 if (_webUsageEnabled == webUsageEnabled) { |
| 139 return; |
| 140 } |
| 141 _webUsageEnabled = webUsageEnabled; |
| 142 if (!_webUsageEnabled) { |
| 143 _staticHTMLViewController.reset(); |
| 144 } |
| 145 } |
| 146 |
| 147 - (UIScrollView*)scrollView { |
| 148 return [_staticHTMLViewController scrollView]; |
| 149 } |
| 150 |
| 151 @end |
OLD | NEW |