prerender_manager.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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 CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ 6#define CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ 7#pragma once 8 9#include <list> 10#include <vector> 11 12#include "base/ref_counted.h" 13#include "base/scoped_ptr.h" 14#include "base/time.h" 15#include "chrome/browser/prerender/prerender_contents.h" 16#include "googleurl/src/gurl.h" 17 18class Profile; 19class TabContents; 20 21// PrerenderManager is responsible for initiating and keeping prerendered 22// views of webpages. 23class PrerenderManager : public base::RefCounted<PrerenderManager> { 24 public: 25 // PrerenderManagerMode is used in a UMA_HISTOGRAM, so please do not 26 // add in the middle. 27 enum PrerenderManagerMode { 28 PRERENDER_MODE_DISABLED, 29 PRERENDER_MODE_ENABLED, 30 PRERENDER_MODE_EXPERIMENT_CONTROL_GROUP, 31 PRERENDER_MODE_EXPERIMENT_PRERENDER_GROUP, 32 PRERENDER_MODE_MAX 33 }; 34 35 // Owned by a Profile object for the lifetime of the profile. 36 explicit PrerenderManager(Profile* profile); 37 38 // Preloads the URL supplied. alias_urls indicates URLs that redirect 39 // to the same URL to be preloaded. 40 void AddPreload(const GURL& url, const std::vector<GURL>& alias_urls); 41 42 // For a given TabContents that wants to navigate to the URL supplied, 43 // determines whether a preloaded version of the URL can be used, 44 // and substitutes the prerendered RVH into the TabContents. Returns 45 // whether or not a prerendered RVH could be used or not. 46 bool MaybeUsePreloadedPage(TabContents* tc, const GURL& url); 47 48 // Allows PrerenderContents to remove itself when prerendering should 49 // be cancelled. 50 void RemoveEntry(PrerenderContents* entry); 51 52 // Retrieves the PrerenderContents object for the specified URL, if it 53 // has been prerendered. The caller will then have ownership of the 54 // PrerenderContents object and is responsible for freeing it. 55 // Returns NULL if the specified URL has not been prerendered. 56 PrerenderContents* GetEntry(const GURL& url); 57 58 // The following two methods should only be called from the UI thread. 59 void RecordPerceivedPageLoadTime(base::TimeDelta pplt); 60 void RecordTimeUntilUsed(base::TimeDelta time_until_used); 61 62 base::TimeDelta max_prerender_age() const { return max_prerender_age_; } 63 void set_max_prerender_age(base::TimeDelta td) { max_prerender_age_ = td; } 64 unsigned int max_elements() const { return max_elements_; } 65 void set_max_elements(unsigned int num) { max_elements_ = num; } 66 67 static PrerenderManagerMode GetMode(); 68 static void SetMode(PrerenderManagerMode mode); 69 static bool IsPrerenderingEnabled(); 70 71 // The following static method can be called from any thread, but will result 72 // in posting a task to the UI thread if we are not in the UI thread. 73 static void RecordPrefetchTagObserved(); 74 75 protected: 76 virtual ~PrerenderManager(); 77 78 void SetPrerenderContentsFactory( 79 PrerenderContents::Factory* prerender_contents_factory); 80 81 private: 82 // Test that needs needs access to internal functions. 83 friend class PrerenderBrowserTest; 84 85 friend class base::RefCounted<PrerenderManager>; 86 struct PrerenderContentsData; 87 88 bool IsPrerenderElementFresh(const base::Time start) const; 89 void DeleteOldEntries(); 90 virtual base::Time GetCurrentTime() const; 91 virtual PrerenderContents* CreatePrerenderContents( 92 const GURL& url, 93 const std::vector<GURL>& alias_urls); 94 95 // Finds the specified PrerenderContents and returns it, if it exists. 96 // Returns NULL otherwise. Unlike GetEntry, the PrerenderManager maintains 97 // ownership of the PrerenderContents. 98 PrerenderContents* FindEntry(const GURL& url); 99 100 bool ShouldRecordWindowedPPLT() const; 101 102 static void RecordPrefetchTagObservedOnUIThread(); 103 104 Profile* profile_; 105 106 base::TimeDelta max_prerender_age_; 107 unsigned int max_elements_; 108 109 // List of prerendered elements. 110 std::list<PrerenderContentsData> prerender_list_; 111 112 // Default maximum permitted elements to prerender. 113 static const unsigned int kDefaultMaxPrerenderElements = 1; 114 115 // Default maximum age a prerendered element may have, in seconds. 116 static const int kDefaultMaxPrerenderAgeSeconds = 20; 117 118 // Time window for which we will record windowed PLT's from the last 119 // observed link rel=prefetch tag. 120 static const int kWindowedPPLTSeconds = 30; 121 122 scoped_ptr<PrerenderContents::Factory> prerender_contents_factory_; 123 124 static PrerenderManagerMode mode_; 125 126 // The time when we last saw a prefetch request coming from a renderer. 127 // This is used to record perceived PLT's for a certain amount of time 128 // from the point that we last saw a <link rel=prefetch> tag. 129 // This static variable should only be modified on the UI thread. 130 static base::TimeTicks last_prefetch_seen_time_; 131 132 DISALLOW_COPY_AND_ASSIGN(PrerenderManager); 133}; 134 135#endif // CHROME_BROWSER_PRERENDER_PRERENDER_MANAGER_H_ 136