Changes for start_with_ext Telemetry test to work on Windows.
[chromium-blink-merge.git] / chrome / browser / metrics / omnibox_metrics_provider.cc
blob77db37b6c2fa3c59ee10c0814fb714aec6517327
1 // Copyright 2014 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/metrics/omnibox_metrics_provider.h"
7 #include <vector>
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/ui/browser_otr_state.h"
15 #include "components/metrics/metrics_log.h"
16 #include "components/metrics/proto/omnibox_event.pb.h"
17 #include "components/metrics/proto/omnibox_input_type.pb.h"
18 #include "components/omnibox/browser/autocomplete_match.h"
19 #include "components/omnibox/browser/autocomplete_provider.h"
20 #include "components/omnibox/browser/autocomplete_result.h"
21 #include "components/omnibox/browser/omnibox_log.h"
22 #include "content/public/browser/notification_service.h"
24 using metrics::OmniboxEventProto;
26 namespace {
28 OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
29 AutocompleteMatch::Type type) {
30 switch (type) {
31 case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
32 return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
33 case AutocompleteMatchType::HISTORY_URL:
34 return OmniboxEventProto::Suggestion::HISTORY_URL;
35 case AutocompleteMatchType::HISTORY_TITLE:
36 return OmniboxEventProto::Suggestion::HISTORY_TITLE;
37 case AutocompleteMatchType::HISTORY_BODY:
38 return OmniboxEventProto::Suggestion::HISTORY_BODY;
39 case AutocompleteMatchType::HISTORY_KEYWORD:
40 return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
41 case AutocompleteMatchType::NAVSUGGEST:
42 return OmniboxEventProto::Suggestion::NAVSUGGEST;
43 case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
44 return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
45 case AutocompleteMatchType::SEARCH_HISTORY:
46 return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
47 case AutocompleteMatchType::SEARCH_SUGGEST:
48 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
49 case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
50 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ENTITY;
51 case AutocompleteMatchType::SEARCH_SUGGEST_TAIL:
52 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_TAIL;
53 case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
54 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PERSONALIZED;
55 case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
56 return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PROFILE;
57 case AutocompleteMatchType::CALCULATOR:
58 return OmniboxEventProto::Suggestion::CALCULATOR;
59 case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
60 return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
61 case AutocompleteMatchType::EXTENSION_APP:
62 return OmniboxEventProto::Suggestion::EXTENSION_APP;
63 case AutocompleteMatchType::BOOKMARK_TITLE:
64 return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
65 case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
66 return OmniboxEventProto::Suggestion::NAVSUGGEST_PERSONALIZED;
67 case AutocompleteMatchType::CONTACT_DEPRECATED:
68 case AutocompleteMatchType::NUM_TYPES:
69 break;
71 NOTREACHED();
72 return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
75 } // namespace
77 OmniboxMetricsProvider::OmniboxMetricsProvider() {
80 OmniboxMetricsProvider::~OmniboxMetricsProvider() {
83 void OmniboxMetricsProvider::OnRecordingEnabled() {
84 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
85 content::NotificationService::AllSources());
88 void OmniboxMetricsProvider::OnRecordingDisabled() {
89 registrar_.RemoveAll();
92 void OmniboxMetricsProvider::ProvideGeneralMetrics(
93 metrics::ChromeUserMetricsExtension* uma_proto) {
94 uma_proto->mutable_omnibox_event()->Swap(
95 omnibox_events_cache.mutable_omnibox_event());
98 void OmniboxMetricsProvider::Observe(
99 int type,
100 const content::NotificationSource& source,
101 const content::NotificationDetails& details) {
102 DCHECK_EQ(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, type);
104 // We simply don't log events to UMA if there is a single incognito
105 // session visible. In the future, it may be worth revisiting this to
106 // still log events from non-incognito sessions.
107 if (!chrome::IsOffTheRecordSessionActive())
108 RecordOmniboxOpenedURL(*content::Details<OmniboxLog>(details).ptr());
111 void OmniboxMetricsProvider::RecordOmniboxOpenedURL(const OmniboxLog& log) {
112 std::vector<base::StringPiece16> terms = base::SplitStringPiece(
113 log.text, base::kWhitespaceUTF16,
114 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
116 OmniboxEventProto* omnibox_event = omnibox_events_cache.add_omnibox_event();
117 omnibox_event->set_time(metrics::MetricsLog::GetCurrentTime());
118 if (log.tab_id != -1) {
119 // If we know what tab the autocomplete URL was opened in, log it.
120 omnibox_event->set_tab_id(log.tab_id);
122 omnibox_event->set_typed_length(log.text.length());
123 omnibox_event->set_just_deleted_text(log.just_deleted_text);
124 omnibox_event->set_num_typed_terms(static_cast<int>(terms.size()));
125 omnibox_event->set_selected_index(log.selected_index);
126 if (log.completed_length != base::string16::npos)
127 omnibox_event->set_completed_length(log.completed_length);
128 const base::TimeDelta default_time_delta =
129 base::TimeDelta::FromMilliseconds(-1);
130 if (log.elapsed_time_since_user_first_modified_omnibox !=
131 default_time_delta) {
132 // Only upload the typing duration if it is set/valid.
133 omnibox_event->set_typing_duration_ms(
134 log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
136 if (log.elapsed_time_since_last_change_to_default_match !=
137 default_time_delta) {
138 omnibox_event->set_duration_since_last_default_match_update_ms(
139 log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
141 omnibox_event->set_current_page_classification(
142 log.current_page_classification);
143 omnibox_event->set_input_type(log.input_type);
144 // We consider a paste-and-search/paste-and-go action to have a closed popup
145 // (as explained in omnibox_event.proto) even if it was not, because such
146 // actions ignore the contents of the popup so it doesn't matter that it was
147 // open.
148 omnibox_event->set_is_popup_open(log.is_popup_open && !log.is_paste_and_go);
149 omnibox_event->set_is_paste_and_go(log.is_paste_and_go);
151 for (AutocompleteResult::const_iterator i(log.result.begin());
152 i != log.result.end(); ++i) {
153 OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
154 suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
155 suggestion->set_result_type(AsOmniboxEventResultType(i->type));
156 suggestion->set_relevance(i->relevance);
157 if (i->typed_count != -1)
158 suggestion->set_typed_count(i->typed_count);
160 for (ProvidersInfo::const_iterator i(log.providers_info.begin());
161 i != log.providers_info.end(); ++i) {
162 OmniboxEventProto::ProviderInfo* provider_info =
163 omnibox_event->add_provider_info();
164 provider_info->CopyFrom(*i);