[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / frame_host / navigation_controller_impl.h
bloba5136d932bd05c2d43f79c316699c4e17e8e459b
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. Setting a
202 // NULL manager recreates the default screenshot manager and uses that.
203 void SetScreenshotManager(
204 scoped_ptr<NavigationEntryScreenshotManager> manager);
206 // Discards only the pending entry. |was_failure| should be set if the pending
207 // entry is being discarded because it failed to load.
208 void DiscardPendingEntry(bool was_failure);
210 private:
211 friend class RestoreHelper;
213 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
214 PurgeScreenshot);
215 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, Basic);
216 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, SingleDuplicate);
217 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ManyDuplicates);
218 FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ClockBackwardsJump);
220 // Used for identifying which frames need to navigate.
221 using FrameLoadVector =
222 std::vector<std::pair<FrameTreeNode*, FrameNavigationEntry*>>;
224 // Helper class to smooth out runs of duplicate timestamps while still
225 // allowing time to jump backwards.
226 class CONTENT_EXPORT TimeSmoother {
227 public:
228 // Returns |t| with possibly some time added on.
229 base::Time GetSmoothedTime(base::Time t);
231 private:
232 // |low_water_mark_| is the first time in a sequence of adjusted
233 // times and |high_water_mark_| is the last.
234 base::Time low_water_mark_;
235 base::Time high_water_mark_;
238 // Causes the controller to load the specified entry. The function assumes
239 // ownership of the pointer since it is put in the navigation list.
240 // NOTE: Do not pass an entry that the controller already owns!
241 void LoadEntry(scoped_ptr<NavigationEntryImpl> entry);
243 // Identifies which frames need to be navigated for the pending
244 // NavigationEntry and instructs their Navigator to navigate them. Returns
245 // whether any frame successfully started a navigation.
246 bool NavigateToPendingEntryInternal(ReloadType reload_type);
248 // Recursively identifies which frames need to be navigated for the pending
249 // NavigationEntry, starting at |frame| and exploring its children. Only used
250 // in --site-per-process.
251 void FindFramesToNavigate(FrameTreeNode* frame,
252 FrameLoadVector* sameDocumentLoads,
253 FrameLoadVector* differentDocumentLoads);
255 // Classifies the given renderer navigation (see the NavigationType enum).
256 NavigationType ClassifyNavigation(
257 RenderFrameHostImpl* rfh,
258 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
260 // Handlers for the different types of navigation types. They will actually
261 // handle the navigations corresponding to the different NavClasses above.
262 // They will NOT broadcast the commit notification, that should be handled by
263 // the caller.
265 // RendererDidNavigateAutoSubframe is special, it may not actually change
266 // anything if some random subframe is loaded. It will return true if anything
267 // changed, or false if not.
269 // The functions taking |did_replace_entry| will fill into the given variable
270 // whether the last entry has been replaced or not.
271 // See LoadCommittedDetails.did_replace_entry.
272 void RendererDidNavigateToNewPage(
273 RenderFrameHostImpl* rfh,
274 const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
275 bool replace_entry);
276 void RendererDidNavigateToExistingPage(
277 RenderFrameHostImpl* rfh,
278 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
279 void RendererDidNavigateToSamePage(
280 RenderFrameHostImpl* rfh,
281 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
282 void RendererDidNavigateNewSubframe(
283 RenderFrameHostImpl* rfh,
284 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
285 bool RendererDidNavigateAutoSubframe(
286 RenderFrameHostImpl* rfh,
287 const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
289 // Helper function for code shared between Reload() and ReloadIgnoringCache().
290 void ReloadInternal(bool check_for_repost, ReloadType reload_type);
292 // Actually issues the navigation held in pending_entry.
293 void NavigateToPendingEntry(ReloadType reload_type);
295 // Allows the derived class to issue notifications that a load has been
296 // committed. This will fill in the active entry to the details structure.
297 void NotifyNavigationEntryCommitted(LoadCommittedDetails* details);
299 // Updates the virtual URL of an entry to match a new URL, for cases where
300 // the real renderer URL is derived from the virtual URL, like view-source:
301 void UpdateVirtualURLToURL(NavigationEntryImpl* entry,
302 const GURL& new_url);
304 // Invoked after session/tab restore or cloning a tab. Resets the transition
305 // type of the entries, updates the max page id and creates the active
306 // contents.
307 void FinishRestore(int selected_index, RestoreType type);
309 // Inserts a new entry or replaces the current entry with a new one, removing
310 // all entries after it. The new entry will become the active one.
311 void InsertOrReplaceEntry(scoped_ptr<NavigationEntryImpl> entry,
312 bool replace);
314 // Removes the entry at |index|, as long as it is not the current entry.
315 void RemoveEntryAtIndexInternal(int index);
317 // Discards both the pending and transient entries.
318 void DiscardNonCommittedEntriesInternal();
320 // Discards only the transient entry.
321 void DiscardTransientEntry();
323 // If we have the maximum number of entries, remove the oldest one in
324 // preparation to add another.
325 void PruneOldestEntryIfFull();
327 // Removes all entries except the last committed entry. If there is a new
328 // pending navigation it is preserved. In contrast to
329 // PruneAllButLastCommitted() this does not update the session history of the
330 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns
331 // true before calling this.
332 void PruneAllButLastCommittedInternal();
334 // Returns true if the navigation is likley to be automatic rather than
335 // user-initiated.
336 bool IsLikelyAutoNavigation(base::TimeTicks now);
338 // Inserts up to |max_index| entries from |source| into this. This does NOT
339 // adjust any of the members that reference entries_
340 // (last_committed_entry_index_, pending_entry_index_ or
341 // transient_entry_index_).
342 void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index);
344 // Returns the navigation index that differs from the current entry by the
345 // specified |offset|. The index returned is not guaranteed to be valid.
346 int GetIndexForOffset(int offset) const;
348 // ---------------------------------------------------------------------------
350 // The user browser context associated with this controller.
351 BrowserContext* browser_context_;
353 // List of NavigationEntry for this tab
354 using NavigationEntries = ScopedVector<NavigationEntryImpl>;
355 NavigationEntries entries_;
357 // An entry we haven't gotten a response for yet. This will be discarded
358 // when we navigate again. It's used only so we know what the currently
359 // displayed tab is.
361 // This may refer to an item in the entries_ list if the pending_entry_index_
362 // == -1, or it may be its own entry that should be deleted. Be careful with
363 // the memory management.
364 NavigationEntryImpl* pending_entry_;
366 // If a new entry fails loading, details about it are temporarily held here
367 // until the error page is shown. These variables are only valid if
368 // |failed_pending_entry_id_| is not 0.
370 // TODO(avi): We need a better way to handle the connection between failed
371 // loads and the subsequent load of the error page. This current approach has
372 // issues: 1. This might hang around longer than we'd like if there is no
373 // error page loaded, and 2. This doesn't work very well for frames.
374 // http://crbug.com/474261
375 int failed_pending_entry_id_;
376 bool failed_pending_entry_should_replace_;
378 // The index of the currently visible entry.
379 int last_committed_entry_index_;
381 // The index of the pending entry if it is in entries_, or -1 if
382 // pending_entry_ is a new entry (created by LoadURL).
383 int pending_entry_index_;
385 // The index for the entry that is shown until a navigation occurs. This is
386 // used for interstitial pages. -1 if there are no such entry.
387 // Note that this entry really appears in the list of entries, but only
388 // temporarily (until the next navigation). Any index pointing to an entry
389 // after the transient entry will become invalid if you navigate forward.
390 int transient_entry_index_;
392 // The delegate associated with the controller. Possibly NULL during
393 // setup.
394 NavigationControllerDelegate* delegate_;
396 // The max restored page ID in this controller, if it was restored. We must
397 // store this so that WebContentsImpl can tell any renderer in charge of one
398 // of the restored entries to update its max page ID.
399 int32 max_restored_page_id_;
401 // Manages the SSL security UI.
402 SSLManager ssl_manager_;
404 // Whether we need to be reloaded when made active.
405 bool needs_reload_;
407 // Whether this is the initial navigation.
408 // Becomes false when initial navigation commits.
409 bool is_initial_navigation_;
411 // Prevent unsafe re-entrant calls to NavigateToPendingEntry.
412 bool in_navigate_to_pending_entry_;
414 // Used to find the appropriate SessionStorageNamespace for the storage
415 // partition of a NavigationEntry.
417 // A NavigationController may contain NavigationEntries that correspond to
418 // different StoragePartitions. Even though they are part of the same
419 // NavigationController, only entries in the same StoragePartition may
420 // share session storage state with one another.
421 SessionStorageNamespaceMap session_storage_namespace_map_;
423 // The maximum number of entries that a navigation controller can store.
424 static size_t max_entry_count_for_testing_;
426 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
427 // NO_RELOAD otherwise.
428 ReloadType pending_reload_;
430 // Used to get timestamps for newly-created navigation entries.
431 base::Callback<base::Time()> get_timestamp_callback_;
433 // Used to smooth out timestamps from |get_timestamp_callback_|.
434 // Without this, whenever there is a run of redirects or
435 // code-generated navigations, those navigations may occur within
436 // the timer resolution, leading to things sometimes showing up in
437 // the wrong order in the history view.
438 TimeSmoother time_smoother_;
440 scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_;
442 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl);
445 } // namespace content
447 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_