Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / frame_host / navigation_controller_impl.h
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
29     : public NON_EXPORTED_BASE(NavigationController) {
30  public:
31   NavigationControllerImpl(
32       NavigationControllerDelegate* delegate,
33       BrowserContext* browser_context);
34   ~NavigationControllerImpl() override;
35
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,
41                RestoreType type,
42                std::vector<NavigationEntry*>* entries) override;
43   NavigationEntry* GetActiveEntry() const override;
44   NavigationEntry* GetVisibleEntry() const override;
45   int GetCurrentEntryIndex() const override;
46   NavigationEntry* GetLastCommittedEntry() const override;
47   int GetLastCommittedEntryIndex() const override;
48   bool CanViewSource() const override;
49   int GetEntryCount() const override;
50   NavigationEntry* GetEntryAtIndex(int index) const override;
51   NavigationEntry* GetEntryAtOffset(int offset) const override;
52   void DiscardNonCommittedEntries() override;
53   NavigationEntry* GetPendingEntry() const override;
54   int GetPendingEntryIndex() const override;
55   NavigationEntry* 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()
72       const override;
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;
91
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;
95
96   // The session storage namespace that all child RenderViews belonging to
97   // |instance| should use.
98   SessionStorageNamespace* GetSessionStorageNamespace(
99       SiteInstance* instance);
100
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;
104
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;
109
110   // Return the entry with the corresponding instance and page_id, or NULL if
111   // not found.
112   NavigationEntryImpl* GetEntryWithPageID(
113       SiteInstance* instance,
114       int32 page_id) const;
115
116   NavigationControllerDelegate* delegate() const {
117     return delegate_;
118   }
119
120   // For use by WebContentsImpl ------------------------------------------------
121
122   // Allow renderer-initiated navigations to create a pending entry when the
123   // provisional load starts.
124   void SetPendingEntry(content::NavigationEntryImpl* entry);
125
126   // Handles updating the navigation state after the renderer has navigated.
127   // This is used by the WebContentsImpl.
128   //
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.
133   //
134   // In the case that nothing has changed, the details structure is undefined
135   // and it will return false.
136   bool RendererDidNavigate(
137       RenderFrameHost* rfh,
138       const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
139       LoadCommittedDetails* details);
140
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);
144
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.
150   //
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).
157   //
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(
163       const GURL& url,
164       bool renderer_says_in_page,
165       RenderFrameHost* rfh) const;
166
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
171   // of this API.
172   //
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);
178
179   // Random data ---------------------------------------------------------------
180
181   SSLManager* ssl_manager() { return &ssl_manager_; }
182
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;
186   }
187   static size_t max_entry_count();
188
189   void SetGetTimestampCallbackForTest(
190       const base::Callback<base::Time()>& get_timestamp_callback);
191
192   // Takes a screenshot of the page at the current state.
193   void TakeScreenshot();
194
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
199   // that.
200   void SetScreenshotManager(NavigationEntryScreenshotManager* manager);
201
202   // Discards only the pending entry.
203   void DiscardPendingEntry();
204
205  private:
206   friend class RestoreHelper;
207
208   FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
209                            PurgeScreenshot);
210   FRIEND_TEST_ALL_PREFIXES(TimeSmoother, Basic);
211   FRIEND_TEST_ALL_PREFIXES(TimeSmoother, SingleDuplicate);
212   FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ManyDuplicates);
213   FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ClockBackwardsJump);
214
215   // Helper class to smooth out runs of duplicate timestamps while still
216   // allowing time to jump backwards.
217   class CONTENT_EXPORT TimeSmoother {
218    public:
219     // Returns |t| with possibly some time added on.
220     base::Time GetSmoothedTime(base::Time t);
221
222    private:
223     // |low_water_mark_| is the first time in a sequence of adjusted
224     // times and |high_water_mark_| is the last.
225     base::Time low_water_mark_;
226     base::Time high_water_mark_;
227   };
228
229   // Classifies the given renderer navigation (see the NavigationType enum).
230   NavigationType ClassifyNavigation(
231       RenderFrameHost* rfh,
232       const FrameHostMsg_DidCommitProvisionalLoad_Params& params) const;
233
234   // Causes the controller to load the specified entry. The function assumes
235   // ownership of the pointer since it is put in the navigation list.
236   // NOTE: Do not pass an entry that the controller already owns!
237   void LoadEntry(NavigationEntryImpl* entry);
238
239   // Handlers for the different types of navigation types. They will actually
240   // handle the navigations corresponding to the different NavClasses above.
241   // They will NOT broadcast the commit notification, that should be handled by
242   // the caller.
243   //
244   // RendererDidNavigateAutoSubframe is special, it may not actually change
245   // anything if some random subframe is loaded. It will return true if anything
246   // changed, or false if not.
247   //
248   // The functions taking |did_replace_entry| will fill into the given variable
249   // whether the last entry has been replaced or not.
250   // See LoadCommittedDetails.did_replace_entry.
251   void RendererDidNavigateToNewPage(
252       RenderFrameHost* rfh,
253       const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
254       bool replace_entry);
255   void RendererDidNavigateToExistingPage(
256       RenderFrameHost* rfh,
257       const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
258   void RendererDidNavigateToSamePage(
259       RenderFrameHost* rfh,
260       const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
261   void RendererDidNavigateInPage(
262       RenderFrameHost* rfh,
263       const FrameHostMsg_DidCommitProvisionalLoad_Params& params,
264       bool* did_replace_entry);
265   void RendererDidNavigateNewSubframe(
266       RenderFrameHost* rfh,
267       const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
268   bool RendererDidNavigateAutoSubframe(
269       RenderFrameHost* rfh,
270       const FrameHostMsg_DidCommitProvisionalLoad_Params& params);
271
272   // Helper function for code shared between Reload() and ReloadIgnoringCache().
273   void ReloadInternal(bool check_for_repost, ReloadType reload_type);
274
275   // Actually issues the navigation held in pending_entry.
276   void NavigateToPendingEntry(ReloadType reload_type);
277
278   // Allows the derived class to issue notifications that a load has been
279   // committed. This will fill in the active entry to the details structure.
280   void NotifyNavigationEntryCommitted(LoadCommittedDetails* details);
281
282   // Updates the virtual URL of an entry to match a new URL, for cases where
283   // the real renderer URL is derived from the virtual URL, like view-source:
284   void UpdateVirtualURLToURL(NavigationEntryImpl* entry,
285                              const GURL& new_url);
286
287   // Invoked after session/tab restore or cloning a tab. Resets the transition
288   // type of the entries, updates the max page id and creates the active
289   // contents.
290   void FinishRestore(int selected_index, RestoreType type);
291
292   // Inserts a new entry or replaces the current entry with a new one, removing
293   // all entries after it. The new entry will become the active one.
294   void InsertOrReplaceEntry(NavigationEntryImpl* entry, bool replace);
295
296   // Removes the entry at |index|, as long as it is not the current entry.
297   void RemoveEntryAtIndexInternal(int index);
298
299   // Discards both the pending and transient entries.
300   void DiscardNonCommittedEntriesInternal();
301
302   // Discards only the transient entry.
303   void DiscardTransientEntry();
304
305   // If we have the maximum number of entries, remove the oldest one in
306   // preparation to add another.
307   void PruneOldestEntryIfFull();
308
309   // Removes all entries except the last committed entry.  If there is a new
310   // pending navigation it is preserved. In contrast to
311   // PruneAllButLastCommitted() this does not update the session history of the
312   // RenderView.  Callers must ensure that |CanPruneAllButLastCommitted| returns
313   // true before calling this.
314   void PruneAllButLastCommittedInternal();
315
316   // Returns true if the navigation is likley to be automatic rather than
317   // user-initiated.
318   bool IsLikelyAutoNavigation(base::TimeTicks now);
319
320   // Inserts up to |max_index| entries from |source| into this. This does NOT
321   // adjust any of the members that reference entries_
322   // (last_committed_entry_index_, pending_entry_index_ or
323   // transient_entry_index_).
324   void InsertEntriesFrom(const NavigationControllerImpl& source, int max_index);
325
326   // Returns the navigation index that differs from the current entry by the
327   // specified |offset|.  The index returned is not guaranteed to be valid.
328   int GetIndexForOffset(int offset) const;
329
330   // ---------------------------------------------------------------------------
331
332   // The user browser context associated with this controller.
333   BrowserContext* browser_context_;
334
335   // List of NavigationEntry for this tab
336   typedef std::vector<linked_ptr<NavigationEntryImpl> > NavigationEntries;
337   NavigationEntries entries_;
338
339   // An entry we haven't gotten a response for yet.  This will be discarded
340   // when we navigate again.  It's used only so we know what the currently
341   // displayed tab is.
342   //
343   // This may refer to an item in the entries_ list if the pending_entry_index_
344   // == -1, or it may be its own entry that should be deleted. Be careful with
345   // the memory management.
346   NavigationEntryImpl* pending_entry_;
347
348   // currently visible entry
349   int last_committed_entry_index_;
350
351   // index of pending entry if it is in entries_, or -1 if pending_entry_ is a
352   // new entry (created by LoadURL).
353   int pending_entry_index_;
354
355   // The index for the entry that is shown until a navigation occurs.  This is
356   // used for interstitial pages. -1 if there are no such entry.
357   // Note that this entry really appears in the list of entries, but only
358   // temporarily (until the next navigation).  Any index pointing to an entry
359   // after the transient entry will become invalid if you navigate forward.
360   int transient_entry_index_;
361
362   // The delegate associated with the controller. Possibly NULL during
363   // setup.
364   NavigationControllerDelegate* delegate_;
365
366   // The max restored page ID in this controller, if it was restored.  We must
367   // store this so that WebContentsImpl can tell any renderer in charge of one
368   // of the restored entries to update its max page ID.
369   int32 max_restored_page_id_;
370
371   // Manages the SSL security UI.
372   SSLManager ssl_manager_;
373
374   // Whether we need to be reloaded when made active.
375   bool needs_reload_;
376
377   // Whether this is the initial navigation.
378   // Becomes false when initial navigation commits.
379   bool is_initial_navigation_;
380
381   // Prevent unsafe re-entrant calls to NavigateToPendingEntry.
382   bool in_navigate_to_pending_entry_;
383
384   // Used to find the appropriate SessionStorageNamespace for the storage
385   // partition of a NavigationEntry.
386   //
387   // A NavigationController may contain NavigationEntries that correspond to
388   // different StoragePartitions. Even though they are part of the same
389   // NavigationController, only entries in the same StoragePartition may
390   // share session storage state with one another.
391   SessionStorageNamespaceMap session_storage_namespace_map_;
392
393   // The maximum number of entries that a navigation controller can store.
394   static size_t max_entry_count_for_testing_;
395
396   // If a repost is pending, its type (RELOAD or RELOAD_IGNORING_CACHE),
397   // NO_RELOAD otherwise.
398   ReloadType pending_reload_;
399
400   // Used to get timestamps for newly-created navigation entries.
401   base::Callback<base::Time()> get_timestamp_callback_;
402
403   // Used to smooth out timestamps from |get_timestamp_callback_|.
404   // Without this, whenever there is a run of redirects or
405   // code-generated navigations, those navigations may occur within
406   // the timer resolution, leading to things sometimes showing up in
407   // the wrong order in the history view.
408   TimeSmoother time_smoother_;
409
410   scoped_ptr<NavigationEntryScreenshotManager> screenshot_manager_;
411
412   DISALLOW_COPY_AND_ASSIGN(NavigationControllerImpl);
413 };
414
415 }  // namespace content
416
417 #endif  // CONTENT_BROWSER_FRAME_HOST_NAVIGATION_CONTROLLER_IMPL_H_