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. 4 5 #ifndef CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_ 6 #define CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_ 7 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" 18 19 struct FrameHostMsg_DidCommitProvisionalLoad_Params; 20 21 namespace content { 22 class NavigationEntryImpl; 23 class RenderViewHost; 24 class NavigationEntryScreenshotManager; 25 class SiteInstance; 26 struct LoadCommittedDetails; 27 28 class CONTENT_EXPORT NavigationControllerImpl NON_EXPORTED_BASE(NavigationController)29 : public NON_EXPORTED_BASE(NavigationController) { 30 public: 31 NavigationControllerImpl( 32 NavigationControllerDelegate* delegate, 33 BrowserContext* browser_context); 34 virtual ~NavigationControllerImpl(); 35 36 // NavigationController implementation: 37 virtual WebContents* GetWebContents() const OVERRIDE; 38 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 39 virtual void SetBrowserContext( 40 BrowserContext* browser_context) OVERRIDE; 41 virtual void Restore( 42 int selected_navigation, 43 RestoreType type, 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, 61 ui::PageTransition type, 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, 88 int index) OVERRIDE; 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; 96 97 // Whether this is the initial navigation in an unmodified new tab. In this 98 // case, we know there is no content displayed in the page. 99 bool IsUnmodifiedBlankTab() const; 100 101 // The session storage namespace that all child RenderViews belonging to 102 // |instance| should use. 103 SessionStorageNamespace* GetSessionStorageNamespace( 104 SiteInstance* instance); 105 106 // Returns the index of the specified entry, or -1 if entry is not contained 107 // in this NavigationController. 108 int GetIndexOfEntry(const NavigationEntryImpl* entry) const; 109 110 // Return the index of the entry with the corresponding instance and page_id, 111 // or -1 if not found. 112 int GetEntryIndexWithPageID(SiteInstance* instance, 113 int32 page_id) const; 114 115 // Return the entry with the corresponding instance and page_id, or NULL if 116 // not found. 117 NavigationEntryImpl* GetEntryWithPageID( 118 SiteInstance* instance, 119 int32 page_id) const; 120 121 NavigationControllerDelegate* delegate() const { 122 return delegate_; 123 } 124 125 // For use by WebContentsImpl ------------------------------------------------ 126 127 // Allow renderer-initiated navigations to create a pending entry when the 128 // provisional load starts. 129 void SetPendingEntry(content::NavigationEntryImpl* entry); 130 131 // Handles updating the navigation state after the renderer has navigated. 132 // This is used by the WebContentsImpl. 133 // 134 // If a new entry is created, it will return true and will have filled the 135 // given details structure and broadcast the NOTIFY_NAV_ENTRY_COMMITTED 136 // notification. The caller can then use the details without worrying about 137 // listening for the notification. 138 // 139 // In the case that nothing has changed, the details structure is undefined 140 // and it will return false. 141 bool RendererDidNavigate( 142 RenderFrameHost* rfh, 143 const FrameHostMsg_DidCommitProvisionalLoad_Params& params, 144 LoadCommittedDetails* details); 145 146 // Notifies us that we just became active. This is used by the WebContentsImpl 147 // so that we know to load URLs that were pending as "lazy" loads. 148 void SetActive(bool is_active); 149 150 // Returns true if the given URL would be an in-page navigation (i.e. only 151 // the reference fragment is different) from the "last committed entry". We do 152 // not compare it against the "active entry" since the active entry can be 153 // pending and in page navigations only happen on committed pages. If there 154 // is no last committed entry, then nothing will be in-page. 155 // 156 // Special note: if the URLs are the same, it does NOT automatically count as 157 // an in-page navigation. Neither does an input URL that has no ref, even if 158 // the rest is the same. This may seem weird, but when we're considering 159 // whether a navigation happened without loading anything, the same URL could 160 // be a reload, while only a different ref would be in-page (pages can't clear 161 // refs without reload, only change to "#" which we don't count as empty). 162 // 163 // The situation is made murkier by history.replaceState(), which could 164 // provide the same URL as part of an in-page navigation, not a reload. So 165 // we need to let the (untrustworthy) renderer resolve the ambiguity, but 166 // only when the URLs are on the same origin. 167 bool IsURLInPageNavigation( 168 const GURL& url, 169 bool renderer_says_in_page, 170 RenderFrameHost* rfh) const; 171 172 // Sets the SessionStorageNamespace for the given |partition_id|. This is 173 // used during initialization of a new NavigationController to allow 174 // pre-population of the SessionStorageNamespace objects. Session restore, 175 // prerendering, and the implementaion of window.open() are the primary users 176 // of this API. 177 // 178 // Calling this function when a SessionStorageNamespace has already been 179 // associated with a |partition_id| will CHECK() fail. 180 void SetSessionStorageNamespace( 181 const std::string& partition_id, 182 SessionStorageNamespace* session_storage_namespace); 183 184 // Random data --------------------------------------------------------------- 185 186 SSLManager* ssl_manager() { return &ssl_manager_; } 187 188 // Maximum number of entries before we start removing entries from the front. 189 static void set_max_entry_count_for_testing(size_t max_entry_count) { 190 max_entry_count_for_testing_ = max_entry_count; 191 } 192 static size_t max_entry_count(); 193 194 void SetGetTimestampCallbackForTest( 195 const base::Callback<base::Time()>& get_timestamp_callback); 196 197 // Takes a screenshot of the page at the current state. 198 void TakeScreenshot(); 199 200 // Sets the screenshot manager for this NavigationControllerImpl. The 201 // controller takes ownership of the screenshot manager and destroys it when 202 // a new screenshot-manager is set, or when the controller is destroyed. 203 // Setting a NULL manager recreates the default screenshot manager and uses 204 // that. 205 void SetScreenshotManager(NavigationEntryScreenshotManager* manager); 206 207 // Discards only the pending entry. 208 void DiscardPendingEntry(); 209 210 private: 211 friend class RestoreHelper; 212 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); 219 220 // Helper class to smooth out runs of duplicate timestamps while still 221 // allowing time to jump backwards. 222 class CONTENT_EXPORT TimeSmoother { 223 public: 224 // Returns |t| with possibly some time added on. 225 base::Time GetSmoothedTime(base::Time t); 226 227 private: 228 // |low_water_mark_| is the first time in a sequence of adjusted 229 // times and |high_water_mark_| is the last. 230 base::Time low_water_mark_; 231 base::Time high_water_mark_; 232 }; 233 234 // Classifies the given renderer navigation (see the NavigationType enum). 235 NavigationType ClassifyNavigation( 236 RenderFrameHost* rfh, 237 const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const; 238 239 // Causes the controller to load the specified entry. The function assumes 240 // ownership of the pointer since it is put in the navigation list. 241 // NOTE: Do not pass an entry that the controller already owns! 242 void LoadEntry(NavigationEntryImpl* entry); 243 244 // Handlers for the different types of navigation types. They will actually 245 // handle the navigations corresponding to the different NavClasses above. 246 // They will NOT broadcast the commit notification, that should be handled by 247 // the caller. 248 // 249 // RendererDidNavigateAutoSubframe is special, it may not actually change 250 // anything if some random subframe is loaded. It will return true if anything 251 // changed, or false if not. 252 // 253 // The functions taking |did_replace_entry| will fill into the given variable 254 // whether the last entry has been replaced or not. 255 // See LoadCommittedDetails.did_replace_entry. 256 void RendererDidNavigateToNewPage( 257 RenderFrameHost* rfh, 258 const FrameHostMsg_DidCommitProvisionalLoad_Params& params, 259 bool replace_entry); 260 void RendererDidNavigateToExistingPage( 261 RenderFrameHost* rfh, 262 const FrameHostMsg_DidCommitProvisionalLoad_Params& params); 263 void RendererDidNavigateToSamePage( 264 RenderFrameHost* rfh, 265 const FrameHostMsg_DidCommitProvisionalLoad_Params& params); 266 void RendererDidNavigateInPage( 267 RenderFrameHost* rfh, 268 const FrameHostMsg_DidCommitProvisionalLoad_Params& params, 269 bool* did_replace_entry); 270 void RendererDidNavigateNewSubframe( 271 RenderFrameHost* rfh, 272 const FrameHostMsg_DidCommitProvisionalLoad_Params& params); 273 bool RendererDidNavigateAutoSubframe( 274 RenderFrameHost* rfh, 275 const FrameHostMsg_DidCommitProvisionalLoad_Params& params); 276 277 // Helper function for code shared between Reload() and ReloadIgnoringCache(). 278 void ReloadInternal(bool check_for_repost, ReloadType reload_type); 279 280 // Actually issues the navigation held in pending_entry. 281 void NavigateToPendingEntry(ReloadType reload_type); 282 283 // Allows the derived class to issue notifications that a load has been 284 // committed. This will fill in the active entry to the details structure. 285 void NotifyNavigationEntryCommitted(LoadCommittedDetails* details); 286 287 // Updates the virtual URL of an entry to match a new URL, for cases where 288 // the real renderer URL is derived from the virtual URL, like view-source: 289 void UpdateVirtualURLToURL(NavigationEntryImpl* entry, 290 const GURL& new_url); 291 292 // Invoked after session/tab restore or cloning a tab. Resets the transition 293 // type of the entries, updates the max page id and creates the active 294 // contents. 295 void FinishRestore(int selected_index, RestoreType type); 296 297 // Inserts a new entry or replaces the current entry with a new one, removing 298 // all entries after it. The new entry will become the active one. 299 void InsertOrReplaceEntry(NavigationEntryImpl* entry, bool replace); 300 301 // Removes the entry at |index|, as long as it is not the current entry. 302 void RemoveEntryAtIndexInternal(int index); 303 304 // Discards both the pending and transient entries. 305 void DiscardNonCommittedEntriesInternal(); 306 307 // Discards only the transient entry. 308 void DiscardTransientEntry(); 309 310 // If we have the maximum number of entries, remove the oldest one in 311 // preparation to add another. 312 void PruneOldestEntryIfFull(); 313 314 // Removes all entries except the last committed entry. If there is a new 315 // pending navigation it is preserved. In contrast to 316 // PruneAllButLastCommitted() this does not update the session history of the 317 // RenderView. Callers must ensure that |CanPruneAllButLastCommitted| returns 318 // true before calling this. 319 void PruneAllButLastCommittedInternal(); 320 321 // Returns true if the navigation is likley to be automatic rather than 322 // user-initiated. 323 bool IsLikelyAutoNavigation(base::TimeTicks now); 324 325 // Inserts up to |max_index| entries from |source| into this. This does NOT 326 // adjust any of the members that reference entries_ 327 // (last_committed_entry_index_, pending_entry_index_ or 328 // transient_entry_index_). 329 void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index); 330 331 // Returns the navigation index that differs from the current entry by the 332 // specified |offset|. The index returned is not guaranteed to be valid. 333 int GetIndexForOffset(int offset) const; 334 335 // --------------------------------------------------------------------------- 336 337 // The user browser context associated with this controller. 338 BrowserContext* browser_context_; 339 340 // List of NavigationEntry for this tab 341 typedef std::vector<linked_ptr<NavigationEntryImpl> > NavigationEntries; 342 NavigationEntries entries_; 343 344 // An entry we haven't gotten a response for yet. This will be discarded 345 // when we navigate again. It's used only so we know what the currently 346 // displayed tab is. 347 // 348 // This may refer to an item in the entries_ list if the pending_entry_index_ 349 // == -1, or it may be its own entry that should be deleted. Be careful with 350 // the memory management. 351 NavigationEntryImpl* pending_entry_; 352 353 // currently visible entry 354 int last_committed_entry_index_; 355 356 // index of pending entry if it is in entries_, or -1 if pending_entry_ is a 357 // new entry (created by LoadURL). 358 int pending_entry_index_; 359 360 // The index for the entry that is shown until a navigation occurs. This is 361 // used for interstitial pages. -1 if there are no such entry. 362 // Note that this entry really appears in the list of entries, but only 363 // temporarily (until the next navigation). Any index pointing to an entry 364 // after the transient entry will become invalid if you navigate forward. 365 int transient_entry_index_; 366 367 // The delegate associated with the controller. Possibly NULL during 368 // setup. 369 NavigationControllerDelegate* delegate_; 370 371 // The max restored page ID in this controller, if it was restored. We must 372 // store this so that WebContentsImpl can tell any renderer in charge of one 373 // of the restored entries to update its max page ID. 374 int32 max_restored_page_id_; 375 376 // Manages the SSL security UI. 377 SSLManager ssl_manager_; 378 379 // Whether we need to be reloaded when made active. 380 bool needs_reload_; 381 382 // Whether this is the initial navigation. 383 // Becomes false when initial navigation commits. 384 bool is_initial_navigation_; 385 386 // Prevent unsafe re-entrant calls to NavigateToPendingEntry. 387 bool in_navigate_to_pending_entry_; 388 389 // Used to find the appropriate SessionStorageNamespace for the storage 390 // partition of a NavigationEntry. 391 // 392 // A NavigationController may contain NavigationEntries that correspond to 393 // different StoragePartitions. Even though they are part of the same 394 // NavigationController, only entries in the same StoragePartition may 395 // share session storage state with one another. 396 SessionStorageNamespaceMap session_storage_namespace_map_; 397 398 // The maximum number of entries that a navigation controller can store. 399 static size_t max_entry_count_for_testing_; 400 401 // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE), 402 // NO_RELOAD otherwise. 403 ReloadType pending_reload_; 404 405 // Used to get timestamps for newly-created navigation entries. 406 base::Callback<base::Time()> get_timestamp_callback_; 407 408 // Used to smooth out timestamps from |get_timestamp_callback_|. 409 // Without this, whenever there is a run of redirects or 410 // code-generated navigations, those navigations may occur within 411 // the timer resolution, leading to things sometimes showing up in 412 // the wrong order in the history view. 413 TimeSmoother time_smoother_; 414 415 scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_; 416 417 DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl); 418 }; 419 420 } // namespace content 421 422 #endif // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_ 423