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