1 // Copyright 2013 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_WEB_STATE_IMPL_H_
6 #define IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/values.h"
16 #include "ios/web/navigation/navigation_manager_delegate.h"
17 #include "ios/web/navigation/navigation_manager_impl.h"
18 #include "ios/web/net/request_tracker_impl.h"
19 #include "ios/web/public/web_state/web_state.h"
22 @protocol CRWRequestTrackerDelegate
;
23 @
class CRWWebController
;
24 @protocol CRWWebViewProxy
;
29 class HttpResponseHeaders
;
37 struct LoadCommittedDetails
;
38 class NavigationManager
;
39 class WebInterstitialImpl
;
40 class WebStateFacadeDelegate
;
41 class WebStatePolicyDecider
;
44 // Implementation of WebState.
45 // Generally mirrors upstream's WebContents implementation.
46 // General notes on expected WebStateImpl ownership patterns:
47 // - Outside of tests, WebStateImpls are created
48 // (a) By @Tab, when creating a new Tab.
49 // (b) By @SessionWindow, when decoding a saved session.
50 // (c) By the Copy() method, below, used when marshalling a session
51 // in preparation for saving.
52 // - WebControllers are the eventual long-term owners of WebStateImpls.
53 // - SessionWindows are transient owners, passing ownership into WebControllers
54 // during session restore, and discarding owned copies of WebStateImpls after
55 // writing them out for session saves.
56 class WebStateImpl
: public WebState
, public NavigationManagerDelegate
{
58 WebStateImpl(BrowserState
* browser_state
);
59 ~WebStateImpl() override
;
61 // Sets the CRWWebController that backs this object. Typically
62 // |web_controller| will also take ownership of this object. This will also
63 // create the WebContentsIOS facade.
64 void SetWebController(CRWWebController
* web_controller
);
66 // Gets or sets the delegate used to communicate with the web contents facade.
67 WebStateFacadeDelegate
* GetFacadeDelegate() const;
68 void SetFacadeDelegate(WebStateFacadeDelegate
* facade_delegate
);
70 // Returns a WebStateImpl that doesn't have a browser context, web
71 // controller, or facade set, but which otherwise has the same state variables
72 // as the calling object (including copies of the NavigationManager and its
73 // attendant CRWSessionController).
74 // TODO(marq): Revisit this function and the ownership model described above;
75 // too this depends on and interacts directly with above-the-web-level
77 WebStateImpl
* CopyForSessionWindow();
79 // Notifies the observers that a provisional navigation has started.
80 void OnProvisionalNavigationStarted(const GURL
& url
);
82 // Notifies the observers that the URL hash of the current page changed.
83 void OnUrlHashChanged();
85 // Notifies the observers that the history state of the current page changed.
86 void OnHistoryStateChanged();
88 // Called when a script command is received.
89 // Returns true if the command was handled.
90 bool OnScriptCommandReceived(const std::string
& command
,
91 const base::DictionaryValue
& value
,
93 bool user_is_interacting
);
95 void SetIsLoading(bool is_loading
);
97 // Called when a page is loaded. Must be called only once per page.
98 void OnPageLoaded(const GURL
& url
, bool load_success
);
100 // Called on form submission.
101 void OnDocumentSubmitted(const std::string
& form_name
, bool user_initiated
);
103 // Called when form activity is registered.
104 void OnFormActivityRegistered(const std::string
& form_name
,
105 const std::string
& field_name
,
106 const std::string
& type
,
107 const std::string
& value
,
111 // Called when new FaviconURL candidates are received.
112 void OnFaviconUrlUpdated(const std::vector
<FaviconURL
>& candidates
);
114 // Called when the page requests a credential.
115 void OnCredentialsRequested(int request_id
,
116 const GURL
& source_url
,
118 const std::vector
<std::string
>& federations
,
119 bool user_interaction
);
121 // Called when the page sends a notification that the user signed in with
123 void OnSignedIn(int request_id
,
124 const GURL
& source_url
,
125 const web::Credential
& credential
);
127 // Called when the page sends a notification that the user signed in.
128 void OnSignedIn(int request_id
, const GURL
& source_url
);
130 // Called when the page sends a notification that the user was signed out.
131 void OnSignedOut(int request_id
, const GURL
& source_url
);
133 // Called when the page sends a notification that the user failed to sign in
134 // with |credential|.
135 void OnSignInFailed(int request_id
,
136 const GURL
& source_url
,
137 const web::Credential
& credential
);
139 // Called when the page sends a notification that the user failed to sign in.
140 void OnSignInFailed(int request_id
, const GURL
& source_url
);
142 // Returns the NavigationManager for this WebState.
143 const NavigationManagerImpl
& GetNavigationManagerImpl() const;
144 NavigationManagerImpl
& GetNavigationManagerImpl();
146 // Creates a WebUI page for the given url, owned by this object.
147 void CreateWebUI(const GURL
& url
);
148 // Clears any current WebUI. Should be called when the page changes.
149 // TODO(stuartmorgan): Remove once more logic is moved from WebController
152 // Returns true if there is a WebUI active.
154 // Processes a message from a WebUI displayed at the given URL.
155 void ProcessWebUIMessage(const GURL
& source_url
,
156 const std::string
& message
,
157 const base::ListValue
& args
);
158 // Invokes page load for WebUI URL with HTML. URL must have an application
160 virtual void LoadWebUIHtml(const base::string16
& html
, const GURL
& url
);
162 // Gets the HTTP response headers associated with the current page.
163 // NOTE: For a WKWebView-based WebState, these headers are generated via
164 // net::CreateHeadersFromNSHTTPURLResponse(); see comments in
165 // http_response_headers_util.h for limitations.
166 net::HttpResponseHeaders
* GetHttpResponseHeaders() const;
168 // Called when HTTP response headers are received.
169 // |resource_url| is the URL associated with the headers.
170 // This function has no visible effects until UpdateHttpResponseHeaders() is
172 void OnHttpResponseHeadersReceived(net::HttpResponseHeaders
* response_headers
,
173 const GURL
& resource_url
);
175 // Executes a JavaScript string on the page asynchronously.
176 // TODO(shreyasv): Rename this to ExecuteJavaScript for consitency with
178 virtual void ExecuteJavaScriptAsync(const base::string16
& script
);
180 // Returns whether the navigation corresponding to |request| should be allowed
181 // to continue by asking its policy deciders. Defaults to true.
182 bool ShouldAllowRequest(NSURLRequest
* request
);
183 // Returns whether the navigation corresponding to |response| should be
184 // allowed to continue by asking its policy deciders. Defaults to true.
185 bool ShouldAllowResponse(NSURLResponse
* response
);
187 // Request tracker management. For now, this exposes the RequestTracker for
189 // TODO(stuartmorgan): RequestTracker should become an internal detail of this
192 // Create a new tracker using |delegate| as its delegate.
193 void InitializeRequestTracker(id
<CRWRequestTrackerDelegate
> delegate
);
195 // Close the request tracker and delete it.
196 void CloseRequestTracker();
198 // Returns the tracker for this WebStateImpl.
199 RequestTrackerImpl
* GetRequestTracker();
201 // Gets and sets the mode controlling the HTTP cache behavior.
202 // TODO(rohitrao): As with the other RequestTracker-related methods, this
203 // should become an internal detail of this class.
204 net::RequestTracker::CacheMode
GetCacheMode();
205 void SetCacheMode(net::RequestTracker::CacheMode mode
);
207 // Lazily creates (if necessary) and returns |request_group_id_|.
208 // IMPORTANT: This should not be used for anything other than associating this
209 // instance to network requests.
210 // This function is only intended to be used in web/.
211 // TODO(stuartmorgan): Move this method in an implementation file in web/.
212 NSString
* GetRequestGroupID();
215 UIView
* GetView() override
;
216 web::WebViewType
GetWebViewType() const override
;
217 BrowserState
* GetBrowserState() const override
;
218 void OpenURL(const WebState::OpenURLParams
& params
) override
;
219 NavigationManager
* GetNavigationManager() override
;
220 CRWJSInjectionReceiver
* GetJSInjectionReceiver() const override
;
221 const std::string
& GetContentLanguageHeader() const override
;
222 const std::string
& GetContentsMimeType() const override
;
223 bool ContentIsHTML() const override
;
224 const base::string16
& GetTitle() const override
;
225 bool IsLoading() const override
;
226 const GURL
& GetVisibleURL() const override
;
227 const GURL
& GetLastCommittedURL() const override
;
228 GURL
GetCurrentURL(URLVerificationTrustLevel
* trust_level
) const override
;
229 void ShowTransientContentView(CRWContentView
* content_view
) override
;
230 bool IsShowingWebInterstitial() const override
;
231 WebInterstitial
* GetWebInterstitial() const override
;
232 void AddScriptCommandCallback(const ScriptCommandCallback
& callback
,
233 const std::string
& command_prefix
) override
;
234 void RemoveScriptCommandCallback(const std::string
& command_prefix
) override
;
235 id
<CRWWebViewProxy
> GetWebViewProxy() const override
;
236 int DownloadImage(const GURL
& url
,
238 uint32_t max_bitmap_size
,
240 const ImageDownloadCallback
& callback
) override
;
242 // Adds |interstitial|'s view to the web controller's content view.
243 void ShowWebInterstitial(WebInterstitialImpl
* interstitial
);
245 // Called to dismiss the currently-displayed transient content view.
246 void ClearTransientContentView();
248 // NavigationManagerDelegate:
249 void NavigateToPendingEntry() override
;
250 void OnNavigationItemCommitted(
251 const LoadCommittedDetails
& load_details
) override
;
252 WebState
* GetWebState() override
;
255 void AddObserver(WebStateObserver
* observer
) override
;
256 void RemoveObserver(WebStateObserver
* observer
) override
;
257 void AddPolicyDecider(WebStatePolicyDecider
* decider
) override
;
258 void RemovePolicyDecider(WebStatePolicyDecider
* decider
) override
;
261 // Creates a WebUIIOS object for |url| that is owned by the caller. Returns
262 // nullptr if |url| does not correspond to a WebUI page.
263 WebUIIOS
* CreateWebUIIOS(const GURL
& url
);
265 // Updates the HTTP response headers for the main page using the headers
266 // passed to the OnHttpResponseHeadersReceived() function below.
267 // GetHttpResponseHeaders() can be used to get the headers.
268 void UpdateHttpResponseHeaders(const GURL
& url
);
270 // Returns true if |web_controller_| has been set.
271 bool Configured() const;
273 // Stores whether the web state is currently loading a page.
276 // The delegate used to pass state to the web contents facade.
277 WebStateFacadeDelegate
* facade_delegate_
;
279 // The CRWWebController that backs and owns this object.
280 CRWWebController
* web_controller_
;
282 NavigationManagerImpl navigation_manager_
;
284 // |web::WebUIIOS| object for the current page if it is a WebUI page that
285 // uses the web-based WebUI framework, or nullptr otherwise.
286 scoped_ptr
<web::WebUIIOS
> web_ui_
;
288 // A list of observers notified when page state changes. Weak references.
289 base::ObserverList
<WebStateObserver
, true> observers_
;
291 // All the WebStatePolicyDeciders asked for navigation decision. Weak
293 // WebStatePolicyDeciders are semantically different from observers (they
294 // modify the behavior of the WebState) but are used like observers in the
295 // code, hence the ObserverList.
296 base::ObserverList
<WebStatePolicyDecider
, true> policy_deciders_
;
298 // Map of all the HTTP response headers received, for each URL.
299 // This map is cleared after each page load, and only the headers of the main
301 std::map
<GURL
, scoped_refptr
<net::HttpResponseHeaders
> >
302 response_headers_map_
;
303 scoped_refptr
<net::HttpResponseHeaders
> http_response_headers_
;
304 std::string mime_type_
;
305 std::string content_language_header_
;
307 // Weak pointer to the interstitial page being displayed, if any.
308 WebInterstitialImpl
* interstitial_
;
310 // Returned by reference.
311 base::string16 empty_string16_
;
313 // Request tracker associted with this object.
314 scoped_refptr
<RequestTrackerImpl
> request_tracker_
;
316 // Mode controlling the HTTP cache behavior.
317 net::RequestTracker::CacheMode cache_mode_
;
319 // A number identifying this object. This number is injected into the user
320 // agent to allow the network layer to know which web view requests originated
322 base::scoped_nsobject
<NSString
> request_group_id_
;
324 // Callbacks associated to command prefixes.
325 std::map
<std::string
, ScriptCommandCallback
> script_command_callbacks_
;
327 DISALLOW_COPY_AND_ASSIGN(WebStateImpl
);
332 #endif // IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_