Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / prerender / prerender_manager.h
blob63874784bcf20624532404e5f0b712b866208e52
1 // Copyright (c) 2012 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 CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_
6 #define CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_
8 #include <list>
9 #include <string>
10 #include <vector>
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
20 #include "chrome/browser/predictors/logged_in_predictor_table.h"
21 #include "chrome/browser/prerender/prerender_config.h"
22 #include "chrome/browser/prerender/prerender_contents.h"
23 #include "chrome/browser/prerender/prerender_final_status.h"
24 #include "chrome/browser/prerender/prerender_histograms.h"
25 #include "chrome/browser/prerender/prerender_origin.h"
26 #include "components/keyed_service/core/keyed_service.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/render_process_host_observer.h"
30 #include "net/cookies/cookie_monster.h"
31 #include "url/gurl.h"
33 class Profile;
34 class InstantSearchPrerendererTest;
35 struct ChromeCookieDetails;
37 namespace base {
38 class DictionaryValue;
41 namespace chrome {
42 struct NavigateParams;
45 namespace content {
46 class WebContents;
49 namespace gfx {
50 class Size;
53 namespace prerender {
55 class PrerenderHandle;
56 class PrerenderHistory;
57 class PrerenderLocalPredictor;
59 // PrerenderManager is responsible for initiating and keeping prerendered
60 // views of web pages. All methods must be called on the UI thread unless
61 // indicated otherwise.
62 class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>,
63 public base::NonThreadSafe,
64 public content::NotificationObserver,
65 public content::RenderProcessHostObserver,
66 public KeyedService,
67 public MediaCaptureDevicesDispatcher::Observer {
68 public:
69 // NOTE: New values need to be appended, since they are used in histograms.
70 enum PrerenderManagerMode {
71 PRERENDER_MODE_DISABLED = 0,
72 PRERENDER_MODE_ENABLED = 1,
73 PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP = 2,
74 PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP = 3,
75 // Obsolete: PRERENDER_MODE_EXPERIMENT_5MIN_TTL_GROUP = 4,
76 PRERENDER_MODE_EXPERIMENT_NO_USE_GROUP = 5,
77 PRERENDER_MODE_EXPERIMENT_MULTI_PRERENDER_GROUP = 6,
78 PRERENDER_MODE_EXPERIMENT_15MIN_TTL_GROUP = 7,
79 PRERENDER_MODE_EXPERIMENT_MATCH_COMPLETE_GROUP = 8,
80 PRERENDER_MODE_MAX
83 // One or more of these flags must be passed to ClearData() to specify just
84 // what data to clear. See function declaration for more information.
85 enum ClearFlags {
86 CLEAR_PRERENDER_CONTENTS = 0x1 << 0,
87 CLEAR_PRERENDER_HISTORY = 0x1 << 1,
88 CLEAR_MAX = 0x1 << 2
91 typedef predictors::LoggedInPredictorTable::LoggedInStateMap LoggedInStateMap;
93 // ID indicating that no experiment is active.
94 static const uint8 kNoExperiment = 0;
96 // Owned by a Profile object for the lifetime of the profile.
97 explicit PrerenderManager(Profile* profile);
99 ~PrerenderManager() override;
101 // From KeyedService:
102 void Shutdown() override;
104 // Entry points for adding prerenders.
106 // Adds a prerender for |url| if valid. |process_id| and |route_id| identify
107 // the RenderView that the prerender request came from. If |size| is empty, a
108 // default from the PrerenderConfig is used. Returns a caller-owned
109 // PrerenderHandle* if the URL was added, NULL if it was not. If the launching
110 // RenderView is itself prerendering, the prerender is added as a pending
111 // prerender.
112 PrerenderHandle* AddPrerenderFromLinkRelPrerender(
113 int process_id,
114 int route_id,
115 const GURL& url,
116 uint32 rel_types,
117 const content::Referrer& referrer,
118 const gfx::Size& size);
120 // Adds a prerender for |url| if valid. As the prerender request is coming
121 // from a source without a RenderFrameHost (i.e., the omnibox) we don't have a
122 // child or route id, or a referrer. This method uses sensible values for
123 // those. The |session_storage_namespace| matches the namespace of the active
124 // tab at the time the prerender is generated from the omnibox. Returns a
125 // caller-owned PrerenderHandle*, or NULL.
126 PrerenderHandle* AddPrerenderFromOmnibox(
127 const GURL& url,
128 content::SessionStorageNamespace* session_storage_namespace,
129 const gfx::Size& size);
131 PrerenderHandle* AddPrerenderFromLocalPredictor(
132 const GURL& url,
133 content::SessionStorageNamespace* session_storage_namespace,
134 const gfx::Size& size);
136 PrerenderHandle* AddPrerenderFromExternalRequest(
137 const GURL& url,
138 const content::Referrer& referrer,
139 content::SessionStorageNamespace* session_storage_namespace,
140 const gfx::Size& size);
142 // Adds a prerender for Instant Search |url| if valid. The
143 // |session_storage_namespace| matches the namespace of the active tab at the
144 // time the prerender is generated. Returns a caller-owned PrerenderHandle* or
145 // NULL.
146 PrerenderHandle* AddPrerenderForInstant(
147 const GURL& url,
148 content::SessionStorageNamespace* session_storage_namespace,
149 const gfx::Size& size);
151 // Cancels all active prerenders.
152 void CancelAllPrerenders();
154 // If |url| matches a valid prerendered page and |params| are compatible, try
155 // to swap it and merge browsing histories. Returns |true| and updates
156 // |params->target_contents| if a prerendered page is swapped in, |false|
157 // otherwise.
158 bool MaybeUsePrerenderedPage(const GURL& url,
159 chrome::NavigateParams* params);
161 // Moves a PrerenderContents to the pending delete list from the list of
162 // active prerenders when prerendering should be cancelled.
163 virtual void MoveEntryToPendingDelete(PrerenderContents* entry,
164 FinalStatus final_status);
166 // Records the page load time for a prerender that wasn't swapped in.
167 void RecordPageLoadTimeNotSwappedIn(Origin origin,
168 base::TimeDelta page_load_time,
169 const GURL& url);
171 // Records the perceived page load time for a page - effectively the time from
172 // when the user navigates to a page to when it finishes loading. The actual
173 // load may have started prior to navigation due to prerender hints.
174 // This must be called on the UI thread.
175 // |fraction_plt_elapsed_at_swap_in| must either be in [0.0, 1.0], or a value
176 // outside that range indicating that it doesn't apply.
177 void RecordPerceivedPageLoadTime(
178 Origin origin,
179 NavigationType navigation_type,
180 base::TimeDelta perceived_page_load_time,
181 double fraction_plt_elapsed_at_swap_in,
182 const GURL& url);
184 static PrerenderManagerMode GetMode();
185 static void SetMode(PrerenderManagerMode mode);
186 static const char* GetModeString();
187 static bool IsPrerenderingPossible();
188 static bool ActuallyPrerendering();
189 static bool IsControlGroup(uint8 experiment_id);
190 static bool IsNoUseGroup();
192 // Query the list of current prerender pages to see if the given web contents
193 // is prerendering a page. The optional parameter |origin| is an output
194 // parameter which, if a prerender is found, is set to the Origin of the
195 // prerender |web_contents|.
196 bool IsWebContentsPrerendering(const content::WebContents* web_contents,
197 Origin* origin) const;
199 // Whether the PrerenderManager has an active prerender with the given url and
200 // SessionStorageNamespace associated with the given WebContens.
201 bool HasPrerenderedUrl(GURL url, content::WebContents* web_contents) const;
203 // Returns the PrerenderContents object for the given web_contents, otherwise
204 // returns NULL. Note that the PrerenderContents may have been Destroy()ed,
205 // but not yet deleted.
206 PrerenderContents* GetPrerenderContents(
207 const content::WebContents* web_contents) const;
209 // Returns the PrerenderContents object for a given child_id, route_id pair,
210 // otherwise returns NULL. Note that the PrerenderContents may have been
211 // Destroy()ed, but not yet deleted.
212 virtual PrerenderContents* GetPrerenderContentsForRoute(
213 int child_id, int route_id) const;
215 // Returns a list of all WebContents being prerendered.
216 const std::vector<content::WebContents*> GetAllPrerenderingContents() const;
218 // Checks whether |url| has been recently navigated to.
219 bool HasRecentlyBeenNavigatedTo(Origin origin, const GURL& url);
221 // Returns true iff the method given is valid for prerendering.
222 static bool IsValidHttpMethod(const std::string& method);
224 // Returns true iff the scheme of the URL given is valid for prerendering.
225 static bool DoesURLHaveValidScheme(const GURL& url);
227 // Returns true iff the scheme of the subresource URL given is valid for
228 // prerendering.
229 static bool DoesSubresourceURLHaveValidScheme(const GURL& url);
231 // Returns a Value object containing the active pages being prerendered, and
232 // a history of pages which were prerendered. The caller is responsible for
233 // deleting the return value.
234 base::DictionaryValue* GetAsValue() const;
236 // Clears the data indicated by which bits of clear_flags are set.
238 // If the CLEAR_PRERENDER_CONTENTS bit is set, all active prerenders are
239 // cancelled and then deleted, and any WebContents queued for destruction are
240 // destroyed as well.
242 // If the CLEAR_PRERENDER_HISTORY bit is set, the prerender history is
243 // cleared, including any entries newly created by destroying them in
244 // response to the CLEAR_PRERENDER_CONTENTS flag.
246 // Intended to be used when clearing the cache or history.
247 void ClearData(int clear_flags);
249 // Record a final status of a prerendered page in a histogram.
250 // This variation allows specifying whether prerendering had been started
251 // (necessary to flag MatchComplete dummies).
252 void RecordFinalStatusWithMatchCompleteStatus(
253 Origin origin,
254 uint8 experiment_id,
255 PrerenderContents::MatchCompleteStatus mc_status,
256 FinalStatus final_status) const;
258 // content::NotificationObserver
259 void Observe(int type,
260 const content::NotificationSource& source,
261 const content::NotificationDetails& details) override;
263 // MediaCaptureDevicesDispatcher::Observer
264 void OnCreatingAudioStream(int render_process_id,
265 int render_frame_id) override;
267 const Config& config() const { return config_; }
268 Config& mutable_config() { return config_; }
270 // Records that some visible tab navigated (or was redirected) to the
271 // provided URL.
272 void RecordNavigation(const GURL& url);
274 // Updates the LoggedInPredictor state to reflect that a login has likely
275 // on the URL provided.
276 void RecordLikelyLoginOnURL(const GURL& url);
278 // Checks if the LoggedInPredictor shows that the user is likely logged on
279 // to the site for the URL provided.
280 void CheckIfLikelyLoggedInOnURL(const GURL& url,
281 bool* lookup_result,
282 bool* database_was_present,
283 const base::Closure& result_cb);
285 Profile* profile() const { return profile_; }
287 // Classes which will be tested in prerender unit browser tests should use
288 // these methods to get times for comparison, so that the test framework can
289 // mock advancing/retarding time.
290 virtual base::Time GetCurrentTime() const;
291 virtual base::TimeTicks GetCurrentTimeTicks() const;
293 scoped_refptr<predictors::LoggedInPredictorTable>
294 logged_in_predictor_table() {
295 return logged_in_predictor_table_;
298 PrerenderLocalPredictor* local_predictor() {
299 return local_predictor_.get();
302 // Notification that a prerender has completed and its bytes should be
303 // recorded.
304 void RecordNetworkBytes(Origin origin, bool used, int64 prerender_bytes);
306 // Returns whether prerendering is currently enabled for this manager.
307 bool IsEnabled() const;
309 // Add to the running tally of bytes transferred over the network for this
310 // profile if prerendering is currently enabled.
311 void AddProfileNetworkBytesIfEnabled(int64 bytes);
313 // Registers a new ProcessHost performing a prerender. Called by
314 // PrerenderContents.
315 void AddPrerenderProcessHost(content::RenderProcessHost* process_host);
317 // Returns whether or not |process_host| may be reused for new navigations
318 // from a prerendering perspective. Currently, if Prerender Cookie Stores are
319 // enabled, prerenders must be in their own processes that may not be shared.
320 bool MayReuseProcessHost(content::RenderProcessHost* process_host);
322 // content::RenderProcessHostObserver implementation.
323 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
325 protected:
326 class PrerenderData : public base::SupportsWeakPtr<PrerenderData> {
327 public:
328 struct OrderByExpiryTime;
330 PrerenderData(PrerenderManager* manager,
331 PrerenderContents* contents,
332 base::TimeTicks expiry_time);
334 ~PrerenderData();
336 // Turn this PrerenderData into a Match Complete replacement for itself,
337 // placing the current prerender contents into |to_delete_prerenders_|.
338 void MakeIntoMatchCompleteReplacement();
340 // A new PrerenderHandle has been created for this PrerenderData.
341 void OnHandleCreated(PrerenderHandle* prerender_handle);
343 // The launcher associated with a handle is navigating away from the context
344 // that launched this prerender. If the prerender is active, it may stay
345 // alive briefly though, in case we we going through a redirect chain that
346 // will eventually land at it.
347 void OnHandleNavigatedAway(PrerenderHandle* prerender_handle);
349 // The launcher associated with a handle has taken explicit action to cancel
350 // this prerender. We may well destroy the prerender in this case if no
351 // other handles continue to track it.
352 void OnHandleCanceled(PrerenderHandle* prerender_handle);
354 PrerenderContents* contents() { return contents_.get(); }
356 PrerenderContents* ReleaseContents();
358 int handle_count() const { return handle_count_; }
360 base::TimeTicks abandon_time() const { return abandon_time_; }
362 base::TimeTicks expiry_time() const { return expiry_time_; }
363 void set_expiry_time(base::TimeTicks expiry_time) {
364 expiry_time_ = expiry_time;
367 private:
368 PrerenderManager* manager_;
369 scoped_ptr<PrerenderContents> contents_;
371 // The number of distinct PrerenderHandles created for |this|, including
372 // ones that have called PrerenderData::OnHandleNavigatedAway(), but not
373 // counting the ones that have called PrerenderData::OnHandleCanceled(). For
374 // pending prerenders, this will always be 1, since the PrerenderManager
375 // only merges handles of running prerenders.
376 int handle_count_;
378 // The time when OnHandleNavigatedAway was called.
379 base::TimeTicks abandon_time_;
381 // After this time, this prerender is no longer fresh, and should be
382 // removed.
383 base::TimeTicks expiry_time_;
385 DISALLOW_COPY_AND_ASSIGN(PrerenderData);
388 void SetPrerenderContentsFactory(
389 PrerenderContents::Factory* prerender_contents_factory);
391 // Called by a PrerenderData to signal that the launcher has navigated away
392 // from the context that launched the prerender. A user may have clicked
393 // a link in a page containing a <link rel=prerender> element, or the user
394 // might have committed an omnibox navigation. This is used to possibly
395 // shorten the TTL of the prerendered page.
396 void SourceNavigatedAway(PrerenderData* prerender_data);
398 private:
399 friend class ::InstantSearchPrerendererTest;
400 friend class PrerenderBrowserTest;
401 friend class PrerenderContents;
402 friend class PrerenderHandle;
403 friend class UnitTestPrerenderManager;
405 class OnCloseWebContentsDeleter;
406 struct NavigationRecord;
408 // Time interval before a new prerender is allowed.
409 static const int kMinTimeBetweenPrerendersMs = 500;
411 // Time window for which we record old navigations, in milliseconds.
412 static const int kNavigationRecordWindowMs = 5000;
414 void OnCancelPrerenderHandle(PrerenderData* prerender_data);
416 // Adds a prerender for |url| from |referrer|. The |origin| specifies how the
417 // prerender was added. If |size| is empty, then
418 // PrerenderContents::StartPrerendering will instead use a default from
419 // PrerenderConfig. Returns a PrerenderHandle*, owned by the caller, or NULL.
420 PrerenderHandle* AddPrerender(
421 Origin origin,
422 const GURL& url,
423 const content::Referrer& referrer,
424 const gfx::Size& size,
425 content::SessionStorageNamespace* session_storage_namespace);
427 void StartSchedulingPeriodicCleanups();
428 void StopSchedulingPeriodicCleanups();
430 void EvictOldestPrerendersIfNecessary();
432 // Deletes stale and cancelled prerendered PrerenderContents, as well as
433 // WebContents that have been replaced by prerendered WebContents.
434 // Also identifies and kills PrerenderContents that use too much
435 // resources.
436 void PeriodicCleanup();
438 // Posts a task to call PeriodicCleanup. Results in quicker destruction of
439 // objects. If |this| is deleted before the task is run, the task will
440 // automatically be cancelled.
441 void PostCleanupTask();
443 base::TimeTicks GetExpiryTimeForNewPrerender(Origin origin) const;
444 base::TimeTicks GetExpiryTimeForNavigatedAwayPrerender() const;
446 void DeleteOldEntries();
447 virtual PrerenderContents* CreatePrerenderContents(
448 const GURL& url,
449 const content::Referrer& referrer,
450 Origin origin,
451 uint8 experiment_id);
453 // Insures the |active_prerenders_| are sorted by increasing expiry time. Call
454 // after every mutation of active_prerenders_ that can possibly make it
455 // unsorted (e.g. an insert, or changing an expiry time).
456 void SortActivePrerenders();
458 // Finds the active PrerenderData object for a running prerender matching
459 // |url| and |session_storage_namespace|.
460 PrerenderData* FindPrerenderData(
461 const GURL& url,
462 const content::SessionStorageNamespace* session_storage_namespace);
464 // Given the |prerender_contents|, find the iterator in active_prerenders_
465 // correponding to the given prerender.
466 ScopedVector<PrerenderData>::iterator
467 FindIteratorForPrerenderContents(PrerenderContents* prerender_contents);
469 bool DoesRateLimitAllowPrerender(Origin origin) const;
471 // Deletes old WebContents that have been replaced by prerendered ones. This
472 // is needed because they're replaced in a callback from the old WebContents,
473 // so cannot immediately be deleted.
474 void DeleteOldWebContents();
476 // Cleans up old NavigationRecord's.
477 void CleanUpOldNavigations();
479 // Arrange for the given WebContents to be deleted asap. If deleter is not
480 // NULL, deletes that as well.
481 void ScheduleDeleteOldWebContents(content::WebContents* tab,
482 OnCloseWebContentsDeleter* deleter);
484 // Adds to the history list.
485 void AddToHistory(PrerenderContents* contents);
487 // Returns a new Value representing the pages currently being prerendered. The
488 // caller is responsible for delete'ing the return value.
489 base::Value* GetActivePrerendersAsValue() const;
491 // Destroys all pending prerenders using FinalStatus. Also deletes them as
492 // well as any swapped out WebContents queued for destruction.
493 // Used both on destruction, and when clearing the browsing history.
494 void DestroyAllContents(FinalStatus final_status);
496 // Helper function to destroy a PrerenderContents with the specified
497 // final_status, while at the same time recording that for the MatchComplete
498 // case, that this prerender would have been used.
499 void DestroyAndMarkMatchCompleteAsUsed(PrerenderContents* prerender_contents,
500 FinalStatus final_status);
502 // Records the final status a prerender in the case that a PrerenderContents
503 // was never created, and also adds a PrerenderHistory entry.
504 // This is a helper function which will ultimately call
505 // RecordFinalStatusWthMatchCompleteStatus, using MATCH_COMPLETE_DEFAULT.
506 void RecordFinalStatusWithoutCreatingPrerenderContents(
507 const GURL& url, Origin origin, uint8 experiment_id,
508 FinalStatus final_status) const;
511 void CookieChanged(ChromeCookieDetails* details);
512 void CookieChangedAnyCookiesLeftLookupResult(const std::string& domain_key,
513 bool cookies_exist);
514 void LoggedInPredictorDataReceived(scoped_ptr<LoggedInStateMap> new_map);
516 // Swaps a prerender |prerender_data| for |url| into the tab, replacing
517 // |web_contents|. Returns the new WebContents that was swapped in, or NULL
518 // if a swap-in was not possible. If |should_replace_current_entry| is true,
519 // the current history entry in |web_contents| is replaced.
520 content::WebContents* SwapInternal(const GURL& url,
521 content::WebContents* web_contents,
522 PrerenderData* prerender_data,
523 bool should_replace_current_entry);
525 // The configuration.
526 Config config_;
528 // The profile that owns this PrerenderManager.
529 Profile* profile_;
531 // All running prerenders. Sorted by expiry time, in ascending order.
532 ScopedVector<PrerenderData> active_prerenders_;
534 // Prerenders awaiting deletion.
535 ScopedVector<PrerenderData> to_delete_prerenders_;
537 // List of recent navigations in this profile, sorted by ascending
538 // navigate_time_.
539 std::list<NavigationRecord> navigations_;
541 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_;
543 static PrerenderManagerMode mode_;
545 // A count of how many prerenders we do per session. Initialized to 0 then
546 // incremented and emitted to a histogram on each successful prerender.
547 static int prerenders_per_session_count_;
549 // RepeatingTimer to perform periodic cleanups of pending prerendered
550 // pages.
551 base::RepeatingTimer<PrerenderManager> repeating_timer_;
553 // Track time of last prerender to limit prerender spam.
554 base::TimeTicks last_prerender_start_time_;
556 std::list<content::WebContents*> old_web_contents_list_;
558 ScopedVector<OnCloseWebContentsDeleter> on_close_web_contents_deleters_;
560 scoped_ptr<PrerenderHistory> prerender_history_;
562 scoped_ptr<PrerenderHistograms> histograms_;
564 scoped_ptr<PrerenderLocalPredictor> local_predictor_;
566 scoped_refptr<predictors::LoggedInPredictorTable> logged_in_predictor_table_;
568 // Here, we keep the logged in predictor state, but potentially a superset
569 // of its actual (database-backed) state, since we do not incorporate
570 // browser data deletion. We do not use this for actual lookups, but only
571 // to query cookie data for domains we know there was a login before.
572 // This is required to avoid a large number of cookie lookups on bulk
573 // deletion of cookies.
574 scoped_ptr<LoggedInStateMap> logged_in_state_;
576 content::NotificationRegistrar notification_registrar_;
578 // The number of bytes transferred over the network for the profile this
579 // PrerenderManager is attached to.
580 int64 profile_network_bytes_;
582 // The value of profile_network_bytes_ that was last recorded.
583 int64 last_recorded_profile_network_bytes_;
585 // Set of process hosts being prerendered.
586 typedef std::set<content::RenderProcessHost*> PrerenderProcessSet;
587 PrerenderProcessSet prerender_process_hosts_;
589 DISALLOW_COPY_AND_ASSIGN(PrerenderManager);
592 } // namespace prerender
594 #endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_