1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/metrics/omnibox_metrics_provider.h"
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector>
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/logging.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/string16.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/strings/string_util.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/omnibox/omnibox_log.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/browser_otr_state.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/metrics/metrics_log.h"
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/metrics/proto/omnibox_event.pb.h"
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/metrics/proto/omnibox_input_type.pb.h"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_match.h"
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_provider.h"
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/omnibox/autocomplete_result.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/notification_service.h"
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using metrics::OmniboxEventProto;
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace {
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)OmniboxEventProto::Suggestion::ResultType AsOmniboxEventResultType(
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    AutocompleteMatch::Type type) {
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  switch (type) {
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::URL_WHAT_YOU_TYPED:
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::URL_WHAT_YOU_TYPED;
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::HISTORY_URL:
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::HISTORY_URL;
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::HISTORY_TITLE:
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::HISTORY_TITLE;
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::HISTORY_BODY:
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::HISTORY_BODY;
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::HISTORY_KEYWORD:
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::HISTORY_KEYWORD;
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::NAVSUGGEST:
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::NAVSUGGEST;
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED:
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_WHAT_YOU_TYPED;
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_HISTORY:
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_HISTORY;
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST:
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST;
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST_ENTITY:
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ENTITY;
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST_INFINITE:
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_INFINITE;
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED:
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PERSONALIZED;
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST_PROFILE:
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_PROFILE;
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_SUGGEST_ANSWER:
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_SUGGEST_ANSWER;
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::SEARCH_OTHER_ENGINE:
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::SEARCH_OTHER_ENGINE;
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::EXTENSION_APP:
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::EXTENSION_APP;
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::BOOKMARK_TITLE:
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::BOOKMARK_TITLE;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::NAVSUGGEST_PERSONALIZED:
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return OmniboxEventProto::Suggestion::NAVSUGGEST_PERSONALIZED;
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::CONTACT_DEPRECATED:
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case AutocompleteMatchType::NUM_TYPES:
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      break;
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  NOTREACHED();
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return OmniboxEventProto::Suggestion::UNKNOWN_RESULT_TYPE;
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)OmniboxMetricsProvider::OmniboxMetricsProvider() {
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)OmniboxMetricsProvider::~OmniboxMetricsProvider() {
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OmniboxMetricsProvider::OnRecordingEnabled() {
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                 content::NotificationService::AllSources());
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OmniboxMetricsProvider::OnRecordingDisabled() {
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  registrar_.RemoveAll();
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OmniboxMetricsProvider::ProvideGeneralMetrics(
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    metrics::ChromeUserMetricsExtension* uma_proto) {
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  uma_proto->mutable_omnibox_event()->Swap(
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      omnibox_events_cache.mutable_omnibox_event());
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OmniboxMetricsProvider::Observe(
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int type,
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const content::NotificationSource& source,
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const content::NotificationDetails& details) {
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK_EQ(chrome::NOTIFICATION_OMNIBOX_OPENED_URL, type);
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // We simply don't log events to UMA if there is a single incognito
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // session visible. In the future, it may be worth revisiting this to
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // still log events from non-incognito sessions.
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!chrome::IsOffTheRecordSessionActive())
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    RecordOmniboxOpenedURL(*content::Details<OmniboxLog>(details).ptr());
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void OmniboxMetricsProvider::RecordOmniboxOpenedURL(const OmniboxLog& log) {
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<base::string16> terms;
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const int num_terms =
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      static_cast<int>(Tokenize(log.text, base::kWhitespaceUTF16, &terms));
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  OmniboxEventProto* omnibox_event = omnibox_events_cache.add_omnibox_event();
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  omnibox_event->set_time(metrics::MetricsLog::GetCurrentTime());
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (log.tab_id != -1) {
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // If we know what tab the autocomplete URL was opened in, log it.
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    omnibox_event->set_tab_id(log.tab_id);
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_typed_length(log.text.length());
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_just_deleted_text(log.just_deleted_text);
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_num_typed_terms(num_terms);
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_selected_index(log.selected_index);
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (log.completed_length != base::string16::npos)
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    omnibox_event->set_completed_length(log.completed_length);
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const base::TimeDelta default_time_delta =
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      base::TimeDelta::FromMilliseconds(-1);
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (log.elapsed_time_since_user_first_modified_omnibox !=
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      default_time_delta) {
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Only upload the typing duration if it is set/valid.
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    omnibox_event->set_typing_duration_ms(
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds());
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (log.elapsed_time_since_last_change_to_default_match !=
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      default_time_delta) {
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    omnibox_event->set_duration_since_last_default_match_update_ms(
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        log.elapsed_time_since_last_change_to_default_match.InMilliseconds());
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_current_page_classification(
1416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      log.current_page_classification);
142f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  omnibox_event->set_input_type(log.input_type);
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // We consider a paste-and-search/paste-and-go action to have a closed popup
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // (as explained in omnibox_event.proto) even if it was not, because such
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // actions ignore the contents of the popup so it doesn't matter that it was
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // open.
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const bool consider_popup_open = log.is_popup_open && !log.is_paste_and_go;
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_is_popup_open(consider_popup_open);
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  omnibox_event->set_is_paste_and_go(log.is_paste_and_go);
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (consider_popup_open) {
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    omnibox_event->set_is_top_result_hidden_in_dropdown(
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        log.result.ShouldHideTopMatch());
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (AutocompleteResult::const_iterator i(log.result.begin());
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)       i != log.result.end(); ++i) {
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion();
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    suggestion->set_provider(i->provider->AsOmniboxEventProviderType());
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    suggestion->set_result_type(AsOmniboxEventResultType(i->type));
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    suggestion->set_relevance(i->relevance);
161cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (i->typed_count != -1)
162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      suggestion->set_typed_count(i->typed_count);
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (ProvidersInfo::const_iterator i(log.providers_info.begin());
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)       i != log.providers_info.end(); ++i) {
166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    OmniboxEventProto::ProviderInfo* provider_info =
167cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        omnibox_event->add_provider_info();
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    provider_info->CopyFrom(*i);
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
171