navigation_entry_impl.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_IMPL_H_
6#define CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_IMPL_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "content/browser/site_instance_impl.h"
11#include "content/public/browser/favicon_status.h"
12#include "content/public/browser/global_request_id.h"
13#include "content/public/browser/navigation_entry.h"
14#include "content/public/common/page_state.h"
15#include "content/public/common/ssl_status.h"
16
17namespace content {
18
19class CONTENT_EXPORT NavigationEntryImpl
20    : public NON_EXPORTED_BASE(NavigationEntry) {
21 public:
22  static NavigationEntryImpl* FromNavigationEntry(NavigationEntry* entry);
23
24  // The value of bindings() before it is set during commit.
25  static int kInvalidBindings;
26
27  NavigationEntryImpl();
28  NavigationEntryImpl(SiteInstanceImpl* instance,
29                      int page_id,
30                      const GURL& url,
31                      const Referrer& referrer,
32                      const base::string16& title,
33                      PageTransition transition_type,
34                      bool is_renderer_initiated);
35  virtual ~NavigationEntryImpl();
36
37  // NavigationEntry implementation:
38  virtual int GetUniqueID() const OVERRIDE;
39  virtual PageType GetPageType() const OVERRIDE;
40  virtual void SetURL(const GURL& url) OVERRIDE;
41  virtual const GURL& GetURL() const OVERRIDE;
42  virtual void SetBaseURLForDataURL(const GURL& url) OVERRIDE;
43  virtual const GURL& GetBaseURLForDataURL() const OVERRIDE;
44  virtual void SetReferrer(const Referrer& referrer) OVERRIDE;
45  virtual const Referrer& GetReferrer() const OVERRIDE;
46  virtual void SetVirtualURL(const GURL& url) OVERRIDE;
47  virtual const GURL& GetVirtualURL() const OVERRIDE;
48  virtual void SetTitle(const base::string16& title) OVERRIDE;
49  virtual const base::string16& GetTitle() const OVERRIDE;
50  virtual void SetPageState(const PageState& state) OVERRIDE;
51  virtual const PageState& GetPageState() const OVERRIDE;
52  virtual void SetPageID(int page_id) OVERRIDE;
53  virtual int32 GetPageID() const OVERRIDE;
54  virtual const base::string16& GetTitleForDisplay(
55      const std::string& languages) const OVERRIDE;
56  virtual bool IsViewSourceMode() const OVERRIDE;
57  virtual void SetTransitionType(PageTransition transition_type) OVERRIDE;
58  virtual PageTransition GetTransitionType() const OVERRIDE;
59  virtual const GURL& GetUserTypedURL() const OVERRIDE;
60  virtual void SetHasPostData(bool has_post_data) OVERRIDE;
61  virtual bool GetHasPostData() const OVERRIDE;
62  virtual void SetPostID(int64 post_id) OVERRIDE;
63  virtual int64 GetPostID() const OVERRIDE;
64  virtual void SetBrowserInitiatedPostData(
65      const base::RefCountedMemory* data) OVERRIDE;
66  virtual const base::RefCountedMemory*
67      GetBrowserInitiatedPostData() const OVERRIDE;
68  virtual const FaviconStatus& GetFavicon() const OVERRIDE;
69  virtual FaviconStatus& GetFavicon() OVERRIDE;
70  virtual const SSLStatus& GetSSL() const OVERRIDE;
71  virtual SSLStatus& GetSSL() OVERRIDE;
72  virtual void SetOriginalRequestURL(const GURL& original_url) OVERRIDE;
73  virtual const GURL& GetOriginalRequestURL() const OVERRIDE;
74  virtual void SetIsOverridingUserAgent(bool override) OVERRIDE;
75  virtual bool GetIsOverridingUserAgent() const OVERRIDE;
76  virtual void SetTimestamp(base::Time timestamp) OVERRIDE;
77  virtual base::Time GetTimestamp() const OVERRIDE;
78  virtual void SetCanLoadLocalResources(bool allow) OVERRIDE;
79  virtual bool GetCanLoadLocalResources() const OVERRIDE;
80  virtual void SetFrameToNavigate(const std::string& frame_name) OVERRIDE;
81  virtual const std::string& GetFrameToNavigate() const OVERRIDE;
82  virtual void SetExtraData(const std::string& key,
83                            const base::string16& data) OVERRIDE;
84  virtual bool GetExtraData(const std::string& key,
85                            base::string16* data) const OVERRIDE;
86  virtual void ClearExtraData(const std::string& key) OVERRIDE;
87  virtual void SetHttpStatusCode(int http_status_code) OVERRIDE;
88  virtual int GetHttpStatusCode() const OVERRIDE;
89  virtual void SetRedirectChain(const std::vector<GURL>& redirects) OVERRIDE;
90  virtual const std::vector<GURL>& GetRedirectChain() const OVERRIDE;
91  virtual bool IsRestored() const OVERRIDE;
92  virtual bool GetXssDetected() const OVERRIDE;
93
94  // Once a navigation entry is committed, we should no longer track several
95  // pieces of non-persisted state, as documented on the members below.
96  void ResetForCommit();
97
98  void set_unique_id(int unique_id) {
99    unique_id_ = unique_id;
100  }
101
102  // The SiteInstance tells us how to share sub-processes. This is a reference
103  // counted pointer to a shared site instance.
104  //
105  // Note that the SiteInstance should usually not be changed after it is set,
106  // but this may happen if the NavigationEntry was cloned and needs to use a
107  // different SiteInstance.
108  void set_site_instance(SiteInstanceImpl* site_instance);
109  SiteInstanceImpl* site_instance() const {
110    return site_instance_.get();
111  }
112
113  // Remember the set of bindings granted to this NavigationEntry at the time
114  // of commit, to ensure that we do not grant it additional bindings if we
115  // navigate back to it in the future.  This can only be changed once.
116  void SetBindings(int bindings);
117  int bindings() const {
118    return bindings_;
119  }
120
121  void set_page_type(PageType page_type) {
122    page_type_ = page_type;
123  }
124
125  bool has_virtual_url() const {
126    return !virtual_url_.is_empty();
127  }
128
129  bool update_virtual_url_with_url() const {
130    return update_virtual_url_with_url_;
131  }
132  void set_update_virtual_url_with_url(bool update) {
133    update_virtual_url_with_url_ = update;
134  }
135
136  // Extra headers (separated by \n) to send during the request.
137  void set_extra_headers(const std::string& extra_headers) {
138    extra_headers_ = extra_headers;
139  }
140  const std::string& extra_headers() const {
141    return extra_headers_;
142  }
143
144  // Whether this (pending) navigation is renderer-initiated.  Resets to false
145  // for all types of navigations after commit.
146  void set_is_renderer_initiated(bool is_renderer_initiated) {
147    is_renderer_initiated_ = is_renderer_initiated;
148  }
149  bool is_renderer_initiated() const {
150    return is_renderer_initiated_;
151  }
152
153  void set_user_typed_url(const GURL& user_typed_url) {
154    user_typed_url_ = user_typed_url;
155  }
156
157  // Enumerations of the possible restore types.
158  enum RestoreType {
159    // Restore from the previous session.
160    RESTORE_LAST_SESSION_EXITED_CLEANLY,
161    RESTORE_LAST_SESSION_CRASHED,
162
163    // The entry has been restored from the current session. This is used when
164    // the user issues 'reopen closed tab'.
165    RESTORE_CURRENT_SESSION,
166
167    // The entry was not restored.
168    RESTORE_NONE
169  };
170
171  // The RestoreType for this entry. This is set if the entry was retored. This
172  // is set to RESTORE_NONE once the entry is loaded.
173  void set_restore_type(RestoreType type) {
174    restore_type_ = type;
175  }
176  RestoreType restore_type() const {
177    return restore_type_;
178  }
179
180  void set_transferred_global_request_id(
181      const GlobalRequestID& transferred_global_request_id) {
182    transferred_global_request_id_ = transferred_global_request_id;
183  }
184
185  GlobalRequestID transferred_global_request_id() const {
186    return transferred_global_request_id_;
187  }
188
189  // Whether this (pending) navigation needs to replace current entry.
190  // Resets to false after commit.
191  bool should_replace_entry() const {
192    return should_replace_entry_;
193  }
194
195  void set_should_replace_entry(bool should_replace_entry) {
196    should_replace_entry_ = should_replace_entry;
197  }
198
199  void SetScreenshotPNGData(scoped_refptr<base::RefCountedBytes> png_data);
200  const scoped_refptr<base::RefCountedBytes> screenshot() const {
201    return screenshot_;
202  }
203
204  // Whether this (pending) navigation should clear the session history. Resets
205  // to false after commit.
206  bool should_clear_history_list() const {
207    return should_clear_history_list_;
208  }
209  void set_should_clear_history_list(bool should_clear_history_list) {
210    should_clear_history_list_ = should_clear_history_list;
211  }
212
213  // Indicates which FrameTreeNode to navigate.  Currently only used if the
214  // --site-per-process flag is passed.
215  int64 frame_tree_node_id() const {
216    return frame_tree_node_id_;
217  }
218  void set_frame_tree_node_id(int64 frame_tree_node_id) {
219    frame_tree_node_id_ = frame_tree_node_id;
220  }
221
222  // Called when an XSS detected by Blink's XSSAuditor caused the page to
223  // be blocked.
224  void set_xss_detected(bool xss_detected) {
225    xss_detected_ = xss_detected;
226  }
227
228 private:
229  // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
230  // Session/Tab restore save portions of this class so that it can be recreated
231  // later. If you add a new field that needs to be persisted you'll have to
232  // update SessionService/TabRestoreService and Android WebView
233  // state_serializer.cc appropriately.
234  // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
235
236  // See the accessors above for descriptions.
237  int unique_id_;
238  scoped_refptr<SiteInstanceImpl> site_instance_;
239  // TODO(creis): Persist bindings_. http://crbug.com/173672.
240  int bindings_;
241  PageType page_type_;
242  GURL url_;
243  Referrer referrer_;
244  GURL virtual_url_;
245  bool update_virtual_url_with_url_;
246  base::string16 title_;
247  FaviconStatus favicon_;
248  PageState page_state_;
249  int32 page_id_;
250  SSLStatus ssl_;
251  PageTransition transition_type_;
252  GURL user_typed_url_;
253  bool has_post_data_;
254  int64 post_id_;
255  RestoreType restore_type_;
256  GURL original_request_url_;
257  bool is_overriding_user_agent_;
258  base::Time timestamp_;
259  int http_status_code_;
260
261  // This member is not persisted with session restore because it is transient.
262  // If the post request succeeds, this field is cleared since the same
263  // information is stored in |content_state_| above. It is also only shallow
264  // copied with compiler provided copy constructor.
265  // Cleared in |ResetForCommit|.
266  scoped_refptr<const base::RefCountedMemory> browser_initiated_post_data_;
267
268  // This is also a transient member (i.e. is not persisted with session
269  // restore). The screenshot of a page is taken when navigating away from the
270  // page. This screenshot is displayed during an overscroll-navigation
271  // gesture. |screenshot_| will be NULL when the screenshot is not available
272  // (e.g. after a session restore, or if taking the screenshot of a page
273  // failed). The UI is responsible for dealing with missing screenshots
274  // appropriately (e.g. display a placeholder image instead).
275  scoped_refptr<base::RefCountedBytes> screenshot_;
276
277  // This member is not persisted with session restore.
278  std::string extra_headers_;
279
280  // Used for specifying base URL for pages loaded via data URLs. Only used and
281  // persisted by Android WebView.
282  GURL base_url_for_data_url_;
283
284  // Whether the entry, while loading, was created for a renderer-initiated
285  // navigation.  This dictates whether the URL should be displayed before the
286  // navigation commits.  It is cleared in |ResetForCommit| and not persisted.
287  bool is_renderer_initiated_;
288
289  // This is a cached version of the result of GetTitleForDisplay. It prevents
290  // us from having to do URL formatting on the URL every time the title is
291  // displayed. When the URL, virtual URL, or title is set, this should be
292  // cleared to force a refresh.
293  mutable base::string16 cached_display_title_;
294
295  // In case a navigation is transferred to a new RVH but the request has
296  // been generated in the renderer already, this identifies the old request so
297  // that it can be resumed. The old request is stored until the
298  // ResourceDispatcher receives the navigation from the renderer which
299  // carries this |transferred_global_request_id_| annotation. Once the request
300  // is transferred to the new process, this is cleared and the request
301  // continues as normal.
302  // Cleared in |ResetForCommit|.
303  GlobalRequestID transferred_global_request_id_;
304
305  // This is set to true when this entry is being reloaded and due to changes in
306  // the state of the URL, it has to be reloaded in a different site instance.
307  // In such case, we must treat it as an existing navigation in the new site
308  // instance, instead of a new navigation. This value should not be persisted
309  // and is cleared in |ResetForCommit|.
310  //
311  // We also use this flag for cross-process redirect navigations, so that the
312  // browser will replace the current navigation entry (which is the page
313  // doing the redirect).
314  bool should_replace_entry_;
315
316  // This is used when transferring a pending entry from one process to another.
317  // We also send this data through session sync for offline analysis.
318  // It is preserved after commit but should not be persisted.
319  std::vector<GURL> redirect_chain_;
320
321  // This is set to true when this entry's navigation should clear the session
322  // history both on the renderer and browser side. The browser side history
323  // won't be cleared until the renderer has committed this navigation. This
324  // entry is not persisted by the session restore system, as it is always
325  // cleared in |ResetForCommit|.
326  bool should_clear_history_list_;
327
328  // Set when this entry should be able to access local file:// resources. This
329  // value is not needed after the entry commits and is not persisted.
330  bool can_load_local_resources_;
331
332  // Set when this entry was blocked by Blink's XSSAuditor.
333  // TODO(tsepez): persist xss_detected_ (see WARNING section above).
334  bool xss_detected_;
335
336  // If not empty, the name of the frame to navigate. This field is not
337  // persisted, because it is currently only used in tests.
338  std::string frame_to_navigate_;
339
340  // If not -1, this indicates which FrameTreeNode to navigate.  This field is
341  // not persisted because it is experimental and only used when the
342  // --site-per-process flag is passed.  It is cleared in |ResetForCommit|
343  // because we only use it while the navigation is pending.
344  // TODO(creis): Move this to FrameNavigationEntry.
345  int64 frame_tree_node_id_;
346
347  // Used to store extra data to support browser features. This member is not
348  // persisted, unless specific data is taken out/put back in at save/restore
349  // time (see TabNavigation for an example of this).
350  std::map<std::string, base::string16> extra_data_;
351
352  // Copy and assignment is explicitly allowed for this class.
353};
354
355}  // namespace content
356
357#endif  // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_ENTRY_IMPL_H_
358