Prevent app list doodle from being pinch-to-zoomed.
[chromium-blink-merge.git] / ios / web / web_state / ui / crw_web_controller.h
blobb434b57693b0aca656b7b0578d975281baf81bbb
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_delegate.h"
13 #import "ios/web/public/web_state/crw_web_user_interface_delegate.h"
14 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
15 #include "ios/web/public/web_state/url_verification_constants.h"
16 #import "ios/web/web_state/ui/crw_touch_tracking_recognizer.h"
18 namespace web {
20 // Page load phases.
21 enum LoadPhase {
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.
24 LOAD_REQUESTED = 0,
25 // In the PAGE_LOADING phase, the page URL has changed but the whole document
26 // may not be available for use.
27 PAGE_LOADING = 1,
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.
30 PAGE_LOADED = 2
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 extern NSString* const kPageChangedNotification;
47 // The accessibility identifier of the top-level container view.
48 extern NSString* const kContainerViewID;
50 } // namespace web
52 @class CRWJSInjectionReceiver;
53 @protocol CRWNativeContent;
54 @protocol CRWNativeContentProvider;
55 @protocol CRWSwipeRecognizerProvider;
56 @protocol CRWWebControllerObserver;
57 @protocol CRWWebViewProxy;
58 class GURL;
60 namespace base {
61 class Value;
64 namespace web {
65 class BrowserState;
66 struct Referrer;
67 class WebState;
68 class WebInterstitialImpl;
69 class WebStateImpl;
72 // Manages a view that can be used either for rendering web content in a web
73 // view, or native content in a view provided by a NativeContentProvider.
74 // CRWWebController also transparently evicts and restores the internal web
75 // view based on memory pressure, and manages access to interact with the
76 // web view.
77 // This is an abstract class which must not be instantiated directly. A factory
78 // function from web_controller_factory.h should be used instead.
79 // TODO(stuartmorgan): Move all of the navigation APIs out of this class.
80 @interface CRWWebController : NSObject<CRWJSInjectionEvaluator,
81 CRWRequestTrackerDelegate,
82 CRWTouchTrackingDelegate,
83 CRWWebControllerScripting,
84 UIActionSheetDelegate,
85 UIGestureRecognizerDelegate>
87 // Whether or not a UIWebView is allowed to exist in this CRWWebController.
88 // Defaults to NO; this should be enabled before attempting to access the view.
89 @property(nonatomic, assign) BOOL webUsageEnabled;
91 @property(nonatomic, assign) id<CRWWebDelegate> delegate;
92 @property(nonatomic, weak) id<CRWWebUserInterfaceDelegate> UIDelegate;
93 @property(nonatomic, assign) id<CRWNativeContentProvider> nativeProvider;
94 @property(nonatomic, assign)
95 id<CRWSwipeRecognizerProvider> swipeRecognizerProvider;
96 @property(nonatomic, readonly) web::WebState* webState;
97 @property(nonatomic, readonly) web::WebStateImpl* webStateImpl;
99 // If on a regular webpage, the UIWebView responsible for rendering it. If
100 // on an internal page, the native view implementing the functionality. If the
101 // view has been purged due to low memory, this will re-create it. It is up
102 // to the caller to size the view.
103 @property(nonatomic, readonly) UIView* view;
105 // The web view proxy associated with this controller.
106 @property(nonatomic, readonly) id<CRWWebViewProxy> webViewProxy;
108 // The view that generates print data when printing. It is nil if printing
109 // is not supported.
110 @property(nonatomic, readonly) UIView* viewForPrinting;
112 // Content view was reset due to low memory. Use placeholder overlay view on
113 // next creation.
114 @property(nonatomic, readwrite, assign) BOOL usePlaceholderOverlay;
116 // Returns the current page loading phase.
117 @property(nonatomic, readonly) web::LoadPhase loadPhase;
119 // Returns whether the page can navigate backwards or forwards.
120 @property(nonatomic, readonly) BOOL canGoBack;
121 @property(nonatomic, readonly) BOOL canGoForward;
123 // Returns the x, y offset the content has been scrolled.
124 @property(nonatomic, readonly) CGPoint scrollPosition;
126 // Returns whether the top of the content is visible.
127 @property(nonatomic, readonly) BOOL atTop;
129 // Whether or not content can programmatically display the keyboard.
130 @property(nonatomic, assign) BOOL keyboardDisplayRequiresUserAction;
132 // Return an image to use as replacement of a missing snapshot.
133 + (UIImage*)defaultSnapshotImage;
135 // Adds |interstitialView| to the content view, copying the current scroll
136 // offsets to |scrollView|.
137 - (void)displayInterstitialView:(UIView*)interstitialView
138 withScrollView:(UIScrollView*)scrollView;
140 // Give the unload listeners a chance to fire. Returns YES if they complete
141 // and the CRWWebController is in a state it may be closed.
142 - (BOOL)runUnloadListenerBeforeClosing;
144 // Call to stop the CRWWebController from doing stuff, in particular to
145 // stop all network requests. Called as part of the close sequence if it hasn't
146 // already been halted; also called from [Tab halt] as part of the shutdown
147 // sequence (which doesn't call -close).
148 - (void)terminateNetworkActivity;
150 // Dismisses all modals owned by the web view or native view.
151 - (void)dismissModals;
153 // Call when the CRWWebController needs go away. Do not call until first calling
154 // |-runUnloadListenerBeforeClosing|. Caller must reset the delegate before
155 // calling.
156 - (void)close;
158 // Call when there is a need to free up memory.
159 - (void)handleLowMemory;
161 // Returns YES if there is currently a live view in the tab (e.g., the view
162 // hasn't been discarded due to low memory).
163 // NOTE: This should be used for metrics-gathering only; for any other purpose
164 // callers should not know or care whether the view is live.
165 - (BOOL)isViewAlive;
167 // Returns YES if the current live view is a web view with HTML.
168 - (BOOL)contentIsHTML;
170 // Returns the CRWWebController's view of the current URL. Moreover, this method
171 // will set the trustLevel enum to the appropriate level from a security point
172 // of view. The caller has to handle the case where |trustLevel| is not
173 // appropriate, as this method won't display any error to the user.
174 - (GURL)currentURLWithTrustLevel:(web::URLVerificationTrustLevel*)trustLevel;
176 // Methods for navigation and properties to interrogate state.
177 - (void)reload;
178 - (void)stopLoading;
179 // YES if the CRWWebController's view is deemed appropriate for saving in order
180 // to generate an overlay placeholder view.
181 - (BOOL)canUseViewForGeneratingOverlayPlaceholderView;
183 // Start loading the URL specified in |originalParams|, with the specified
184 // settings. Always resets the openedByScript property to NO.
185 - (void)loadWithParams:(const web::WebLoadParams&)originalParams;
187 // Loads the URL indicated by current session state.
188 - (void)loadCurrentURL;
189 // Updates UIWebView's URL and urlOnStartLoading_ during back/forward navigation
190 // over pushed URLs. Needed so that sites that depend on URL params/fragment
191 // continue to work correctly and checks for the URL don't incorrectly trigger
192 // -pageChanged calls.
193 - (void)finishPushStateNavigationToURL:(const GURL&)url
194 withStateObject:(NSString*)stateObject;
195 // Loads the HTML into the page.
196 - (void)loadHTML:(NSString*)html;
198 // Causes the page to start loading immediately if there is a pending load;
199 // normally if the web view has been paged out for memory reasons, loads are
200 // started lazily the next time the view is displayed. This can be called to
201 // bypass the lazy behavior. This is equivalent to calling -view, but should be
202 // used when deliberately pre-triggering a load without displaying.
203 - (void)triggerPendingLoad;
205 // Navigate forwards or backwards by one page.
206 - (void)goBack;
207 - (void)goForward;
208 // Navigate forwards or backwards by |delta| pages.
209 - (void)goDelta:(int)delta;
210 // Perform necessary setup in order to navigate backwards.
211 // TODO(rohitrao): Remove this from the public API.
212 - (void)prepareForGoBack;
214 // Evaluates the user-entered |script| in the web view.
215 - (void)evaluateUserJavaScript:(NSString*)script;
217 // Dismisses the soft keyboard.
218 - (void)dismissKeyboard;
220 // Requires that the next load rebuild the UIWebView. This is expensive, and
221 // should be used only in the case where something has changed that UIWebView
222 // only checks on creation, such that the whole object needs to be rebuilt.
223 // TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are
224 // currently subtly different in terms of implementation, but are for
225 // fundamentally the same purpose.
226 - (void)requirePageReconstruction;
228 - (void)reinitializeWebViewAndReload:(BOOL)reload;
230 // Sets the closed property to true for the child window with the given name.
231 - (void)childWindowClosed:(NSString*)windowName;
233 // Show overlay, don't reload web page. Used when the view will be
234 // visible only briefly (e.g., tablet side swipe).
235 - (void)setOverlayPreviewMode:(BOOL)overlayPreviewMode;
237 // Sets policy for web page dialog handling. Controls dialog suppression and
238 // notifying the WebDelegate.
239 - (void)setPageDialogOpenPolicy:(web::PageDialogOpenPolicy)policy;
241 // Records the state (scroll position, form values, whatever can be harvested)
242 // from the current page into the current session entry.
243 - (void)recordStateInHistory;
244 // Restores the state for this page from session history.
245 // TODO(stuartmorgan): This is public only temporarily; once refactoring is
246 // complete it will be handled internally.
247 - (void)restoreStateFromHistory;
249 // Asynchronously checks whether the element at the location of
250 // |gestureRecognizer| is a link.
251 - (void)checkLinkPresenceUnderGesture:(UIGestureRecognizer*)gestureRecognizer
252 completionHandler:(void (^)(BOOL))completionHandler;
254 // Notifies the CRWWebController that it has been shown.
255 - (void)wasShown;
257 // Notifies the CRWWebController that it has been hidden.
258 - (void)wasHidden;
260 // Returns |YES| if the current page should show the keyboard shield.
261 - (BOOL)wantsKeyboardShield;
263 // Returns |YES| if the current page should should the location bar hint text.
264 - (BOOL)wantsLocationBarHintText;
266 // Adds |recognizer| as a gesture recognizer to the web view.
267 - (void)addGestureRecognizerToWebView:(UIGestureRecognizer*)recognizer;
268 // Removes |recognizer| from the web view.
269 - (void)removeGestureRecognizerFromWebView:(UIGestureRecognizer*)recognizer;
271 // Adds |toolbar| to the web view.
272 - (void)addToolbarViewToWebView:(UIView*)toolbarView;
273 // Removes |toolbar| from the web view.
274 - (void)removeToolbarViewFromWebView:(UIView*)toolbarView;
276 // Adds a CRWWebControllerObserver to subscribe to page events. |observer|
277 // cannot be nil.
278 - (void)addObserver:(id<CRWWebControllerObserver>)observer;
280 // Removes an attached CRWWebControllerObserver.
281 - (void)removeObserver:(id<CRWWebControllerObserver>)observer;
283 // Returns the always-visible frame, not including the part that could be
284 // covered by the toolbar.
285 - (CGRect)visibleFrame;
287 - (CRWJSInjectionReceiver*)jsInjectionReceiver;
289 // Load the correct HTML page for |error| in a native controller, retrieved
290 // from the native provider. Call |loadNativeViewWithSuccess:NO| to load the
291 // native controller.
292 - (void)loadErrorInNativeView:(NSError*)error;
294 // Resets the state of a page where a load was rejected. This method must
295 // be called if an embedder rejected the page load (e.g. by returning NO from
296 // |-[WebDelegate shouldOpenURL:linkClicked:]|) but wants to continue working
297 // with CRWWebController.
298 - (void)restoreStateAfterURLRejection;
300 // Helper method called at the end of history navigation methods goBack,
301 // goForward, and goDelta. Determines whether to load a new URL or call
302 // |finishPushStateNavigationToURL:withStateObject:|. |fromEntry| is the
303 // CRWSessionEntry that was the current entry prior to the navigation.
304 // TODO(rohitrao): This is only exposed so Tab can call it temporarily. Remove
305 // as soon as all the Tab calls have moved into CRWWebController.
306 - (void)finishHistoryNavigationFromEntry:(CRWSessionEntry*)fromEntry;
308 // Returns the native controller (if any) current mananging the content.
309 - (id<CRWNativeContent>)nativeController;
310 @end
312 #pragma mark Testing
314 @interface CRWWebController (UsedOnlyForTesting) // Testing or internal API.
316 // Injects a web view for testing. Takes ownership of the |webView|.
317 - (void)injectWebView:(id)webView;
318 - (void)resetInjectedWebView;
319 // Returns the number of observers registered for this CRWWebController.
320 - (NSUInteger)observerCount;
321 - (NSString*)windowId;
322 - (void)setWindowId:(NSString*)windowId;
323 - (void)setURLOnStartLoading:(const GURL&)url;
324 - (void)simulateLoadRequestWithURL:(const GURL&)URL;
325 - (NSString*)externalRequestWindowName;
327 // Returns the header height.
328 - (CGFloat)headerHeight;
330 // Caches request POST data in the given session entry. Exposed for testing.
331 - (void)cachePOSTDataForRequest:(NSURLRequest*)request
332 inSessionEntry:(CRWSessionEntry*)currentSessionEntry;
334 @end
336 #endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_