Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / browser / navigation_controller.h
blob7d201e542ae5c92c25411303142f23f524baea48
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/global_request_id.h"
16 #include "content/public/browser/session_storage_namespace.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/common/referrer.h"
19 #include "ui/base/page_transition_types.h"
20 #include "url/gurl.h"
22 namespace base {
24 class RefCountedMemory;
26 } // namespace base
28 namespace content {
30 class BrowserContext;
31 class NavigationEntry;
32 class WebContents;
34 // A NavigationController maintains the back-forward list for a WebContents and
35 // manages all navigation within that list.
37 // Each NavigationController belongs to one WebContents; each WebContents has
38 // exactly one NavigationController.
39 class NavigationController {
40 public:
41 enum ReloadType {
42 NO_RELOAD, // Normal load.
43 RELOAD, // Normal (cache-validating) reload.
44 RELOAD_IGNORING_CACHE, // Reload bypassing the cache (shift-reload).
45 RELOAD_ORIGINAL_REQUEST_URL // Reload using the original request URL.
48 // Load type used in LoadURLParams.
50 // A Java counterpart will be generated for this enum.
51 // GENERATED_JAVA_ENUM_PACKAGE: (
52 // org.chromium.content_public.browser.navigation_controller)
53 // GENERATED_JAVA_PREFIX_TO_STRIP: LOAD_TYPE_
54 enum LoadURLType {
55 // For loads that do not fall into any types below.
56 LOAD_TYPE_DEFAULT,
58 // An http post load request initiated from browser side.
59 // The post data is passed in |browser_initiated_post_data|.
60 LOAD_TYPE_BROWSER_INITIATED_HTTP_POST,
62 // Loads a 'data:' scheme URL with specified base URL and a history entry
63 // URL. This is only safe to be used for browser-initiated data: URL
64 // navigations, since it shows arbitrary content as if it comes from
65 // |virtual_url_for_data_url|.
66 LOAD_TYPE_DATA
68 // Adding new LoadURLType? Also update LoadUrlParams.java static constants.
71 // User agent override type used in LoadURLParams.
73 // A Java counterpart will be generated for this enum.
74 // GENERATED_JAVA_ENUM_PACKAGE: (
75 // org.chromium.content_public.browser.navigation_controller)
76 // GENERATED_JAVA_PREFIX_TO_STRIP: UA_OVERRIDE_
77 enum UserAgentOverrideOption {
78 // Use the override value from the previous NavigationEntry in the
79 // NavigationController.
80 UA_OVERRIDE_INHERIT,
82 // Use the default user agent.
83 UA_OVERRIDE_FALSE,
85 // Use the user agent override, if it's available.
86 UA_OVERRIDE_TRUE
88 // Adding new UserAgentOverrideOption? Also update LoadUrlParams.java
89 // static constants.
92 enum RestoreType {
93 // Indicates the restore is from the current session. For example, restoring
94 // a closed tab.
95 RESTORE_CURRENT_SESSION,
97 // Restore from the previous session.
98 RESTORE_LAST_SESSION_EXITED_CLEANLY,
99 RESTORE_LAST_SESSION_CRASHED,
102 // Creates a navigation entry and translates the virtual url to a real one.
103 // This is a general call; prefer LoadURL[FromRenderer]/TransferURL below.
104 // Extra headers are separated by \n.
105 CONTENT_EXPORT static NavigationEntry* CreateNavigationEntry(
106 const GURL& url,
107 const Referrer& referrer,
108 ui::PageTransition transition,
109 bool is_renderer_initiated,
110 const std::string& extra_headers,
111 BrowserContext* browser_context);
113 // Extra optional parameters for LoadURLWithParams.
114 struct CONTENT_EXPORT LoadURLParams {
115 // The url to load. This field is required.
116 GURL url;
118 // SiteInstance of the frame that initiated the navigation or null if we
119 // don't know it.
120 scoped_refptr<SiteInstance> source_site_instance;
122 // See LoadURLType comments above.
123 LoadURLType load_type;
125 // PageTransition for this load. See PageTransition for details.
126 // Note the default value in constructor below.
127 ui::PageTransition transition_type;
129 // The FrameTreeNode ID for the frame to navigate, or -1 for the main frame.
130 int64 frame_tree_node_id;
132 // Referrer for this load. Empty if none.
133 Referrer referrer;
135 // Any redirect URLs that occurred for this navigation before |url|.
136 // Defaults to an empty vector.
137 std::vector<GURL> redirect_chain;
139 // Extra headers for this load, separated by \n.
140 std::string extra_headers;
142 // True for renderer-initiated navigations. This is
143 // important for tracking whether to display pending URLs.
144 bool is_renderer_initiated;
146 // User agent override for this load. See comments in
147 // UserAgentOverrideOption definition.
148 UserAgentOverrideOption override_user_agent;
150 // Marks the new navigation as being transferred from one RVH to another.
151 // In this case the browser can recycle the old request once the new
152 // renderer wants to navigate. Identifies the request ID of the old request.
153 GlobalRequestID transferred_global_request_id;
155 // Used in LOAD_TYPE_DATA loads only. Used for specifying a base URL
156 // for pages loaded via data URLs.
157 GURL base_url_for_data_url;
159 // Used in LOAD_TYPE_DATA loads only. URL displayed to the user for
160 // data loads.
161 GURL virtual_url_for_data_url;
163 // Used in LOAD_TYPE_BROWSER_INITIATED_HTTP_POST loads only. Carries the
164 // post data of the load. Ownership is transferred to NavigationController
165 // after LoadURLWithParams call.
166 scoped_refptr<base::RefCountedMemory> browser_initiated_post_data;
168 // True if this URL should be able to access local resources.
169 bool can_load_local_resources;
171 // Indicates whether this navigation should replace the current
172 // navigation entry.
173 bool should_replace_current_entry;
175 // Used to specify which frame to navigate. If empty, the main frame is
176 // navigated. This is currently only used in tests.
177 std::string frame_name;
179 // Indicates that during this navigation, the session history should be
180 // cleared such that the resulting page is the first and only entry of the
181 // session history.
183 // The clearing is done asynchronously, and completes when this navigation
184 // commits.
185 bool should_clear_history_list;
187 #if defined(OS_ANDROID)
188 // On Android, for a load triggered by an intent, the time Chrome received
189 // the original intent that prompted the load (in milliseconds active time
190 // since boot).
191 int64 intent_received_timestamp;
192 #endif
194 explicit LoadURLParams(const GURL& url);
195 ~LoadURLParams();
197 // Allows copying of LoadURLParams struct.
198 LoadURLParams(const LoadURLParams& other);
199 LoadURLParams& operator=(const LoadURLParams& other);
202 // Disables checking for a repost and prompting the user. This is used during
203 // testing.
204 CONTENT_EXPORT static void DisablePromptOnRepost();
206 virtual ~NavigationController() {}
208 // Returns the web contents associated with this controller. It can never be
209 // nullptr.
210 virtual WebContents* GetWebContents() const = 0;
212 // Get/set the browser context for this controller. It can never be nullptr.
213 virtual BrowserContext* GetBrowserContext() const = 0;
214 virtual void SetBrowserContext(BrowserContext* browser_context) = 0;
216 // Initializes this NavigationController with the given saved navigations,
217 // using |selected_navigation| as the currently loaded entry. Before this call
218 // the controller should be unused (there should be no current entry). |type|
219 // indicates where the restor comes from. This takes ownership of the
220 // NavigationEntrys in |entries| and clears it out. This is used for session
221 // restore.
222 virtual void Restore(int selected_navigation,
223 RestoreType type,
224 std::vector<NavigationEntry*>* entries) = 0;
226 // Entries -------------------------------------------------------------------
228 // There are two basic states for entries: pending and committed. When an
229 // entry is navigated to, a request is sent to the server. While that request
230 // has not been responded to, the NavigationEntry is pending. Once data is
231 // received for that entry, that NavigationEntry is committed.
233 // A transient entry is an entry that, when the user navigates away, is
234 // removed and discarded rather than being added to the back-forward list.
235 // Transient entries are useful for interstitial pages and the like.
237 // Active entry --------------------------------------------------------------
239 // THIS IS DEPRECATED. DO NOT USE. Use GetVisibleEntry instead.
240 // See http://crbug.com/273710.
242 // Returns the active entry, which is the transient entry if any, the pending
243 // entry if a navigation is in progress or the last committed entry otherwise.
244 // NOTE: This can be nullptr!!
245 virtual NavigationEntry* GetActiveEntry() const = 0;
247 // Returns the entry that should be displayed to the user in the address bar.
248 // This is the transient entry if any, the pending entry if a navigation is
249 // in progress *and* is safe to display to the user (see below), or the last
250 // committed entry otherwise.
251 // NOTE: This can be nullptr if no entry has committed!
253 // A pending entry is safe to display if it started in the browser process or
254 // if it's a renderer-initiated navigation in a new tab which hasn't been
255 // accessed by another tab. (If it has been accessed, it risks a URL spoof.)
256 virtual NavigationEntry* GetVisibleEntry() const = 0;
258 // Returns the index from which we would go back/forward or reload. This is
259 // the last_committed_entry_index_ if pending_entry_index_ is -1. Otherwise,
260 // it is the pending_entry_index_.
261 virtual int GetCurrentEntryIndex() const = 0;
263 // Returns the last committed entry, which may be null if there are no
264 // committed entries.
265 virtual NavigationEntry* GetLastCommittedEntry() const = 0;
267 // Returns the index of the last committed entry.
268 virtual int GetLastCommittedEntryIndex() const = 0;
270 // Returns true if the source for the current entry can be viewed.
271 virtual bool CanViewSource() const = 0;
273 // Navigation list -----------------------------------------------------------
275 // Returns the number of entries in the NavigationController, excluding
276 // the pending entry if there is one, but including the transient entry if
277 // any.
278 virtual int GetEntryCount() const = 0;
280 virtual NavigationEntry* GetEntryAtIndex(int index) const = 0;
282 // Returns the entry at the specified offset from current. Returns nullptr
283 // if out of bounds.
284 virtual NavigationEntry* GetEntryAtOffset(int offset) const = 0;
286 // Pending entry -------------------------------------------------------------
288 // Discards the pending and transient entries if any.
289 virtual void DiscardNonCommittedEntries() = 0;
291 // Returns the pending entry corresponding to the navigation that is
292 // currently in progress, or null if there is none.
293 virtual NavigationEntry* GetPendingEntry() const = 0;
295 // Returns the index of the pending entry or -1 if the pending entry
296 // corresponds to a new navigation (created via LoadURL).
297 virtual int GetPendingEntryIndex() const = 0;
299 // Transient entry -----------------------------------------------------------
301 // Returns the transient entry if any. This is an entry which is removed and
302 // discarded if any navigation occurs. Note that the returned entry is owned
303 // by the navigation controller and may be deleted at any time.
304 virtual NavigationEntry* GetTransientEntry() const = 0;
306 // Adds an entry that is returned by GetActiveEntry(). The entry is
307 // transient: any navigation causes it to be removed and discarded. The
308 // NavigationController becomes the owner of |entry| and deletes it when
309 // it discards it. This is useful with interstitial pages that need to be
310 // represented as an entry, but should go away when the user navigates away
311 // from them.
312 // Note that adding a transient entry does not change the active contents.
313 virtual void SetTransientEntry(NavigationEntry* entry) = 0;
315 // New navigations -----------------------------------------------------------
317 // Loads the specified URL, specifying extra http headers to add to the
318 // request. Extra headers are separated by \n.
319 virtual void LoadURL(const GURL& url,
320 const Referrer& referrer,
321 ui::PageTransition type,
322 const std::string& extra_headers) = 0;
324 // More general version of LoadURL. See comments in LoadURLParams for
325 // using |params|.
326 virtual void LoadURLWithParams(const LoadURLParams& params) = 0;
328 // Loads the current page if this NavigationController was restored from
329 // history and the current page has not loaded yet or if the load was
330 // explicitly requested using SetNeedsReload().
331 virtual void LoadIfNecessary() = 0;
333 // Renavigation --------------------------------------------------------------
335 // Navigation relative to the "current entry"
336 virtual bool CanGoBack() const = 0;
337 virtual bool CanGoForward() const = 0;
338 virtual bool CanGoToOffset(int offset) const = 0;
339 virtual void GoBack() = 0;
340 virtual void GoForward() = 0;
342 // Navigates to the specified absolute index.
343 virtual void GoToIndex(int index) = 0;
345 // Navigates to the specified offset from the "current entry". Does nothing if
346 // the offset is out of bounds.
347 virtual void GoToOffset(int offset) = 0;
349 // Reloads the current entry. If |check_for_repost| is true and the current
350 // entry has POST data the user is prompted to see if they really want to
351 // reload the page. In nearly all cases pass in true. If a transient entry
352 // is showing, initiates a new navigation to its URL.
353 virtual void Reload(bool check_for_repost) = 0;
355 // Like Reload(), but don't use caches (aka "shift-reload").
356 virtual void ReloadIgnoringCache(bool check_for_repost) = 0;
358 // Reloads the current entry using the original URL used to create it. This
359 // is used for cases where the user wants to refresh a page using a different
360 // user agent after following a redirect.
361 virtual void ReloadOriginalRequestURL(bool check_for_repost) = 0;
363 // Removing of entries -------------------------------------------------------
365 // Removes the entry at the specified |index|. If the index is the last
366 // committed index or the pending entry, this does nothing and returns false.
367 // Otherwise this call discards any transient or pending entries.
368 virtual bool RemoveEntryAtIndex(int index) = 0;
370 // Random --------------------------------------------------------------------
372 // Session storage depends on dom_storage that depends on blink::WebString,
373 // which cannot be used on iOS.
374 #if !defined(OS_IOS)
375 // Returns all the SessionStorageNamespace objects that this
376 // NavigationController knows about, the map key is a StoragePartition id.
377 virtual const SessionStorageNamespaceMap&
378 GetSessionStorageNamespaceMap() const = 0;
380 // TODO(ajwong): Remove this once prerendering, instant, and session restore
381 // are migrated.
382 virtual SessionStorageNamespace* GetDefaultSessionStorageNamespace() = 0;
383 #endif
385 // Sets the max restored page ID this NavigationController has seen, if it
386 // was restored from a previous session.
387 virtual void SetMaxRestoredPageID(int32 max_id) = 0;
389 // Returns the largest restored page ID seen in this navigation controller,
390 // if it was restored from a previous session. (-1 otherwise)
391 virtual int32 GetMaxRestoredPageID() const = 0;
393 // Returns true if a reload happens when activated (SetActive(true) is
394 // invoked). This is true for session/tab restore, cloned tabs and tabs that
395 // requested a reload (using SetNeedsReload()) after their renderer was
396 // killed.
397 virtual bool NeedsReload() const = 0;
399 // Request a reload to happen when activated. This can be used when a renderer
400 // backing a background tab is killed by the system on Android or ChromeOS.
401 virtual void SetNeedsReload() = 0;
403 // Cancels a repost that brought up a warning.
404 virtual void CancelPendingReload() = 0;
405 // Continues a repost that brought up a warning.
406 virtual void ContinuePendingReload() = 0;
408 // Returns true if we are navigating to the URL the tab is opened with.
409 // Returns false after the initial navigation has committed.
410 virtual bool IsInitialNavigation() const = 0;
412 // Broadcasts the NOTIFICATION_NAV_ENTRY_CHANGED notification for the given
413 // entry (which must be at the given index). This will keep things in sync
414 // like the saved session.
415 virtual void NotifyEntryChanged(const NavigationEntry* entry, int index) = 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:
424 // source: A B *C* D
425 // this: E F *G*
426 // result: A B C *G*
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,
432 // or it will crash.
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
439 // entry.
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
444 // page.
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
457 // crash.
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;
464 private:
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_