blob: a01262d5e33548742656605fccb0e597d683e4b4 [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,
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
[email protected]0dd3a0ab2011-02-18 08:17:44279 SSLManager* ssl_manager() { return &ssl_manager_; }
280
281 // Returns true if a reload happens when activated (SetActive(true) is
282 // invoked). This is true for session/tab restore and cloned tabs.
283 bool needs_reload() const { return needs_reload_; }
284
285 // Sets the max restored page ID this NavigationController has seen, if it
286 // was restored from a previous session.
287 void set_max_restored_page_id(int32 max_id) {
288 max_restored_page_id_ = max_id;
289 }
290
291 // Returns the largest restored page ID seen in this navigation controller,
292 // if it was restored from a previous session. (-1 otherwise)
293 int32 max_restored_page_id() const { return max_restored_page_id_; }
294
295 // The session storage namespace that all child render views should use.
296 SessionStorageNamespace* session_storage_namespace() const {
297 return session_storage_namespace_;
298 }
299
300 // Disables checking for a repost and prompting the user. This is used during
301 // testing.
302 static void DisablePromptOnRepost();
303
304 // Maximum number of entries before we start removing entries from the front.
305#ifdef UNIT_TEST
306 static void set_max_entry_count(size_t max_entry_count) {
307 max_entry_count_ = max_entry_count;
308 }
309#endif
310 static size_t max_entry_count() { return max_entry_count_; }
311
312 // Cancels a repost that brought up a warning.
313 void CancelPendingReload();
314 // Continues a repost that brought up a warning.
315 void ContinuePendingReload();
316
317 // Returns true if we are navigating to the URL the tab is opened with.
318 bool IsInitialNavigation();
319
320 // Creates navigation entry and translates the virtual url to a real one.
[email protected]03838e22011-06-06 15:27:14321 // Used when navigating to a new URL using LoadURL.
[email protected]3d7474ff2011-07-27 17:47:37322 static NavigationEntry* CreateNavigationEntry(
323 const GURL& url,
324 const GURL& referrer,
325 PageTransition::Type transition,
326 content::BrowserContext* browser_context);
[email protected]0dd3a0ab2011-02-18 08:17:44327
328 private:
329 class RestoreHelper;
330 friend class RestoreHelper;
331 friend class TabContents; // For invoking OnReservedPageIDRange.
332
333 // Classifies the given renderer navigation (see the NavigationType enum).
334 NavigationType::Type ClassifyNavigation(
335 const ViewHostMsg_FrameNavigate_Params& params) const;
336
337 // Causes the controller to load the specified entry. The function assumes
338 // ownership of the pointer since it is put in the navigation list.
339 // NOTE: Do not pass an entry that the controller already owns!
340 void LoadEntry(NavigationEntry* entry);
341
342 // Handlers for the different types of navigation types. They will actually
343 // handle the navigations corresponding to the different NavClasses above.
344 // They will NOT broadcast the commit notification, that should be handled by
345 // the caller.
346 //
347 // RendererDidNavigateAutoSubframe is special, it may not actually change
348 // anything if some random subframe is loaded. It will return true if anything
349 // changed, or false if not.
350 //
351 // The functions taking |did_replace_entry| will fill into the given variable
352 // whether the last entry has been replaced or not.
353 // See LoadCommittedDetails.did_replace_entry.
354 void RendererDidNavigateToNewPage(
355 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry);
356 void RendererDidNavigateToExistingPage(
357 const ViewHostMsg_FrameNavigate_Params& params);
358 void RendererDidNavigateToSamePage(
359 const ViewHostMsg_FrameNavigate_Params& params);
360 void RendererDidNavigateInPage(
361 const ViewHostMsg_FrameNavigate_Params& params, bool* did_replace_entry);
362 void RendererDidNavigateNewSubframe(
363 const ViewHostMsg_FrameNavigate_Params& params);
364 bool RendererDidNavigateAutoSubframe(
365 const ViewHostMsg_FrameNavigate_Params& params);
366
367 // Helper function for code shared between Reload() and ReloadIgnoringCache().
368 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
369
370 // Actually issues the navigation held in pending_entry.
371 void NavigateToPendingEntry(ReloadType reload_type);
372
373 // Allows the derived class to issue notifications that a load has been
374 // committed. This will fill in the active entry to the details structure.
[email protected]93f230e02011-06-01 14:40:00375 void NotifyNavigationEntryCommitted(content::LoadCommittedDetails* details);
[email protected]0dd3a0ab2011-02-18 08:17:44376
377 // Updates the virtual URL of an entry to match a new URL, for cases where
378 // the real renderer URL is derived from the virtual URL, like view-source:
379 void UpdateVirtualURLToURL(NavigationEntry* entry, const GURL& new_url);
380
381 // Invoked after session/tab restore or cloning a tab. Resets the transition
382 // type of the entries, updates the max page id and creates the active
383 // contents. See RestoreFromState for a description of from_last_session.
384 void FinishRestore(int selected_index, bool from_last_session);
385
386 // Inserts a new entry or replaces the current entry with a new one, removing
387 // all entries after it. The new entry will become the active one.
388 void InsertOrReplaceEntry(NavigationEntry* entry, bool replace);
389
[email protected]43032342011-03-21 14:10:31390 // Removes the entry at |index|.
391 void RemoveEntryAtIndexInternal(int index);
392
[email protected]0dd3a0ab2011-02-18 08:17:44393 // Discards the pending and transient entries.
394 void DiscardNonCommittedEntriesInternal();
395
396 // Discards the transient entry.
397 void DiscardTransientEntry();
398
399 // Returns true if the navigation is redirect.
400 bool IsRedirect(const ViewHostMsg_FrameNavigate_Params& params);
401
402 // Returns true if the navigation is likley to be automatic rather than
403 // user-initiated.
404 bool IsLikelyAutoNavigation(base::TimeTicks now);
405
[email protected]0dd3a0ab2011-02-18 08:17:44406 // Inserts up to |max_index| entries from |source| into this. This does NOT
407 // adjust any of the members that reference entries_
408 // (last_committed_entry_index_, pending_entry_index_ or
409 // transient_entry_index_).
410 void InsertEntriesFrom(const NavigationController& source, int max_index);
411
412 // ---------------------------------------------------------------------------
413
[email protected]3d7474ff2011-07-27 17:47:37414 // The user browser context associated with this controller.
415 content::BrowserContext* browser_context_;
[email protected]0dd3a0ab2011-02-18 08:17:44416
417 // List of NavigationEntry for this tab
418 typedef std::vector<linked_ptr<NavigationEntry> > NavigationEntries;
419 NavigationEntries entries_;
420
421 // An entry we haven't gotten a response for yet. This will be discarded
422 // when we navigate again. It's used only so we know what the currently
423 // displayed tab is.
424 //
425 // This may refer to an item in the entries_ list if the pending_entry_index_
426 // == -1, or it may be its own entry that should be deleted. Be careful with
427 // the memory management.
428 NavigationEntry* pending_entry_;
429
430 // currently visible entry
431 int last_committed_entry_index_;
432
433 // index of pending entry if it is in entries_, or -1 if pending_entry_ is a
434 // new entry (created by LoadURL).
435 int pending_entry_index_;
436
437 // The index for the entry that is shown until a navigation occurs. This is
438 // used for interstitial pages. -1 if there are no such entry.
439 // Note that this entry really appears in the list of entries, but only
440 // temporarily (until the next navigation). Any index pointing to an entry
441 // after the transient entry will become invalid if you navigate forward.
442 int transient_entry_index_;
443
444 // The tab contents associated with the controller. Possibly NULL during
445 // setup.
446 TabContents* tab_contents_;
447
448 // The max restored page ID in this controller, if it was restored. We must
449 // store this so that TabContents can tell any renderer in charge of one of
450 // the restored entries to update its max page ID.
451 int32 max_restored_page_id_;
452
453 // Manages the SSL security UI
454 SSLManager ssl_manager_;
455
456 // Whether we need to be reloaded when made active.
457 bool needs_reload_;
458
[email protected]0dd3a0ab2011-02-18 08:17:44459 // The time ticks at which the last document was loaded.
460 base::TimeTicks last_document_loaded_;
461
462 // The session storage id that any (indirectly) owned RenderView should use.
463 scoped_refptr<SessionStorageNamespace> session_storage_namespace_;
464
465 // Should Reload check for post data? The default is true, but is set to false
466 // when testing.
467 static bool check_for_repost_;
468
469 // The maximum number of entries that a navigation controller can store.
470 static size_t max_entry_count_;
471
472 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
473 // NO_RELOAD otherwise.
474 ReloadType pending_reload_;
475
476 DISALLOW_COPY_AND_ASSIGN(NavigationController);
477};
478
479#endif // CONTENT_BROWSER_TAB_CONTENTS_NAVIGATION_CONTROLLER_H_