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.
5 #ifndef IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_
6 #define IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_
8 #import <UIKit/UIKit.h>
10 #include "base/memory/scoped_ptr.h"
11 #import "ios/web/net/crw_request_tracker_delegate.h"
12 #import "ios/web/public/web_state/crw_web_user_interface_delegate.h"
13 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
14 #import "ios/web/public/web_state/ui/crw_web_delegate.h"
15 #include "ios/web/public/web_state/url_verification_constants.h"
16 #import "ios/web/web_state/ui/crw_touch_tracking_recognizer.h"
22 // In the LOAD_REQUESTED phase, the system predicts a page change is going to
23 // happen but the page URL has not yet changed.
25 // In the PAGE_LOADING phase, the page URL has changed but the whole document
26 // may not be available for use.
28 // In the PAGE_LOADED phase, either the page had loaded and is available for
29 // use, the load was cancelled, or the UIWebView is new and ready for a load.
33 // Policy for web page dialog handling.
34 enum PageDialogOpenPolicy
{
35 // Default policy. Dialogs are allowed, clients are not notified on display.
36 DIALOG_POLICY_ALLOW
= 0,
37 // Dialogs are allowed, clients are notified when dialog will display with
38 // -[WebDelegate webControllerWillShowDialog:] delegate method call.
39 DIALOG_POLICY_NOTIFY_FIRST
,
40 // Dialogs are not allowed, client are notified when dialog did block with
41 // -[WebDelegate webControllerDidSuppressDialog:] delegate method call.
42 DIALOG_POLICY_SUPPRESS
45 // The accessibility identifier of the top-level container view.
46 extern NSString
* const kContainerViewID
;
50 @
class CRWJSInjectionReceiver
;
51 @protocol CRWNativeContent
;
52 @protocol CRWNativeContentProvider
;
53 @protocol CRWSwipeRecognizerProvider
;
54 @protocol CRWWebControllerObserver
;
55 @
class CRWWebViewContentView
;
56 @protocol CRWWebViewProxy
;
67 class WebInterstitialImpl
;
71 // Manages a view that can be used either for rendering web content in a web
72 // view, or native content in a view provided by a NativeContentProvider.
73 // CRWWebController also transparently evicts and restores the internal web
74 // view based on memory pressure, and manages access to interact with the
76 // This is an abstract class which must not be instantiated directly. A factory
77 // function from web_controller_factory.h should be used instead.
78 // TODO(stuartmorgan): Move all of the navigation APIs out of this class.
79 @interface CRWWebController
: NSObject
<CRWJSInjectionEvaluator
,
80 CRWRequestTrackerDelegate
,
81 CRWTouchTrackingDelegate
,
82 CRWWebControllerScripting
,
83 UIActionSheetDelegate
,
84 UIGestureRecognizerDelegate
>
86 // Whether or not a UIWebView is allowed to exist in this CRWWebController.
87 // Defaults to NO; this should be enabled before attempting to access the view.
88 @
property(nonatomic
, assign
) BOOL webUsageEnabled
;
90 @
property(nonatomic
, assign
) id
<CRWWebDelegate
> delegate
;
91 @
property(nonatomic
, weak
) id
<CRWWebUserInterfaceDelegate
> UIDelegate
;
92 @
property(nonatomic
, assign
) id
<CRWNativeContentProvider
> nativeProvider
;
93 @
property(nonatomic
, assign
)
94 id
<CRWSwipeRecognizerProvider
> swipeRecognizerProvider
;
95 @
property(nonatomic
, readonly
) web::WebState
* webState
;
96 @
property(nonatomic
, readonly
) web::WebStateImpl
* webStateImpl
;
98 // The container view used to display content. If the view has been purged due
99 // to low memory, this will recreate it.
100 @
property(nonatomic
, readonly
) UIView
* view
;
102 // The web view proxy associated with this controller.
103 @
property(nonatomic
, readonly
) id
<CRWWebViewProxy
> webViewProxy
;
105 // The view that generates print data when printing. It is nil if printing
107 @
property(nonatomic
, readonly
) UIView
* viewForPrinting
;
109 // Content view was reset due to low memory. Use placeholder overlay view on
111 @
property(nonatomic
, readwrite
, assign
) BOOL usePlaceholderOverlay
;
113 // Returns the current page loading phase.
114 @
property(nonatomic
, readonly
) web::LoadPhase loadPhase
;
116 // Returns whether the page can navigate backwards or forwards.
117 @
property(nonatomic
, readonly
) BOOL canGoBack
;
118 @
property(nonatomic
, readonly
) BOOL canGoForward
;
120 // Returns the x, y offset the content has been scrolled.
121 @
property(nonatomic
, readonly
) CGPoint scrollPosition
;
123 // Returns whether the top of the content is visible.
124 @
property(nonatomic
, readonly
) BOOL atTop
;
126 // Whether or not content can programmatically display the keyboard.
127 @
property(nonatomic
, assign
) BOOL keyboardDisplayRequiresUserAction
;
129 // Return an image to use as replacement of a missing snapshot.
130 + (UIImage
*)defaultSnapshotImage
;
132 // Replaces the currently displayed content with |contentView|. The content
133 // view will be dismissed for the next navigation.
134 - (void)showTransientContentView
:(CRWContentView
*)contentView
;
136 // Clear the transient content view, if one is shown.
137 - (void)clearTransientContentView
;
139 // Give the unload listeners a chance to fire. Returns YES if they complete
140 // and the CRWWebController is in a state it may be closed.
141 - (BOOL
)runUnloadListenerBeforeClosing
;
143 // Call to stop the CRWWebController from doing stuff, in particular to
144 // stop all network requests. Called as part of the close sequence if it hasn't
145 // already been halted; also called from [Tab halt] as part of the shutdown
146 // sequence (which doesn't call -close).
147 - (void)terminateNetworkActivity
;
149 // Dismisses all modals owned by the web view or native view.
150 - (void)dismissModals
;
152 // Call when the CRWWebController needs go away. Do not call until first calling
153 // |-runUnloadListenerBeforeClosing|. Caller must reset the delegate before
157 // Call when there is a need to free up memory.
158 - (void)handleLowMemory
;
160 // Returns YES if there is currently a live view in the tab (e.g., the view
161 // hasn't been discarded due to low memory).
162 // NOTE: This should be used for metrics-gathering only; for any other purpose
163 // callers should not know or care whether the view is live.
166 // Returns YES if the current live view is a web view with HTML.
167 - (BOOL
)contentIsHTML
;
169 // Returns the CRWWebController's view of the current URL. Moreover, this method
170 // will set the trustLevel enum to the appropriate level from a security point
171 // of view. The caller has to handle the case where |trustLevel| is not
172 // appropriate, as this method won't display any error to the user.
173 - (GURL
)currentURLWithTrustLevel
:(web::URLVerificationTrustLevel
*)trustLevel
;
175 // Methods for navigation and properties to interrogate state.
178 // YES if the CRWWebController's view is deemed appropriate for saving in order
179 // to generate an overlay placeholder view.
180 - (BOOL
)canUseViewForGeneratingOverlayPlaceholderView
;
182 // Start loading the URL specified in |originalParams|, with the specified
183 // settings. Always resets the openedByScript property to NO.
184 - (void)loadWithParams
:(const web::WebLoadParams
&)originalParams
;
186 // Loads the URL indicated by current session state.
187 - (void)loadCurrentURL
;
188 // Loads the HTML into the page.
189 - (void)loadHTML
:(NSString
*)html
;
190 // Loads HTML in the page and presents it as if it was originating from an
191 // application specific URL.
192 - (void)loadHTML
:(NSString
*)HTML forAppSpecificURL
:(const GURL
&)URL
;
194 // Causes the page to start loading immediately if there is a pending load;
195 // normally if the web view has been paged out for memory reasons, loads are
196 // started lazily the next time the view is displayed. This can be called to
197 // bypass the lazy behavior. This is equivalent to calling -view, but should be
198 // used when deliberately pre-triggering a load without displaying.
199 - (void)triggerPendingLoad
;
201 // Navigate forwards or backwards by one page.
204 // Navigate forwards or backwards by |delta| pages.
205 - (void)goDelta
:(int)delta
;
206 // Perform necessary setup in order to navigate backwards.
207 // TODO(rohitrao): Remove this from the public API.
208 - (void)prepareForGoBack
;
210 // Evaluates the user-entered |script| in the web view.
211 - (void)evaluateUserJavaScript
:(NSString
*)script
;
213 // Dismisses the soft keyboard.
214 - (void)dismissKeyboard
;
216 // Requires that the next load rebuild the UIWebView. This is expensive, and
217 // should be used only in the case where something has changed that UIWebView
218 // only checks on creation, such that the whole object needs to be rebuilt.
219 // TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are
220 // currently subtly different in terms of implementation, but are for
221 // fundamentally the same purpose.
222 - (void)requirePageReconstruction
;
224 - (void)reinitializeWebViewAndReload
:(BOOL
)reload
;
226 // Requires that the next display reload the page, using a placeholder while
227 // loading. This could be used, e.g., to handle a crash in a WebController that
228 // is not currently visible.
229 // TODO(stuartmorgan): When revisiting the methods above, revisit this as well.
230 - (void)requirePageReload
;
232 // Sets the closed property to true for the child window with the given name.
233 - (void)childWindowClosed
:(NSString
*)windowName
;
235 // Show overlay, don't reload web page. Used when the view will be
236 // visible only briefly (e.g., tablet side swipe).
237 - (void)setOverlayPreviewMode
:(BOOL
)overlayPreviewMode
;
239 // Sets policy for web page dialog handling. Controls dialog suppression and
240 // notifying the WebDelegate.
241 - (void)setPageDialogOpenPolicy
:(web::PageDialogOpenPolicy
)policy
;
243 // Records the state (scroll position, form values, whatever can be harvested)
244 // from the current page into the current session entry.
245 - (void)recordStateInHistory
;
246 // Restores the state for this page from session history.
247 // TODO(stuartmorgan): This is public only temporarily; once refactoring is
248 // complete it will be handled internally.
249 - (void)restoreStateFromHistory
;
251 // Asynchronously checks whether the element at the location of
252 // |gestureRecognizer| is a link.
253 - (void)checkLinkPresenceUnderGesture
:(UIGestureRecognizer
*)gestureRecognizer
254 completionHandler
:(void (^)(BOOL
))completionHandler
;
256 // Notifies the CRWWebController that it has been shown.
259 // Notifies the CRWWebController that it has been hidden.
262 // Returns |YES| if the current page should show the keyboard shield.
263 - (BOOL
)wantsKeyboardShield
;
265 // Returns |YES| if the current page should should the location bar hint text.
266 - (BOOL
)wantsLocationBarHintText
;
268 // Adds |recognizer| as a gesture recognizer to the web view.
269 - (void)addGestureRecognizerToWebView
:(UIGestureRecognizer
*)recognizer
;
270 // Removes |recognizer| from the web view.
271 - (void)removeGestureRecognizerFromWebView
:(UIGestureRecognizer
*)recognizer
;
273 // Adds |toolbar| to the web view.
274 - (void)addToolbarViewToWebView
:(UIView
*)toolbarView
;
275 // Removes |toolbar| from the web view.
276 - (void)removeToolbarViewFromWebView
:(UIView
*)toolbarView
;
278 // Adds a CRWWebControllerObserver to subscribe to page events. |observer|
280 - (void)addObserver
:(id
<CRWWebControllerObserver
>)observer
;
282 // Removes an attached CRWWebControllerObserver.
283 - (void)removeObserver
:(id
<CRWWebControllerObserver
>)observer
;
285 // Returns the always-visible frame, not including the part that could be
286 // covered by the toolbar.
287 - (CGRect
)visibleFrame
;
289 - (CRWJSInjectionReceiver
*)jsInjectionReceiver
;
291 // Load the correct HTML page for |error| in a native controller, retrieved
292 // from the native provider. Call |loadNativeViewWithSuccess:NO| to load the
293 // native controller.
294 - (void)loadErrorInNativeView
:(NSError
*)error
;
296 // Resets the state of a page where a load was rejected. This method must
297 // be called if an embedder rejected the page load (e.g. by returning NO from
298 // |-[WebDelegate shouldOpenURL:linkClicked:]|) but wants to continue working
299 // with CRWWebController.
300 - (void)restoreStateAfterURLRejection
;
302 // Helper method called at the end of history navigation methods goBack,
303 // goForward, and goDelta. Loads a new URL if the current entry is not from a
304 // pushState() navigation from |fromEntry|. |fromEntry| is the
305 // CRWSessionEntry that was the current entry prior to the navigation.
306 // TODO(rohitrao): This is only exposed so Tab can call it temporarily. Remove
307 // as soon as all the Tab calls have moved into CRWWebController.
308 - (void)finishHistoryNavigationFromEntry
:(CRWSessionEntry
*)fromEntry
;
310 // Returns the native controller (if any) current mananging the content.
311 - (id
<CRWNativeContent
>)nativeController
;
316 @interface
CRWWebController (UsedOnlyForTesting
) // Testing or internal API.
318 // Injects a CRWWebViewContentView for testing. Takes ownership of
319 // |webViewContentView|.
320 - (void)injectWebViewContentView
:(CRWWebViewContentView
*)webViewContentView
;
321 - (void)resetInjectedWebViewContentView
;
322 // Returns the number of observers registered for this CRWWebController.
323 - (NSUInteger
)observerCount
;
324 - (NSString
*)windowId
;
325 - (void)setWindowId
:(NSString
*)windowId
;
326 - (void)setURLOnStartLoading
:(const GURL
&)url
;
327 - (void)simulateLoadRequestWithURL
:(const GURL
&)URL
;
328 - (NSString
*)externalRequestWindowName
;
330 // Returns the header height.
331 - (CGFloat
)headerHeight
;
333 // Caches request POST data in the given session entry. Exposed for testing.
334 - (void)cachePOSTDataForRequest
:(NSURLRequest
*)request
335 inSessionEntry
:(CRWSessionEntry
*)currentSessionEntry
;
339 #endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_