blob: 14b9c5c23464c2f5108ae480f61e3ad740c35ed8 [file] [log] [blame]
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <UIKit/UIKit.h>
#include "ios/web/public/deprecated/url_verification_constants.h"
#import "ios/web/web_state/ui/crw_touch_tracking_recognizer.h"
#import "ios/web/web_state/ui/crw_web_view_navigation_proxy.h"
namespace web {
enum class NavigationInitiationType;
enum class WKNavigationState;
} // namespace web
@class CRWJSInjector;
@protocol CRWScrollableContent;
@protocol CRWSwipeRecognizerProvider;
@class CRWWebViewContentView;
@protocol CRWWebViewProxy;
class GURL;
@class WKWebView;
namespace web {
class NavigationItem;
class NavigationItemImpl;
class WebState;
class WebStateImpl;
// Manages a view that can be used either for rendering web content in a web
// view. CRWWebController also transparently evicts and restores the internal
// web view based on memory pressure, and manages access to interact with the
// web view.
// This is an abstract class which must not be instantiated directly.
// TODO(stuartmorgan): Move all of the navigation APIs out of this class.
@interface CRWWebController : NSObject <CRWTouchTrackingDelegate>
// Whether or not a UIWebView is allowed to exist in this CRWWebController.
// Defaults to NO; this should be enabled before attempting to access the view.
@property(nonatomic, assign) BOOL webUsageEnabled;
@property(nonatomic, weak) id<CRWSwipeRecognizerProvider>
// The container view used to display content. If the view has been purged due
// to low memory, this will recreate it.
@property(weak, nonatomic, readonly) UIView* view;
// The web view proxy associated with this controller.
@property(strong, nonatomic, readonly) id<CRWWebViewProxy> webViewProxy;
// The web view navigation proxy associated with this controller.
@property(weak, nonatomic, readonly) id<CRWWebViewNavigationProxy>
// The fraction of the page load that has completed as a number between 0.0
// (nothing loaded) and 1.0 (fully loaded).
@property(nonatomic, readonly) double loadingProgress;
// YES if the web process backing WebView is believed to currently be crashed.
@property(nonatomic, readonly, assign, getter=isWebProcessCrashed)
BOOL webProcessCrashed;
// Whether or not the user is currently interacting with the web content
// presented by this controller.
@property(nonatomic, readonly, assign, getter=isUserInteracting)
BOOL userInteracting;
// Whether the WebController is visible. Returns YES after wasShown call and
// NO after wasHidden() call.
@property(nonatomic, assign, getter=isVisible) BOOL visible;
// A Boolean value indicating whether horizontal swipe gestures will trigger
// back-forward list navigations.
@property(nonatomic) BOOL allowsBackForwardNavigationGestures;
// JavaScript injector.
@property(nonatomic, strong, readonly) CRWJSInjector* jsInjector;
// Whether the WebController should attempt to keep the render process alive.
@property(nonatomic, assign, getter=shouldKeepRenderProcessAlive)
BOOL keepsRenderProcessAlive;
// Designated initializer. Initializes web controller with |webState|. The
// calling code must retain the ownership of |webState|.
- (instancetype)initWithWebState:(web::WebStateImpl*)webState;
// Returns the latest navigation item created for new navigation, which is
// stored in navigation context.
- (web::NavigationItemImpl*)lastPendingItemForNewNavigation;
// Replaces the currently displayed content with |contentView|. The content
// view will be dismissed for the next navigation.
- (void)showTransientContentView:(UIView<CRWScrollableContent>*)contentView;
// Clear the transient content view, if one is shown. This is a delegate
// method for WebStateImpl::ClearTransientContent(). Callers should use the
// WebStateImpl API instead of calling this method directly.
- (void)clearTransientContentView;
// Removes the back WebView. DANGER: this method is exposed for the sole purpose
// of allowing NavigationManagerImpl to reset the back-forward history. Please
// reconsider before using this method.
- (void)removeWebView;
// Call when the CRWWebController needs go away. Caller must reset the delegate
// before calling.
- (void)close;
// Returns YES if there is currently a live view in the tab (e.g., the view
// hasn't been discarded due to low memory).
// NOTE: This should be used for metrics-gathering only; for any other purpose
// callers should not know or care whether the view is live.
- (BOOL)isViewAlive;
// Returns YES if the current live view is a web view with HTML.
// TODO( Remove once JSFindInPageManager is removed.
- (BOOL)contentIsHTML;
// Returns the CRWWebController's view of the current URL. Moreover, this method
// will set the trustLevel enum to the appropriate level from a security point
// of view. The caller has to handle the case where |trustLevel| is not
// appropriate, as this method won't display any error to the user.
- (GURL)currentURLWithTrustLevel:(web::URLVerificationTrustLevel*)trustLevel;
// Reloads web view. |isRendererInitiated| is YES for renderer-initiated
// navigation. |isRendererInitiated| is NO for browser-initiated navigation.
- (void)reloadWithRendererInitiatedNavigation:(BOOL)isRendererInitiated;
// Loads the URL indicated by current session state.
- (void)loadCurrentURLWithRendererInitiatedNavigation:(BOOL)rendererInitiated;
// Loads the URL indicated by current session state if the current page has not
// loaded yet. This method should never be called directly. Use
// NavigationManager::LoadIfNecessary() instead.
- (void)loadCurrentURLIfNecessary;
// Loads |data| of type |MIMEType| and replaces last committed URL with the
// given |URL|.
// If a load is in progress, it will be stopped before the data is loaded.
- (void)loadData:(NSData*)data
forURL:(const GURL&)URL;
// Stops loading the page.
- (void)stopLoading;
// Records the state (scroll position, form values, whatever can be harvested)
// from the current page into the current session entry.
- (void)recordStateInHistory;
// Notifies the CRWWebController that it has been shown.
- (void)wasShown;
// Notifies the CRWWebController that it has been hidden.
- (void)wasHidden;
// Instructs WKWebView to navigate to the given navigation item. |wk_item| and
// |item| must point to the same navigation item. Calling this method may
// result in an iframe navigation.
- (void)goToBackForwardListItem:(WKBackForwardListItem*)item
// Takes snapshot of web view with |rect|. |rect| should be in self.view's
// coordinate system. |completion| is always called, but |snapshot| may be nil.
// Prior to iOS 11, |completion| is called with a nil
// snapshot. |completion| may be called more than once.
- (void)takeSnapshotWithRect:(CGRect)rect
completion:(void (^)(UIImage* snapshot))completion;
// Creates PDF representation of the web page and invokes the |completion| with
// the NSData of the PDF or nil if a PDF couldn't be generated.
- (void)createFullPagePDFWithCompletion:
(void (^)(NSData* PDFDocumentData))completion;
// Creates a web view if it's not yet created. Returns the web view.
- (WKWebView*)ensureWebViewCreated;
// Removes the webView from the view hierarchy.
- (void)removeWebViewFromViewHierarchy;
// Adds the webView back in the view hierarchy.
- (void)addWebViewToViewHierarchy;
// Notifies this controller that the surface size has changed due to
// multiwindow action or orientation change.
- (void)surfaceSizeChanged;
#pragma mark Testing
@interface CRWWebController (UsedOnlyForTesting) // Testing or internal API.
@property(nonatomic, readonly) web::WebState* webState;
@property(nonatomic, readonly) web::WebStateImpl* webStateImpl;
// Returns the current page loading phase.
// TODO( Remove this once refactor is done.
@property(nonatomic, readonly, assign) web::WKNavigationState navigationState;
// Injects a CRWWebViewContentView for testing. Takes ownership of
// |webViewContentView|.
- (void)injectWebViewContentView:(CRWWebViewContentView*)webViewContentView;
- (void)resetInjectedWebViewContentView;
// Returns whether any observers are registered with the CRWWebController.
- (BOOL)hasObservers;
// Loads the HTML into the page at the given URL.
- (void)loadHTML:(NSString*)HTML forURL:(const GURL&)URL;