summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/ui/crw_web_controller.h
blob: 5eef87fa2d13fe33770f57f74875e89f0c611f25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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.

#ifndef IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_
#define IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_

#import <UIKit/UIKit.h>

#include "base/memory/scoped_ptr.h"
#import "ios/web/net/crw_request_tracker_delegate.h"
#import "ios/web/public/web_state/crw_web_user_interface_delegate.h"
#import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
#import "ios/web/public/web_state/ui/crw_web_delegate.h"
#include "ios/web/public/web_state/url_verification_constants.h"
#import "ios/web/web_state/ui/crw_touch_tracking_recognizer.h"

namespace web {

// Page load phases.
enum LoadPhase {
  // In the LOAD_REQUESTED phase, the system predicts a page change is going to
  // happen but the page URL has not yet changed.
  LOAD_REQUESTED = 0,
  // In the PAGE_LOADING phase, the page URL has changed but the whole document
  // may not be available for use.
  PAGE_LOADING = 1,
  // In the PAGE_LOADED phase, either the page had loaded and is available for
  // use, the load was cancelled, or the UIWebView is new and ready for a load.
  PAGE_LOADED = 2
};

// Policy for web page dialog handling.
enum PageDialogOpenPolicy {
  // Default policy. Dialogs are allowed, clients are not notified on display.
  DIALOG_POLICY_ALLOW = 0,
  // Dialogs are not allowed, client are notified when dialog did block with
  // -[WebDelegate webControllerDidSuppressDialog:] delegate method call.
  DIALOG_POLICY_SUPPRESS
};

// The accessibility identifier of the top-level container view.
extern NSString* const kContainerViewID;

}  // namespace web

@class CRWJSInjectionReceiver;
@protocol CRWNativeContent;
@protocol CRWNativeContentProvider;
@protocol CRWSwipeRecognizerProvider;
@protocol CRWWebControllerObserver;
@class CRWWebViewContentView;
@protocol CRWWebViewProxy;
class GURL;

namespace base {
class Value;
}

namespace web {
class BrowserState;
struct Referrer;
class WebState;
class WebInterstitialImpl;
class WebStateImpl;
}

// Manages a view that can be used either for rendering web content in a web
// view, or native content in a view provided by a NativeContentProvider.
// 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. A factory
// function from web_controller_factory.h should be used instead.
// TODO(stuartmorgan): Move all of the navigation APIs out of this class.
@interface CRWWebController : NSObject<CRWJSInjectionEvaluator,
                                       CRWRequestTrackerDelegate,
                                       CRWTouchTrackingDelegate,
                                       UIGestureRecognizerDelegate>

// 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, assign) id<CRWWebDelegate> delegate;
@property(nonatomic, weak) id<CRWWebUserInterfaceDelegate> UIDelegate;
@property(nonatomic, assign) id<CRWNativeContentProvider> nativeProvider;
@property(nonatomic, assign)
    id<CRWSwipeRecognizerProvider> swipeRecognizerProvider;
@property(nonatomic, readonly) web::WebState* webState;
@property(nonatomic, readonly) web::WebStateImpl* webStateImpl;

// The container view used to display content.  If the view has been purged due
// to low memory, this will recreate it.
@property(nonatomic, readonly) UIView* view;

// The web view proxy associated with this controller.
@property(nonatomic, readonly) id<CRWWebViewProxy> webViewProxy;

// The view that generates print data when printing. It is nil if printing
// is not supported.
@property(nonatomic, readonly) UIView* viewForPrinting;

// Content view was reset due to low memory.  Use placeholder overlay view on
// next creation.
@property(nonatomic, readwrite, assign) BOOL usePlaceholderOverlay;

// Returns the current page loading phase.
@property(nonatomic, readonly) web::LoadPhase loadPhase;

// Returns whether the page can navigate backwards or forwards.
@property(nonatomic, readonly) BOOL canGoBack;
@property(nonatomic, readonly) BOOL canGoForward;

// Returns the x, y offset the content has been scrolled.
@property(nonatomic, readonly) CGPoint scrollPosition;

// Returns whether the top of the content is visible.
@property(nonatomic, readonly) BOOL atTop;

// Whether or not content can programmatically display the keyboard.
@property(nonatomic, assign) BOOL keyboardDisplayRequiresUserAction;

// YES if JavaScript dialogs, HTTP authentication dialogs and window.open
// calls should be suppressed. Default is NO. When dialog is suppressed
// |CRWWebDelegate webControllerDidSuppressDialog:| will be called.
@property(nonatomic, assign) BOOL shouldSuppressDialogs;

// Return an image to use as replacement of a missing snapshot.
+ (UIImage*)defaultSnapshotImage;

// Replaces the currently displayed content with |contentView|.  The content
// view will be dismissed for the next navigation.
- (void)showTransientContentView:(CRWContentView*)contentView;

// Clear the transient content view, if one is shown.
- (void)clearTransientContentView;

// Give the unload listeners a chance to fire. Returns YES if they complete
// and the CRWWebController is in a state it may be closed.
- (BOOL)runUnloadListenerBeforeClosing;

// Call to stop the CRWWebController from doing stuff, in particular to
// stop all network requests. Called as part of the close sequence if it hasn't
// already been halted; also called from [Tab halt] as part of the shutdown
// sequence (which doesn't call -close).
- (void)terminateNetworkActivity;

// Dismisses all modals owned by the web view or native view.
- (void)dismissModals;

// Call when the CRWWebController needs go away. Do not call until first calling
// |-runUnloadListenerBeforeClosing|. Caller must reset the delegate before
// calling.
- (void)close;

// Call when there is a need to free up memory.
- (void)handleLowMemory;

// 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.
- (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;

// Methods for navigation and properties to interrogate state.
- (void)reload;
- (void)stopLoading;
// YES if the CRWWebController's view is deemed appropriate for saving in order
// to generate an overlay placeholder view.
- (BOOL)canUseViewForGeneratingOverlayPlaceholderView;

// Start loading the URL specified in |originalParams|, with the specified
// settings.  Always resets the openedByScript property to NO.
- (void)loadWithParams:(const web::WebLoadParams&)originalParams;

// Loads the URL indicated by current session state.
- (void)loadCurrentURL;

// Loads HTML in the page and presents it as if it was originating from an
// application specific URL.
- (void)loadHTML:(NSString*)HTML forAppSpecificURL:(const GURL&)URL;

// Loads HTML in the page and presents it as if it was originating from the
// URL itself. Should be used only in specific cases, where the injected html
// is guaranteed to be some derived representation of the original content.
- (void)loadHTMLForCurrentURL:(NSString*)HTML;

// Stops loading the page.
- (void)stopLoading;

// Causes the page to start loading immediately if there is a pending load;
// normally if the web view has been paged out for memory reasons, loads are
// started lazily the next time the view is displayed. This can be called to
// bypass the lazy behavior. This is equivalent to calling -view, but should be
// used when deliberately pre-triggering a load without displaying.
- (void)triggerPendingLoad;

// Navigate forwards or backwards by one page.
- (void)goBack;
- (void)goForward;
// Navigate forwards or backwards by |delta| pages.
- (void)goDelta:(int)delta;
// Perform necessary setup in order to navigate backwards.
// TODO(rohitrao): Remove this from the public API.
- (void)prepareForGoBack;

// Evaluates the user-entered |script| in the web view.
- (void)evaluateUserJavaScript:(NSString*)script;

// Dismisses the soft keyboard.
- (void)dismissKeyboard;

// Requires that the next load rebuild the UIWebView. This is expensive, and
// should be used only in the case where something has changed that UIWebView
// only checks on creation, such that the whole object needs to be rebuilt.
// TODO(stuartmorgan): Merge this and reinitializeWebViewAndReload:. They are
// currently subtly different in terms of implementation, but are for
// fundamentally the same purpose.
- (void)requirePageReconstruction;

- (void)reinitializeWebViewAndReload:(BOOL)reload;

// Requires that the next display reload the page, using a placeholder while
// loading. This could be used, e.g., to handle a crash in a WebController that
// is not currently visible.
// TODO(stuartmorgan): When revisiting the methods above, revisit this as well.
- (void)requirePageReload;

// Show overlay, don't reload web page. Used when the view will be
// visible only briefly (e.g., tablet side swipe).
- (void)setOverlayPreviewMode:(BOOL)overlayPreviewMode;

// Sets policy for web page dialog handling. Controls dialog suppression and
// notifying the WebDelegate.
// TODO(crbug.com/595463): remove this method, once embedder uses
// |setSuppressDialogs|.
- (void)setPageDialogOpenPolicy:(web::PageDialogOpenPolicy)policy;

// Records the state (scroll position, form values, whatever can be harvested)
// from the current page into the current session entry.
- (void)recordStateInHistory;
// Restores the state for this page from session history.
// TODO(stuartmorgan): This is public only temporarily; once refactoring is
// complete it will be handled internally.
- (void)restoreStateFromHistory;

// Asynchronously checks whether the element at the location of
// |gestureRecognizer| is a link.
- (void)checkLinkPresenceUnderGesture:(UIGestureRecognizer*)gestureRecognizer
                    completionHandler:(void (^)(BOOL))completionHandler;

// Notifies the CRWWebController that it has been shown.
- (void)wasShown;

// Notifies the CRWWebController that it has been hidden.
- (void)wasHidden;

// Returns |YES| if the current page should show the keyboard shield.
- (BOOL)wantsKeyboardShield;

// Returns |YES| if the current page should should the location bar hint text.
- (BOOL)wantsLocationBarHintText;

// Adds |recognizer| as a gesture recognizer to the web view.
- (void)addGestureRecognizerToWebView:(UIGestureRecognizer*)recognizer;
// Removes |recognizer| from the web view.
- (void)removeGestureRecognizerFromWebView:(UIGestureRecognizer*)recognizer;

// Adds |toolbar| to the web view.
- (void)addToolbarViewToWebView:(UIView*)toolbarView;
// Removes |toolbar| from the web view.
- (void)removeToolbarViewFromWebView:(UIView*)toolbarView;

// Adds a CRWWebControllerObserver to subscribe to page events. |observer|
// cannot be nil.
- (void)addObserver:(id<CRWWebControllerObserver>)observer;

// Removes an attached CRWWebControllerObserver.
- (void)removeObserver:(id<CRWWebControllerObserver>)observer;

// Returns the always-visible frame, not including the part that could be
// covered by the toolbar.
- (CGRect)visibleFrame;

- (CRWJSInjectionReceiver*)jsInjectionReceiver;

// Load the correct HTML page for |error| in a native controller, retrieved
// from the native provider. Call |loadNativeViewWithSuccess:NO| to load the
// native controller.
- (void)loadErrorInNativeView:(NSError*)error;

// Helper method called at the end of history navigation methods goBack,
// goForward, and goDelta.  Loads a new URL if the current entry is not from a
// pushState() navigation from |fromEntry|. |fromEntry| is the
// CRWSessionEntry that was the current entry prior to the navigation.
// TODO(rohitrao): This is only exposed so Tab can call it temporarily.  Remove
// as soon as all the Tab calls have moved into CRWWebController.
- (void)finishHistoryNavigationFromEntry:(CRWSessionEntry*)fromEntry;

// Returns the native controller (if any) current mananging the content.
- (id<CRWNativeContent>)nativeController;
@end

#pragma mark Testing

@interface CRWWebController (UsedOnlyForTesting)  // Testing or internal API.

// Injects a CRWWebViewContentView for testing.  Takes ownership of
// |webViewContentView|.
- (void)injectWebViewContentView:(CRWWebViewContentView*)webViewContentView;
- (void)resetInjectedWebViewContentView;
// Returns the number of observers registered for this CRWWebController.
- (NSUInteger)observerCount;
- (NSString*)windowId;
- (void)setWindowId:(NSString*)windowId;
- (void)setURLOnStartLoading:(const GURL&)url;
- (void)simulateLoadRequestWithURL:(const GURL&)URL;
- (NSString*)externalRequestWindowName;

// Returns the header height.
- (CGFloat)headerHeight;

// Caches request POST data in the given session entry.  Exposed for testing.
- (void)cachePOSTDataForRequest:(NSURLRequest*)request
                 inSessionEntry:(CRWSessionEntry*)currentSessionEntry;

@end

#endif  // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_H_