blob: c7d9d0318ac68ce206a3a8fc4d843deef48bc7d8 [file] [log] [blame]
[email protected]43032342011-03-21 14:10:311// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]0dd3a0ab2011-02-18 08:17:442// 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_TAB_CONTENTS_NAVIGATION_CONTROLLER_H_
6#define CONTENT_BROWSER_TAB_CONTENTS_NAVIGATION_CONTROLLER_H_
7#pragma once
8
9#include "build/build_config.h"
10
11#include <string>
12#include <vector>
13
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/linked_ptr.h"
[email protected]0dd3a0ab2011-02-18 08:17:4415#include "base/time.h"
16#include "googleurl/src/gurl.h"
[email protected]74b962a2011-06-03 21:22:5417#include "content/browser/ssl/ssl_manager.h"
[email protected]4dd57932011-03-17 06:06:1218#include "content/common/navigation_types.h"
19#include "content/common/page_transition_types.h"
[email protected]0dd3a0ab2011-02-18 08:17:4420
21class NavigationEntry;
[email protected]0dd3a0ab2011-02-18 08:17:4422class SessionStorageNamespace;
23class SiteInstance;
24class TabContents;
[email protected]0dd3a0ab2011-02-18 08:17:4425struct ViewHostMsg_FrameNavigate_Params;
26
[email protected]8286f51a2011-05-31 17:39:1327namespace content {
[email protected]3d7474ff2011-07-27 17:47:3728class BrowserContext;
[email protected]8286f51a2011-05-31 17:39:1329struct LoadCommittedDetails;
30}
31
[email protected]0dd3a0ab2011-02-18 08:17:4432// A NavigationController maintains the back-forward list for a single tab and
33// manages all navigation within that list.
34//
35// The NavigationController also owns all TabContents for the tab. This is to
36// make sure that we have at most one TabContents instance per type.
37class NavigationController {
38 public:
[email protected]0dd3a0ab2011-02-18 08:17:4439
40 enum ReloadType {
41 NO_RELOAD, // Normal load.
42 RELOAD, // Normal (cache-validating) reload.
43 RELOAD_IGNORING_CACHE // Reload bypassing the cache, aka shift-reload.
44 };
45
46 // ---------------------------------------------------------------------------
47
48 NavigationController(TabContents* tab_contents,
[email protected]3d7474ff2011-07-27 17:47:3749 content::BrowserContext* browser_context,
[email protected]0dd3a0ab2011-02-18 08:17:4450 SessionStorageNamespace* session_storage_namespace);
51 ~NavigationController();
52
[email protected]3d7474ff2011-07-27 17:47:3753 // Returns the browser context for this controller. It can never be NULL.
54 content::BrowserContext* browser_context() const {
55 return browser_context_;
[email protected]0dd3a0ab2011-02-18 08:17:4456 }
57
[email protected]3d7474ff2011-07-27 17:47:3758 // Sets the browser context for this controller.
59 void set_browser_context(content::BrowserContext* browser_context) {
60 browser_context_ = browser_context;
61 }
62
[email protected]0dd3a0ab2011-02-18 08:17:4463 // Initializes this NavigationController with the given saved navigations,
64 // using selected_navigation as the currently loaded entry. Before this call
65 // the controller should be unused (there should be no current entry). If
66 // from_last_session is true, navigations are from the previous session,
[email protected]03838e22011-06-06 15:27:1467 // otherwise they are from the current session (undo tab close). This takes
68 // ownership of the NavigationEntrys in |entries| and clears it out.
[email protected]0dd3a0ab2011-02-18 08:17:4469 // This is used for session restore.
[email protected]03838e22011-06-06 15:27:1470 void Restore(int selected_navigation,
71 bool from_last_session,
72 std::vector<NavigationEntry*>* entries);
[email protected]0dd3a0ab2011-02-18 08:17:4473
74 // Active entry --------------------------------------------------------------
75
76 // Returns the active entry, which is the transient entry if any, the pending
77 // entry if a navigation is in progress or the last committed entry otherwise.
78 // NOTE: This can be NULL!!
79 //
80 // If you are trying to get the current state of the NavigationController,
[email protected]867e1f92011-08-30 19:01:1981 // this is the method you will typically want to call. If you want to display
82 // the active entry to the user (e.g., in the location bar), use
83 // GetVisibleEntry instead.
[email protected]0dd3a0ab2011-02-18 08:17:4484 NavigationEntry* GetActiveEntry() const;
85
[email protected]867e1f92011-08-30 19:01:1986 // Returns the same entry as GetActiveEntry, except that it ignores pending
87 // history navigation entries. This should be used when displaying info to
88 // the user, so that the location bar and other indicators do not update for
89 // a back/forward navigation until the pending entry commits. This approach
90 // guards against URL spoofs on slow history navigations.
91 NavigationEntry* GetVisibleEntry() const;
92
[email protected]0dd3a0ab2011-02-18 08:17:4493 // Returns the index from which we would go back/forward or reload. This is
94 // the last_committed_entry_index_ if pending_entry_index_ is -1. Otherwise,
95 // it is the pending_entry_index_.
96 int GetCurrentEntryIndex() const;
97
98 // Returns the last committed entry, which may be null if there are no
99 // committed entries.
100 NavigationEntry* GetLastCommittedEntry() const;
101
102 // Returns true if the source for the current entry can be viewed.
103 bool CanViewSource() const;
104
105 // Returns the index of the last committed entry.
106 int last_committed_entry_index() const {
107 return last_committed_entry_index_;
108 }
109
110 // Navigation list -----------------------------------------------------------
111
112 // Returns the number of entries in the NavigationController, excluding
113 // the pending entry if there is one, but including the transient entry if
114 // any.
115 int entry_count() const {
116 return static_cast<int>(entries_.size());
117 }
118
119 NavigationEntry* GetEntryAtIndex(int index) const {
120 return entries_.at(index).get();
121 }
122
123 // Returns the entry at the specified offset from current. Returns NULL
124 // if out of bounds.
125 NavigationEntry* GetEntryAtOffset(int offset) const;
126
127 // Returns the index of the specified entry, or -1 if entry is not contained
128 // in this NavigationController.
129 int GetIndexOfEntry(const NavigationEntry* entry) const;
130
131 // Return the index of the entry with the corresponding instance and page_id,
132 // or -1 if not found.
133 int GetEntryIndexWithPageID(SiteInstance* instance,
134 int32 page_id) const;
135
136 // Return the entry with the corresponding instance and page_id, or NULL if
137 // not found.
138 NavigationEntry* GetEntryWithPageID(SiteInstance* instance,
139 int32 page_id) const;
140
141 // Pending entry -------------------------------------------------------------
142
[email protected]0dd3a0ab2011-02-18 08:17:44143 // Discards the pending and transient entries if any.
144 void DiscardNonCommittedEntries();
145
146 // Returns the pending entry corresponding to the navigation that is
147 // currently in progress, or null if there is none.
148 NavigationEntry* pending_entry() const {
149 return pending_entry_;
150 }
151
152 // Returns the index of the pending entry or -1 if the pending entry
153 // corresponds to a new navigation (created via LoadURL).
154 int pending_entry_index() const {
155 return pending_entry_index_;
156 }
157
158 // Transient entry -----------------------------------------------------------
159
160 // Adds an entry that is returned by GetActiveEntry(). The entry is
161 // transient: any navigation causes it to be removed and discarded.
162 // The NavigationController becomes the owner of |entry| and deletes it when
163 // it discards it. This is useful with interstitial page that need to be
164 // represented as an entry, but should go away when the user navigates away
165 // from them.
166 // Note that adding a transient entry does not change the active contents.
167 void AddTransientEntry(NavigationEntry* entry);
168
169 // Returns the transient entry if any. Note that the returned entry is owned
170 // by the navigation controller and may be deleted at any time.
171 NavigationEntry* GetTransientEntry() const;
172
173 // New navigations -----------------------------------------------------------
174
175 // Loads the specified URL.
176 void LoadURL(const GURL& url, const GURL& referrer,
177 PageTransition::Type type);
178
179 // Loads the current page if this NavigationController was restored from
180 // history and the current page has not loaded yet.
181 void LoadIfNecessary();
182
183 // Renavigation --------------------------------------------------------------
184
185 // Navigation relative to the "current entry"
186 bool CanGoBack() const;
187 bool CanGoForward() const;
188 void GoBack();
189 void GoForward();
190
191 // Navigates to the specified absolute index.
192 void GoToIndex(int index);
193
194 // Navigates to the specified offset from the "current entry". Does nothing if
195 // the offset is out of bounds.
196 void GoToOffset(int offset);
197
198 // Reloads the current entry. If |check_for_repost| is true and the current
199 // entry has POST data the user is prompted to see if they really want to
200 // reload the page. In nearly all cases pass in true.
201 void Reload(bool check_for_repost);
202 // Like Reload(), but don't use caches (aka "shift-reload").
203 void ReloadIgnoringCache(bool check_for_repost);
204
205 // Removing of entries -------------------------------------------------------
206
207 // Removes the entry at the specified |index|. This call dicards any pending
208 // and transient entries. |default_url| is the URL that the navigation
209 // controller navigates to if there are no more entries after the removal.
210 // If |default_url| is empty, we default to "about:blank".
211 void RemoveEntryAtIndex(int index, const GURL& default_url);
212
213 // TabContents ---------------------------------------------------------------
214
215 // Returns the tab contents associated with this controller. Non-NULL except
216 // during set-up of the tab.
217 TabContents* tab_contents() const {
218 // This currently returns the active tab contents which should be renamed to
219 // tab_contents.
220 return tab_contents_;
221 }
222
223 // Called when a document has been loaded in a frame.
224 void DocumentLoadedInFrame();
225
226 // For use by TabContents ----------------------------------------------------
227
228 // Handles updating the navigation state after the renderer has navigated.
[email protected]9a7e68c2011-05-26 17:35:50229 // This is used by the TabContents.
[email protected]0dd3a0ab2011-02-18 08:17:44230 //
231 // If a new entry is created, it will return true and will have filled the
232 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED
233 // notification. The caller can then use the details without worrying about
234 // listening for the notification.
235 //
236 // In the case that nothing has changed, the details structure is undefined
237 // and it will return false.
[email protected]0dd3a0ab2011-02-18 08:17:44238 bool RendererDidNavigate(const ViewHostMsg_FrameNavigate_Params& params,
[email protected]8286f51a2011-05-31 17:39:13239 content::LoadCommittedDetails* details);
[email protected]0dd3a0ab2011-02-18 08:17:44240
241 // Notifies us that we just became active. This is used by the TabContents
242 // so that we know to load URLs that were pending as "lazy" loads.
243 void SetActive(bool is_active);
244
245 // Broadcasts the NOTIFY_NAV_ENTRY_CHANGED notification for the given entry
246 // (which must be at the given index). This will keep things in sync like
247 // the saved session.
248 void NotifyEntryChanged(const NavigationEntry* entry, int index);
249
250 // Returns true if the given URL would be an in-page navigation (i.e. only
251 // the reference fragment is different) from the "last committed entry". We do
252 // not compare it against the "active entry" since the active entry can be
253 // pending and in page navigations only happen on committed pages. If there
254 // is no last committed entry, then nothing will be in-page.
255 //
256 // Special note: if the URLs are the same, it does NOT count as an in-page
257 // navigation. Neither does an input URL that has no ref, even if the rest is
258 // the same. This may seem weird, but when we're considering whether a
259 // navigation happened without loading anything, the same URL would be a
260 // reload, while only a different ref would be in-page (pages can't clear
261 // refs without reload, only change to "#" which we don't count as empty).
262 bool IsURLInPageNavigation(const GURL& url) const;
263
264 // Copies the navigation state from the given controller to this one. This
265 // one should be empty (just created).
266 void CopyStateFrom(const NavigationController& source);
267
268 // A variant of CopyStateFrom. Removes all entries from this except the last
269 // entry, inserts all entries from |source| before and including the active
270 // entry. This method is intended for use when the last entry of |this| is the
271 // active entry. For example:
272 // source: A B *C* D
273 // this: E F *G* (last must be active or pending)
274 // result: A B *G*
275 // This ignores the transient index of the source and honors that of 'this'.
[email protected]43032342011-03-21 14:10:31276 //
277 // If |remove_first_entry| is true, the first NavigationEntry is removed
278 // from this before merging.
279 void CopyStateFromAndPrune(NavigationController* source,
280 bool remove_first_entry);
[email protected]0dd3a0ab2011-02-18 08:17:44281
282 // Removes all the entries except the active entry. If there is a new pending
283 // navigation it is preserved.
284 void PruneAllButActive();
285
286 // Random data ---------------------------------------------------------------
287
[email protected]0dd3a0ab2011-02-18 08:17:44288 SSLManager* ssl_manager() { return &ssl_manager_; }
289
290 // Returns true if a reload happens when activated (SetActive(true) is
291 // invoked). This is true for session/tab restore and cloned tabs.
292 bool needs_reload() const { return needs_reload_; }
293
294 // Sets the max restored page ID this NavigationController has seen, if it
295 // was restored from a previous session.
296 void set_max_restored_page_id(int32 max_id) {
297 max_restored_page_id_ = max_id;
298 }
299
300 // Returns the largest restored page ID seen in this navigation controller,
301 // if it was restored from a previous session. (-1 otherwise)
302 int32 max_restored_page_id() const { return max_restored_page_id_; }
303
304 // The session storage namespace that all child render views should use.
305 SessionStorageNamespace* session_storage_namespace() const {
306 return session_storage_namespace_;
307 }
308
309 // Disables checking for a repost and prompting the user. This is used during
310 // testing.
311 static void DisablePromptOnRepost();
312
313 // Maximum number of entries before we start removing entries from the front.
314#ifdef UNIT_TEST
315 static void set_max_entry_count(size_t max_entry_count) {
316 max_entry_count_ = max_entry_count;
317 }
318#endif
319 static size_t max_entry_count() { return max_entry_count_; }
320
321 // Cancels a repost that brought up a warning.
322 void CancelPendingReload();
323 // Continues a repost that brought up a warning.
324 void ContinuePendingReload();
325
326 // Returns true if we are navigating to the URL the tab is opened with.
327 bool IsInitialNavigation();
328
329 // Creates navigation entry and translates the virtual url to a real one.
[email protected]03838e22011-06-06 15:27:14330 // Used when navigating to a new URL using LoadURL.
[email protected]3d7474ff2011-07-27 17:47:37331 static NavigationEntry* CreateNavigationEntry(
332 const GURL& url,
333 const GURL& referrer,
334 PageTransition::Type transition,
335 content::BrowserContext* browser_context);
[email protected]0dd3a0ab2011-02-18 08:17:44336
337 private:
338 class RestoreHelper;
339 friend class RestoreHelper;
340 friend class TabContents; // For invoking OnReservedPageIDRange.
341
342 // Classifies the given renderer navigation (see the NavigationType enum).
343 NavigationType::Type ClassifyNavigation(
344 const ViewHostMsg_FrameNavigate_Params& params) const;
345
346 // Causes the controller to load the specified entry. The function assumes
347 // ownership of the pointer since it is put in the navigation list.
348 // NOTE: Do not pass an entry that the controller already owns!
349 void LoadEntry(NavigationEntry* entry);
350
351 // Handlers for the different types of navigation types. They will actually
352 // handle the navigations corresponding to the different NavClasses above.
353 // They will NOT broadcast the commit notification, that should be handled by
354 // the caller.
355 //
356 // RendererDidNavigateAutoSubframe is special, it may not actually change
357 // anything if some random subframe is loaded. It will return true if anything
358 // changed, or false if not.
359 //
360 // The functions taking |did_replace_entry| will fill into the given variable
361 // whether the last entry has been replaced or not.
362 // See LoadCommittedDetails.did_replace_entry.
363 void RendererDidNavigateToNewPage(
364 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry);
365 void RendererDidNavigateToExistingPage(
366 const ViewHostMsg_FrameNavigate_Params& params);
367 void RendererDidNavigateToSamePage(
368 const ViewHostMsg_FrameNavigate_Params& params);
369 void RendererDidNavigateInPage(
370 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry);
371 void RendererDidNavigateNewSubframe(
372 const ViewHostMsg_FrameNavigate_Params& params);
373 bool RendererDidNavigateAutoSubframe(
374 const ViewHostMsg_FrameNavigate_Params& params);
375
376 // Helper function for code shared between Reload() and ReloadIgnoringCache().
377 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
378
379 // Actually issues the navigation held in pending_entry.
380 void NavigateToPendingEntry(ReloadType reload_type);
381
382 // Allows the derived class to issue notifications that a load has been
383 // committed. This will fill in the active entry to the details structure.
[email protected]93f230e02011-06-01 14:40:00384 void NotifyNavigationEntryCommitted(content::LoadCommittedDetails* details);
[email protected]0dd3a0ab2011-02-18 08:17:44385
386 // Updates the virtual URL of an entry to match a new URL, for cases where
387 // the real renderer URL is derived from the virtual URL, like view-source:
388 void UpdateVirtualURLToURL(NavigationEntry* entry, const GURL& new_url);
389
390 // Invoked after session/tab restore or cloning a tab. Resets the transition
391 // type of the entries, updates the max page id and creates the active
392 // contents. See RestoreFromState for a description of from_last_session.
393 void FinishRestore(int selected_index, bool from_last_session);
394
395 // Inserts a new entry or replaces the current entry with a new one, removing
396 // all entries after it. The new entry will become the active one.
397 void InsertOrReplaceEntry(NavigationEntry* entry, bool replace);
398
[email protected]43032342011-03-21 14:10:31399 // Removes the entry at |index|.
400 void RemoveEntryAtIndexInternal(int index);
401
[email protected]0dd3a0ab2011-02-18 08:17:44402 // Discards the pending and transient entries.
403 void DiscardNonCommittedEntriesInternal();
404
405 // Discards the transient entry.
406 void DiscardTransientEntry();
407
408 // Returns true if the navigation is redirect.
409 bool IsRedirect(const ViewHostMsg_FrameNavigate_Params& params);
410
411 // Returns true if the navigation is likley to be automatic rather than
412 // user-initiated.
413 bool IsLikelyAutoNavigation(base::TimeTicks now);
414
[email protected]0dd3a0ab2011-02-18 08:17:44415 // Inserts up to |max_index| entries from |source| into this. This does NOT
416 // adjust any of the members that reference entries_
417 // (last_committed_entry_index_, pending_entry_index_ or
418 // transient_entry_index_).
419 void InsertEntriesFrom(const NavigationController& source, int max_index);
420
421 // ---------------------------------------------------------------------------
422
[email protected]3d7474ff2011-07-27 17:47:37423 // The user browser context associated with this controller.
424 content::BrowserContext* browser_context_;
[email protected]0dd3a0ab2011-02-18 08:17:44425
426 // List of NavigationEntry for this tab
427 typedef std::vector<linked_ptr<NavigationEntry> > NavigationEntries;
428 NavigationEntries entries_;
429
430 // An entry we haven't gotten a response for yet. This will be discarded
431 // when we navigate again. It's used only so we know what the currently
432 // displayed tab is.
433 //
434 // This may refer to an item in the entries_ list if the pending_entry_index_
435 // == -1, or it may be its own entry that should be deleted. Be careful with
436 // the memory management.
437 NavigationEntry* pending_entry_;
438
439 // currently visible entry
440 int last_committed_entry_index_;
441
442 // index of pending entry if it is in entries_, or -1 if pending_entry_ is a
443 // new entry (created by LoadURL).
444 int pending_entry_index_;
445
446 // The index for the entry that is shown until a navigation occurs. This is
447 // used for interstitial pages. -1 if there are no such entry.
448 // Note that this entry really appears in the list of entries, but only
449 // temporarily (until the next navigation). Any index pointing to an entry
450 // after the transient entry will become invalid if you navigate forward.
451 int transient_entry_index_;
452
453 // The tab contents associated with the controller. Possibly NULL during
454 // setup.
455 TabContents* tab_contents_;
456
457 // The max restored page ID in this controller, if it was restored. We must
458 // store this so that TabContents can tell any renderer in charge of one of
459 // the restored entries to update its max page ID.
460 int32 max_restored_page_id_;
461
462 // Manages the SSL security UI
463 SSLManager ssl_manager_;
464
465 // Whether we need to be reloaded when made active.
466 bool needs_reload_;
467
[email protected]0dd3a0ab2011-02-18 08:17:44468 // The time ticks at which the last document was loaded.
469 base::TimeTicks last_document_loaded_;
470
471 // The session storage id that any (indirectly) owned RenderView should use.
472 scoped_refptr<SessionStorageNamespace> session_storage_namespace_;
473
474 // Should Reload check for post data? The default is true, but is set to false
475 // when testing.
476 static bool check_for_repost_;
477
478 // The maximum number of entries that a navigation controller can store.
479 static size_t max_entry_count_;
480
481 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
482 // NO_RELOAD otherwise.
483 ReloadType pending_reload_;
484
485 DISALLOW_COPY_AND_ASSIGN(NavigationController);
486};
487
488#endif // CONTENT_BROWSER_TAB_CONTENTS_NAVIGATION_CONTROLLER_H_