1 // Copyright 2014 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_PROTECTED_H_
6 #define IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_PROTECTED_H_
8 #import "ios/web/web_state/ui/crw_web_controller.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "ios/web/public/referrer.h"
12 #include "ios/web/public/web_state/page_scroll_state.h"
14 @
class CRWSessionController
;
20 // Separator between window href and name.
21 extern const char* kWindowNameSeparator
;
22 // Key for user interaction data in JavaScript message context.
23 extern NSString
* const kUserIsInteractingKey
;
24 // Key for origin URL data in JavaScript message context.
25 extern NSString
* const kOriginURLKey
;
27 // Values of the UMA |Web.URLVerificationFailure| histogram.
28 enum WebViewDocumentType
{
29 // Generic contents (e.g. PDF documents).
30 WEB_VIEW_DOCUMENT_TYPE_GENERIC
= 0,
32 WEB_VIEW_DOCUMENT_TYPE_HTML
,
34 WEB_VIEW_DOCUMENT_TYPE_UNKNOWN
,
35 WEB_VIEW_DOCUMENT_TYPE_COUNT
,
38 // A guess for how likely a page change is to happen very soon.
39 // TODO(stuartmorgan): Eliminate this, or at least move to the UIWebView
41 enum PageChangeProbability
{
42 // No expectation that the page will be changing.
43 PAGE_CHANGE_PROBABILITY_LOW
,
44 // Reasonably high expectation that the page will be changing (e.g., the
45 // user just tapped a link).
46 PAGE_CHANGE_PROBABILITY_HIGH
,
47 // Very high expectation that the page will be changing (e.g., window.unload
49 PAGE_CHANGE_PROBABILITY_VERY_HIGH
,
52 struct NewWindowInfo
{
54 base::scoped_nsobject
<NSString
> window_name
;
55 web::ReferrerPolicy referrer_policy
;
56 bool user_is_interacting
;
57 NewWindowInfo(GURL url
,
58 NSString
* window_name
,
59 web::ReferrerPolicy referrer_policy
,
60 bool user_is_interacting
);
65 // Category for methods used or implemented by implementation subclasses of
67 @interface
CRWWebController (ProtectedMethods
)
69 #pragma mark Methods implemented by subclasses
70 // Everything in this section must be implemented by subclasses.
72 // If |contentView_| contains a web view, this is the web view it contains.
74 @
property(nonatomic
, readonly
) UIView
* webView
;
76 // The scroll view of |webView|.
77 @
property(nonatomic
, readonly
) UIScrollView
* webScrollView
;
79 // Whether or not to ignore URL verification failures. This may return YES in
80 // very limited situations where the URL can't be verified but there is no
81 // security impact to ignoring the failure (i.e., it's safe not to show the
83 @
property(nonatomic
, readonly
) BOOL ignoreURLVerificationFailures
;
85 // The title of the page.
86 @
property(nonatomic
, readonly
) NSString
* title
;
88 // Referrer for the current page; does not include the fragment.
89 @
property(nonatomic
, readonly
) NSString
* currentReferrerString
;
91 // This public property should be implemented by subclasses.
92 // TODO(stuartmorgan): See if we can get rid of this (it looks like it may only
93 // be fallback code for autocomplete that's necessarily used). If not, file a
94 // Radar since WKWebView doesn't appear to have this property.
95 // @property(nonatomic, assign) BOOL keyboardDisplayRequiresUserAction;
97 // Designated initializer.
98 - (instancetype
)initWithWebState
:(scoped_ptr
<web::WebStateImpl
>)webState
;
100 // Creates a web view if it's not yet created.
101 - (void)ensureWebViewCreated
;
103 // Destroys the web view by setting webView property to nil.
104 - (void)resetWebView
;
106 // Returns the current URL of the web view, and sets |trustLevel| accordingly
107 // based on the confidence in the verification.
108 - (GURL
)webURLWithTrustLevel
:(web::URLVerificationTrustLevel
*)trustLevel
;
110 // Registers the current user agent with the web view.
111 - (void)registerUserAgent
;
113 // Returns the type of document object loaded in the web view.
114 - (web::WebViewDocumentType
)webViewDocumentType
;
116 // Loads the given HTML in the web view.
117 - (void)loadWebHTMLString
:(NSString
*)html forURL
:(const GURL
&)URL
;
119 // These public methods should be implemented by subclasses.
120 //- (void)evaluateJavaScript:(NSString*)script
121 // stringResultHandler:(web::JavaScriptCompletion)handler;
122 //- (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass
123 // presenceBeacon:(NSString*)beacon;
124 //- (void)loadRequest:(NSMutableURLRequest*)request;
125 // Subclasses must call super's implementation.
126 //- (void)injectScript:(NSString*)script
127 // forClass:(Class)jsInjectionManagerClass;
128 //- (web::WebViewType)webViewType;
129 //- (void)evaluateUserJavaScript:(NSString*)script;
131 // Called before loading current URL in WebView.
132 - (void)willLoadCurrentURLInWebView
;
134 // Indicates whether or not there's an indication that the page is probably
135 // about to change. This is called as a hint to the UIWebView-based subclass to
136 // change polling behavior.
137 // TODO(stuartmorgan): Remove once the hook points are driven from the subclass.
138 - (void)setPageChangeProbability
:(web::PageChangeProbability
)probability
;
140 // Cancels any load in progress in the web view.
141 - (void)abortWebLoad
;
143 // Called whenever any in-progress-load state should be reset.
144 // TODO(stuartmorgan): Remove this; it should be tracked internally to each
145 // subclass, since the existing logic is somewhat UIWebView-guesswork-based.
146 - (void)resetLoadState
;
148 // Evaluates given JavaScript to suppress the dialogs. Subclasses should prefer
149 // synchronous execution.
150 - (void)setSuppressDialogsWithHelperScript
:(NSString
*)script
;
152 // Called when CRWWebController believes that web page title has been changed.
153 - (void)titleDidChange
;
155 // Returns selector to handle JavaScript message with command property
156 // |command|. Subclasses may override to handle class-specific messages.
157 - (SEL
)selectorToHandleJavaScriptCommand
:(const std::string
&)command
;
159 // Extracts the absolute zoom scale of content displayed with |scrollState|,
160 // given the differing ways in which UIWebView and WKWebView use UIScrollView's
161 // zoom scale properties. See comments in
162 // |applyWebViewScrollZoomScaleFromScrollState:| implementations for details.
163 - (CGFloat
)absoluteZoomScaleForScrollState
:
164 (const web::PageScrollState
&)scrollState
;
166 // Sets zoom scale value for webview scroll view from |scrollState|.
167 - (void)applyWebViewScrollZoomScaleFromScrollState
:
168 (const web::PageScrollState
&)scrollState
;
170 #pragma mark - Optional methods for subclasses
171 // Subclasses may overwrite methods in this section.
173 // Checks if the URL has changed unexpectedly, and handles such changes.
174 // Returns true if the URL has changed.
175 // TODO(stuartmorgan): Remove once the hook points are driven from the subclass.
176 - (BOOL
)checkForUnexpectedURLChange
;
178 // Handles 'window.history.didPushState' message.
179 - (BOOL
)handleWindowHistoryDidPushStateMessage
:(base::DictionaryValue
*)message
180 context
:(NSDictionary
*)context
;
182 // Handles 'window.history.didReplaceState' message.
183 - (BOOL
)handleWindowHistoryDidReplaceStateMessage
:
184 (base::DictionaryValue
*)message
185 context
:(NSDictionary
*)context
;
187 // Sets up WebUI for URL.
188 - (void)createWebUIForURL
:(const GURL
&)URL
;
190 // Clears WebUI, if one exists.
193 #pragma mark - Internal methods for use by subclasses
195 // The web view's view of the current URL. During page transitions
196 // this may not be the same as the session history's view of the current URL.
197 // This method can change the state of the CRWWebController, as it will display
198 // an error if the returned URL is not reliable from a security point of view.
199 // Note that this method is expensive, so it should always be cached locally if
200 // it's needed multiple times in a method.
201 @
property(nonatomic
, readonly
) GURL currentURL
;
203 // The default URL for a newly created web view.
204 @
property(nonatomic
, readonly
) const GURL
& defaultURL
;
206 // Last URL change reported to webDidStartLoadingURL. Used to detect page
207 // location changes in practice.
208 @
property(nonatomic
, readonly
) GURL URLOnStartLoading
;
210 // Last URL change registered for load request.
211 @
property(nonatomic
, readonly
) GURL lastRegisteredRequestURL
;
213 // Returns YES if the object is being deallocated.
214 @
property(nonatomic
, readonly
) BOOL isBeingDestroyed
;
216 // Return YES if network activity is being halted. Halting happens prior to
218 @
property(nonatomic
, readonly
) BOOL isHalted
;
220 // Returns whether the user is interacting with the page.
221 @
property(nonatomic
, readonly
) BOOL userIsInteracting
;
223 // YES if a user interaction has been registered at any time once the page has
225 @
property(nonatomic
, readonly
) BOOL userInteractionRegistered
;
227 // Returns the current window id.
228 @
property(nonatomic
, readonly
) NSString
* windowId
;
230 // Returns windowID that is saved when a page changes. Used to detect refreshes.
231 @
property(nonatomic
, readonly
) NSString
* lastSeenWindowID
;
233 // Returns NavigationManager's session controller.
234 @
property(nonatomic
, readonly
) CRWSessionController
* sessionController
;
236 // Returns a new script which wraps |script| with windowID check so |script| is
237 // not evaluated on windowID mismatch.
238 - (NSString
*)scriptByAddingWindowIDCheckForScript
:(NSString
*)script
;
240 // Removes webView, optionally tracking the URL of the evicted
241 // page for later cache-based reconstruction.
242 - (void)removeWebViewAllowingCachedReconstruction
:(BOOL
)allowCache
;
244 // Subclasses must call this method every time when web view has been created
245 // or recreated. This method should not be called if a web view property has
246 // changed (e.g. view's background color). Web controller adds |webView| to its
248 - (void)webViewDidChange
;
250 // Updates the internal state and informs the delegate that any outstanding load
251 // operations are cancelled.
252 - (void)loadCancelled
;
254 // Returns the URL that the navigation system believes should be currently
256 // TODO(stuartmorgan):Remove this in favor of more specific getters.
257 - (const GURL
&)currentNavigationURL
;
259 // Called when the web page has changed document and/or URL, and so the page
260 // navigation should be reported to the delegate, and internal state updated to
261 // reflect the fact that the navigation has occurred.
262 // TODO(stuartmorgan): The code conflates URL changes and document object
263 // changes; the two need to be separated and handled differently.
264 - (void)webPageChanged
;
266 // Injects all scripts registered for early injection, as well as the window ID,
267 // if necssary. If they are already injected, this is a no-op.
268 - (void)injectEarlyInjectionScripts
;
270 // Inject windowID if not yet injected.
271 - (void)injectWindowID
;
273 // Called when a page (native or web) has actually started loading (i.e., for
274 // a web page the document has actually changed), or after the load request has
275 // been registered for a non-document-changing URL change. Updates internal
276 // state not specific to web pages, and informs the delegate.
277 - (void)didStartLoadingURL
:(const GURL
&)URL updateHistory
:(BOOL
)updateHistory
;
279 // Should be called with YES if a user interaction has been registered at any
280 // time once the page has loaded.
281 - (void)setUserInteractionRegistered
:(BOOL
)flag
;
283 // Returns YES if the user interacted with the page recently.
284 - (BOOL
)userClickedRecently
;
286 // Returns whether the desktop user agent should be used when setting the user
288 - (BOOL
)useDesktopUserAgent
;
290 // Called when SSL status has been updated for the current navigation item.
291 - (void)didUpdateSSLStatusForCurrentNavigationItem
;
293 // Processes the given web invocation; urlSchemeIsWebInvoke: must return YES
294 // for the given URL. |request| should be the request associated with that load.
295 - (void)handleWebInvokeURL
:(const GURL
&)url request
:(NSURLRequest
*)request
;
297 // Returns YES if the given load request should be allowed to continue. If this
298 // returns NO, the load should be cancelled. |targetFrame| contains information
299 // about the frame to which navigation is targeted, can be null.
300 // |isLinkClick| should indicate whether the navigation is the
301 // result of a link click (either directly, or via JS triggered by a link).
302 - (BOOL
)shouldAllowLoadWithRequest
:(NSURLRequest
*)request
303 targetFrame
:(const web::FrameInfo
*)targetFrame
304 isLinkClick
:(BOOL
)isLinkClick
;
306 // Prepares web controller and delegates for anticipated page change.
307 // Allows several methods to invoke webWill/DidAddPendingURL on anticipated page
308 // change, using the same cached request and calculated transition types.
309 - (void)registerLoadRequest
:(const GURL
&)URL
310 referrer
:(const web::Referrer
&)referrer
311 transition
:(ui::PageTransition
)transition
;
313 // Called when a load ends in an error.
314 // TODO(stuartmorgan): Figure out if there's actually enough shared logic that
315 // this makes sense. At the very least remove inMainFrame since that only makes
316 // sense for UIWebView.
317 - (void)handleLoadError
:(NSError
*)error inMainFrame
:(BOOL
)inMainFrame
;
319 // Update the appropriate parts of the model and broadcast to the embedder. This
320 // may be called multiple times and thus must be idempotent.
321 - (void)loadCompleteWithSuccess
:(BOOL
)loadSuccess
;
323 // Creates a new opened by DOM window and returns its autoreleased web
325 - (CRWWebController
*)createChildWebControllerWithReferrerURL
:
326 (const GURL
&)referrerURL
;
328 // Called following navigation completion to generate final navigation lifecycle
329 // events. Navigation is considered complete when the document has finished
330 // loading, or when other page load mechanics are completed on a
331 // non-document-changing URL change.
332 - (void)didFinishNavigation
;
334 // Returns the referrer policy for the given referrer policy string (as reported
336 - (web::ReferrerPolicy
)referrerPolicyFromString
:(const std::string
&)policy
;
338 // Returns YES if the popup should be blocked, NO otherwise.
339 - (BOOL
)shouldBlockPopupWithURL
:(const GURL
&)popupURL
340 sourceURL
:(const GURL
&)sourceURL
;
342 // Call to stop web controller activity, in particular to stop all network
343 // requests. Called as part of the close sequence if it hasn't already been
344 // halted; should also be called from the web delegate as part of any shutdown
345 // sequence which doesn't call -close.
346 - (void)terminateNetworkActivity
;
348 // Acts on a single message from the JS object, parsed from JSON into a
349 // DictionaryValue. Returns NO if the format for the message was invalid.
350 - (BOOL
)respondToMessage
:(base::DictionaryValue
*)crwMessage
351 userIsInteracting
:(BOOL
)userIsInteracting
352 originURL
:(const GURL
&)originURL
;
354 // Asynchronously determines window size of the web page. |handler| cannot
356 - (void)fetchWebPageSizeWithCompletionHandler
:(void (^)(CGSize
))handler
;
358 // Tries to open a popup with the given new window information.
359 - (void)openPopupWithInfo
:(const web::NewWindowInfo
&)windowInfo
;
361 // Returns the referrer for the current page.
362 - (web::Referrer
)currentReferrer
;
364 // Returns the current transition type.
365 - (ui::PageTransition
)currentTransition
;
367 // Resets pending external request information.
368 - (void)resetExternalRequest
;
372 #endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_PROTECTED_H_