navigation_controller.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_
6#define CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/memory/ref_counted.h"
13#include "base/string16.h"
14#include "content/common/content_export.h"
15#include "content/public/browser/global_request_id.h"
16#include "content/public/common/page_transition_types.h"
17#include "content/public/common/referrer.h"
18#include "googleurl/src/gurl.h"
19
20namespace base {
21
22class RefCountedMemory;
23
24}  // namespace base
25
26namespace content {
27
28class BrowserContext;
29class NavigationEntry;
30class SessionStorageNamespace;
31class WebContents;
32
33// Used to store the mapping of a StoragePartition id to
34// SessionStorageNamespace.
35typedef std::map<std::string, scoped_refptr<SessionStorageNamespace> >
36    SessionStorageNamespaceMap;
37
38// A NavigationController maintains the back-forward list for a WebContents and
39// manages all navigation within that list.
40//
41// Each NavigationController belongs to one WebContents; each WebContents has
42// exactly one NavigationController.
43class NavigationController {
44 public:
45  enum ReloadType {
46    NO_RELOAD,                   // Normal load.
47    RELOAD,                      // Normal (cache-validating) reload.
48    RELOAD_IGNORING_CACHE,       // Reload bypassing the cache (shift-reload).
49    RELOAD_ORIGINAL_REQUEST_URL  // Reload using the original request URL.
50  };
51
52  // Load type used in LoadURLParams.
53  enum LoadURLType {
54    // For loads that do not fall into any types below.
55    LOAD_TYPE_DEFAULT,
56
57    // An http post load request initiated from browser side.
58    // The post data is passed in |browser_initiated_post_data|.
59    LOAD_TYPE_BROWSER_INITIATED_HTTP_POST,
60
61    // Loads a 'data:' scheme URL with specified base URL and a history entry
62    // URL. This is only safe to be used for browser-initiated data: URL
63    // navigations, since it shows arbitrary content as if it comes from
64    // |virtual_url_for_data_url|.
65    LOAD_TYPE_DATA
66
67    // Adding new LoadURLType? Also update LoadUrlParams.java static constants.
68  };
69
70  // User agent override type used in LoadURLParams.
71  enum UserAgentOverrideOption {
72    // Use the override value from the previous NavigationEntry in the
73    // NavigationController.
74    UA_OVERRIDE_INHERIT,
75
76    // Use the default user agent.
77    UA_OVERRIDE_FALSE,
78
79    // Use the user agent override, if it's available.
80    UA_OVERRIDE_TRUE
81
82    // Adding new UserAgentOverrideOption? Also update LoadUrlParams.java
83    // static constants.
84  };
85
86  enum RestoreType {
87    // Indicates the restore is from the current session. For example, restoring
88    // a closed tab.
89    RESTORE_CURRENT_SESSION,
90
91    // Restore from the previous session.
92    RESTORE_LAST_SESSION_EXITED_CLEANLY,
93    RESTORE_LAST_SESSION_CRASHED,
94  };
95
96  // Creates a navigation entry and translates the virtual url to a real one.
97  // This is a general call; prefer LoadURL[FromRenderer]/TransferURL below.
98  // Extra headers are separated by \n.
99  CONTENT_EXPORT static NavigationEntry* CreateNavigationEntry(
100      const GURL& url,
101      const Referrer& referrer,
102      PageTransition transition,
103      bool is_renderer_initiated,
104      const std::string& extra_headers,
105      BrowserContext* browser_context);
106
107  // Extra optional parameters for LoadURLWithParams.
108  struct CONTENT_EXPORT LoadURLParams {
109    // The url to load. This field is required.
110    GURL url;
111
112    // See LoadURLType comments above.
113    LoadURLType load_type;
114
115    // PageTransition for this load. See PageTransition for details.
116    // Note the default value in constructor below.
117    PageTransition transition_type;
118
119    // Referrer for this load. Empty if none.
120    Referrer referrer;
121
122    // Extra headers for this load, separated by \n.
123    std::string extra_headers;
124
125    // True for renderer-initiated navigations. This is
126    // important for tracking whether to display pending URLs.
127    bool is_renderer_initiated;
128
129    // User agent override for this load. See comments in
130    // UserAgentOverrideOption definition.
131    UserAgentOverrideOption override_user_agent;
132
133    // Marks the new navigation as being transferred from one RVH to another.
134    // In this case the browser can recycle the old request once the new
135    // renderer wants to navigate. Identifies the request ID of the old request.
136    GlobalRequestID transferred_global_request_id;
137
138    // Used in LOAD_TYPE_DATA loads only. Used for specifying a base URL
139    // for pages loaded via data URLs.
140    GURL base_url_for_data_url;
141
142    // Used in LOAD_TYPE_DATA loads only. URL displayed to the user for
143    // data loads.
144    GURL virtual_url_for_data_url;
145
146    // Used in LOAD_TYPE_BROWSER_INITIATED_HTTP_POST loads only. Carries the
147    // post data of the load. Ownership is transferred to NavigationController
148    // after LoadURLWithParams call.
149    scoped_refptr<base::RefCountedMemory> browser_initiated_post_data;
150
151    // True if this URL should be able to access local resources.
152    bool can_load_local_resources;
153
154    // Indicates whether this navigation involves a cross-process redirect,
155    // in which case it should replace the current navigation entry.
156    bool is_cross_site_redirect;
157
158    // Used to specify which frame to navigate. If empty, the main frame is
159    // navigated. This is currently only used in tests.
160    std::string frame_name;
161
162    explicit LoadURLParams(const GURL& url);
163    ~LoadURLParams();
164
165    // Allows copying of LoadURLParams struct.
166    LoadURLParams(const LoadURLParams& other);
167    LoadURLParams& operator=(const LoadURLParams& other);
168  };
169
170  // Disables checking for a repost and prompting the user. This is used during
171  // testing.
172  CONTENT_EXPORT static void DisablePromptOnRepost();
173
174  virtual ~NavigationController() {}
175
176  // Returns the web contents associated with this controller. It can never be
177  // NULL.
178  virtual WebContents* GetWebContents() const = 0;
179
180  // Get/set the browser context for this controller. It can never be NULL.
181  virtual BrowserContext* GetBrowserContext() const = 0;
182  virtual void SetBrowserContext(BrowserContext* browser_context) = 0;
183
184  // Initializes this NavigationController with the given saved navigations,
185  // using |selected_navigation| as the currently loaded entry. Before this call
186  // the controller should be unused (there should be no current entry). |type|
187  // indicates where the restor comes from. This takes ownership of the
188  // NavigationEntrys in |entries| and clears it out.  This is used for session
189  // restore.
190  virtual void Restore(int selected_navigation,
191                       RestoreType type,
192                       std::vector<NavigationEntry*>* entries) = 0;
193
194  // Entries -------------------------------------------------------------------
195
196  // There are two basic states for entries: pending and committed. When an
197  // entry is navigated to, a request is sent to the server. While that request
198  // has not been responded to, the NavigationEntry is pending. Once data is
199  // received for that entry, that NavigationEntry is committed.
200
201  // A transient entry is an entry that, when the user navigates away, is
202  // removed and discarded rather than being added to the back-forward list.
203  // Transient entries are useful for interstitial pages and the like.
204
205  // Active entry --------------------------------------------------------------
206
207  // Returns the active entry, which is the transient entry if any, the pending
208  // entry if a navigation is in progress or the last committed entry otherwise.
209  // NOTE: This can be NULL!!
210  //
211  // If you are trying to get the current state of the NavigationController,
212  // this is the method you will typically want to call.  If you want to display
213  // the active entry to the user (e.g., in the location bar), use
214  // GetVisibleEntry instead.
215  virtual NavigationEntry* GetActiveEntry() const = 0;
216
217  // Returns the same entry as GetActiveEntry, except that it ignores pending
218  // history navigation entries.  This should be used when displaying info to
219  // the user, so that the location bar and other indicators do not update for
220  // a back/forward navigation until the pending entry commits.  This approach
221  // guards against URL spoofs on slow history navigations.
222  virtual NavigationEntry* GetVisibleEntry() const = 0;
223
224  // Returns the index from which we would go back/forward or reload.  This is
225  // the last_committed_entry_index_ if pending_entry_index_ is -1.  Otherwise,
226  // it is the pending_entry_index_.
227  virtual int GetCurrentEntryIndex() const = 0;
228
229  // Returns the last committed entry, which may be null if there are no
230  // committed entries.
231  virtual NavigationEntry* GetLastCommittedEntry() const = 0;
232
233  // Returns the index of the last committed entry.
234  virtual int GetLastCommittedEntryIndex() const = 0;
235
236  // Returns true if the source for the current entry can be viewed.
237  virtual bool CanViewSource() const = 0;
238
239  // Navigation list -----------------------------------------------------------
240
241  // Returns the number of entries in the NavigationController, excluding
242  // the pending entry if there is one, but including the transient entry if
243  // any.
244  virtual int GetEntryCount() const = 0;
245
246  virtual NavigationEntry* GetEntryAtIndex(int index) const = 0;
247
248  // Returns the entry at the specified offset from current.  Returns NULL
249  // if out of bounds.
250  virtual NavigationEntry* GetEntryAtOffset(int offset) const = 0;
251
252  // Pending entry -------------------------------------------------------------
253
254  // Discards the pending and transient entries if any.
255  virtual void DiscardNonCommittedEntries() = 0;
256
257  // Returns the pending entry corresponding to the navigation that is
258  // currently in progress, or null if there is none.
259  virtual NavigationEntry* GetPendingEntry() const = 0;
260
261  // Returns the index of the pending entry or -1 if the pending entry
262  // corresponds to a new navigation (created via LoadURL).
263  virtual int GetPendingEntryIndex() const = 0;
264
265  // Transient entry -----------------------------------------------------------
266
267  // Returns the transient entry if any. This is an entry which is removed and
268  // discarded if any navigation occurs. Note that the returned entry is owned
269  // by the navigation controller and may be deleted at any time.
270  virtual NavigationEntry* GetTransientEntry() const = 0;
271
272  // Adds an entry that is returned by GetActiveEntry(). The entry is
273  // transient: any navigation causes it to be removed and discarded.  The
274  // NavigationController becomes the owner of |entry| and deletes it when
275  // it discards it. This is useful with interstitial pages that need to be
276  // represented as an entry, but should go away when the user navigates away
277  // from them.
278  // Note that adding a transient entry does not change the active contents.
279  virtual void SetTransientEntry(NavigationEntry* entry) = 0;
280
281  // New navigations -----------------------------------------------------------
282
283  // Loads the specified URL, specifying extra http headers to add to the
284  // request.  Extra headers are separated by \n.
285  virtual void LoadURL(const GURL& url,
286                       const Referrer& referrer,
287                       PageTransition type,
288                       const std::string& extra_headers) = 0;
289
290  // More general version of LoadURL. See comments in LoadURLParams for
291  // using |params|.
292  virtual void LoadURLWithParams(const LoadURLParams& params) = 0;
293
294  // Loads the current page if this NavigationController was restored from
295  // history and the current page has not loaded yet.
296  virtual void LoadIfNecessary() = 0;
297
298  // Renavigation --------------------------------------------------------------
299
300  // Navigation relative to the "current entry"
301  virtual bool CanGoBack() const = 0;
302  virtual bool CanGoForward() const = 0;
303  virtual bool CanGoToOffset(int offset) const = 0;
304  virtual void GoBack() = 0;
305  virtual void GoForward() = 0;
306
307  // Navigates to the specified absolute index.
308  virtual void GoToIndex(int index) = 0;
309
310  // Navigates to the specified offset from the "current entry". Does nothing if
311  // the offset is out of bounds.
312  virtual void GoToOffset(int offset) = 0;
313
314  // Reloads the current entry. If |check_for_repost| is true and the current
315  // entry has POST data the user is prompted to see if they really want to
316  // reload the page. In nearly all cases pass in true.  If a transient entry
317  // is showing, initiates a new navigation to its URL.
318  virtual void Reload(bool check_for_repost) = 0;
319
320  // Like Reload(), but don't use caches (aka "shift-reload").
321  virtual void ReloadIgnoringCache(bool check_for_repost) = 0;
322
323  // Reloads the current entry using the original URL used to create it.  This
324  // is used for cases where the user wants to refresh a page using a different
325  // user agent after following a redirect.
326  virtual void ReloadOriginalRequestURL(bool check_for_repost) = 0;
327
328  // Removing of entries -------------------------------------------------------
329
330  // Removes the entry at the specified |index|.  This call dicards any pending
331  // and transient entries.  If the index is the last committed index, this does
332  // nothing and returns false.
333  virtual void RemoveEntryAtIndex(int index) = 0;
334
335  // Random --------------------------------------------------------------------
336
337  // Session storage depends on dom_storage that depends on WebKit::WebString,
338  // which cannot be used on iOS.
339#if !defined(OS_IOS)
340  // Returns all the SessionStorageNamespace objects that this
341  // NavigationController knows about.
342  virtual const SessionStorageNamespaceMap&
343      GetSessionStorageNamespaceMap() const = 0;
344
345  // TODO(ajwong): Remove this once prerendering, instant, and session restore
346  // are migrated.
347  virtual SessionStorageNamespace* GetDefaultSessionStorageNamespace() = 0;
348#endif
349
350  // Sets the max restored page ID this NavigationController has seen, if it
351  // was restored from a previous session.
352  virtual void SetMaxRestoredPageID(int32 max_id) = 0;
353
354  // Returns the largest restored page ID seen in this navigation controller,
355  // if it was restored from a previous session.  (-1 otherwise)
356  virtual int32 GetMaxRestoredPageID() const = 0;
357
358  // Returns true if a reload happens when activated (SetActive(true) is
359  // invoked). This is true for session/tab restore and cloned tabs.
360  virtual bool NeedsReload() const = 0;
361
362  // Cancels a repost that brought up a warning.
363  virtual void CancelPendingReload() = 0;
364  // Continues a repost that brought up a warning.
365  virtual void ContinuePendingReload() = 0;
366
367  // Returns true if we are navigating to the URL the tab is opened with.
368  // Returns false after the initial navigation has committed.
369  virtual bool IsInitialNavigation() const = 0;
370
371  // Broadcasts the NOTIFY_NAV_ENTRY_CHANGED notification for the given entry
372  // (which must be at the given index). This will keep things in sync like
373  // the saved session.
374  virtual void NotifyEntryChanged(const NavigationEntry* entry, int index) = 0;
375
376  // Copies the navigation state from the given controller to this one. This
377  // one should be empty (just created).
378  virtual void CopyStateFrom(const NavigationController& source) = 0;
379
380  // A variant of CopyStateFrom. Removes all entries from this except the last
381  // entry, inserts all entries from |source| before and including the active
382  // entry. This method is intended for use when the last entry of |this| is the
383  // active entry. For example:
384  // source: A B *C* D
385  // this:   E F *G*   (last must be active or pending)
386  // result: A B C *G*
387  // This ignores the transient index of the source and honors that of 'this'.
388  virtual void CopyStateFromAndPrune(NavigationController* source) = 0;
389
390  // Removes all the entries except the active entry. If there is a new pending
391  // navigation it is preserved.
392  virtual void PruneAllButActive() = 0;
393
394  // Clears all screenshots associated with navigation entries in this
395  // controller. Useful to reduce memory consumption in low-memory situations.
396  virtual void ClearAllScreenshots() = 0;
397
398 private:
399  // This interface should only be implemented inside content.
400  friend class NavigationControllerImpl;
401  NavigationController() {}
402};
403
404}  // namespace content
405
406#endif  // CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_
407