1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/app_list/search/federated_metrics_manager.h"
#include "ash/constants/ash_features.h"
#include "ash/shell.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chromeos/ash/services/federated/public/cpp/federated_example_util.h"
#include "chromeos/ash/services/federated/public/mojom/example.mojom.h"
#include "chromeos/ash/services/federated/public/mojom/federated_service.mojom.h"
#include "chromeos/ash/services/federated/public/mojom/tables.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace app_list::federated {
namespace {
using ash::federated::CreateStringList;
using chromeos::federated::mojom::Example;
using chromeos::federated::mojom::ExamplePtr;
using chromeos::federated::mojom::Features;
using chromeos::federated::mojom::FederatedExampleTableId;
void LogSearchSessionConclusion(ash::SearchSessionConclusion conclusion) {
base::UmaHistogramEnumeration(kHistogramSearchSessionConclusion, conclusion);
}
void LogInitStatus(FederatedMetricsManager::InitStatus status) {
base::UmaHistogramEnumeration(kHistogramInitStatus, status);
}
void LogReportStatus(FederatedMetricsManager::ReportStatus status) {
base::UmaHistogramEnumeration(kHistogramReportStatus, status);
}
void LogQueryLength(int query_length) {
base::UmaHistogramExactLinear(kHistogramQueryLengthOnStorageSuccess,
query_length,
kMaxLoggedQueryLengthOnStorageSuccess);
}
ExamplePtr CreateExamplePtr(const std::string& query,
const std::string& user_action) {
ExamplePtr example = Example::New();
example->features = Features::New();
auto& feature_map = example->features->feature;
// TODO(b/262611120): To be decided: Conversion to lowercase, white space
// stripping, truncation, etc. of query.
feature_map["query"] = CreateStringList({query});
feature_map["user_action"] = CreateStringList({user_action});
return example;
}
} // namespace
FederatedMetricsManager::FederatedMetricsManager(
ash::AppListNotifier* notifier,
ash::federated::FederatedServiceController* controller)
: controller_(controller) {
if (!notifier) {
LogInitStatus(InitStatus::kMissingNotifier);
return;
}
if (!controller_) {
LogInitStatus(InitStatus::kMissingController);
return;
}
// Observe notifier only after all init checks have suceeded.
observation_.Observe(notifier);
LogInitStatus(InitStatus::kOk);
}
FederatedMetricsManager::~FederatedMetricsManager() = default;
void FederatedMetricsManager::OnSearchSessionStarted() {
if (!IsLoggingEnabled()) {
return;
}
session_active_ = true;
}
void FederatedMetricsManager::OnSearchSessionEnded(
const std::u16string& query) {
if (!IsLoggingEnabled() || query.empty() || !session_active_) {
return;
}
// UMA logging:
LogSearchSessionConclusion(session_result_);
// Federated logging:
LogExample(base::UTF16ToUTF8(query));
session_result_ = ash::SearchSessionConclusion::kQuit;
session_active_ = false;
}
void FederatedMetricsManager::OnSeen(Location location,
const std::vector<Result>& results,
const std::u16string& query) {
if (!IsLoggingEnabled() || query.empty()) {
return;
}
if (location == Location::kAnswerCard) {
DCHECK(session_active_);
session_result_ = ash::SearchSessionConclusion::kAnswerCardSeen;
}
}
void FederatedMetricsManager::OnLaunch(Location location,
const Result& launched,
const std::vector<Result>& shown,
const std::u16string& query) {
if (!IsLoggingEnabled() || query.empty()) {
return;
}
if (location == Location::kList || location == Location::kAnswerCard) {
DCHECK(session_active_);
session_result_ = ash::SearchSessionConclusion::kLaunch;
}
}
void FederatedMetricsManager::OnDefaultSearchIsGoogleSet(bool is_google) {
is_default_search_engine_google_ = is_google;
}
bool FederatedMetricsManager::IsFederatedServiceAvailable() {
return controller_ && controller_->IsServiceAvailable();
}
bool FederatedMetricsManager::IsLoggingEnabled() {
CHECK(is_default_search_engine_google_.has_value());
return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled() &&
is_default_search_engine_google_.value();
}
void FederatedMetricsManager::TryToBindFederatedServiceIfNecessary() {
if (federated_service_.is_bound()) {
return;
}
if (IsFederatedServiceAvailable()) {
ash::federated::ServiceConnection::GetInstance()->BindReceiver(
federated_service_.BindNewPipeAndPassReceiver());
}
}
void FederatedMetricsManager::LogExample(const std::string& query) {
if (!IsLoggingEnabled()) {
return;
}
TryToBindFederatedServiceIfNecessary();
if (!IsFederatedServiceAvailable()) {
LogReportStatus(ReportStatus::kFederatedServiceNotAvailable);
} else if (!federated_service_.is_connected()) {
LogReportStatus(ReportStatus::kFederatedServiceNotConnected);
} else {
// Federated service is available and connected.
// Store example for launcher FA version 1.
ExamplePtr example_1 = CreateExamplePtr(
query, ash::SearchSessionConclusionToString(session_result_));
federated_service_->ReportExampleToTable(
FederatedExampleTableId::LAUNCHER_QUERY, std::move(example_1));
// Store example for launcher FA version 2.
// TODO(b/318575870): De-duplicate query collection once support is
// available on the infrastructure side.
ExamplePtr example_2 = CreateExamplePtr(
query, ash::SearchSessionConclusionToString(session_result_));
federated_service_->ReportExampleToTable(
FederatedExampleTableId::LAUNCHER_QUERY_V2, std::move(example_2));
LogReportStatus(ReportStatus::kOk);
LogQueryLength(query.length());
}
}
} // namespace app_list::federated
|