Home | History | Annotate | Download | only in ui
      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.
      4 
      5 #include "chrome/browser/ui/uma_browsing_activity_observer.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/search_engines/template_url_service.h"
     10 #include "chrome/browser/search_engines/template_url_service_factory.h"
     11 #include "chrome/browser/ui/browser.h"
     12 #include "chrome/browser/ui/browser_finder.h"
     13 #include "chrome/browser/ui/browser_iterator.h"
     14 #include "chrome/browser/ui/browser_window.h"
     15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     16 #include "content/public/browser/navigation_controller.h"
     17 #include "content/public/browser/navigation_details.h"
     18 #include "content/public/browser/navigation_entry.h"
     19 #include "content/public/browser/notification_service.h"
     20 #include "content/public/browser/render_process_host.h"
     21 #include "content/public/browser/user_metrics.h"
     22 
     23 namespace chrome {
     24 namespace {
     25 
     26 UMABrowsingActivityObserver* g_instance = NULL;
     27 
     28 }  // namespace
     29 
     30 // static
     31 void UMABrowsingActivityObserver::Init() {
     32   DCHECK(!g_instance);
     33   // Must be created before any Browsers are.
     34   DCHECK_EQ(0U, chrome::GetTotalBrowserCount());
     35   g_instance = new UMABrowsingActivityObserver;
     36 }
     37 
     38 UMABrowsingActivityObserver::UMABrowsingActivityObserver() {
     39   registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
     40                   content::NotificationService::AllSources());
     41   registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
     42                   content::NotificationService::AllSources());
     43 }
     44 
     45 UMABrowsingActivityObserver::~UMABrowsingActivityObserver() {
     46 }
     47 
     48 void UMABrowsingActivityObserver::Observe(
     49     int type,
     50     const content::NotificationSource& source,
     51     const content::NotificationDetails& details) {
     52   if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
     53     const content::LoadCommittedDetails& load =
     54         *content::Details<content::LoadCommittedDetails>(details).ptr();
     55 
     56     content::NavigationController* controller =
     57       content::Source<content::NavigationController>(source).ptr();
     58     // Track whether the page loaded is a search results page (SRP). Track
     59     // the non-SRP navigations as well so there is a control.
     60     content::RecordAction(content::UserMetricsAction("NavEntryCommitted"));
     61     if (TemplateURLServiceFactory::GetForProfile(
     62             Profile::FromBrowserContext(controller->GetBrowserContext()))->
     63             IsSearchResultsPageFromDefaultSearchProvider(
     64                 load.entry->GetURL())) {
     65       content::RecordAction(
     66           content::UserMetricsAction("NavEntryCommitted.SRP"));
     67     }
     68 
     69     if (!load.is_navigation_to_different_page())
     70       return;  // Don't log for subframes or other trivial types.
     71 
     72     LogRenderProcessHostCount();
     73     LogBrowserTabCount();
     74   } else if (type == chrome::NOTIFICATION_APP_TERMINATING) {
     75     delete g_instance;
     76     g_instance = NULL;
     77   }
     78 }
     79 
     80 void UMABrowsingActivityObserver::LogRenderProcessHostCount() const {
     81   int hosts_count = 0;
     82   for (content::RenderProcessHost::iterator i(
     83           content::RenderProcessHost::AllHostsIterator());
     84         !i.IsAtEnd(); i.Advance())
     85     ++hosts_count;
     86   UMA_HISTOGRAM_CUSTOM_COUNTS("MPArch.RPHCountPerLoad", hosts_count,
     87                               1, 50, 50);
     88 }
     89 
     90 void UMABrowsingActivityObserver::LogBrowserTabCount() const {
     91   int tab_count = 0;
     92   int app_window_count = 0;
     93   int popup_window_count = 0;
     94   int tabbed_window_count = 0;
     95   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
     96     // Record how many tabs each window has open.
     97     Browser* browser = *it;
     98     UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerWindow",
     99                                 browser->tab_strip_model()->count(),
    100                                 1, 200, 50);
    101     tab_count += browser->tab_strip_model()->count();
    102 
    103     if (browser->window()->IsActive()) {
    104       // Record how many tabs the active window has open.
    105       UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountActiveWindow",
    106                                   browser->tab_strip_model()->count(),
    107                                   1, 200, 50);
    108     }
    109 
    110     if (browser->is_app())
    111       app_window_count++;
    112     else if (browser->is_type_popup())
    113       popup_window_count++;
    114     else if (browser->is_type_tabbed())
    115       tabbed_window_count++;
    116   }
    117   // Record how many tabs total are open (across all windows).
    118   UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tab_count, 1, 200, 50);
    119 
    120   // Record how many windows are open, by type.
    121   UMA_HISTOGRAM_COUNTS_100("WindowManager.AppWindowCountPerLoad",
    122                            app_window_count);
    123   UMA_HISTOGRAM_COUNTS_100("WindowManager.PopUpWindowCountPerLoad",
    124                            popup_window_count);
    125   UMA_HISTOGRAM_COUNTS_100("WindowManager.TabbedWindowCountPerLoad",
    126                            tabbed_window_count);
    127 }
    128 
    129 }  // namespace chrome
    130