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 // Indicates that during this navigation, the session history should be
181 // cleared such that the resulting page is the first and only entry of the
184 // The clearing is done asynchronously, and completes when this navigation
186 bool should_clear_history_list
;
188 #if defined(OS_ANDROID)
189 // On Android, for a load triggered by an intent, the time Chrome received
190 // the original intent that prompted the load (in milliseconds active time
192 int64 intent_received_timestamp
;
195 explicit LoadURLParams(const GURL
& url
);
198 // Allows copying of LoadURLParams struct.
199 LoadURLParams(const LoadURLParams
& other
);
200 LoadURLParams
& operator=(const LoadURLParams
& other
);
203 // Disables checking for a repost and prompting the user. This is used during
205 CONTENT_EXPORT
static void DisablePromptOnRepost();
207 virtual ~NavigationController() {}
209 // Returns the web contents associated with this controller. It can never be
211 virtual WebContents
* GetWebContents() const = 0;
213 // Get/set the browser context for this controller. It can never be nullptr.
214 virtual BrowserContext
* GetBrowserContext() const = 0;
215 virtual void SetBrowserContext(BrowserContext
* browser_context
) = 0;
217 // Initializes this NavigationController with the given saved navigations,
218 // using |selected_navigation| as the currently loaded entry. Before this call
219 // the controller should be unused (there should be no current entry). |type|
220 // indicates where the restor comes from. This takes ownership of the
221 // NavigationEntrys in |entries| and clears it out. This is used for session
223 virtual void Restore(int selected_navigation
,
225 ScopedVector
<NavigationEntry
>* entries
) = 0;
227 // Entries -------------------------------------------------------------------
229 // There are two basic states for entries: pending and committed. When an
230 // entry is navigated to, a request is sent to the server. While that request
231 // has not been responded to, the NavigationEntry is pending. Once data is
232 // received for that entry, that NavigationEntry is committed.
234 // A transient entry is an entry that, when the user navigates away, is
235 // removed and discarded rather than being added to the back-forward list.
236 // Transient entries are useful for interstitial pages and the like.
238 // Active entry --------------------------------------------------------------
240 // THIS IS DEPRECATED. DO NOT USE. Use GetVisibleEntry instead.
241 // See http://crbug.com/273710.
243 // Returns the active entry, which is the transient entry if any, the pending
244 // entry if a navigation is in progress or the last committed entry otherwise.
245 // NOTE: This can be nullptr!!
246 virtual NavigationEntry
* GetActiveEntry() const = 0;
248 // Returns the entry that should be displayed to the user in the address bar.
249 // This is the transient entry if any, the pending entry if a navigation is
250 // in progress *and* is safe to display to the user (see below), or the last
251 // committed entry otherwise.
252 // NOTE: This can be nullptr if no entry has committed!
254 // A pending entry is safe to display if it started in the browser process or
255 // if it's a renderer-initiated navigation in a new tab which hasn't been
256 // accessed by another tab. (If it has been accessed, it risks a URL spoof.)
257 virtual NavigationEntry
* GetVisibleEntry() const = 0;
259 // Returns the index from which we would go back/forward or reload. This is
260 // the last_committed_entry_index_ if pending_entry_index_ is -1. Otherwise,
261 // it is the pending_entry_index_.
262 virtual int GetCurrentEntryIndex() const = 0;
264 // Returns the last committed entry, which may be null if there are no
265 // committed entries.
266 virtual NavigationEntry
* GetLastCommittedEntry() const = 0;
268 // Returns the index of the last committed entry.
269 virtual int GetLastCommittedEntryIndex() const = 0;
271 // Returns true if the source for the current entry can be viewed.
272 virtual bool CanViewSource() const = 0;
274 // Navigation list -----------------------------------------------------------
276 // Returns the number of entries in the NavigationController, excluding
277 // the pending entry if there is one, but including the transient entry if
279 virtual int GetEntryCount() const = 0;
281 virtual NavigationEntry
* GetEntryAtIndex(int index
) const = 0;
283 // Returns the entry at the specified offset from current. Returns nullptr
285 virtual NavigationEntry
* GetEntryAtOffset(int offset
) const = 0;
287 // Pending entry -------------------------------------------------------------
289 // Discards the pending and transient entries if any.
290 virtual void DiscardNonCommittedEntries() = 0;
292 // Returns the pending entry corresponding to the navigation that is
293 // currently in progress, or null if there is none.
294 virtual NavigationEntry
* GetPendingEntry() const = 0;
296 // Returns the index of the pending entry or -1 if the pending entry
297 // corresponds to a new navigation (created via LoadURL).
298 virtual int GetPendingEntryIndex() const = 0;
300 // Transient entry -----------------------------------------------------------
302 // Returns the transient entry if any. This is an entry which is removed and
303 // discarded if any navigation occurs. Note that the returned entry is owned
304 // by the navigation controller and may be deleted at any time.
305 virtual NavigationEntry
* GetTransientEntry() const = 0;
307 // Adds an entry that is returned by GetActiveEntry(). The entry is
308 // transient: any navigation causes it to be removed and discarded. The
309 // NavigationController becomes the owner of |entry| and deletes it when
310 // it discards it. This is useful with interstitial pages that need to be
311 // represented as an entry, but should go away when the user navigates away
313 // Note that adding a transient entry does not change the active contents.
314 virtual void SetTransientEntry(scoped_ptr
<NavigationEntry
> entry
) = 0;
316 // New navigations -----------------------------------------------------------
318 // Loads the specified URL, specifying extra http headers to add to the
319 // request. Extra headers are separated by \n.
320 virtual void LoadURL(const GURL
& url
,
321 const Referrer
& referrer
,
322 ui::PageTransition type
,
323 const std::string
& extra_headers
) = 0;
325 // More general version of LoadURL. See comments in LoadURLParams for
327 virtual void LoadURLWithParams(const LoadURLParams
& params
) = 0;
329 // Loads the current page if this NavigationController was restored from
330 // history and the current page has not loaded yet or if the load was
331 // explicitly requested using SetNeedsReload().
332 virtual void LoadIfNecessary() = 0;
334 // Renavigation --------------------------------------------------------------
336 // Navigation relative to the "current entry"
337 virtual bool CanGoBack() const = 0;
338 virtual bool CanGoForward() const = 0;
339 virtual bool CanGoToOffset(int offset
) const = 0;
340 virtual void GoBack() = 0;
341 virtual void GoForward() = 0;
343 // Navigates to the specified absolute index.
344 virtual void GoToIndex(int index
) = 0;
346 // Navigates to the specified offset from the "current entry". Does nothing if
347 // the offset is out of bounds.
348 virtual void GoToOffset(int offset
) = 0;
350 // Reloads the current entry. If |check_for_repost| is true and the current
351 // entry has POST data the user is prompted to see if they really want to
352 // reload the page. In nearly all cases pass in true. If a transient entry
353 // is showing, initiates a new navigation to its URL.
354 virtual void Reload(bool check_for_repost
) = 0;
356 // Like Reload(), but don't use caches (aka "shift-reload").
357 virtual void ReloadIgnoringCache(bool check_for_repost
) = 0;
359 // Reloads the current entry using the original URL used to create it. This
360 // is used for cases where the user wants to refresh a page using a different
361 // user agent after following a redirect.
362 virtual void ReloadOriginalRequestURL(bool check_for_repost
) = 0;
364 // Removing of entries -------------------------------------------------------
366 // Removes the entry at the specified |index|. If the index is the last
367 // committed index or the pending entry, this does nothing and returns false.
368 // Otherwise this call discards any transient or pending entries.
369 virtual bool RemoveEntryAtIndex(int index
) = 0;
371 // Random --------------------------------------------------------------------
373 // Session storage depends on dom_storage that depends on blink::WebString,
374 // which cannot be used on iOS.
376 // Returns all the SessionStorageNamespace objects that this
377 // NavigationController knows about, the map key is a StoragePartition id.
378 virtual const SessionStorageNamespaceMap
&
379 GetSessionStorageNamespaceMap() const = 0;
381 // TODO(ajwong): Remove this once prerendering, instant, and session restore
383 virtual SessionStorageNamespace
* GetDefaultSessionStorageNamespace() = 0;
386 // Sets the max restored page ID this NavigationController has seen, if it
387 // was restored from a previous session.
388 virtual void SetMaxRestoredPageID(int32 max_id
) = 0;
390 // Returns the largest restored page ID seen in this navigation controller,
391 // if it was restored from a previous session. (-1 otherwise)
392 virtual int32
GetMaxRestoredPageID() const = 0;
394 // Returns true if a reload happens when activated (SetActive(true) is
395 // invoked). This is true for session/tab restore, cloned tabs and tabs that
396 // requested a reload (using SetNeedsReload()) after their renderer was
398 virtual bool NeedsReload() const = 0;
400 // Request a reload to happen when activated. This can be used when a renderer
401 // backing a background tab is killed by the system on Android or ChromeOS.
402 virtual void SetNeedsReload() = 0;
404 // Cancels a repost that brought up a warning.
405 virtual void CancelPendingReload() = 0;
406 // Continues a repost that brought up a warning.
407 virtual void ContinuePendingReload() = 0;
409 // Returns true if we are navigating to the URL the tab is opened with.
410 // Returns false after the initial navigation has committed.
411 virtual bool IsInitialNavigation() const = 0;
413 // Broadcasts the NOTIFICATION_NAV_ENTRY_CHANGED notification for the given
414 // entry. This will keep things in sync like the saved session.
415 virtual void NotifyEntryChanged(const NavigationEntry
* entry
) = 0;
417 // Copies the navigation state from the given controller to this one. This
418 // one should be empty (just created).
419 virtual void CopyStateFrom(const NavigationController
& source
) = 0;
421 // A variant of CopyStateFrom. Removes all entries from this except the last
422 // committed entry, and inserts all entries from |source| before and including
423 // its last committed entry. For example:
427 // If there is a pending entry after *G* in |this|, it is also preserved.
428 // If |replace_entry| is true, the current entry in |source| is replaced. So
429 // the result above would be A B *G*.
430 // This ignores any pending or transient entries in |source|. Callers must
431 // ensure that |CanPruneAllButLastCommitted| returns true before calling this,
433 virtual void CopyStateFromAndPrune(NavigationController
* source
,
434 bool replace_entry
) = 0;
436 // Returns whether it is safe to call PruneAllButLastCommitted or
437 // CopyStateFromAndPrune. There must be a last committed entry, no transient
438 // entry, and if there is a pending entry, it must be new and not an existing
441 // If there were no last committed entry, the pending entry might not commit,
442 // leaving us with a blank page. This is unsafe when used with
443 // |CopyStateFromAndPrune|, which would show an existing entry above the blank
445 // If there were a transient entry, we would not want to prune the other
446 // entries, which the transient entry could be referring to.
447 // If there were an existing pending entry, we could not prune the last
448 // committed entry, in case it did not commit. That would leave us with no
449 // sensible place to put the pending entry when it did commit, after all other
450 // entries are pruned. For example, it could be going back several entries.
451 // (New pending entries are safe, because they can always commit to the end.)
452 virtual bool CanPruneAllButLastCommitted() = 0;
454 // Removes all the entries except the last committed entry. If there is a new
455 // pending navigation it is preserved. Callers must ensure
456 // |CanPruneAllButLastCommitted| returns true before calling this, or it will
458 virtual void PruneAllButLastCommitted() = 0;
460 // Clears all screenshots associated with navigation entries in this
461 // controller. Useful to reduce memory consumption in low-memory situations.
462 virtual void ClearAllScreenshots() = 0;
465 // This interface should only be implemented inside content.
466 friend class NavigationControllerImpl
;
467 NavigationController() {}
470 } // namespace content
472 #endif // CONTENT_PUBLIC_BROWSER_NAVIGATION_CONTROLLER_H_