1 // Copyright 2013 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_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_
6 #define CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/time/time.h"
13 #include "build/build_config.h"
14 #include "content/browser/frame_host/navigation_controller_delegate.h"
15 #include "content/browser/ssl/ssl_manager.h"
16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_type.h"
19 struct FrameHostMsg_DidCommitProvisionalLoad_Params;
22 class NavigationEntryImpl;
24 class NavigationEntryScreenshotManager;
26 struct LoadCommittedDetails;
28 class CONTENT_EXPORT NavigationControllerImpl
29 : public NON_EXPORTED_BASE(NavigationController) {
31 NavigationControllerImpl(
32 NavigationControllerDelegate* delegate,
33 BrowserContext* browser_context);
34 virtual ~NavigationControllerImpl();
36 // NavigationController implementation:
37 virtual WebContents* GetWebContents() const OVERRIDE;
38 virtual BrowserContext* GetBrowserContext() const OVERRIDE;
39 virtual void SetBrowserContext(
40 BrowserContext* browser_context) OVERRIDE;
42 int selected_navigation,
44 std::vector<NavigationEntry*>* entries) OVERRIDE;
45 virtual NavigationEntry* GetActiveEntry() const OVERRIDE;
46 virtual NavigationEntry* GetVisibleEntry() const OVERRIDE;
47 virtual int GetCurrentEntryIndex() const OVERRIDE;
48 virtual NavigationEntry* GetLastCommittedEntry() const OVERRIDE;
49 virtual int GetLastCommittedEntryIndex() const OVERRIDE;
50 virtual bool CanViewSource() const OVERRIDE;
51 virtual int GetEntryCount() const OVERRIDE;
52 virtual NavigationEntry* GetEntryAtIndex(int index) const OVERRIDE;
53 virtual NavigationEntry* GetEntryAtOffset(int offset) const OVERRIDE;
54 virtual void DiscardNonCommittedEntries() OVERRIDE;
55 virtual NavigationEntry* GetPendingEntry() const OVERRIDE;
56 virtual int GetPendingEntryIndex() const OVERRIDE;
57 virtual NavigationEntry* GetTransientEntry() const OVERRIDE;
58 virtual void SetTransientEntry(NavigationEntry* entry) OVERRIDE;
59 virtual void LoadURL(const GURL& url,
60 const Referrer& referrer,
62 const std::string& extra_headers) OVERRIDE;
63 virtual void LoadURLWithParams(const LoadURLParams& params) OVERRIDE;
64 virtual void LoadIfNecessary() OVERRIDE;
65 virtual bool CanGoBack() const OVERRIDE;
66 virtual bool CanGoForward() const OVERRIDE;
67 virtual bool CanGoToOffset(int offset) const OVERRIDE;
68 virtual void GoBack() OVERRIDE;
69 virtual void GoForward() OVERRIDE;
70 virtual void GoToIndex(int index) OVERRIDE;
71 virtual void GoToOffset(int offset) OVERRIDE;
72 virtual bool RemoveEntryAtIndex(int index) OVERRIDE;
73 virtual const SessionStorageNamespaceMap&
74 GetSessionStorageNamespaceMap() const OVERRIDE;
75 virtual SessionStorageNamespace*
76 GetDefaultSessionStorageNamespace() OVERRIDE;
77 virtual void SetMaxRestoredPageID(int32 max_id) OVERRIDE;
78 virtual int32 GetMaxRestoredPageID() const OVERRIDE;
79 virtual bool NeedsReload() const OVERRIDE;
80 virtual void SetNeedsReload() OVERRIDE;
81 virtual void CancelPendingReload() OVERRIDE;
82 virtual void ContinuePendingReload() OVERRIDE;
83 virtual bool IsInitialNavigation() const OVERRIDE;
84 virtual void Reload(bool check_for_repost) OVERRIDE;
85 virtual void ReloadIgnoringCache(bool check_for_repost) OVERRIDE;
86 virtual void ReloadOriginalRequestURL(bool check_for_repost) OVERRIDE;
87 virtual void NotifyEntryChanged(const NavigationEntry* entry,
89 virtual void CopyStateFrom(
90 const NavigationController& source) OVERRIDE;
91 virtual void CopyStateFromAndPrune(NavigationController* source,
92 bool replace_entry) OVERRIDE;
93 virtual bool CanPruneAllButLastCommitted() OVERRIDE;
94 virtual void PruneAllButLastCommitted() OVERRIDE;
95 virtual void ClearAllScreenshots() OVERRIDE;
97 // The session storage namespace that all child RenderViews belonging to
98 // |instance| should use.
99 SessionStorageNamespace* GetSessionStorageNamespace(
100 SiteInstance* instance);
102 // Returns the index of the specified entry, or -1 if entry is not contained
103 // in this NavigationController.
104 int GetIndexOfEntry(const NavigationEntryImpl* entry) const;
106 // Return the index of the entry with the corresponding instance and page_id,
107 // or -1 if not found.
108 int GetEntryIndexWithPageID(SiteInstance* instance,
109 int32 page_id) const;
111 // Return the entry with the corresponding instance and page_id, or NULL if
113 NavigationEntryImpl* GetEntryWithPageID(
114 SiteInstance* instance,
115 int32 page_id) const;
117 NavigationControllerDelegate* delegate() const {
121 // For use by WebContentsImpl ------------------------------------------------
123 // Allow renderer-initiated navigations to create a pending entry when the
124 // provisional load starts.
125 void SetPendingEntry(content::NavigationEntryImpl* entry);
127 // Handles updating the navigation state after the renderer has navigated.
128 // This is used by the WebContentsImpl.
130 // If a new entry is created, it will return true and will have filled the
131 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED
132 // notification. The caller can then use the details without worrying about
133 // listening for the notification.
135 // In the case that nothing has changed, the details structure is undefined
136 // and it will return false.
138 // TODO(creis): Change RenderViewHost to RenderFrameHost.
139 bool RendererDidNavigate(
141 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
142 LoadCommittedDetails* details);
144 // Notifies us that we just became active. This is used by the WebContentsImpl
145 // so that we know to load URLs that were pending as "lazy" loads.
146 void SetActive(bool is_active);
148 // Returns true if the given URL would be an in-page navigation (i.e. only
149 // the reference fragment is different) from the "last committed entry". We do
150 // not compare it against the "active entry" since the active entry can be
151 // pending and in page navigations only happen on committed pages. If there
152 // is no last committed entry, then nothing will be in-page.
154 // Special note: if the URLs are the same, it does NOT automatically count as
155 // an in-page navigation. Neither does an input URL that has no ref, even if
156 // the rest is the same. This may seem weird, but when we're considering
157 // whether a navigation happened without loading anything, the same URL could
158 // be a reload, while only a different ref would be in-page (pages can't clear
159 // refs without reload, only change to "#" which we don't count as empty).
160 bool IsURLInPageNavigation(const GURL& url) const {
161 return IsURLInPageNavigation(url, false, NAVIGATION_TYPE_UNKNOWN);
164 // The situation is made murkier by history.replaceState(), which could
165 // provide the same URL as part of an in-page navigation, not a reload. So
166 // we need this form which lets the (untrustworthy) renderer resolve the
167 // ambiguity, but only when the URLs are equal. This should be safe since the
168 // origin isn't changing.
169 bool IsURLInPageNavigation(
171 bool renderer_says_in_page,
172 NavigationType navigation_type) const;
174 // Sets the SessionStorageNamespace for the given |partition_id|. This is
175 // used during initialization of a new NavigationController to allow
176 // pre-population of the SessionStorageNamespace objects. Session restore,
177 // prerendering, and the implementaion of window.open() are the primary users
180 // Calling this function when a SessionStorageNamespace has already been
181 // associated with a |partition_id| will CHECK() fail.
182 void SetSessionStorageNamespace(
183 const std::string& partition_id,
184 SessionStorageNamespace* session_storage_namespace);
186 // Random data ---------------------------------------------------------------
188 SSLManager* ssl_manager() { return &ssl_manager_; }
190 // Maximum number of entries before we start removing entries from the front.
191 static void set_max_entry_count_for_testing(size_t max_entry_count) {
192 max_entry_count_for_testing_ = max_entry_count;
194 static size_t max_entry_count();
196 void SetGetTimestampCallbackForTest(
197 const base::Callback<base::Time()>& get_timestamp_callback);
199 // Takes a screenshot of the page at the current state.
200 void TakeScreenshot();
202 // Sets the screenshot manager for this NavigationControllerImpl. The
203 // controller takes ownership of the screenshot manager and destroys it when
204 // a new screenshot-manager is set, or when the controller is destroyed.
205 // Setting a NULL manager recreates the default screenshot manager and uses
207 void SetScreenshotManager(NavigationEntryScreenshotManager* manager);
209 // Discards only the pending entry.
210 void DiscardPendingEntry();
213 friend class RestoreHelper;
215 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
217 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, Basic);
218 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, SingleDuplicate);
219 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ManyDuplicates);
220 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ClockBackwardsJump);
222 // Helper class to smooth out runs of duplicate timestamps while still
223 // allowing time to jump backwards.
224 class CONTENT_EXPORT TimeSmoother {
226 // Returns |t| with possibly some time added on.
227 base::Time GetSmoothedTime(base::Time t);
230 // |low_water_mark_| is the first time in a sequence of adjusted
231 // times and |high_water_mark_| is the last.
232 base::Time low_water_mark_;
233 base::Time high_water_mark_;
236 // Classifies the given renderer navigation (see the NavigationType enum).
237 NavigationType ClassifyNavigation(
239 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
241 // Causes the controller to load the specified entry. The function assumes
242 // ownership of the pointer since it is put in the navigation list.
243 // NOTE: Do not pass an entry that the controller already owns!
244 void LoadEntry(NavigationEntryImpl* entry);
246 // Handlers for the different types of navigation types. They will actually
247 // handle the navigations corresponding to the different NavClasses above.
248 // They will NOT broadcast the commit notification, that should be handled by
251 // RendererDidNavigateAutoSubframe is special, it may not actually change
252 // anything if some random subframe is loaded. It will return true if anything
253 // changed, or false if not.
255 // The functions taking |did_replace_entry| will fill into the given variable
256 // whether the last entry has been replaced or not.
257 // See LoadCommittedDetails.did_replace_entry.
259 // TODO(creis): Change RenderViewHost to RenderFrameHost.
260 void RendererDidNavigateToNewPage(
262 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
264 void RendererDidNavigateToExistingPage(
266 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
267 void RendererDidNavigateToSamePage(
269 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
270 void RendererDidNavigateInPage(
272 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
273 bool* did_replace_entry);
274 void RendererDidNavigateNewSubframe(
276 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
277 bool RendererDidNavigateAutoSubframe(
279 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
281 // Helper function for code shared between Reload() and ReloadIgnoringCache().
282 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
284 // Actually issues the navigation held in pending_entry.
285 void NavigateToPendingEntry(ReloadType reload_type);
287 // Allows the derived class to issue notifications that a load has been
288 // committed. This will fill in the active entry to the details structure.
289 void NotifyNavigationEntryCommitted(LoadCommittedDetails* details);
291 // Updates the virtual URL of an entry to match a new URL, for cases where
292 // the real renderer URL is derived from the virtual URL, like view-source:
293 void UpdateVirtualURLToURL(NavigationEntryImpl* entry,
294 const GURL& new_url);
296 // Invoked after session/tab restore or cloning a tab. Resets the transition
297 // type of the entries, updates the max page id and creates the active
299 void FinishRestore(int selected_index, RestoreType type);
301 // Inserts a new entry or replaces the current entry with a new one, removing
302 // all entries after it. The new entry will become the active one.
303 void InsertOrReplaceEntry(NavigationEntryImpl* entry, bool replace);
305 // Removes the entry at |index|, as long as it is not the current entry.
306 void RemoveEntryAtIndexInternal(int index);
308 // Discards both the pending and transient entries.
309 void DiscardNonCommittedEntriesInternal();
311 // Discards only the transient entry.
312 void DiscardTransientEntry();
314 // If we have the maximum number of entries, remove the oldest one in
315 // preparation to add another.
316 void PruneOldestEntryIfFull();
318 // Removes all entries except the last committed entry. If there is a new
319 // pending navigation it is preserved. In contrast to
320 // PruneAllButLastCommitted() this does not update the session history of the
321 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns
322 // true before calling this.
323 void PruneAllButLastCommittedInternal();
325 // Returns true if the navigation is likley to be automatic rather than
327 bool IsLikelyAutoNavigation(base::TimeTicks now);
329 // Inserts up to |max_index| entries from |source| into this. This does NOT
330 // adjust any of the members that reference entries_
331 // (last_committed_entry_index_, pending_entry_index_ or
332 // transient_entry_index_).
333 void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index);
335 // Returns the navigation index that differs from the current entry by the
336 // specified |offset|. The index returned is not guaranteed to be valid.
337 int GetIndexForOffset(int offset) const;
339 // ---------------------------------------------------------------------------
341 // The user browser context associated with this controller.
342 BrowserContext* browser_context_;
344 // List of NavigationEntry for this tab
345 typedef std::vector<linked_ptr<NavigationEntryImpl> > NavigationEntries;
346 NavigationEntries entries_;
348 // An entry we haven't gotten a response for yet. This will be discarded
349 // when we navigate again. It's used only so we know what the currently
352 // This may refer to an item in the entries_ list if the pending_entry_index_
353 // == -1, or it may be its own entry that should be deleted. Be careful with
354 // the memory management.
355 NavigationEntryImpl* pending_entry_;
357 // currently visible entry
358 int last_committed_entry_index_;
360 // index of pending entry if it is in entries_, or -1 if pending_entry_ is a
361 // new entry (created by LoadURL).
362 int pending_entry_index_;
364 // The index for the entry that is shown until a navigation occurs. This is
365 // used for interstitial pages. -1 if there are no such entry.
366 // Note that this entry really appears in the list of entries, but only
367 // temporarily (until the next navigation). Any index pointing to an entry
368 // after the transient entry will become invalid if you navigate forward.
369 int transient_entry_index_;
371 // The delegate associated with the controller. Possibly NULL during
373 NavigationControllerDelegate* delegate_;
375 // The max restored page ID in this controller, if it was restored. We must
376 // store this so that WebContentsImpl can tell any renderer in charge of one
377 // of the restored entries to update its max page ID.
378 int32 max_restored_page_id_;
380 // Manages the SSL security UI.
381 SSLManager ssl_manager_;
383 // Whether we need to be reloaded when made active.
386 // Whether this is the initial navigation.
387 // Becomes false when initial navigation commits.
388 bool is_initial_navigation_;
390 // Used to find the appropriate SessionStorageNamespace for the storage
391 // partition of a NavigationEntry.
393 // A NavigationController may contain NavigationEntries that correspond to
394 // different StoragePartitions. Even though they are part of the same
395 // NavigationController, only entries in the same StoragePartition may
396 // share session storage state with one another.
397 SessionStorageNamespaceMap session_storage_namespace_map_;
399 // The maximum number of entries that a navigation controller can store.
400 static size_t max_entry_count_for_testing_;
402 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
403 // NO_RELOAD otherwise.
404 ReloadType pending_reload_;
406 // Used to get timestamps for newly-created navigation entries.
407 base::Callback<base::Time()> get_timestamp_callback_;
409 // Used to smooth out timestamps from |get_timestamp_callback_|.
410 // Without this, whenever there is a run of redirects or
411 // code-generated navigations, those navigations may occur within
412 // the timer resolution, leading to things sometimes showing up in
413 // the wrong order in the history view.
414 TimeSmoother time_smoother_;
416 scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_;
418 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl);
421 } // namespace content
423 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_