Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_impl.h
blobe867d7fadb9df074ea20f6d0339cadad42a8572b
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 // Return the entry with the given unique id, or null if not found.
121 NavigationEntryImpl* GetEntryWithUniqueID(int nav_entry_id) const;
123 NavigationControllerDelegate* delegate() const {
124 return delegate_;
127 // For use by WebContentsImpl ------------------------------------------------
129 // Allow renderer-initiated navigations to create a pending entry when the
130 // provisional load starts.
131 void SetPendingEntry(scoped_ptr<NavigationEntryImpl> entry);
133 // Handles updating the navigation state after the renderer has navigated.
134 // This is used by the WebContentsImpl.
136 // If a new entry is created, it will return true and will have filled the
137 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED
138 // notification. The caller can then use the details without worrying about
139 // listening for the notification.
141 // In the case that nothing has changed, the details structure is undefined
142 // and it will return false.
143 bool RendererDidNavigate(
144 RenderFrameHostImpl* rfh,
145 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
146 LoadCommittedDetails* details);
148 // Notifies us that we just became active. This is used by the WebContentsImpl
149 // so that we know to load URLs that were pending as "lazy" loads.
150 void SetActive(bool is_active);
152 // Returns true if the given URL would be an in-page navigation (i.e. only the
153 // reference fragment is different) from the last committed URL in the
154 // specified frame. If there is no last committed entry, then nothing will be
155 // in-page.
157 // Special note: if the URLs are the same, it does NOT automatically count as
158 // an in-page navigation. Neither does an input URL that has no ref, even if
159 // the rest is the same. This may seem weird, but when we're considering
160 // whether a navigation happened without loading anything, the same URL could
161 // be a reload, while only a different ref would be in-page (pages can't clear
162 // refs without reload, only change to "#" which we don't count as empty).
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 to let the (untrustworthy) renderer resolve the ambiguity, but
167 // only when the URLs are on the same origin.
168 bool IsURLInPageNavigation(
169 const GURL& url,
170 bool renderer_says_in_page,
171 RenderFrameHost* rfh) const;
173 // Sets the SessionStorageNamespace for the given |partition_id|. This is
174 // used during initialization of a new NavigationController to allow
175 // pre-population of the SessionStorageNamespace objects. Session restore,
176 // prerendering, and the implementaion of window.open() are the primary users
177 // of this API.
179 // Calling this function when a SessionStorageNamespace has already been
180 // associated with a |partition_id| will CHECK() fail.
181 void SetSessionStorageNamespace(
182 const std::string& partition_id,
183 SessionStorageNamespace* session_storage_namespace);
185 // Random data ---------------------------------------------------------------
187 SSLManager* ssl_manager() { return &ssl_manager_; }
189 // Maximum number of entries before we start removing entries from the front.
190 static void set_max_entry_count_for_testing(size_t max_entry_count) {
191 max_entry_count_for_testing_ = max_entry_count;
193 static size_t max_entry_count();
195 void SetGetTimestampCallbackForTest(
196 const base::Callback<base::Time()>& get_timestamp_callback);
198 // Takes a screenshot of the page at the current state.
199 void TakeScreenshot();
201 // Sets the screenshot manager for this NavigationControllerImpl. The
202 // controller takes ownership of the screenshot manager and destroys it when
203 // a new screenshot-manager is set, or when the controller is destroyed.
204 // Setting a NULL manager recreates the default screenshot manager and uses
205 // that.
206 void SetScreenshotManager(NavigationEntryScreenshotManager* manager);
208 // Discards only the pending entry. |was_failure| should be set if the pending
209 // entry is being discarded because it failed to load.
210 void DiscardPendingEntry(bool was_failure);
212 private:
213 friend class RestoreHelper;
215 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
216 PurgeScreenshot);
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 // Used for identifying which frames need to navigate.
223 using FrameLoadVector =
224 std::vector<std::pair<FrameTreeNode*, FrameNavigationEntry*>>;
226 // Helper class to smooth out runs of duplicate timestamps while still
227 // allowing time to jump backwards.
228 class CONTENT_EXPORT TimeSmoother {
229 public:
230 // Returns |t| with possibly some time added on.
231 base::Time GetSmoothedTime(base::Time t);
233 private:
234 // |low_water_mark_| is the first time in a sequence of adjusted
235 // times and |high_water_mark_| is the last.
236 base::Time low_water_mark_;
237 base::Time high_water_mark_;
240 // Causes the controller to load the specified entry. The function assumes
241 // ownership of the pointer since it is put in the navigation list.
242 // NOTE: Do not pass an entry that the controller already owns!
243 void LoadEntry(scoped_ptr<NavigationEntryImpl> entry);
245 // Identifies which frames need to be navigated for the pending
246 // NavigationEntry and instructs their Navigator to navigate them. Returns
247 // whether any frame successfully started a navigation.
248 bool NavigateToPendingEntryInternal(ReloadType reload_type);
250 // Recursively identifies which frames need to be navigated for the pending
251 // NavigationEntry, starting at |frame| and exploring its children. Only used
252 // in --site-per-process.
253 void FindFramesToNavigate(FrameTreeNode* frame,
254 FrameLoadVector* sameDocumentLoads,
255 FrameLoadVector* differentDocumentLoads);
257 // Classifies the given renderer navigation (see the NavigationType enum).
258 NavigationType ClassifyNavigation(
259 RenderFrameHostImpl* rfh,
260 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
262 // Handlers for the different types of navigation types. They will actually
263 // handle the navigations corresponding to the different NavClasses above.
264 // They will NOT broadcast the commit notification, that should be handled by
265 // the caller.
267 // RendererDidNavigateAutoSubframe is special, it may not actually change
268 // anything if some random subframe is loaded. It will return true if anything
269 // changed, or false if not.
271 // The functions taking |did_replace_entry| will fill into the given variable
272 // whether the last entry has been replaced or not.
273 // See LoadCommittedDetails.did_replace_entry.
274 void RendererDidNavigateToNewPage(
275 RenderFrameHostImpl* rfh,
276 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
277 bool replace_entry);
278 void RendererDidNavigateToExistingPage(
279 RenderFrameHostImpl* rfh,
280 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
281 void RendererDidNavigateToSamePage(
282 RenderFrameHostImpl* rfh,
283 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
284 void RendererDidNavigateNewSubframe(
285 RenderFrameHostImpl* rfh,
286 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
287 bool RendererDidNavigateAutoSubframe(
288 RenderFrameHostImpl* rfh,
289 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
291 // Helper function for code shared between Reload() and ReloadIgnoringCache().
292 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
294 // Actually issues the navigation held in pending_entry.
295 void NavigateToPendingEntry(ReloadType reload_type);
297 // Allows the derived class to issue notifications that a load has been
298 // committed. This will fill in the active entry to the details structure.
299 void NotifyNavigationEntryCommitted(LoadCommittedDetails* details);
301 // Updates the virtual URL of an entry to match a new URL, for cases where
302 // the real renderer URL is derived from the virtual URL, like view-source:
303 void UpdateVirtualURLToURL(NavigationEntryImpl* entry,
304 const GURL& new_url);
306 // Invoked after session/tab restore or cloning a tab. Resets the transition
307 // type of the entries, updates the max page id and creates the active
308 // contents.
309 void FinishRestore(int selected_index, RestoreType type);
311 // Inserts a new entry or replaces the current entry with a new one, removing
312 // all entries after it. The new entry will become the active one.
313 void InsertOrReplaceEntry(scoped_ptr<NavigationEntryImpl> entry,
314 bool replace);
316 // Removes the entry at |index|, as long as it is not the current entry.
317 void RemoveEntryAtIndexInternal(int index);
319 // Discards both the pending and transient entries.
320 void DiscardNonCommittedEntriesInternal();
322 // Discards only the transient entry.
323 void DiscardTransientEntry();
325 // If we have the maximum number of entries, remove the oldest one in
326 // preparation to add another.
327 void PruneOldestEntryIfFull();
329 // Removes all entries except the last committed entry. If there is a new
330 // pending navigation it is preserved. In contrast to
331 // PruneAllButLastCommitted() this does not update the session history of the
332 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns
333 // true before calling this.
334 void PruneAllButLastCommittedInternal();
336 // Returns true if the navigation is likley to be automatic rather than
337 // user-initiated.
338 bool IsLikelyAutoNavigation(base::TimeTicks now);
340 // Inserts up to |max_index| entries from |source| into this. This does NOT
341 // adjust any of the members that reference entries_
342 // (last_committed_entry_index_, pending_entry_index_ or
343 // transient_entry_index_).
344 void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index);
346 // Returns the navigation index that differs from the current entry by the
347 // specified |offset|. The index returned is not guaranteed to be valid.
348 int GetIndexForOffset(int offset) const;
350 // ---------------------------------------------------------------------------
352 // The user browser context associated with this controller.
353 BrowserContext* browser_context_;
355 // List of NavigationEntry for this tab
356 using NavigationEntries = ScopedVector<NavigationEntryImpl>;
357 NavigationEntries entries_;
359 // An entry we haven't gotten a response for yet. This will be discarded
360 // when we navigate again. It's used only so we know what the currently
361 // displayed tab is.
363 // This may refer to an item in the entries_ list if the pending_entry_index_
364 // == -1, or it may be its own entry that should be deleted. Be careful with
365 // the memory management.
366 NavigationEntryImpl* pending_entry_;
368 // If a new entry fails loading, details about it are temporarily held here
369 // until the error page is shown. These variables are only valid if
370 // |failed_pending_entry_id_| is not 0.
372 // TODO(avi): We need a better way to handle the connection between failed
373 // loads and the subsequent load of the error page. This current approach has
374 // issues: 1. This might hang around longer than we'd like if there is no
375 // error page loaded, and 2. This doesn't work very well for frames.
376 // http://crbug.com/474261
377 int failed_pending_entry_id_;
378 bool failed_pending_entry_should_replace_;
380 // The index of the currently visible entry.
381 int last_committed_entry_index_;
383 // The index of the pending entry if it is in entries_, or -1 if
384 // pending_entry_ is a new entry (created by LoadURL).
385 int pending_entry_index_;
387 // The index for the entry that is shown until a navigation occurs. This is
388 // used for interstitial pages. -1 if there are no such entry.
389 // Note that this entry really appears in the list of entries, but only
390 // temporarily (until the next navigation). Any index pointing to an entry
391 // after the transient entry will become invalid if you navigate forward.
392 int transient_entry_index_;
394 // The delegate associated with the controller. Possibly NULL during
395 // setup.
396 NavigationControllerDelegate* delegate_;
398 // The max restored page ID in this controller, if it was restored. We must
399 // store this so that WebContentsImpl can tell any renderer in charge of one
400 // of the restored entries to update its max page ID.
401 int32 max_restored_page_id_;
403 // Manages the SSL security UI.
404 SSLManager ssl_manager_;
406 // Whether we need to be reloaded when made active.
407 bool needs_reload_;
409 // Whether this is the initial navigation.
410 // Becomes false when initial navigation commits.
411 bool is_initial_navigation_;
413 // Prevent unsafe re-entrant calls to NavigateToPendingEntry.
414 bool in_navigate_to_pending_entry_;
416 // Used to find the appropriate SessionStorageNamespace for the storage
417 // partition of a NavigationEntry.
419 // A NavigationController may contain NavigationEntries that correspond to
420 // different StoragePartitions. Even though they are part of the same
421 // NavigationController, only entries in the same StoragePartition may
422 // share session storage state with one another.
423 SessionStorageNamespaceMap session_storage_namespace_map_;
425 // The maximum number of entries that a navigation controller can store.
426 static size_t max_entry_count_for_testing_;
428 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
429 // NO_RELOAD otherwise.
430 ReloadType pending_reload_;
432 // Used to get timestamps for newly-created navigation entries.
433 base::Callback<base::Time()> get_timestamp_callback_;
435 // Used to smooth out timestamps from |get_timestamp_callback_|.
436 // Without this, whenever there is a run of redirects or
437 // code-generated navigations, those navigations may occur within
438 // the timer resolution, leading to things sometimes showing up in
439 // the wrong order in the history view.
440 TimeSmoother time_smoother_;
442 scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_;
444 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl);
447 } // namespace content
449 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_