Use scoped_ptr for NavigationEntries.
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_impl.h
blob0d057e5f5a88ef0e50ef1fb5e8a00126aaa9a5dd
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/scoped_vector.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;
22 namespace content {
23 class FrameTreeNode;
24 class RenderFrameHostImpl;
25 class NavigationEntryScreenshotManager;
26 class SiteInstance;
27 struct LoadCommittedDetails;
29 class CONTENT_EXPORT NavigationControllerImpl
30 : public NON_EXPORTED_BASE(NavigationController) {
31 public:
32 NavigationControllerImpl(
33 NavigationControllerDelegate* delegate,
34 BrowserContext* browser_context);
35 ~NavigationControllerImpl() override;
37 // NavigationController implementation:
38 WebContents* GetWebContents() const override;
39 BrowserContext* GetBrowserContext() const override;
40 void SetBrowserContext(BrowserContext* browser_context) override;
41 void Restore(int selected_navigation,
42 RestoreType type,
43 ScopedVector<NavigationEntry>* entries) override;
44 NavigationEntryImpl* GetActiveEntry() const override;
45 NavigationEntryImpl* GetVisibleEntry() const override;
46 int GetCurrentEntryIndex() const override;
47 NavigationEntryImpl* GetLastCommittedEntry() const override;
48 int GetLastCommittedEntryIndex() const override;
49 bool CanViewSource() const override;
50 int GetEntryCount() const override;
51 NavigationEntryImpl* GetEntryAtIndex(int index) const override;
52 NavigationEntryImpl* GetEntryAtOffset(int offset) const override;
53 void DiscardNonCommittedEntries() override;
54 NavigationEntryImpl* GetPendingEntry() const override;
55 int GetPendingEntryIndex() const override;
56 NavigationEntryImpl* GetTransientEntry() const override;
57 void SetTransientEntry(scoped_ptr<NavigationEntry> entry) override;
58 void LoadURL(const GURL& url,
59 const Referrer& referrer,
60 ui::PageTransition type,
61 const std::string& extra_headers) override;
62 void LoadURLWithParams(const LoadURLParams& params) override;
63 void LoadIfNecessary() override;
64 bool CanGoBack() const override;
65 bool CanGoForward() const override;
66 bool CanGoToOffset(int offset) const override;
67 void GoBack() override;
68 void GoForward() override;
69 void GoToIndex(int index) override;
70 void GoToOffset(int offset) override;
71 bool RemoveEntryAtIndex(int index) override;
72 const SessionStorageNamespaceMap& GetSessionStorageNamespaceMap()
73 const override;
74 SessionStorageNamespace* GetDefaultSessionStorageNamespace() override;
75 void SetMaxRestoredPageID(int32 max_id) override;
76 int32 GetMaxRestoredPageID() const override;
77 bool NeedsReload() const override;
78 void SetNeedsReload() override;
79 void CancelPendingReload() override;
80 void ContinuePendingReload() override;
81 bool IsInitialNavigation() const override;
82 void Reload(bool check_for_repost) override;
83 void ReloadIgnoringCache(bool check_for_repost) override;
84 void ReloadOriginalRequestURL(bool check_for_repost) override;
85 void NotifyEntryChanged(const NavigationEntry* entry) override;
86 void CopyStateFrom(const NavigationController& source) override;
87 void CopyStateFromAndPrune(NavigationController* source,
88 bool replace_entry) override;
89 bool CanPruneAllButLastCommitted() override;
90 void PruneAllButLastCommitted() override;
91 void ClearAllScreenshots() override;
93 // Whether this is the initial navigation in an unmodified new tab. In this
94 // case, we know there is no content displayed in the page.
95 bool IsUnmodifiedBlankTab() const;
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 index of the entry with the given unique id, or -1 if not found.
112 int GetEntryIndexWithUniqueID(int nav_entry_id) const;
114 // Return the entry with the corresponding instance and page_id, or NULL if
115 // not found.
116 NavigationEntryImpl* GetEntryWithPageID(
117 SiteInstance* instance,
118 int32 page_id) const;
120 // Whether the given frame has committed any navigations yet.
121 // This currently only returns true in --site-per-process mode.
122 // TODO(creis): Create FrameNavigationEntries by default so this always works.
123 bool HasCommittedRealLoad(FrameTreeNode* frame_tree_node) const;
125 NavigationControllerDelegate* delegate() const {
126 return delegate_;
129 // For use by WebContentsImpl ------------------------------------------------
131 // Allow renderer-initiated navigations to create a pending entry when the
132 // provisional load starts.
133 void SetPendingEntry(scoped_ptr<NavigationEntryImpl> entry);
135 // Handles updating the navigation state after the renderer has navigated.
136 // This is used by the WebContentsImpl.
138 // If a new entry is created, it will return true and will have filled the
139 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED
140 // notification. The caller can then use the details without worrying about
141 // listening for the notification.
143 // In the case that nothing has changed, the details structure is undefined
144 // and it will return false.
145 bool RendererDidNavigate(
146 RenderFrameHostImpl* rfh,
147 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
148 LoadCommittedDetails* details);
150 // Notifies us that we just became active. This is used by the WebContentsImpl
151 // so that we know to load URLs that were pending as "lazy" loads.
152 void SetActive(bool is_active);
154 // Returns true if the given URL would be an in-page navigation (i.e. only the
155 // reference fragment is different) from the last committed URL in the
156 // specified frame. If there is no last committed entry, then nothing will be
157 // in-page.
159 // Special note: if the URLs are the same, it does NOT automatically count as
160 // an in-page navigation. Neither does an input URL that has no ref, even if
161 // the rest is the same. This may seem weird, but when we're considering
162 // whether a navigation happened without loading anything, the same URL could
163 // be a reload, while only a different ref would be in-page (pages can't clear
164 // refs without reload, only change to "#" which we don't count as empty).
166 // The situation is made murkier by history.replaceState(), which could
167 // provide the same URL as part of an in-page navigation, not a reload. So
168 // we need to let the (untrustworthy) renderer resolve the ambiguity, but
169 // only when the URLs are on the same origin.
170 bool IsURLInPageNavigation(
171 const GURL& url,
172 bool renderer_says_in_page,
173 RenderFrameHost* rfh) const;
175 // Sets the SessionStorageNamespace for the given |partition_id|. This is
176 // used during initialization of a new NavigationController to allow
177 // pre-population of the SessionStorageNamespace objects. Session restore,
178 // prerendering, and the implementaion of window.open() are the primary users
179 // of this API.
181 // Calling this function when a SessionStorageNamespace has already been
182 // associated with a |partition_id| will CHECK() fail.
183 void SetSessionStorageNamespace(
184 const std::string& partition_id,
185 SessionStorageNamespace* session_storage_namespace);
187 // Random data ---------------------------------------------------------------
189 SSLManager* ssl_manager() { return &ssl_manager_; }
191 // Maximum number of entries before we start removing entries from the front.
192 static void set_max_entry_count_for_testing(size_t max_entry_count) {
193 max_entry_count_for_testing_ = max_entry_count;
195 static size_t max_entry_count();
197 void SetGetTimestampCallbackForTest(
198 const base::Callback<base::Time()>& get_timestamp_callback);
200 // Takes a screenshot of the page at the current state.
201 void TakeScreenshot();
203 // Sets the screenshot manager for this NavigationControllerImpl. The
204 // controller takes ownership of the screenshot manager and destroys it when
205 // a new screenshot-manager is set, or when the controller is destroyed.
206 // Setting a NULL manager recreates the default screenshot manager and uses
207 // that.
208 void SetScreenshotManager(NavigationEntryScreenshotManager* manager);
210 // Discards only the pending entry. |was_failure| should be set if the pending
211 // entry is being discarded because it failed to load.
212 void DiscardPendingEntry(bool was_failure);
214 private:
215 friend class RestoreHelper;
217 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
218 PurgeScreenshot);
219 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, Basic);
220 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, SingleDuplicate);
221 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ManyDuplicates);
222 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ClockBackwardsJump);
224 // Used for identifying which frames need to navigate.
225 using FrameLoadVector =
226 std::vector<std::pair<FrameTreeNode*, FrameNavigationEntry*>>;
228 // Helper class to smooth out runs of duplicate timestamps while still
229 // allowing time to jump backwards.
230 class CONTENT_EXPORT TimeSmoother {
231 public:
232 // Returns |t| with possibly some time added on.
233 base::Time GetSmoothedTime(base::Time t);
235 private:
236 // |low_water_mark_| is the first time in a sequence of adjusted
237 // times and |high_water_mark_| is the last.
238 base::Time low_water_mark_;
239 base::Time high_water_mark_;
242 // Causes the controller to load the specified entry. The function assumes
243 // ownership of the pointer since it is put in the navigation list.
244 // NOTE: Do not pass an entry that the controller already owns!
245 void LoadEntry(scoped_ptr<NavigationEntryImpl> entry);
247 // Identifies which frames need to be navigated for the pending
248 // NavigationEntry and instructs their Navigator to navigate them. Returns
249 // whether any frame successfully started a navigation.
250 bool NavigateToPendingEntryInternal(ReloadType reload_type);
252 // Recursively identifies which frames need to be navigated for the pending
253 // NavigationEntry, starting at |frame| and exploring its children. Only used
254 // in --site-per-process.
255 void FindFramesToNavigate(FrameTreeNode* frame,
256 FrameLoadVector* sameDocumentLoads,
257 FrameLoadVector* differentDocumentLoads);
259 // Classifies the given renderer navigation (see the NavigationType enum).
260 NavigationType ClassifyNavigation(
261 RenderFrameHostImpl* rfh,
262 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
263 // This does the same as above (hopefully), but does so without any use of
264 // deprecated page id values. Once it bakes and is verified to behave the
265 // same, it will replace it. http://crbug.com/369661
266 NavigationType ClassifyNavigationWithoutPageID(
267 RenderFrameHostImpl* rfh,
268 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
270 // Handlers for the different types of navigation types. They will actually
271 // handle the navigations corresponding to the different NavClasses above.
272 // They will NOT broadcast the commit notification, that should be handled by
273 // the caller.
275 // RendererDidNavigateAutoSubframe is special, it may not actually change
276 // anything if some random subframe is loaded. It will return true if anything
277 // changed, or false if not.
279 // The functions taking |did_replace_entry| will fill into the given variable
280 // whether the last entry has been replaced or not.
281 // See LoadCommittedDetails.did_replace_entry.
282 void RendererDidNavigateToNewPage(
283 RenderFrameHostImpl* rfh,
284 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
285 bool replace_entry);
286 void RendererDidNavigateToExistingPage(
287 RenderFrameHostImpl* rfh,
288 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
289 void RendererDidNavigateToSamePage(
290 RenderFrameHostImpl* rfh,
291 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
292 void RendererDidNavigateInPage(
293 RenderFrameHostImpl* rfh,
294 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
295 bool* did_replace_entry);
296 void RendererDidNavigateNewSubframe(
297 RenderFrameHostImpl* rfh,
298 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
299 bool RendererDidNavigateAutoSubframe(
300 RenderFrameHostImpl* rfh,
301 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
303 // Helper function for code shared between Reload() and ReloadIgnoringCache().
304 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
306 // Actually issues the navigation held in pending_entry.
307 void NavigateToPendingEntry(ReloadType reload_type);
309 // Allows the derived class to issue notifications that a load has been
310 // committed. This will fill in the active entry to the details structure.
311 void NotifyNavigationEntryCommitted(LoadCommittedDetails* details);
313 // Updates the virtual URL of an entry to match a new URL, for cases where
314 // the real renderer URL is derived from the virtual URL, like view-source:
315 void UpdateVirtualURLToURL(NavigationEntryImpl* entry,
316 const GURL& new_url);
318 // Invoked after session/tab restore or cloning a tab. Resets the transition
319 // type of the entries, updates the max page id and creates the active
320 // contents.
321 void FinishRestore(int selected_index, RestoreType type);
323 // Inserts a new entry or replaces the current entry with a new one, removing
324 // all entries after it. The new entry will become the active one.
325 void InsertOrReplaceEntry(scoped_ptr<NavigationEntryImpl> entry,
326 bool replace);
328 // Removes the entry at |index|, as long as it is not the current entry.
329 void RemoveEntryAtIndexInternal(int index);
331 // Discards both the pending and transient entries.
332 void DiscardNonCommittedEntriesInternal();
334 // Discards only the transient entry.
335 void DiscardTransientEntry();
337 // If we have the maximum number of entries, remove the oldest one in
338 // preparation to add another.
339 void PruneOldestEntryIfFull();
341 // Removes all entries except the last committed entry. If there is a new
342 // pending navigation it is preserved. In contrast to
343 // PruneAllButLastCommitted() this does not update the session history of the
344 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns
345 // true before calling this.
346 void PruneAllButLastCommittedInternal();
348 // Returns true if the navigation is likley to be automatic rather than
349 // user-initiated.
350 bool IsLikelyAutoNavigation(base::TimeTicks now);
352 // Inserts up to |max_index| entries from |source| into this. This does NOT
353 // adjust any of the members that reference entries_
354 // (last_committed_entry_index_, pending_entry_index_ or
355 // transient_entry_index_).
356 void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index);
358 // Returns the navigation index that differs from the current entry by the
359 // specified |offset|. The index returned is not guaranteed to be valid.
360 int GetIndexForOffset(int offset) const;
362 // ---------------------------------------------------------------------------
364 // The user browser context associated with this controller.
365 BrowserContext* browser_context_;
367 // List of NavigationEntry for this tab
368 using NavigationEntries = ScopedVector<NavigationEntryImpl>;
369 NavigationEntries entries_;
371 // An entry we haven't gotten a response for yet. This will be discarded
372 // when we navigate again. It's used only so we know what the currently
373 // displayed tab is.
375 // This may refer to an item in the entries_ list if the pending_entry_index_
376 // == -1, or it may be its own entry that should be deleted. Be careful with
377 // the memory management.
378 NavigationEntryImpl* pending_entry_;
380 // If a new entry fails loading, details about it are temporarily held here
381 // until the error page is shown. These variables are only valid if
382 // |failed_pending_entry_id_| is not 0.
384 // TODO(avi): We need a better way to handle the connection between failed
385 // loads and the subsequent load of the error page. This current approach has
386 // issues: 1. This might hang around longer than we'd like if there is no
387 // error page loaded, and 2. This doesn't work very well for frames.
388 // http://crbug.com/474261
389 int failed_pending_entry_id_;
390 bool failed_pending_entry_should_replace_;
392 // The index of the currently visible entry.
393 int last_committed_entry_index_;
395 // The index of the pending entry if it is in entries_, or -1 if
396 // pending_entry_ is a new entry (created by LoadURL).
397 int pending_entry_index_;
399 // The index for the entry that is shown until a navigation occurs. This is
400 // used for interstitial pages. -1 if there are no such entry.
401 // Note that this entry really appears in the list of entries, but only
402 // temporarily (until the next navigation). Any index pointing to an entry
403 // after the transient entry will become invalid if you navigate forward.
404 int transient_entry_index_;
406 // The delegate associated with the controller. Possibly NULL during
407 // setup.
408 NavigationControllerDelegate* delegate_;
410 // The max restored page ID in this controller, if it was restored. We must
411 // store this so that WebContentsImpl can tell any renderer in charge of one
412 // of the restored entries to update its max page ID.
413 int32 max_restored_page_id_;
415 // Manages the SSL security UI.
416 SSLManager ssl_manager_;
418 // Whether we need to be reloaded when made active.
419 bool needs_reload_;
421 // Whether this is the initial navigation.
422 // Becomes false when initial navigation commits.
423 bool is_initial_navigation_;
425 // Prevent unsafe re-entrant calls to NavigateToPendingEntry.
426 bool in_navigate_to_pending_entry_;
428 // Used to find the appropriate SessionStorageNamespace for the storage
429 // partition of a NavigationEntry.
431 // A NavigationController may contain NavigationEntries that correspond to
432 // different StoragePartitions. Even though they are part of the same
433 // NavigationController, only entries in the same StoragePartition may
434 // share session storage state with one another.
435 SessionStorageNamespaceMap session_storage_namespace_map_;
437 // The maximum number of entries that a navigation controller can store.
438 static size_t max_entry_count_for_testing_;
440 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
441 // NO_RELOAD otherwise.
442 ReloadType pending_reload_;
444 // Used to get timestamps for newly-created navigation entries.
445 base::Callback<base::Time()> get_timestamp_callback_;
447 // Used to smooth out timestamps from |get_timestamp_callback_|.
448 // Without this, whenever there is a run of redirects or
449 // code-generated navigations, those navigations may occur within
450 // the timer resolution, leading to things sometimes showing up in
451 // the wrong order in the history view.
452 TimeSmoother time_smoother_;
454 scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_;
456 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl);
459 } // namespace content
461 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_