Commit 6005d68f authored by tburkard@chromium.org's avatar tburkard@chromium.org

Add code to keep track of what fraction of pageviews are top sites.

This code will be removed again once histograms have been collected.
Review URL: http://codereview.chromium.org/7775003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98734 0039d316-1c4b-4281-b951-d872f2087c98
parent 5d000c47
......@@ -15,6 +15,7 @@
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/prerender/prerender_condition.h"
#include "chrome/browser/prerender/prerender_contents.h"
#include "chrome/browser/prerender/prerender_final_status.h"
......@@ -26,15 +27,19 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper_delegate.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "content/browser/browser_thread.h"
#include "content/browser/cancelable_request.h"
#include "content/browser/renderer_host/render_process_host.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/tab_contents/render_view_host_manager.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/tab_contents/tab_contents_delegate.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"
#include "content/common/notification_service.h"
namespace prerender {
......@@ -165,6 +170,79 @@ struct PrerenderManager::PendingContentsData {
Origin origin_;
};
class PrerenderManager::MostVisitedSites : public NotificationObserver {
public:
explicit MostVisitedSites(Profile* profile) :
profile_(profile) {
// If TopSites is already loaded, we will want to use it right away.
// Otherwise, wait for three seconds to avoid race conditions.
// This is a hack to ensure unit tests don't fail.
// See http://crbug.com/94654
if (profile && profile->GetTopSitesWithoutCreating()) {
Init();
} else {
timer_.Start(base::TimeDelta::FromSeconds(3), this,
&prerender::PrerenderManager::MostVisitedSites::Init);
}
}
void Init() {
history::TopSites* top_sites = GetTopSites();
if (top_sites) {
registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
Source<history::TopSites>(top_sites));
}
UpdateMostVisited();
}
void UpdateMostVisited() {
history::TopSites* top_sites = GetTopSites();
if (top_sites) {
top_sites->GetMostVisitedURLs(
&topsites_consumer_,
NewCallback(this,
&prerender::PrerenderManager::MostVisitedSites::
OnMostVisitedURLsAvailable));
}
}
void OnMostVisitedURLsAvailable(const history::MostVisitedURLList& data) {
urls_.clear();
for (int i = 0; i < static_cast<int>(data.size()); i++)
urls_.insert(data[i].url);
}
void Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
UpdateMostVisited();
}
bool IsTopSite(const GURL& url) const {
return (urls_.count(url) > 0);
}
private:
history::TopSites* GetTopSites() const {
if (profile_)
return profile_->GetTopSites();
return NULL;
}
CancelableRequestConsumer topsites_consumer_;
Profile* profile_;
NotificationRegistrar registrar_;
std::set<GURL> urls_;
base::OneShotTimer<prerender::PrerenderManager::MostVisitedSites> timer_;
};
bool PrerenderManager::IsTopSite(const GURL& url) const {
DCHECK(most_visited_.get());
return most_visited_->IsTopSite(url);
}
PrerenderManager::PrerenderManager(Profile* profile,
PrerenderTracker* prerender_tracker)
: enabled_(true),
......@@ -175,7 +253,8 @@ PrerenderManager::PrerenderManager(Profile* profile,
base::TimeDelta::FromMilliseconds(kMinTimeBetweenPrerendersMs)),
runnable_method_factory_(this),
prerender_history_(new PrerenderHistory(kHistoryLength)),
histograms_(new PrerenderHistograms()) {
histograms_(new PrerenderHistograms()),
most_visited_(new MostVisitedSites(profile)) {
// There are some assumptions that the PrerenderManager is on the UI thread.
// Any other checks simply make sure that the PrerenderManager is accessed on
// the same thread that it was created on.
......
......@@ -204,6 +204,8 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>,
// Adds a condition. This is owned by the PrerenderManager.
void AddCondition(const PrerenderCondition* condition);
bool IsTopSite(const GURL& url) const;
protected:
// Test that needs needs access to internal functions.
FRIEND_TEST_ALL_PREFIXES(PrerenderManagerTest, ExpireTest);
......@@ -227,6 +229,8 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>,
class OnCloseTabContentsDeleter;
class MostVisitedSites;
// Adds a prerender for |url| from referrer |referrer| initiated from the
// RenderViewHost specified by |child_route_id_pair|. The |origin| specifies
// how the prerender was added.
......@@ -390,6 +394,8 @@ class PrerenderManager : public base::SupportsWeakPtr<PrerenderManager>,
scoped_ptr<PrerenderHistograms> histograms_;
scoped_ptr<MostVisitedSites> most_visited_;
DISALLOW_COPY_AND_ASSIGN(PrerenderManager);
};
......
......@@ -80,6 +80,19 @@ class PerHoverThresholdHistograms {
base::Histogram* time_hover_until_click_;
};
enum PAGEVIEW_EVENTS {
PAGEVIEW_EVENT_NEW_URL = 0,
PAGEVIEW_EVENT_TOP_SITE_NEW_URL = 1,
PAGEVIEW_EVENT_LOAD_START = 2,
PAGEVIEW_EVENT_TOP_SITE_LOAD_START = 3,
PAGEVIEW_EVENT_MAX = 4
};
void RecordPageviewEvent(PAGEVIEW_EVENTS event) {
UMA_HISTOGRAM_ENUMERATION("Prerender.PageviewEvents",
event, PAGEVIEW_EVENT_MAX);
}
} // namespace
class PrerenderObserver::HoverData {
......@@ -180,6 +193,9 @@ PrerenderObserver::~PrerenderObserver() {
void PrerenderObserver::ProvisionalChangeToMainFrameUrl(const GURL& url,
bool has_opener_set) {
RecordPageviewEvent(PAGEVIEW_EVENT_NEW_URL);
if (IsTopSite(url))
RecordPageviewEvent(PAGEVIEW_EVENT_TOP_SITE_NEW_URL);
if (!tab_->delegate())
return; // PrerenderManager needs a delegate to handle the swap.
PrerenderManager* prerender_manager = MaybeGetPrerenderManager();
......@@ -205,6 +221,10 @@ void PrerenderObserver::OnDidStartProvisionalLoadForFrame(int64 frame_id,
bool has_opener_set,
const GURL& url) {
if (is_main_frame) {
RecordPageviewEvent(PAGEVIEW_EVENT_LOAD_START);
if (IsTopSite(url))
RecordPageviewEvent(PAGEVIEW_EVENT_TOP_SITE_LOAD_START);
// Record the beginning of a new PPLT navigation.
pplt_load_start_ = base::TimeTicks::Now();
......@@ -241,9 +261,11 @@ void PrerenderObserver::DidStopLoading() {
pplt_load_start_ = base::TimeTicks();
}
PrerenderManager* PrerenderObserver::MaybeGetPrerenderManager() {
PrerenderManager* PrerenderObserver::MaybeGetPrerenderManager() const {
Profile* profile =
Profile::FromBrowserContext(tab_contents()->browser_context());
if (!profile)
return NULL;
return profile->GetPrerenderManager();
}
......@@ -303,4 +325,9 @@ void PrerenderObserver::MaybeLogCurrentHover(bool was_used) {
current_hover_url_ = GURL();
}
bool PrerenderObserver::IsTopSite(const GURL& url) const {
PrerenderManager* pm = MaybeGetPrerenderManager();
return (pm && pm->IsTopSite(url));
}
} // namespace prerender
......@@ -46,7 +46,7 @@ class PrerenderObserver : public TabContentsObserver {
void OnMsgUpdateTargetURL(int32 page_id, const GURL& url);
// Retrieves the PrerenderManager, or NULL, if none was found.
PrerenderManager* MaybeGetPrerenderManager();
PrerenderManager* MaybeGetPrerenderManager() const;
// Checks with the PrerenderManager if the specified URL has been preloaded,
// and if so, swap the RenderViewHost with the preload into this TabContents
......@@ -61,6 +61,8 @@ class PrerenderObserver : public TabContentsObserver {
// Also resets the hover to no hover.
void MaybeLogCurrentHover(bool was_used);
bool IsTopSite(const GURL& url) const;
// TabContentsWrapper we're created for.
TabContentsWrapper* tab_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment