Add ICU message format support
[chromium-blink-merge.git] / ios / web / web_state / web_state_impl.h
blob78d4f9849eb9f46479d2b25544eb73b36e3beea1
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/values.h"
17 #include "ios/web/navigation/navigation_manager_delegate.h"
18 #include "ios/web/navigation/navigation_manager_impl.h"
19 #include "ios/web/net/request_tracker_impl.h"
20 #include "ios/web/public/web_state/web_state.h"
21 #include "url/gurl.h"
23 @protocol CRWRequestTrackerDelegate;
24 @class CRWWebController;
25 @protocol CRWWebViewProxy;
26 @class NSURLRequest;
27 @class NSURLResponse;
29 namespace net {
30 class HttpResponseHeaders;
33 namespace web {
35 class BrowserState;
36 struct Credential;
37 struct FaviconURL;
38 struct LoadCommittedDetails;
39 class NavigationManager;
40 class WebInterstitialImpl;
41 class WebStateFacadeDelegate;
42 class WebStatePolicyDecider;
43 class WebUIIOS;
45 // Implementation of WebState.
46 // Generally mirrors upstream's WebContents implementation.
47 // General notes on expected WebStateImpl ownership patterns:
48 // - Outside of tests, WebStateImpls are created
49 // (a) By @Tab, when creating a new Tab.
50 // (b) By @SessionWindow, when decoding a saved session.
51 // (c) By the Copy() method, below, used when marshalling a session
52 // in preparation for saving.
53 // - WebControllers are the eventual long-term owners of WebStateImpls.
54 // - SessionWindows are transient owners, passing ownership into WebControllers
55 // during session restore, and discarding owned copies of WebStateImpls after
56 // writing them out for session saves.
57 class WebStateImpl : public WebState, public NavigationManagerDelegate {
58 public:
59 WebStateImpl(BrowserState* browser_state);
60 ~WebStateImpl() override;
62 // Sets the CRWWebController that backs this object. Typically
63 // |web_controller| will also take ownership of this object. This will also
64 // create the WebContentsIOS facade.
65 void SetWebController(CRWWebController* web_controller);
67 // Gets or sets the delegate used to communicate with the web contents facade.
68 WebStateFacadeDelegate* GetFacadeDelegate() const;
69 void SetFacadeDelegate(WebStateFacadeDelegate* facade_delegate);
71 // Returns a WebStateImpl that doesn't have a browser context, web
72 // controller, or facade set, but which otherwise has the same state variables
73 // as the calling object (including copies of the NavigationManager and its
74 // attendant CRWSessionController).
75 // TODO(marq): Revisit this function and the ownership model described above;
76 // too this depends on and interacts directly with above-the-web-level
77 // information.
78 WebStateImpl* CopyForSessionWindow();
80 // Notifies the observers that a provisional navigation has started.
81 void OnProvisionalNavigationStarted(const GURL& url);
83 // Notifies the observers that the URL hash of the current page changed.
84 void OnUrlHashChanged();
86 // Notifies the observers that the history state of the current page changed.
87 void OnHistoryStateChanged();
89 // Called when a script command is received.
90 // Returns true if the command was handled.
91 bool OnScriptCommandReceived(const std::string& command,
92 const base::DictionaryValue& value,
93 const GURL& url,
94 bool user_is_interacting);
96 void SetIsLoading(bool is_loading);
98 // Called when a page is loaded. Must be called only once per page.
99 void OnPageLoaded(const GURL& url, bool load_success);
101 // Called on form submission.
102 void OnDocumentSubmitted(const std::string& form_name, bool user_initiated);
104 // Called when form activity is registered.
105 void OnFormActivityRegistered(const std::string& form_name,
106 const std::string& field_name,
107 const std::string& type,
108 const std::string& value,
109 int key_code,
110 bool input_missing);
112 // Called when new FaviconURL candidates are received.
113 void OnFaviconUrlUpdated(const std::vector<FaviconURL>& candidates);
115 // Called when the page requests a credential.
116 void OnCredentialsRequested(int request_id,
117 const GURL& source_url,
118 bool suppress_ui,
119 const std::vector<std::string>& federations,
120 bool user_interaction);
122 // Called when the page sends a notification that the user signed in with
123 // |credential|.
124 void OnSignedIn(int request_id,
125 const GURL& source_url,
126 const web::Credential& credential);
128 // Called when the page sends a notification that the user signed in.
129 void OnSignedIn(int request_id, const GURL& source_url);
131 // Called when the page sends a notification that the user was signed out.
132 void OnSignedOut(int request_id, const GURL& source_url);
134 // Called when the page sends a notification that the user failed to sign in
135 // with |credential|.
136 void OnSignInFailed(int request_id,
137 const GURL& source_url,
138 const web::Credential& credential);
140 // Called when the page sends a notification that the user failed to sign in.
141 void OnSignInFailed(int request_id, const GURL& source_url);
143 // Returns the NavigationManager for this WebState.
144 const NavigationManagerImpl& GetNavigationManagerImpl() const;
145 NavigationManagerImpl& GetNavigationManagerImpl();
147 // Creates a WebUI page for the given url, owned by this object.
148 void CreateWebUI(const GURL& url);
149 // Clears any current WebUI. Should be called when the page changes.
150 // TODO(stuartmorgan): Remove once more logic is moved from WebController
151 // into this class.
152 void ClearWebUI();
153 // Returns true if there is a WebUI active.
154 bool HasWebUI();
155 // Processes a message from a WebUI displayed at the given URL.
156 void ProcessWebUIMessage(const GURL& source_url,
157 const std::string& message,
158 const base::ListValue& args);
159 // Invokes page load for WebUI URL with HTML. URL must have an application
160 // specific scheme.
161 virtual void LoadWebUIHtml(const base::string16& html, const GURL& url);
163 // Gets the HTTP response headers associated with the current page.
164 // NOTE: For a WKWebView-based WebState, these headers are generated via
165 // net::CreateHeadersFromNSHTTPURLResponse(); see comments in
166 // http_response_headers_util.h for limitations.
167 net::HttpResponseHeaders* GetHttpResponseHeaders() const;
169 // Called when HTTP response headers are received.
170 // |resource_url| is the URL associated with the headers.
171 // This function has no visible effects until UpdateHttpResponseHeaders() is
172 // called.
173 void OnHttpResponseHeadersReceived(net::HttpResponseHeaders* response_headers,
174 const GURL& resource_url);
176 // Executes a JavaScript string on the page asynchronously.
177 // TODO(shreyasv): Rename this to ExecuteJavaScript for consitency with
178 // upstream API.
179 virtual void ExecuteJavaScriptAsync(const base::string16& script);
181 // Returns whether the navigation corresponding to |request| should be allowed
182 // to continue by asking its policy deciders. Defaults to true.
183 bool ShouldAllowRequest(NSURLRequest* request);
184 // Returns whether the navigation corresponding to |response| should be
185 // allowed to continue by asking its policy deciders. Defaults to true.
186 bool ShouldAllowResponse(NSURLResponse* response);
188 // Request tracker management. For now, this exposes the RequestTracker for
189 // embedders to use.
190 // TODO(stuartmorgan): RequestTracker should become an internal detail of this
191 // class.
193 // Create a new tracker using |delegate| as its delegate.
194 void InitializeRequestTracker(id<CRWRequestTrackerDelegate> delegate);
196 // Close the request tracker and delete it.
197 void CloseRequestTracker();
199 // Returns the tracker for this WebStateImpl.
200 RequestTrackerImpl* GetRequestTracker();
202 // Gets and sets the mode controlling the HTTP cache behavior.
203 // TODO(rohitrao): As with the other RequestTracker-related methods, this
204 // should become an internal detail of this class.
205 net::RequestTracker::CacheMode GetCacheMode();
206 void SetCacheMode(net::RequestTracker::CacheMode mode);
208 // Lazily creates (if necessary) and returns |request_group_id_|.
209 // IMPORTANT: This should not be used for anything other than associating this
210 // instance to network requests.
211 // This function is only intended to be used in web/.
212 // TODO(stuartmorgan): Move this method in an implementation file in web/.
213 NSString* GetRequestGroupID();
215 // WebState:
216 UIView* GetView() override;
217 web::WebViewType GetWebViewType() const override;
218 BrowserState* GetBrowserState() const override;
219 void OpenURL(const WebState::OpenURLParams& params) override;
220 NavigationManager* GetNavigationManager() override;
221 CRWJSInjectionReceiver* GetJSInjectionReceiver() const override;
222 const std::string& GetContentLanguageHeader() const override;
223 const std::string& GetContentsMimeType() const override;
224 bool ContentIsHTML() const override;
225 const base::string16& GetTitle() const override;
226 bool IsLoading() const override;
227 const GURL& GetVisibleURL() const override;
228 const GURL& GetLastCommittedURL() const override;
229 GURL GetCurrentURL(URLVerificationTrustLevel* trust_level) const override;
230 void ShowTransientContentView(CRWContentView* content_view) override;
231 bool IsShowingWebInterstitial() const override;
232 WebInterstitial* GetWebInterstitial() const override;
233 void AddScriptCommandCallback(const ScriptCommandCallback& callback,
234 const std::string& command_prefix) override;
235 void RemoveScriptCommandCallback(const std::string& command_prefix) override;
236 id<CRWWebViewProxy> GetWebViewProxy() const override;
237 int DownloadImage(const GURL& url,
238 bool is_favicon,
239 uint32_t max_bitmap_size,
240 bool bypass_cache,
241 const ImageDownloadCallback& callback) override;
243 // Adds |interstitial|'s view to the web controller's content view.
244 void ShowWebInterstitial(WebInterstitialImpl* interstitial);
246 // Called to dismiss the currently-displayed transient content view.
247 void ClearTransientContentView();
249 // NavigationManagerDelegate:
250 void NavigateToPendingEntry() override;
251 void OnNavigationItemCommitted(
252 const LoadCommittedDetails& load_details) override;
253 WebState* GetWebState() override;
255 protected:
256 void AddObserver(WebStateObserver* observer) override;
257 void RemoveObserver(WebStateObserver* observer) override;
258 void AddPolicyDecider(WebStatePolicyDecider* decider) override;
259 void RemovePolicyDecider(WebStatePolicyDecider* decider) override;
261 private:
262 // Creates a WebUIIOS object for |url| that is owned by the caller. Returns
263 // nullptr if |url| does not correspond to a WebUI page.
264 WebUIIOS* CreateWebUIIOS(const GURL& url);
266 // Updates the HTTP response headers for the main page using the headers
267 // passed to the OnHttpResponseHeadersReceived() function below.
268 // GetHttpResponseHeaders() can be used to get the headers.
269 void UpdateHttpResponseHeaders(const GURL& url);
271 // Returns true if |web_controller_| has been set.
272 bool Configured() const;
274 // Stores whether the web state is currently loading a page.
275 bool is_loading_;
277 // The delegate used to pass state to the web contents facade.
278 WebStateFacadeDelegate* facade_delegate_;
280 // The CRWWebController that backs and owns this object.
281 CRWWebController* web_controller_;
283 NavigationManagerImpl navigation_manager_;
285 // |web::WebUIIOS| object for the current page if it is a WebUI page that
286 // uses the web-based WebUI framework, or nullptr otherwise.
287 scoped_ptr<web::WebUIIOS> web_ui_;
289 // A list of observers notified when page state changes. Weak references.
290 base::ObserverList<WebStateObserver, true> observers_;
292 // All the WebStatePolicyDeciders asked for navigation decision. Weak
293 // references.
294 std::set<WebStatePolicyDecider*> policy_deciders_;
296 // Map of all the HTTP response headers received, for each URL.
297 // This map is cleared after each page load, and only the headers of the main
298 // page are used.
299 std::map<GURL, scoped_refptr<net::HttpResponseHeaders> >
300 response_headers_map_;
301 scoped_refptr<net::HttpResponseHeaders> http_response_headers_;
302 std::string mime_type_;
303 std::string content_language_header_;
305 // Weak pointer to the interstitial page being displayed, if any.
306 WebInterstitialImpl* interstitial_;
308 // Returned by reference.
309 base::string16 empty_string16_;
311 // Request tracker associted with this object.
312 scoped_refptr<RequestTrackerImpl> request_tracker_;
314 // Mode controlling the HTTP cache behavior.
315 net::RequestTracker::CacheMode cache_mode_;
317 // A number identifying this object. This number is injected into the user
318 // agent to allow the network layer to know which web view requests originated
319 // from.
320 base::scoped_nsobject<NSString> request_group_id_;
322 // Callbacks associated to command prefixes.
323 std::map<std::string, ScriptCommandCallback> script_command_callbacks_;
325 DISALLOW_COPY_AND_ASSIGN(WebStateImpl);
328 } // namespace web
330 #endif // IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_