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.
5 #include "chrome/browser/search/most_visited_iframe_source.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/common/url_constants.h"
11 #include "content/public/browser/user_metrics.h"
12 #include "grit/browser_resources.h"
13 #include "net/base/url_util.h"
18 const char kTitleHTMLPath
[] = "/title.html";
19 const char kTitleCSSPath
[] = "/title.css";
20 const char kTitleJSPath
[] = "/title.js";
21 const char kThumbnailHTMLPath
[] = "/thumbnail.html";
22 const char kThumbnailCSSPath
[] = "/thumbnail.css";
23 const char kThumbnailJSPath
[] = "/thumbnail.js";
24 const char kUtilJSPath
[] = "/util.js";
25 const char kCommonCSSPath
[] = "/common.css";
26 const char kLogHTMLPath
[] = "/log.html";
27 const char kMostVisitedHistogramWithProvider
[] = "NewTabPage.MostVisited.%s";
31 MostVisitedIframeSource::MostVisitedIframeSource() {
34 MostVisitedIframeSource::~MostVisitedIframeSource() {
37 const int MostVisitedIframeSource::kNumMostVisited
= 8;
38 const char MostVisitedIframeSource::kMostVisitedHistogramName
[] =
39 "NewTabPage.MostVisited";
41 std::string
MostVisitedIframeSource::GetSource() const {
42 return chrome::kChromeSearchMostVisitedHost
;
45 void MostVisitedIframeSource::StartDataRequest(
46 const std::string
& path_and_query
,
47 int render_process_id
,
49 const content::URLDataSource::GotDataCallback
& callback
) {
50 GURL
url(chrome::kChromeSearchMostVisitedUrl
+ path_and_query
);
51 std::string
path(url
.path());
52 if (path
== kTitleHTMLPath
) {
53 SendResource(IDR_MOST_VISITED_TITLE_HTML
, callback
);
54 } else if (path
== kTitleCSSPath
) {
55 SendResource(IDR_MOST_VISITED_TITLE_CSS
, callback
);
56 } else if (path
== kTitleJSPath
) {
57 SendResource(IDR_MOST_VISITED_TITLE_JS
, callback
);
58 } else if (path
== kThumbnailHTMLPath
) {
59 SendResource(IDR_MOST_VISITED_THUMBNAIL_HTML
, callback
);
60 } else if (path
== kThumbnailCSSPath
) {
61 SendResource(IDR_MOST_VISITED_THUMBNAIL_CSS
, callback
);
62 } else if (path
== kThumbnailJSPath
) {
63 SendJSWithOrigin(IDR_MOST_VISITED_THUMBNAIL_JS
, render_process_id
,
64 render_frame_id
, callback
);
65 } else if (path
== kUtilJSPath
) {
66 SendResource(IDR_MOST_VISITED_UTIL_JS
, callback
);
67 } else if (path
== kCommonCSSPath
) {
68 SendResource(IDR_MOST_VISITED_IFRAME_CSS
, callback
);
69 } else if (path
== kLogHTMLPath
) {
70 // Log the clicked MostVisited element by position.
71 std::string str_position
;
73 if (net::GetValueForKeyInQuery(url
, "pos", &str_position
) &&
74 base::StringToInt(str_position
, &position
)) {
75 // Log the Most Visited click.
76 UMA_HISTOGRAM_ENUMERATION(kMostVisitedHistogramName
, position
,
78 // If a specific provider is specified, log the metric specific to the
81 if (net::GetValueForKeyInQuery(url
, "pr", &provider
))
82 LogMostVisitedProviderClick(position
, provider
);
84 // Records the action. This will be available as a time-stamped stream
85 // server-side and can be used to compute time-to-long-dwell.
86 content::RecordAction(base::UserMetricsAction("MostVisited_Clicked"));
94 bool MostVisitedIframeSource::ServesPath(const std::string
& path
) const {
95 return path
== kTitleHTMLPath
|| path
== kTitleCSSPath
||
96 path
== kTitleJSPath
|| path
== kThumbnailHTMLPath
||
97 path
== kThumbnailCSSPath
|| path
== kThumbnailJSPath
||
98 path
== kUtilJSPath
|| path
== kCommonCSSPath
|| path
== kLogHTMLPath
;
101 void MostVisitedIframeSource::LogMostVisitedProviderClick(
103 const std::string
& provider
) {
104 std::string histogram_name
=
105 MostVisitedIframeSource::GetHistogramNameForProvider(provider
);
106 base::HistogramBase
* counter
= base::LinearHistogram::FactoryGet(
108 MostVisitedIframeSource::kNumMostVisited
,
109 MostVisitedIframeSource::kNumMostVisited
+ 1,
110 base::Histogram::kUmaTargetedHistogramFlag
);
111 counter
->Add(position
);
115 std::string
MostVisitedIframeSource::GetHistogramNameForProvider(
116 const std::string
& provider
) {
117 return base::StringPrintf(kMostVisitedHistogramWithProvider
,