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.
5 #include "chrome/browser/ui/uma_browsing_activity_observer.h"
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"
26 UMABrowsingActivityObserver
* g_instance
= NULL
;
31 void UMABrowsingActivityObserver::Init() {
33 // Must be created before any Browsers are.
34 DCHECK_EQ(0U, chrome::GetTotalBrowserCount());
35 g_instance
= new UMABrowsingActivityObserver
;
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());
45 UMABrowsingActivityObserver::~UMABrowsingActivityObserver() {
48 void UMABrowsingActivityObserver::Observe(
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();
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(base::UserMetricsAction("NavEntryCommitted"));
61 // Attempting to determine the cause of a crash originating from
62 // IsSearchResultsPageFromDefaultSearchProvider but manifesting in
63 // TemplateURLRef::ExtractSearchTermsFromURL(...).
64 // See http://crbug.com/291348.
66 if (TemplateURLServiceFactory::GetForProfile(
67 Profile::FromBrowserContext(controller
->GetBrowserContext()))->
68 IsSearchResultsPageFromDefaultSearchProvider(
69 load
.entry
->GetURL())) {
70 content::RecordAction(base::UserMetricsAction("NavEntryCommitted.SRP"));
73 if (!load
.is_navigation_to_different_page())
74 return; // Don't log for subframes or other trivial types.
76 LogRenderProcessHostCount();
78 } else if (type
== chrome::NOTIFICATION_APP_TERMINATING
) {
84 void UMABrowsingActivityObserver::LogRenderProcessHostCount() const {
86 for (content::RenderProcessHost::iterator
i(
87 content::RenderProcessHost::AllHostsIterator());
88 !i
.IsAtEnd(); i
.Advance())
90 UMA_HISTOGRAM_CUSTOM_COUNTS("MPArch.RPHCountPerLoad", hosts_count
,
94 void UMABrowsingActivityObserver::LogBrowserTabCount() const {
96 int app_window_count
= 0;
97 int popup_window_count
= 0;
98 int tabbed_window_count
= 0;
99 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
100 // Record how many tabs each window has open.
101 Browser
* browser
= *it
;
102 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerWindow",
103 browser
->tab_strip_model()->count(),
105 tab_count
+= browser
->tab_strip_model()->count();
107 if (browser
->window()->IsActive()) {
108 // Record how many tabs the active window has open.
109 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountActiveWindow",
110 browser
->tab_strip_model()->count(),
114 if (browser
->is_app())
116 else if (browser
->is_type_popup())
117 popup_window_count
++;
118 else if (browser
->is_type_tabbed())
119 tabbed_window_count
++;
121 // Record how many tabs total are open (across all windows).
122 UMA_HISTOGRAM_CUSTOM_COUNTS("Tabs.TabCountPerLoad", tab_count
, 1, 200, 50);
124 // Record how many windows are open, by type.
125 UMA_HISTOGRAM_COUNTS_100("WindowManager.AppWindowCountPerLoad",
127 UMA_HISTOGRAM_COUNTS_100("WindowManager.PopUpWindowCountPerLoad",
129 UMA_HISTOGRAM_COUNTS_100("WindowManager.TabbedWindowCountPerLoad",
130 tabbed_window_count
);
133 } // namespace chrome