summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/web_state_impl.h
blob: 639f293ac291b89642d561ca0c29a497c9741658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_
#define IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "ios/web/navigation/navigation_manager_delegate.h"
#include "ios/web/navigation/navigation_manager_impl.h"
#include "ios/web/net/request_tracker_impl.h"
#include "ios/web/public/web_state/web_state.h"
#include "url/gurl.h"

@protocol CRWRequestTrackerDelegate;
@class CRWWebController;
@protocol CRWWebViewProxy;
@class NSURLRequest;
@class NSURLResponse;

namespace net {
class HttpResponseHeaders;
}

namespace web {

class BrowserState;
struct Credential;
struct FaviconURL;
struct LoadCommittedDetails;
class NavigationManager;
class WebInterstitialImpl;
class WebStateFacadeDelegate;
class WebStatePolicyDecider;
class WebUIIOS;

// Implementation of WebState.
// Generally mirrors //content's WebContents implementation.
// General notes on expected WebStateImpl ownership patterns:
//  - Outside of tests, WebStateImpls are created
//      (a) By @Tab, when creating a new Tab.
//      (b) By @SessionWindow, when decoding a saved session.
//      (c) By the Copy() method, below, used when marshalling a session
//          in preparation for saving.
//  - WebControllers are the eventual long-term owners of WebStateImpls.
//  - SessionWindows are transient owners, passing ownership into WebControllers
//    during session restore, and discarding owned copies of WebStateImpls after
//    writing them out for session saves.
class WebStateImpl : public WebState, public NavigationManagerDelegate {
 public:
  WebStateImpl(BrowserState* browser_state);
  ~WebStateImpl() override;

  // Sets the CRWWebController that backs this object. Typically
  // |web_controller| will also take ownership of this object. This will also
  // create the WebContentsIOS facade.
  void SetWebController(CRWWebController* web_controller);

  // Gets or sets the delegate used to communicate with the web contents facade.
  WebStateFacadeDelegate* GetFacadeDelegate() const;
  void SetFacadeDelegate(WebStateFacadeDelegate* facade_delegate);

  // Returns a WebStateImpl that doesn't have a browser context, web
  // controller, or facade set, but which otherwise has the same state variables
  // as the calling object (including copies of the NavigationManager and its
  // attendant CRWSessionController).
  // TODO(crbug.com/546377): Clean up this method.
  WebStateImpl* CopyForSessionWindow();

  // Notifies the observers that a provisional navigation has started.
  void OnProvisionalNavigationStarted(const GURL& url);

  // Called when a navigation is committed.
  void OnNavigationCommitted(const GURL& url);

  // Notifies the observers that the URL hash of the current page changed.
  void OnUrlHashChanged();

  // Notifies the observers that the history state of the current page changed.
  void OnHistoryStateChanged();

  // Called when a script command is received.
  // Returns true if the command was handled.
  bool OnScriptCommandReceived(const std::string& command,
                               const base::DictionaryValue& value,
                               const GURL& url,
                               bool user_is_interacting);

  void SetIsLoading(bool is_loading);

  // Called when a page is loaded. Must be called only once per page.
  void OnPageLoaded(const GURL& url, bool load_success);

  // Called on form submission.
  void OnDocumentSubmitted(const std::string& form_name, bool user_initiated);

  // Called when form activity is registered.
  void OnFormActivityRegistered(const std::string& form_name,
                                const std::string& field_name,
                                const std::string& type,
                                const std::string& value,
                                int key_code,
                                bool input_missing);

  // Called when new FaviconURL candidates are received.
  void OnFaviconUrlUpdated(const std::vector<FaviconURL>& candidates);

  // Called when the page requests a credential.
  void OnCredentialsRequested(int request_id,
                              const GURL& source_url,
                              bool unmediated,
                              const std::vector<std::string>& federations,
                              bool user_interaction);

  // Called when the page sends a notification that the user signed in with
  // |credential|.
  void OnSignedIn(int request_id,
                  const GURL& source_url,
                  const web::Credential& credential);

  // Called when the page sends a notification that the user signed in.
  void OnSignedIn(int request_id, const GURL& source_url);

  // Called when the page sends a notification that the user was signed out.
  void OnSignedOut(int request_id, const GURL& source_url);

  // Called when the page sends a notification that the user failed to sign in
  // with |credential|.
  void OnSignInFailed(int request_id,
                      const GURL& source_url,
                      const web::Credential& credential);

  // Called when the page sends a notification that the user failed to sign in.
  void OnSignInFailed(int request_id, const GURL& source_url);

  // Returns the NavigationManager for this WebState.
  const NavigationManagerImpl& GetNavigationManagerImpl() const;
  NavigationManagerImpl& GetNavigationManagerImpl();

  // Creates a WebUI page for the given url, owned by this object.
  void CreateWebUI(const GURL& url);
  // Clears any current WebUI. Should be called when the page changes.
  // TODO(stuartmorgan): Remove once more logic is moved from WebController
  // into this class.
  void ClearWebUI();
  // Returns true if there is a WebUI active.
  bool HasWebUI();
  // Processes a message from a WebUI displayed at the given URL.
  void ProcessWebUIMessage(const GURL& source_url,
                           const std::string& message,
                           const base::ListValue& args);
  // Invokes page load for WebUI URL with HTML. URL must have an application
  // specific scheme.
  virtual void LoadWebUIHtml(const base::string16& html, const GURL& url);

  // Gets the HTTP response headers associated with the current page.
  // NOTE: For a WKWebView-based WebState, these headers are generated via
  // net::CreateHeadersFromNSHTTPURLResponse(); see comments in
  // http_response_headers_util.h for limitations.
  net::HttpResponseHeaders* GetHttpResponseHeaders() const;

  // Called when HTTP response headers are received.
  // |resource_url| is the URL associated with the headers.
  // This function has no visible effects until UpdateHttpResponseHeaders() is
  // called.
  void OnHttpResponseHeadersReceived(net::HttpResponseHeaders* response_headers,
                                     const GURL& resource_url);

  // Explicitly sets the MIME type, overwriting any MIME type that was set by
  // headers. Note that this should be called after OnNavigationCommitted, as
  // that is the point where MIME type is set from HTTP headers.
  void SetContentsMimeType(const std::string& mime_type);

  // Executes a JavaScript string on the page asynchronously.
  // TODO(shreyasv): Rename this to ExecuteJavaScript for consistency with
  // upstream API.
  virtual void ExecuteJavaScriptAsync(const base::string16& script);

  // Returns whether the navigation corresponding to |request| should be allowed
  // to continue by asking its policy deciders. Defaults to true.
  bool ShouldAllowRequest(NSURLRequest* request);
  // Returns whether the navigation corresponding to |response| should be
  // allowed to continue by asking its policy deciders. Defaults to true.
  bool ShouldAllowResponse(NSURLResponse* response);

  // Request tracker management. For now, this exposes the RequestTracker for
  // embedders to use.
  // TODO(stuartmorgan): RequestTracker should become an internal detail of this
  // class.

  // Create a new tracker using |delegate| as its delegate.
  void InitializeRequestTracker(id<CRWRequestTrackerDelegate> delegate);

  // Close the request tracker and delete it.
  void CloseRequestTracker();

  // Returns the tracker for this WebStateImpl.
  RequestTrackerImpl* GetRequestTracker();

  // Lazily creates (if necessary) and returns |request_group_id_|.
  // IMPORTANT: This should not be used for anything other than associating this
  // instance to network requests.
  // This function is only intended to be used in web/.
  // TODO(stuartmorgan): Move this method in an implementation file in web/.
  NSString* GetRequestGroupID();

  // WebState:
  UIView* GetView() override;
  web::WebViewType GetWebViewType() const override;
  BrowserState* GetBrowserState() const override;
  void OpenURL(const WebState::OpenURLParams& params) override;
  NavigationManager* GetNavigationManager() override;
  CRWJSInjectionReceiver* GetJSInjectionReceiver() const override;
  const std::string& GetContentLanguageHeader() const override;
  const std::string& GetContentsMimeType() const override;
  bool ContentIsHTML() const override;
  const base::string16& GetTitle() const override;
  bool IsLoading() const override;
  bool IsBeingDestroyed() const override;
  const GURL& GetVisibleURL() const override;
  const GURL& GetLastCommittedURL() const override;
  GURL GetCurrentURL(URLVerificationTrustLevel* trust_level) const override;
  void ShowTransientContentView(CRWContentView* content_view) override;
  bool IsShowingWebInterstitial() const override;
  WebInterstitial* GetWebInterstitial() const override;
  void AddScriptCommandCallback(const ScriptCommandCallback& callback,
                                const std::string& command_prefix) override;
  void RemoveScriptCommandCallback(const std::string& command_prefix) override;
  id<CRWWebViewProxy> GetWebViewProxy() const override;
  int DownloadImage(const GURL& url,
                    bool is_favicon,
                    uint32_t max_bitmap_size,
                    bool bypass_cache,
                    const ImageDownloadCallback& callback) override;
  base::WeakPtr<WebState> AsWeakPtr() override;

  // Adds |interstitial|'s view to the web controller's content view.
  void ShowWebInterstitial(WebInterstitialImpl* interstitial);

  // Called to dismiss the currently-displayed transient content view.
  void ClearTransientContentView();

  // NavigationManagerDelegate:
  void NavigateToPendingEntry() override;
  void OnNavigationItemsPruned(size_t pruned_item_count) override;
  void OnNavigationItemChanged() override;
  void OnNavigationItemCommitted(
      const LoadCommittedDetails& load_details) override;
  WebState* GetWebState() override;

 protected:
  void AddObserver(WebStateObserver* observer) override;
  void RemoveObserver(WebStateObserver* observer) override;
  void AddPolicyDecider(WebStatePolicyDecider* decider) override;
  void RemovePolicyDecider(WebStatePolicyDecider* decider) override;

 private:
  // Creates a WebUIIOS object for |url| that is owned by the caller. Returns
  // nullptr if |url| does not correspond to a WebUI page.
  WebUIIOS* CreateWebUIIOS(const GURL& url);

  // Updates the HTTP response headers for the main page using the headers
  // passed to the OnHttpResponseHeadersReceived() function below.
  // GetHttpResponseHeaders() can be used to get the headers.
  void UpdateHttpResponseHeaders(const GURL& url);

  // Returns true if |web_controller_| has been set.
  bool Configured() const;

  // Stores whether the web state is currently loading a page.
  bool is_loading_;

  // Stores whether the web state is currently being destroyed.
  bool is_being_destroyed_;

  // The delegate used to pass state to the web contents facade.
  WebStateFacadeDelegate* facade_delegate_;

  // The CRWWebController that backs and owns this object.
  CRWWebController* web_controller_;

  NavigationManagerImpl navigation_manager_;

  // |web::WebUIIOS| object for the current page if it is a WebUI page that
  // uses the web-based WebUI framework, or nullptr otherwise.
  scoped_ptr<web::WebUIIOS> web_ui_;

  // A list of observers notified when page state changes. Weak references.
  base::ObserverList<WebStateObserver, true> observers_;

  // All the WebStatePolicyDeciders asked for navigation decision. Weak
  // references.
  // WebStatePolicyDeciders are semantically different from observers (they
  // modify the behavior of the WebState) but are used like observers in the
  // code, hence the ObserverList.
  base::ObserverList<WebStatePolicyDecider, true> policy_deciders_;

  // Map of all the HTTP response headers received, for each URL.
  // This map is cleared after each page load, and only the headers of the main
  // page are used.
  std::map<GURL, scoped_refptr<net::HttpResponseHeaders> >
      response_headers_map_;
  scoped_refptr<net::HttpResponseHeaders> http_response_headers_;
  std::string mime_type_;
  std::string content_language_header_;

  // Weak pointer to the interstitial page being displayed, if any.
  WebInterstitialImpl* interstitial_;

  // Returned by reference.
  base::string16 empty_string16_;

  // Request tracker associted with this object.
  scoped_refptr<RequestTrackerImpl> request_tracker_;

  // A number identifying this object. This number is injected into the user
  // agent to allow the network layer to know which web view requests originated
  // from.
  base::scoped_nsobject<NSString> request_group_id_;

  // Callbacks associated to command prefixes.
  std::map<std::string, ScriptCommandCallback> script_command_callbacks_;

  // Member variables should appear before the WeakPtrFactory<> to ensure that
  // any WeakPtrs to WebStateImpl are invalidated before its member variable's
  // destructors are executed, rendering them invalid.
  base::WeakPtrFactory<WebState> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(WebStateImpl);
};

}  // namespace web

#endif  // IOS_WEB_WEB_STATE_WEB_STATE_IMPL_H_