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/frame_host/navigation_entry_impl.h"
16 #include "content/browser/ssl/ssl_manager.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_type.h"
20 struct FrameHostMsg_DidCommitProvisionalLoad_Params
;
23 class RenderFrameHostImpl
;
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 ~NavigationControllerImpl() override
;
36 // NavigationController implementation:
37 WebContents
* GetWebContents() const override
;
38 BrowserContext
* GetBrowserContext() const override
;
39 void SetBrowserContext(BrowserContext
* browser_context
) override
;
40 void Restore(int selected_navigation
,
42 std::vector
<NavigationEntry
*>* entries
) override
;
43 NavigationEntryImpl
* GetActiveEntry() const override
;
44 NavigationEntryImpl
* GetVisibleEntry() const override
;
45 int GetCurrentEntryIndex() const override
;
46 NavigationEntryImpl
* GetLastCommittedEntry() const override
;
47 int GetLastCommittedEntryIndex() const override
;
48 bool CanViewSource() const override
;
49 int GetEntryCount() const override
;
50 NavigationEntryImpl
* GetEntryAtIndex(int index
) const override
;
51 NavigationEntryImpl
* GetEntryAtOffset(int offset
) const override
;
52 void DiscardNonCommittedEntries() override
;
53 NavigationEntryImpl
* GetPendingEntry() const override
;
54 int GetPendingEntryIndex() const override
;
55 NavigationEntryImpl
* GetTransientEntry() const override
;
56 void SetTransientEntry(NavigationEntry
* entry
) override
;
57 void LoadURL(const GURL
& url
,
58 const Referrer
& referrer
,
59 ui::PageTransition type
,
60 const std::string
& extra_headers
) override
;
61 void LoadURLWithParams(const LoadURLParams
& params
) override
;
62 void LoadIfNecessary() override
;
63 bool CanGoBack() const override
;
64 bool CanGoForward() const override
;
65 bool CanGoToOffset(int offset
) const override
;
66 void GoBack() override
;
67 void GoForward() override
;
68 void GoToIndex(int index
) override
;
69 void GoToOffset(int offset
) override
;
70 bool RemoveEntryAtIndex(int index
) override
;
71 const SessionStorageNamespaceMap
& GetSessionStorageNamespaceMap()
73 SessionStorageNamespace
* GetDefaultSessionStorageNamespace() override
;
74 void SetMaxRestoredPageID(int32 max_id
) override
;
75 int32
GetMaxRestoredPageID() const override
;
76 bool NeedsReload() const override
;
77 void SetNeedsReload() override
;
78 void CancelPendingReload() override
;
79 void ContinuePendingReload() override
;
80 bool IsInitialNavigation() const override
;
81 void Reload(bool check_for_repost
) override
;
82 void ReloadIgnoringCache(bool check_for_repost
) override
;
83 void ReloadOriginalRequestURL(bool check_for_repost
) override
;
84 void NotifyEntryChanged(const NavigationEntry
* entry
, int index
) override
;
85 void CopyStateFrom(const NavigationController
& source
) override
;
86 void CopyStateFromAndPrune(NavigationController
* source
,
87 bool replace_entry
) override
;
88 bool CanPruneAllButLastCommitted() override
;
89 void PruneAllButLastCommitted() override
;
90 void ClearAllScreenshots() override
;
92 // Whether this is the initial navigation in an unmodified new tab. In this
93 // case, we know there is no content displayed in the page.
94 bool IsUnmodifiedBlankTab() const;
96 // The session storage namespace that all child RenderViews belonging to
97 // |instance| should use.
98 SessionStorageNamespace
* GetSessionStorageNamespace(
99 SiteInstance
* instance
);
101 // Returns the index of the specified entry, or -1 if entry is not contained
102 // in this NavigationController.
103 int GetIndexOfEntry(const NavigationEntryImpl
* entry
) const;
105 // Return the index of the entry with the corresponding instance and page_id,
106 // or -1 if not found.
107 int GetEntryIndexWithPageID(SiteInstance
* instance
,
108 int32 page_id
) const;
110 // Return the entry with the corresponding instance and page_id, or NULL if
112 NavigationEntryImpl
* GetEntryWithPageID(
113 SiteInstance
* instance
,
114 int32 page_id
) const;
116 NavigationControllerDelegate
* delegate() const {
120 // For use by WebContentsImpl ------------------------------------------------
122 // Allow renderer-initiated navigations to create a pending entry when the
123 // provisional load starts.
124 void SetPendingEntry(content::NavigationEntryImpl
* entry
);
126 // Handles updating the navigation state after the renderer has navigated.
127 // This is used by the WebContentsImpl.
129 // If a new entry is created, it will return true and will have filled the
130 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED
131 // notification. The caller can then use the details without worrying about
132 // listening for the notification.
134 // In the case that nothing has changed, the details structure is undefined
135 // and it will return false.
136 bool RendererDidNavigate(
137 RenderFrameHostImpl
* rfh
,
138 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
,
139 LoadCommittedDetails
* details
);
141 // Notifies us that we just became active. This is used by the WebContentsImpl
142 // so that we know to load URLs that were pending as "lazy" loads.
143 void SetActive(bool is_active
);
145 // Returns true if the given URL would be an in-page navigation (i.e. only
146 // the reference fragment is different) from the "last committed entry". We do
147 // not compare it against the "active entry" since the active entry can be
148 // pending and in page navigations only happen on committed pages. If there
149 // is no last committed entry, then nothing will be in-page.
151 // Special note: if the URLs are the same, it does NOT automatically count as
152 // an in-page navigation. Neither does an input URL that has no ref, even if
153 // the rest is the same. This may seem weird, but when we're considering
154 // whether a navigation happened without loading anything, the same URL could
155 // be a reload, while only a different ref would be in-page (pages can't clear
156 // refs without reload, only change to "#" which we don't count as empty).
158 // The situation is made murkier by history.replaceState(), which could
159 // provide the same URL as part of an in-page navigation, not a reload. So
160 // we need to let the (untrustworthy) renderer resolve the ambiguity, but
161 // only when the URLs are on the same origin.
162 bool IsURLInPageNavigation(
164 bool renderer_says_in_page
,
165 RenderFrameHost
* rfh
) const;
167 // Sets the SessionStorageNamespace for the given |partition_id|. This is
168 // used during initialization of a new NavigationController to allow
169 // pre-population of the SessionStorageNamespace objects. Session restore,
170 // prerendering, and the implementaion of window.open() are the primary users
173 // Calling this function when a SessionStorageNamespace has already been
174 // associated with a |partition_id| will CHECK() fail.
175 void SetSessionStorageNamespace(
176 const std::string
& partition_id
,
177 SessionStorageNamespace
* session_storage_namespace
);
179 // Random data ---------------------------------------------------------------
181 SSLManager
* ssl_manager() { return &ssl_manager_
; }
183 // Maximum number of entries before we start removing entries from the front.
184 static void set_max_entry_count_for_testing(size_t max_entry_count
) {
185 max_entry_count_for_testing_
= max_entry_count
;
187 static size_t max_entry_count();
189 void SetGetTimestampCallbackForTest(
190 const base::Callback
<base::Time()>& get_timestamp_callback
);
192 // Takes a screenshot of the page at the current state.
193 void TakeScreenshot();
195 // Sets the screenshot manager for this NavigationControllerImpl. The
196 // controller takes ownership of the screenshot manager and destroys it when
197 // a new screenshot-manager is set, or when the controller is destroyed.
198 // Setting a NULL manager recreates the default screenshot manager and uses
200 void SetScreenshotManager(NavigationEntryScreenshotManager
* manager
);
202 // Discards only the pending entry. |was_failure| should be set if the pending
203 // entry is being discarded because it failed to load.
204 void DiscardPendingEntry(bool was_failure
);
207 friend class RestoreHelper
;
209 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest
,
211 FRIEND_TEST_ALL_PREFIXES(TimeSmoother
, Basic
);
212 FRIEND_TEST_ALL_PREFIXES(TimeSmoother
, SingleDuplicate
);
213 FRIEND_TEST_ALL_PREFIXES(TimeSmoother
, ManyDuplicates
);
214 FRIEND_TEST_ALL_PREFIXES(TimeSmoother
, ClockBackwardsJump
);
216 // Helper class to smooth out runs of duplicate timestamps while still
217 // allowing time to jump backwards.
218 class CONTENT_EXPORT TimeSmoother
{
220 // Returns |t| with possibly some time added on.
221 base::Time
GetSmoothedTime(base::Time t
);
224 // |low_water_mark_| is the first time in a sequence of adjusted
225 // times and |high_water_mark_| is the last.
226 base::Time low_water_mark_
;
227 base::Time high_water_mark_
;
230 // Classifies the given renderer navigation (see the NavigationType enum).
231 NavigationType
ClassifyNavigation(
232 RenderFrameHostImpl
* rfh
,
233 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
) const;
235 // Causes the controller to load the specified entry. The function assumes
236 // ownership of the pointer since it is put in the navigation list.
237 // NOTE: Do not pass an entry that the controller already owns!
238 void LoadEntry(NavigationEntryImpl
* entry
);
240 // Handlers for the different types of navigation types. They will actually
241 // handle the navigations corresponding to the different NavClasses above.
242 // They will NOT broadcast the commit notification, that should be handled by
245 // RendererDidNavigateAutoSubframe is special, it may not actually change
246 // anything if some random subframe is loaded. It will return true if anything
247 // changed, or false if not.
249 // The functions taking |did_replace_entry| will fill into the given variable
250 // whether the last entry has been replaced or not.
251 // See LoadCommittedDetails.did_replace_entry.
252 void RendererDidNavigateToNewPage(
253 RenderFrameHostImpl
* rfh
,
254 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
,
256 void RendererDidNavigateToExistingPage(
257 RenderFrameHostImpl
* rfh
,
258 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
);
259 void RendererDidNavigateToSamePage(
260 RenderFrameHostImpl
* rfh
,
261 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
);
262 void RendererDidNavigateInPage(
263 RenderFrameHostImpl
* rfh
,
264 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
,
265 bool* did_replace_entry
);
266 void RendererDidNavigateNewSubframe(
267 RenderFrameHostImpl
* rfh
,
268 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
);
269 bool RendererDidNavigateAutoSubframe(
270 RenderFrameHostImpl
* rfh
,
271 const FrameHostMsg_DidCommitProvisionalLoad_Params
& params
);
273 // Helper function for code shared between Reload() and ReloadIgnoringCache().
274 void ReloadInternal(bool check_for_repost
, ReloadType reload_type
);
276 // Actually issues the navigation held in pending_entry.
277 void NavigateToPendingEntry(ReloadType reload_type
);
279 // Allows the derived class to issue notifications that a load has been
280 // committed. This will fill in the active entry to the details structure.
281 void NotifyNavigationEntryCommitted(LoadCommittedDetails
* details
);
283 // Updates the virtual URL of an entry to match a new URL, for cases where
284 // the real renderer URL is derived from the virtual URL, like view-source:
285 void UpdateVirtualURLToURL(NavigationEntryImpl
* entry
,
286 const GURL
& new_url
);
288 // Invoked after session/tab restore or cloning a tab. Resets the transition
289 // type of the entries, updates the max page id and creates the active
291 void FinishRestore(int selected_index
, RestoreType type
);
293 // Inserts a new entry or replaces the current entry with a new one, removing
294 // all entries after it. The new entry will become the active one.
295 void InsertOrReplaceEntry(NavigationEntryImpl
* entry
, bool replace
);
297 // Removes the entry at |index|, as long as it is not the current entry.
298 void RemoveEntryAtIndexInternal(int index
);
300 // Discards both the pending and transient entries.
301 void DiscardNonCommittedEntriesInternal();
303 // Discards only the transient entry.
304 void DiscardTransientEntry();
306 // If we have the maximum number of entries, remove the oldest one in
307 // preparation to add another.
308 void PruneOldestEntryIfFull();
310 // Removes all entries except the last committed entry. If there is a new
311 // pending navigation it is preserved. In contrast to
312 // PruneAllButLastCommitted() this does not update the session history of the
313 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns
314 // true before calling this.
315 void PruneAllButLastCommittedInternal();
317 // Returns true if the navigation is likley to be automatic rather than
319 bool IsLikelyAutoNavigation(base::TimeTicks now
);
321 // Inserts up to |max_index| entries from |source| into this. This does NOT
322 // adjust any of the members that reference entries_
323 // (last_committed_entry_index_, pending_entry_index_ or
324 // transient_entry_index_).
325 void InsertEntriesFrom(const NavigationControllerImpl
& source
, int max_index
);
327 // Returns the navigation index that differs from the current entry by the
328 // specified |offset|. The index returned is not guaranteed to be valid.
329 int GetIndexForOffset(int offset
) const;
331 // ---------------------------------------------------------------------------
333 // The user browser context associated with this controller.
334 BrowserContext
* browser_context_
;
336 // List of NavigationEntry for this tab
337 typedef std::vector
<linked_ptr
<NavigationEntryImpl
> > NavigationEntries
;
338 NavigationEntries entries_
;
340 // An entry we haven't gotten a response for yet. This will be discarded
341 // when we navigate again. It's used only so we know what the currently
344 // This may refer to an item in the entries_ list if the pending_entry_index_
345 // == -1, or it may be its own entry that should be deleted. Be careful with
346 // the memory management.
347 NavigationEntryImpl
* pending_entry_
;
349 // If a new entry fails loading, details about it are temporarily held here
350 // until the error page is shown. These variables are only valid if
351 // |failed_pending_entry_id_| is not 0.
353 // TODO(avi): We need a better way to handle the connection between failed
354 // loads and the subsequent load of the error page. This current approach has
355 // issues: 1. This might hang around longer than we'd like if there is no
356 // error page loaded, and 2. This doesn't work very well for frames.
357 // http://crbug.com/474261
358 int failed_pending_entry_id_
;
359 bool failed_pending_entry_should_replace_
;
361 // The index of the currently visible entry.
362 int last_committed_entry_index_
;
364 // The index of the pending entry if it is in entries_, or -1 if
365 // pending_entry_ is a new entry (created by LoadURL).
366 int pending_entry_index_
;
368 // The index for the entry that is shown until a navigation occurs. This is
369 // used for interstitial pages. -1 if there are no such entry.
370 // Note that this entry really appears in the list of entries, but only
371 // temporarily (until the next navigation). Any index pointing to an entry
372 // after the transient entry will become invalid if you navigate forward.
373 int transient_entry_index_
;
375 // The delegate associated with the controller. Possibly NULL during
377 NavigationControllerDelegate
* delegate_
;
379 // The max restored page ID in this controller, if it was restored. We must
380 // store this so that WebContentsImpl can tell any renderer in charge of one
381 // of the restored entries to update its max page ID.
382 int32 max_restored_page_id_
;
384 // Manages the SSL security UI.
385 SSLManager ssl_manager_
;
387 // Whether we need to be reloaded when made active.
390 // Whether this is the initial navigation.
391 // Becomes false when initial navigation commits.
392 bool is_initial_navigation_
;
394 // Prevent unsafe re-entrant calls to NavigateToPendingEntry.
395 bool in_navigate_to_pending_entry_
;
397 // Used to find the appropriate SessionStorageNamespace for the storage
398 // partition of a NavigationEntry.
400 // A NavigationController may contain NavigationEntries that correspond to
401 // different StoragePartitions. Even though they are part of the same
402 // NavigationController, only entries in the same StoragePartition may
403 // share session storage state with one another.
404 SessionStorageNamespaceMap session_storage_namespace_map_
;
406 // The maximum number of entries that a navigation controller can store.
407 static size_t max_entry_count_for_testing_
;
409 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
410 // NO_RELOAD otherwise.
411 ReloadType pending_reload_
;
413 // Used to get timestamps for newly-created navigation entries.
414 base::Callback
<base::Time()> get_timestamp_callback_
;
416 // Used to smooth out timestamps from |get_timestamp_callback_|.
417 // Without this, whenever there is a run of redirects or
418 // code-generated navigations, those navigations may occur within
419 // the timer resolution, leading to things sometimes showing up in
420 // the wrong order in the history view.
421 TimeSmoother time_smoother_
;
423 scoped_ptr
<NavigationEntryScreenshotManager
> screenshot_manager_
;
425 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl
);
428 } // namespace content
430 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_