Home | History | Annotate | Download | only in search
      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 #include "chrome/browser/search/most_visited_iframe_source.h"
      6 
      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"
     14 #include "url/gurl.h"
     15 
     16 namespace {
     17 
     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";
     28 
     29 }  // namespace
     30 
     31 MostVisitedIframeSource::MostVisitedIframeSource() {
     32 }
     33 
     34 MostVisitedIframeSource::~MostVisitedIframeSource() {
     35 }
     36 
     37 const int MostVisitedIframeSource::kNumMostVisited = 8;
     38 const char MostVisitedIframeSource::kMostVisitedHistogramName[] =
     39     "NewTabPage.MostVisited";
     40 
     41 std::string MostVisitedIframeSource::GetSource() const {
     42   return chrome::kChromeSearchMostVisitedHost;
     43 }
     44 
     45 void MostVisitedIframeSource::StartDataRequest(
     46     const std::string& path_and_query,
     47     int render_process_id,
     48     int render_view_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     SendResource(IDR_MOST_VISITED_THUMBNAIL_JS, callback);
     64   } else if (path == kUtilJSPath) {
     65     SendResource(IDR_MOST_VISITED_UTIL_JS, callback);
     66   } else if (path == kCommonCSSPath) {
     67     SendResource(IDR_MOST_VISITED_IFRAME_CSS, callback);
     68   } else if (path == kLogHTMLPath) {
     69     // Log the clicked MostVisited element by position.
     70     std::string str_position;
     71     int position;
     72     if (net::GetValueForKeyInQuery(url, "pos", &str_position) &&
     73         base::StringToInt(str_position, &position)) {
     74       // Log the Most Visited click.
     75       UMA_HISTOGRAM_ENUMERATION(kMostVisitedHistogramName, position,
     76                                 kNumMostVisited);
     77       // If a specific provider is specified, log the metric specific to the
     78       // provider.
     79       std::string provider;
     80       if (net::GetValueForKeyInQuery(url, "pr", &provider))
     81         LogMostVisitedProviderClick(position, provider);
     82 
     83       // Records the action. This will be available as a time-stamped stream
     84       // server-side and can be used to compute time-to-long-dwell.
     85       content::RecordAction(content::UserMetricsAction("MostVisited_Clicked"));
     86     }
     87     callback.Run(NULL);
     88   } else {
     89     callback.Run(NULL);
     90   }
     91 }
     92 
     93 bool MostVisitedIframeSource::ServesPath(const std::string& path) const {
     94   return path == kTitleHTMLPath || path == kTitleCSSPath ||
     95          path == kTitleJSPath || path == kThumbnailHTMLPath ||
     96          path == kThumbnailCSSPath || path == kThumbnailJSPath ||
     97          path == kUtilJSPath || path == kCommonCSSPath || path == kLogHTMLPath;
     98 }
     99 
    100 void MostVisitedIframeSource::LogMostVisitedProviderClick(
    101     int position,
    102     const std::string& provider) {
    103   std::string histogram_name =
    104       MostVisitedIframeSource::GetHistogramNameForProvider(provider);
    105   base::HistogramBase* counter = base::LinearHistogram::FactoryGet(
    106       histogram_name, 1,
    107       MostVisitedIframeSource::kNumMostVisited,
    108       MostVisitedIframeSource::kNumMostVisited + 1,
    109       base::Histogram::kUmaTargetedHistogramFlag);
    110   counter->Add(position);
    111 }
    112 
    113 // static
    114 std::string MostVisitedIframeSource::GetHistogramNameForProvider(
    115     const std::string& provider) {
    116   return base::StringPrintf(kMostVisitedHistogramWithProvider,
    117                             provider.c_str());
    118 }
    119