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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/actions/omnibox_action_in_suggest.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/actions/omnibox_action_concepts.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "components/omnibox/browser/actions/omnibox_action_factory_android.h"
#include "url/android/gurl_android.h"
#endif
namespace {
// UMA reported Type of ActionInSuggest.
//
// Automatically generate a corresponding Java enum:
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.omnibox.action
// GENERATED_JAVA_CLASS_NAME_OVERRIDE: ActionInSuggestUmaType
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused. The values should remain
// synchronized with the enum ActionInSuggestType in
// //tools/metrics/histograms/metadata/omnibox/enums.xml.
enum class ActionInSuggestUmaType {
kUnknown = 0,
kCall,
kDirections,
kWebsite,
kReviews,
// Sentinel value. Must be set to the last valid ActionInSuggestUmaType.
kMaxValue = kReviews
};
constexpr const char* ToUmaUsageHistogramName(
omnibox::ActionInfo::ActionType type) {
switch (type) {
case omnibox::ActionInfo_ActionType_CALL:
return "Omnibox.ActionInSuggest.UsageByType.Call";
case omnibox::ActionInfo_ActionType_DIRECTIONS:
return "Omnibox.ActionInSuggest.UsageByType.Directions";
case omnibox::ActionInfo_ActionType_REVIEWS:
return "Omnibox.ActionInSuggest.UsageByType.Reviews";
}
NOTREACHED() << "Unexpected type of Action: " << (int)type;
}
// Get the UMA action type from ActionInfo::ActionType.
constexpr ActionInSuggestUmaType ToUmaActionType(
omnibox::ActionInfo::ActionType action_type) {
switch (action_type) {
case omnibox::ActionInfo_ActionType_CALL:
return ActionInSuggestUmaType::kCall;
case omnibox::ActionInfo_ActionType_DIRECTIONS:
return ActionInSuggestUmaType::kDirections;
case omnibox::ActionInfo_ActionType_REVIEWS:
return ActionInSuggestUmaType::kReviews;
}
NOTREACHED() << "Unrecognized action type: " << action_type;
}
constexpr int ToActionHint(omnibox::ActionInfo::ActionType action_type) {
switch (action_type) {
case omnibox::ActionInfo_ActionType_CALL:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_CALL_HINT;
case omnibox::ActionInfo_ActionType_DIRECTIONS:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_DIRECTIONS_HINT;
case omnibox::ActionInfo_ActionType_REVIEWS:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_REVIEWS_HINT;
}
NOTREACHED() << "Unrecognized action type: " << action_type;
}
constexpr int ToActionContents(omnibox::ActionInfo::ActionType action_type) {
switch (action_type) {
case omnibox::ActionInfo_ActionType_CALL:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_CALL_CONTENTS;
case omnibox::ActionInfo_ActionType_DIRECTIONS:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_DIRECTIONS_CONTENTS;
case omnibox::ActionInfo_ActionType_REVIEWS:
return IDS_OMNIBOX_ACTION_IN_SUGGEST_REVIEWS_CONTENTS;
}
NOTREACHED() << "Unrecognized action type: " << action_type;
}
} // namespace
OmniboxActionInSuggest::OmniboxActionInSuggest(
omnibox::ActionInfo action_info,
std::optional<TemplateURLRef::SearchTermsArgs> search_terms_args)
: OmniboxAction(OmniboxAction::LabelStrings(
ToActionHint(action_info.action_type()),
ToActionContents(action_info.action_type()),
IDS_ACC_OMNIBOX_ACTION_IN_SUGGEST_SUFFIX,
ToActionContents(action_info.action_type())),
{}),
action_info{std::move(action_info)},
search_terms_args{std::move(search_terms_args)} {}
OmniboxActionInSuggest::~OmniboxActionInSuggest() = default;
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject>
OmniboxActionInSuggest::GetOrCreateJavaObject(JNIEnv* env) const {
if (!j_omnibox_action_) {
j_omnibox_action_.Reset(BuildOmniboxActionInSuggest(
env, reinterpret_cast<intptr_t>(this), strings_.hint,
strings_.accessibility_hint, action_info.action_type(),
action_info.action_uri()));
}
return base::android::ScopedJavaLocalRef<jobject>(j_omnibox_action_);
}
#endif
void OmniboxActionInSuggest::RecordActionShown(size_t position,
bool used) const {
RecordShownAndUsedMetrics(action_info.action_type(), used);
}
void OmniboxActionInSuggest::Execute(ExecutionContext& context) const {
// Note: this is platform-dependent.
// There's currently no code wiring ActionInSuggest on Desktop.
// TODO(crbug.com/40257536): log searchboxstats metrics.
NOTREACHED() << "Not implemented";
}
OmniboxActionId OmniboxActionInSuggest::ActionId() const {
return OmniboxActionId::ACTION_IN_SUGGEST;
}
// static
const OmniboxActionInSuggest* OmniboxActionInSuggest::FromAction(
const OmniboxAction* action) {
return FromAction(const_cast<OmniboxAction*>(action));
}
// static
OmniboxActionInSuggest* OmniboxActionInSuggest::FromAction(
OmniboxAction* action) {
if (action && action->ActionId() == OmniboxActionId::ACTION_IN_SUGGEST) {
return static_cast<OmniboxActionInSuggest*>(action);
}
return nullptr;
}
// static
void OmniboxActionInSuggest::RecordShownAndUsedMetrics(
omnibox::ActionInfo::ActionType type,
bool used) {
base::UmaHistogramEnumeration("Omnibox.ActionInSuggest.Shown",
ToUmaActionType(type));
if (used) {
base::UmaHistogramEnumeration("Omnibox.ActionInSuggest.Used",
ToUmaActionType(type));
}
base::UmaHistogramBoolean(ToUmaUsageHistogramName(type), used);
}
omnibox::ActionInfo::ActionType OmniboxActionInSuggest::Type() const {
return action_info.action_type();
}
|