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