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.
5 #ifndef CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_
6 #define CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/strings/string16.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/global_request_id.h"
17 #include "content/public/browser/session_storage_namespace.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/common/referrer.h"
20 #include "ui/base/page_transition_types.h"
25 class RefCountedMemory
;
32 class NavigationEntry
;
35 // A NavigationController maintains the back-forward list for a WebContents and
36 // manages all navigation within that list.
38 // Each NavigationController belongs to one WebContents; each WebContents has
39 // exactly one NavigationController.
40 class NavigationController
{
43 NO_RELOAD
, // Normal load.
44 RELOAD
, // Normal (cache-validating) reload.
45 RELOAD_IGNORING_CACHE
, // Reload bypassing the cache (shift-reload).
46 RELOAD_ORIGINAL_REQUEST_URL
// Reload using the original request URL.
49 // Load type used in LoadURLParams.
51 // A Java counterpart will be generated for this enum.
52 // GENERATED_JAVA_ENUM_PACKAGE: (
53 // org.chromium.content_public.browser.navigation_controller)
54 // GENERATED_JAVA_PREFIX_TO_STRIP: LOAD_TYPE_
56 // For loads that do not fall into any types below.
59 // An http post load request initiated from browser side.
60 // The post data is passed in |browser_initiated_post_data|.
61 LOAD_TYPE_BROWSER_INITIATED_HTTP_POST
,
63 // Loads a 'data:' scheme URL with specified base URL and a history entry
64 // URL. This is only safe to be used for browser-initiated data: URL
65 // navigations, since it shows arbitrary content as if it comes from
66 // |virtual_url_for_data_url|.
69 // Adding new LoadURLType? Also update LoadUrlParams.java static constants.
72 // User agent override type used in LoadURLParams.
74 // A Java counterpart will be generated for this enum.
75 // GENERATED_JAVA_ENUM_PACKAGE: (
76 // org.chromium.content_public.browser.navigation_controller)
77 // GENERATED_JAVA_PREFIX_TO_STRIP: UA_OVERRIDE_
78 enum UserAgentOverrideOption
{
79 // Use the override value from the previous NavigationEntry in the
80 // NavigationController.
83 // Use the default user agent.
86 // Use the user agent override, if it's available.
89 // Adding new UserAgentOverrideOption? Also update LoadUrlParams.java
94 // Indicates the restore is from the current session. For example, restoring
96 RESTORE_CURRENT_SESSION
,
98 // Restore from the previous session.
99 RESTORE_LAST_SESSION_EXITED_CLEANLY
,
100 RESTORE_LAST_SESSION_CRASHED
,
103 // Creates a navigation entry and translates the virtual url to a real one.
104 // This is a general call; prefer LoadURL[FromRenderer]/TransferURL below.
105 // Extra headers are separated by \n.
106 CONTENT_EXPORT
static scoped_ptr
<NavigationEntry
> CreateNavigationEntry(
108 const Referrer
& referrer
,
109 ui::PageTransition transition
,
110 bool is_renderer_initiated
,
111 const std::string
& extra_headers
,
112 BrowserContext
* browser_context
);
114 // Extra optional parameters for LoadURLWithParams.
115 struct CONTENT_EXPORT LoadURLParams
{
116 // The url to load. This field is required.
119 // SiteInstance of the frame that initiated the navigation or null if we
121 scoped_refptr
<SiteInstance
> source_site_instance
;
123 // See LoadURLType comments above.
124 LoadURLType load_type
;
126 // PageTransition for this load. See PageTransition for details.
127 // Note the default value in constructor below.
128 ui::PageTransition transition_type
;
130 // The FrameTreeNode ID for the frame to navigate, or -1 for the main frame.
131 int frame_tree_node_id
;
133 // Referrer for this load. Empty if none.
136 // Any redirect URLs that occurred for this navigation before |url|.
137 // Defaults to an empty vector.
138 std::vector
<GURL
> redirect_chain
;
140 // Extra headers for this load, separated by \n.
141 std::string extra_headers
;
143 // True for renderer-initiated navigations. This is
144 // important for tracking whether to display pending URLs.
145 bool is_renderer_initiated
;
147 // User agent override for this load. See comments in
148 // UserAgentOverrideOption definition.
149 UserAgentOverrideOption override_user_agent
;
151 // Marks the new navigation as being transferred from one RVH to another.
152 // In this case the browser can recycle the old request once the new
153 // renderer wants to navigate. Identifies the request ID of the old request.
154 GlobalRequestID transferred_global_request_id
;
156 // Used in LOAD_TYPE_DATA loads only. Used for specifying a base URL
157 // for pages loaded via data URLs.
158 GURL base_url_for_data_url
;
160 // Used in LOAD_TYPE_DATA loads only. URL displayed to the user for
162 GURL virtual_url_for_data_url
;
164 // Used in LOAD_TYPE_BROWSER_INITIATED_HTTP_POST loads only. Carries the
165 // post data of the load. Ownership is transferred to NavigationController
166 // after LoadURLWithParams call.
167 scoped_refptr
<base::RefCountedMemory
> browser_initiated_post_data
;
169 // True if this URL should be able to access local resources.
170 bool can_load_local_resources
;
172 // Indicates whether this navigation should replace the current
174 bool should_replace_current_entry
;
176 // Used to specify which frame to navigate. If empty, the main frame is
177 // navigated. This is currently only used in tests.
178 std::string frame_name
;
180 #if defined(OS_ANDROID)
181 // On Android, for a load triggered by an intent, the time Chrome received
182 // the original intent that prompted the load (in milliseconds active time
184 int64 intent_received_timestamp
;
186 // When Chrome launches the intent chooser, user can select Chrome itself to
187 // open the intent. In this case, we should carry over the user gesture.
188 bool has_user_gesture
;
191 // Indicates that during this navigation, the session history should be
192 // cleared such that the resulting page is the first and only entry of the
195 // The clearing is done asynchronously, and completes when this navigation
197 bool should_clear_history_list
;
199 explicit LoadURLParams(const GURL
& url
);
202 // Allows copying of LoadURLParams struct.
203 LoadURLParams(const LoadURLParams
& other
);
204 LoadURLParams
& operator=(const LoadURLParams
& other
);
207 // Disables checking for a repost and prompting the user. This is used during
209 CONTENT_EXPORT
static void DisablePromptOnRepost();
211 virtual ~NavigationController() {}
213 // Returns the web contents associated with this controller. It can never be
215 virtual WebContents
* GetWebContents() const = 0;
217 // Get/set the browser context for this controller. It can never be nullptr.
218 virtual BrowserContext
* GetBrowserContext() const = 0;
219 virtual void SetBrowserContext(BrowserContext
* browser_context
) = 0;
221 // Initializes this NavigationController with the given saved navigations,
222 // using |selected_navigation| as the currently loaded entry. Before this call
223 // the controller should be unused (there should be no current entry). |type|
224 // indicates where the restor comes from. This takes ownership of the
225 // NavigationEntrys in |entries| and clears it out. This is used for session
227 virtual void Restore(int selected_navigation
,
229 ScopedVector
<NavigationEntry
>* entries
) = 0;
231 // Entries -------------------------------------------------------------------
233 // There are two basic states for entries: pending and committed. When an
234 // entry is navigated to, a request is sent to the server. While that request
235 // has not been responded to, the NavigationEntry is pending. Once data is
236 // received for that entry, that NavigationEntry is committed.
238 // A transient entry is an entry that, when the user navigates away, is
239 // removed and discarded rather than being added to the back-forward list.
240 // Transient entries are useful for interstitial pages and the like.
242 // Active entry --------------------------------------------------------------
244 // THIS IS DEPRECATED. DO NOT USE. Use GetVisibleEntry instead.
245 // See http://crbug.com/273710.
247 // Returns the active entry, which is the transient entry if any, the pending
248 // entry if a navigation is in progress or the last committed entry otherwise.
249 // NOTE: This can be nullptr!!
250 virtual NavigationEntry
* GetActiveEntry() const = 0;
252 // Returns the entry that should be displayed to the user in the address bar.
253 // This is the transient entry if any, the pending entry if a navigation is
254 // in progress *and* is safe to display to the user (see below), or the last
255 // committed entry otherwise.
256 // NOTE: This can be nullptr if no entry has committed!
258 // A pending entry is safe to display if it started in the browser process or
259 // if it's a renderer-initiated navigation in a new tab which hasn't been
260 // accessed by another tab. (If it has been accessed, it risks a URL spoof.)
261 virtual NavigationEntry
* GetVisibleEntry() const = 0;
263 // Returns the index from which we would go back/forward or reload. This is
264 // the last_committed_entry_index_ if pending_entry_index_ is -1. Otherwise,
265 // it is the pending_entry_index_.
266 virtual int GetCurrentEntryIndex() const = 0;
268 // Returns the last committed entry, which may be null if there are no
269 // committed entries.
270 virtual NavigationEntry
* GetLastCommittedEntry() const = 0;
272 // Returns the index of the last committed entry.
273 virtual int GetLastCommittedEntryIndex() const = 0;
275 // Returns true if the source for the current entry can be viewed.
276 virtual bool CanViewSource() const = 0;
278 // Navigation list -----------------------------------------------------------
280 // Returns the number of entries in the NavigationController, excluding
281 // the pending entry if there is one, but including the transient entry if
283 virtual int GetEntryCount() const = 0;
285 virtual NavigationEntry
* GetEntryAtIndex(int index
) const = 0;
287 // Returns the entry at the specified offset from current. Returns nullptr
289 virtual NavigationEntry
* GetEntryAtOffset(int offset
) const = 0;
291 // Pending entry -------------------------------------------------------------
293 // Discards the pending and transient entries if any.
294 virtual void DiscardNonCommittedEntries() = 0;
296 // Returns the pending entry corresponding to the navigation that is
297 // currently in progress, or null if there is none.
298 virtual NavigationEntry
* GetPendingEntry() const = 0;
300 // Returns the index of the pending entry or -1 if the pending entry
301 // corresponds to a new navigation (created via LoadURL).
302 virtual int GetPendingEntryIndex() const = 0;
304 // Transient entry -----------------------------------------------------------
306 // Returns the transient entry if any. This is an entry which is removed and
307 // discarded if any navigation occurs. Note that the returned entry is owned
308 // by the navigation controller and may be deleted at any time.
309 virtual NavigationEntry
* GetTransientEntry() const = 0;
311 // Adds an entry that is returned by GetActiveEntry(). The entry is
312 // transient: any navigation causes it to be removed and discarded. The
313 // NavigationController becomes the owner of |entry| and deletes it when
314 // it discards it. This is useful with interstitial pages that need to be
315 // represented as an entry, but should go away when the user navigates away
317 // Note that adding a transient entry does not change the active contents.
318 virtual void SetTransientEntry(scoped_ptr
<NavigationEntry
> entry
) = 0;
320 // New navigations -----------------------------------------------------------
322 // Loads the specified URL, specifying extra http headers to add to the
323 // request. Extra headers are separated by \n.
324 virtual void LoadURL(const GURL
& url
,
325 const Referrer
& referrer
,
326 ui::PageTransition type
,
327 const std::string
& extra_headers
) = 0;
329 // More general version of LoadURL. See comments in LoadURLParams for
331 virtual void LoadURLWithParams(const LoadURLParams
& params
) = 0;
333 // Loads the current page if this NavigationController was restored from
334 // history and the current page has not loaded yet or if the load was
335 // explicitly requested using SetNeedsReload().
336 virtual void LoadIfNecessary() = 0;
338 // Renavigation --------------------------------------------------------------
340 // Navigation relative to the "current entry"
341 virtual bool CanGoBack() const = 0;
342 virtual bool CanGoForward() const = 0;
343 virtual bool CanGoToOffset(int offset
) const = 0;
344 virtual void GoBack() = 0;
345 virtual void GoForward() = 0;
347 // Navigates to the specified absolute index.
348 virtual void GoToIndex(int index
) = 0;
350 // Navigates to the specified offset from the "current entry". Does nothing if
351 // the offset is out of bounds.
352 virtual void GoToOffset(int offset
) = 0;
354 // Reloads the current entry. If |check_for_repost| is true and the current
355 // entry has POST data the user is prompted to see if they really want to
356 // reload the page. In nearly all cases pass in true. If a transient entry
357 // is showing, initiates a new navigation to its URL.
358 virtual void Reload(bool check_for_repost
) = 0;
360 // Like Reload(), but don't use caches (aka "shift-reload").
361 virtual void ReloadIgnoringCache(bool check_for_repost
) = 0;
363 // Reloads the current entry using the original URL used to create it. This
364 // is used for cases where the user wants to refresh a page using a different
365 // user agent after following a redirect.
366 virtual void ReloadOriginalRequestURL(bool check_for_repost
) = 0;
368 // Removing of entries -------------------------------------------------------
370 // Removes the entry at the specified |index|. If the index is the last
371 // committed index or the pending entry, this does nothing and returns false.
372 // Otherwise this call discards any transient or pending entries.
373 virtual bool RemoveEntryAtIndex(int index
) = 0;
375 // Random --------------------------------------------------------------------
377 // Session storage depends on dom_storage that depends on blink::WebString,
378 // which cannot be used on iOS.
380 // Returns all the SessionStorageNamespace objects that this
381 // NavigationController knows about, the map key is a StoragePartition id.
382 virtual const SessionStorageNamespaceMap
&
383 GetSessionStorageNamespaceMap() const = 0;
385 // TODO(ajwong): Remove this once prerendering, instant, and session restore
387 virtual SessionStorageNamespace
* GetDefaultSessionStorageNamespace() = 0;
390 // Sets the max restored page ID this NavigationController has seen, if it
391 // was restored from a previous session.
392 virtual void SetMaxRestoredPageID(int32 max_id
) = 0;
394 // Returns the largest restored page ID seen in this navigation controller,
395 // if it was restored from a previous session. (-1 otherwise)
396 virtual int32
GetMaxRestoredPageID() const = 0;
398 // Returns true if a reload happens when activated (SetActive(true) is
399 // invoked). This is true for session/tab restore, cloned tabs and tabs that
400 // requested a reload (using SetNeedsReload()) after their renderer was
402 virtual bool NeedsReload() const = 0;
404 // Request a reload to happen when activated. This can be used when a renderer
405 // backing a background tab is killed by the system on Android or ChromeOS.
406 virtual void SetNeedsReload() = 0;
408 // Cancels a repost that brought up a warning.
409 virtual void CancelPendingReload() = 0;
410 // Continues a repost that brought up a warning.
411 virtual void ContinuePendingReload() = 0;
413 // Returns true if we are navigating to the URL the tab is opened with.
414 // Returns false after the initial navigation has committed.
415 virtual bool IsInitialNavigation() const = 0;
417 // Broadcasts the NOTIFICATION_NAV_ENTRY_CHANGED notification for the given
418 // entry. This will keep things in sync like the saved session.
419 virtual void NotifyEntryChanged(const NavigationEntry
* entry
) = 0;
421 // Copies the navigation state from the given controller to this one. This
422 // one should be empty (just created).
423 virtual void CopyStateFrom(const NavigationController
& source
) = 0;
425 // A variant of CopyStateFrom. Removes all entries from this except the last
426 // committed entry, and inserts all entries from |source| before and including
427 // its last committed entry. For example:
431 // If there is a pending entry after *G* in |this|, it is also preserved.
432 // If |replace_entry| is true, the current entry in |source| is replaced. So
433 // the result above would be A B *G*.
434 // This ignores any pending or transient entries in |source|. Callers must
435 // ensure that |CanPruneAllButLastCommitted| returns true before calling this,
437 virtual void CopyStateFromAndPrune(NavigationController
* source
,
438 bool replace_entry
) = 0;
440 // Returns whether it is safe to call PruneAllButLastCommitted or
441 // CopyStateFromAndPrune. There must be a last committed entry, no transient
442 // entry, and if there is a pending entry, it must be new and not an existing
445 // If there were no last committed entry, the pending entry might not commit,
446 // leaving us with a blank page. This is unsafe when used with
447 // |CopyStateFromAndPrune|, which would show an existing entry above the blank
449 // If there were a transient entry, we would not want to prune the other
450 // entries, which the transient entry could be referring to.
451 // If there were an existing pending entry, we could not prune the last
452 // committed entry, in case it did not commit. That would leave us with no
453 // sensible place to put the pending entry when it did commit, after all other
454 // entries are pruned. For example, it could be going back several entries.
455 // (New pending entries are safe, because they can always commit to the end.)
456 virtual bool CanPruneAllButLastCommitted() = 0;
458 // Removes all the entries except the last committed entry. If there is a new
459 // pending navigation it is preserved. Callers must ensure
460 // |CanPruneAllButLastCommitted| returns true before calling this, or it will
462 virtual void PruneAllButLastCommitted() = 0;
464 // Clears all screenshots associated with navigation entries in this
465 // controller. Useful to reduce memory consumption in low-memory situations.
466 virtual void ClearAllScreenshots() = 0;
469 // This interface should only be implemented inside content.
470 friend class NavigationControllerImpl
;
471 NavigationController() {}
474 } // namespace content
476 #endif // CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_