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