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_display_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 // Sets zoom scale value for webview scroll view from |zoomState|.
160 - (void)applyWebViewScrollZoomScaleFromZoomState
:
161 (const web::PageZoomState
&)zoomState
;
163 // Handles cancelled load in WKWebView (error with NSURLErrorCancelled code).
164 - (void)handleCancelledError
:(NSError
*)error
;
166 #pragma mark - Optional methods for subclasses
167 // Subclasses may overwrite methods in this section.
169 // Checks if the URL has changed unexpectedly, and handles such changes.
170 // Returns true if the URL has changed.
171 // TODO(stuartmorgan): Remove once the hook points are driven from the subclass.
172 - (BOOL
)checkForUnexpectedURLChange
;
174 // Handles 'window.history.willChangeState' message.
175 - (BOOL
)handleWindowHistoryWillChangeStateMessage
:
176 (base::DictionaryValue
*)message
177 context
:(NSDictionary
*)context
;
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 // Aborts any load for both the web view and web controller.
257 // Returns the URL that the navigation system believes should be currently
259 // TODO(stuartmorgan):Remove this in favor of more specific getters.
260 - (const GURL
&)currentNavigationURL
;
262 // Called when the web page has changed document and/or URL, and so the page
263 // navigation should be reported to the delegate, and internal state updated to
264 // reflect the fact that the navigation has occurred.
265 // TODO(stuartmorgan): The code conflates URL changes and document object
266 // changes; the two need to be separated and handled differently.
267 - (void)webPageChanged
;
269 // Injects all scripts registered for early injection, as well as the window ID,
270 // if necssary. If they are already injected, this is a no-op.
271 - (void)injectEarlyInjectionScripts
;
273 // Inject windowID if not yet injected.
274 - (void)injectWindowID
;
276 // Called when a page (native or web) has actually started loading (i.e., for
277 // a web page the document has actually changed), or after the load request has
278 // been registered for a non-document-changing URL change. Updates internal
279 // state not specific to web pages, and informs the delegate.
280 - (void)didStartLoadingURL
:(const GURL
&)URL updateHistory
:(BOOL
)updateHistory
;
282 // Should be called with YES if a user interaction has been registered at any
283 // time once the page has loaded.
284 - (void)setUserInteractionRegistered
:(BOOL
)flag
;
286 // Returns YES if the user interacted with the page recently.
287 - (BOOL
)userClickedRecently
;
289 // Returns whether the desktop user agent should be used when setting the user
291 - (BOOL
)useDesktopUserAgent
;
293 // Called when SSL status has been updated for the current navigation item.
294 - (void)didUpdateSSLStatusForCurrentNavigationItem
;
296 // Processes the given web invocation; urlSchemeIsWebInvoke: must return YES
297 // for the given URL. |request| should be the request associated with that load.
298 - (void)handleWebInvokeURL
:(const GURL
&)url request
:(NSURLRequest
*)request
;
300 // Returns YES if the given load request should be allowed to continue. If this
301 // returns NO, the load should be cancelled. |targetFrame| contains information
302 // about the frame to which navigation is targeted, can be null.
303 // |isLinkClick| should indicate whether the navigation is the
304 // result of a link click (either directly, or via JS triggered by a link).
305 - (BOOL
)shouldAllowLoadWithRequest
:(NSURLRequest
*)request
306 targetFrame
:(const web::FrameInfo
*)targetFrame
307 isLinkClick
:(BOOL
)isLinkClick
;
309 // Prepares web controller and delegates for anticipated page change.
310 // Allows several methods to invoke webWill/DidAddPendingURL on anticipated page
311 // change, using the same cached request and calculated transition types.
312 - (void)registerLoadRequest
:(const GURL
&)URL
313 referrer
:(const web::Referrer
&)referrer
314 transition
:(ui::PageTransition
)transition
;
316 // Called when a load ends in an error.
317 // TODO(stuartmorgan): Figure out if there's actually enough shared logic that
318 // this makes sense. At the very least remove inMainFrame since that only makes
319 // sense for UIWebView.
320 - (void)handleLoadError
:(NSError
*)error inMainFrame
:(BOOL
)inMainFrame
;
322 // Update the appropriate parts of the model and broadcast to the embedder. This
323 // may be called multiple times and thus must be idempotent.
324 - (void)loadCompleteWithSuccess
:(BOOL
)loadSuccess
;
326 // Creates a new opened by DOM window and returns its autoreleased web
328 - (CRWWebController
*)createChildWebControllerWithReferrerURL
:
329 (const GURL
&)referrerURL
;
331 // Called following navigation completion to generate final navigation lifecycle
332 // events. Navigation is considered complete when the document has finished
333 // loading, or when other page load mechanics are completed on a
334 // non-document-changing URL change.
335 - (void)didFinishNavigation
;
337 // Returns the referrer policy for the given referrer policy string (as reported
339 - (web::ReferrerPolicy
)referrerPolicyFromString
:(const std::string
&)policy
;
341 // Returns YES if the popup should be blocked, NO otherwise.
342 - (BOOL
)shouldBlockPopupWithURL
:(const GURL
&)popupURL
343 sourceURL
:(const GURL
&)sourceURL
;
345 // Call to stop web controller activity, in particular to stop all network
346 // requests. Called as part of the close sequence if it hasn't already been
347 // halted; should also be called from the web delegate as part of any shutdown
348 // sequence which doesn't call -close.
349 - (void)terminateNetworkActivity
;
351 // Acts on a single message from the JS object, parsed from JSON into a
352 // DictionaryValue. Returns NO if the format for the message was invalid.
353 - (BOOL
)respondToMessage
:(base::DictionaryValue
*)crwMessage
354 userIsInteracting
:(BOOL
)userIsInteracting
355 originURL
:(const GURL
&)originURL
;
357 // Asynchronously determines window size of the web page. |handler| cannot
359 - (void)fetchWebPageSizeWithCompletionHandler
:(void (^)(CGSize
))handler
;
361 // Tries to open a popup with the given new window information.
362 - (void)openPopupWithInfo
:(const web::NewWindowInfo
&)windowInfo
;
364 // Returns the referrer for the current page.
365 - (web::Referrer
)currentReferrer
;
367 // Returns the current transition type.
368 - (ui::PageTransition
)currentTransition
;
370 // Resets pending external request information.
371 - (void)resetExternalRequest
;
375 #endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_PROTECTED_H_