| // Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "components/omnibox/browser/omnibox_metrics_provider.h" |
| |
| #include <string> |
| #include <vector> |
| |
| #include "base/bind.h" |
| #include "base/strings/string_split.h" |
| #include "base/strings/string_util.h" |
| #include "build/build_config.h" |
| #include "components/metrics/metrics_log.h" |
| #include "components/omnibox/browser/autocomplete_match.h" |
| #include "components/omnibox/browser/autocomplete_provider.h" |
| #include "components/omnibox/browser/autocomplete_result.h" |
| #include "components/omnibox/browser/omnibox_log.h" |
| #include "third_party/metrics_proto/omnibox_event.pb.h" |
| #include "third_party/metrics_proto/omnibox_input_type.pb.h" |
| |
| using metrics::OmniboxEventProto; |
| |
| namespace { |
| |
| } // namespace |
| |
| OmniboxMetricsProvider::OmniboxMetricsProvider() {} |
| |
| OmniboxMetricsProvider::~OmniboxMetricsProvider() { |
| } |
| |
| void OmniboxMetricsProvider::OnRecordingEnabled() { |
| subscription_ = OmniboxEventGlobalTracker::GetInstance()->RegisterCallback( |
| base::BindRepeating(&OmniboxMetricsProvider::OnURLOpenedFromOmnibox, |
| base::Unretained(this))); |
| } |
| |
| void OmniboxMetricsProvider::OnRecordingDisabled() { |
| subscription_ = {}; |
| } |
| |
| void OmniboxMetricsProvider::ProvideCurrentSessionData( |
| metrics::ChromeUserMetricsExtension* uma_proto) { |
| uma_proto->mutable_omnibox_event()->Swap( |
| omnibox_events_cache.mutable_omnibox_event()); |
| } |
| |
| void OmniboxMetricsProvider::OnURLOpenedFromOmnibox(OmniboxLog* log) { |
| RecordOmniboxOpenedURL(*log); |
| } |
| |
| void OmniboxMetricsProvider::RecordOmniboxOpenedURL(const OmniboxLog& log) { |
| std::vector<base::StringPiece16> terms = base::SplitStringPiece( |
| log.text, base::kWhitespaceUTF16, |
| base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| |
| OmniboxEventProto* omnibox_event = omnibox_events_cache.add_omnibox_event(); |
| omnibox_event->set_time_sec(metrics::MetricsLog::GetCurrentTime()); |
| if (log.tab_id.is_valid()) { |
| // If we know what tab the autocomplete URL was opened in, log it. |
| omnibox_event->set_tab_id(log.tab_id.id()); |
| } |
| omnibox_event->set_typed_length(log.text.length()); |
| omnibox_event->set_just_deleted_text(log.just_deleted_text); |
| omnibox_event->set_num_typed_terms(static_cast<int>(terms.size())); |
| omnibox_event->set_selected_index(log.selected_index); |
| omnibox_event->set_selected_tab_match(log.disposition == |
| WindowOpenDisposition::SWITCH_TO_TAB); |
| if (log.completed_length != std::u16string::npos) |
| omnibox_event->set_completed_length(log.completed_length); |
| const base::TimeDelta default_time_delta = base::Milliseconds(-1); |
| if (log.elapsed_time_since_user_first_modified_omnibox != |
| default_time_delta) { |
| // Only upload the typing duration if it is set/valid. |
| omnibox_event->set_typing_duration_ms( |
| log.elapsed_time_since_user_first_modified_omnibox.InMilliseconds()); |
| } |
| if (log.elapsed_time_since_last_change_to_default_match != |
| default_time_delta) { |
| omnibox_event->set_duration_since_last_default_match_update_ms( |
| log.elapsed_time_since_last_change_to_default_match.InMilliseconds()); |
| } |
| omnibox_event->set_current_page_classification( |
| log.current_page_classification); |
| omnibox_event->set_input_type(log.input_type); |
| // We consider a paste-and-search/paste-and-go action to have a closed popup |
| // (as explained in omnibox_event.proto) even if it was not, because such |
| // actions ignore the contents of the popup so it doesn't matter that it was |
| // open. |
| omnibox_event->set_is_popup_open(log.is_popup_open && !log.is_paste_and_go); |
| omnibox_event->set_is_paste_and_go(log.is_paste_and_go); |
| |
| for (auto i(log.result.begin()); i != log.result.end(); ++i) { |
| OmniboxEventProto::Suggestion* suggestion = omnibox_event->add_suggestion(); |
| const auto provider_type = i->provider->AsOmniboxEventProviderType(); |
| suggestion->set_provider(provider_type); |
| suggestion->set_result_type(i->AsOmniboxEventResultType()); |
| suggestion->set_relevance(i->relevance); |
| if (i->typed_count != -1) |
| suggestion->set_typed_count(i->typed_count); |
| |
| // TODO(https://crbug.com/1103056): send the entire set of subtypes. |
| if (!i->subtypes.empty()) { |
| suggestion->set_result_subtype_identifier(*i->subtypes.begin()); |
| } |
| |
| suggestion->set_has_tab_match(i->has_tab_match); |
| suggestion->set_is_keyword_suggestion(i->from_keyword); |
| } |
| for (auto i(log.providers_info.begin()); i != log.providers_info.end(); ++i) { |
| OmniboxEventProto::ProviderInfo* provider_info = |
| omnibox_event->add_provider_info(); |
| provider_info->CopyFrom(*i); |
| } |
| omnibox_event->set_in_keyword_mode(log.in_keyword_mode); |
| if (log.in_keyword_mode) |
| omnibox_event->set_keyword_mode_entry_method(log.keyword_mode_entry_method); |
| if (log.is_query_started_from_tile) |
| omnibox_event->set_is_query_started_from_tile(true); |
| for (auto feature : log.feature_triggered_in_session) { |
| omnibox_event->add_feature_triggered_in_session( |
| static_cast<size_t>(feature)); |
| } |
| } |