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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
// Copyright 2019 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/optimization_guide/core/optimization_guide_util.h"
#include "base/containers/flat_set.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "components/optimization_guide/core/hints/optimization_guide_decision.h"
#include "components/optimization_guide/core/model_execution/feature_keys.h"
#include "components/optimization_guide/core/optimization_guide_enums.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_logger.h"
#include "components/optimization_guide/core/optimization_guide_prefs.h"
#include "components/prefs/pref_service.h"
#include "google_apis/common/api_key_request_util.h"
#include "net/base/url_util.h"
#include "net/http/http_request_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "url/url_canon.h"
namespace {
constexpr char kAuthHeaderBearer[] = "Bearer ";
constexpr char kServerTimeoutHeader[] = "X-Server-Timeout";
optimization_guide::proto::Platform GetPlatform() {
#if BUILDFLAG(IS_WIN)
return optimization_guide::proto::PLATFORM_WINDOWS;
#elif BUILDFLAG(IS_IOS)
return optimization_guide::proto::PLATFORM_IOS;
#elif BUILDFLAG(IS_MAC)
return optimization_guide::proto::PLATFORM_MAC;
#elif BUILDFLAG(IS_CHROMEOS)
return optimization_guide::proto::PLATFORM_CHROMEOS;
#elif BUILDFLAG(IS_ANDROID)
return optimization_guide::proto::PLATFORM_ANDROID;
#elif BUILDFLAG(IS_LINUX)
return optimization_guide::proto::PLATFORM_LINUX;
#else
return optimization_guide::proto::PLATFORM_UNDEFINED;
#endif
}
} // namespace
namespace optimization_guide {
std::string_view GetStringNameForModelExecutionFeature(
std::optional<UserVisibleFeatureKey> feature) {
if (!feature) {
return GetStringNameForModelExecutionFeature(
proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_UNSPECIFIED);
}
return GetStringNameForModelExecutionFeature(
ToModelExecutionFeatureProto(*feature));
}
std::string_view GetStringNameForModelExecutionFeature(
ModelBasedCapabilityKey feature) {
return GetStringNameForModelExecutionFeature(
ToModelExecutionFeatureProto(feature));
}
std::string_view GetStringNameForModelExecutionFeature(
proto::ModelExecutionFeature feature) {
switch (feature) {
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_WALLPAPER_SEARCH:
return "WallpaperSearch";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_TAB_ORGANIZATION:
return "TabOrganization";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_COMPOSE:
return "Compose";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_TEST:
return "Test";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_TEXT_SAFETY:
return "TextSafety";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_PROMPT_API:
return "PromptApi";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_SUMMARIZE:
return "Summarize";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_HISTORY_SEARCH:
return "HistorySearch";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_HISTORY_QUERY_INTENT:
return "HistoryQueryIntent";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_FORMS_CLASSIFICATIONS:
return "FormsClassifications";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_BLING_PROTOTYPING:
return "BlingPrototyping";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_PASSWORD_CHANGE_SUBMISSION:
return "PasswordChangeSubmission";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_SCAM_DETECTION:
return "ScamDetection";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_PERMISSIONS_AI:
return "PermissionsAi";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_WRITING_ASSISTANCE_API:
return "WritingAssistanceApi";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_ENHANCED_CALENDAR:
return "EnhancedCalendar";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_ZERO_STATE_SUGGESTIONS:
return "ZeroStateSuggestions";
case proto::ModelExecutionFeature::
MODEL_EXECUTION_FEATURE_PROOFREADER_API:
return "ProofreaderApi";
case proto::ModelExecutionFeature::MODEL_EXECUTION_FEATURE_UNSPECIFIED:
return "Unknown";
// Must be in sync with the ModelExecutionFeature variant in
// optimization/histograms.xml for metric recording. The output may also
// be used for storing other persistent data (e.g., prefs).
}
}
bool IsHostValidToFetchFromRemoteOptimizationGuide(const std::string& host) {
if (net::HostStringIsLocalhost(host)) {
return false;
}
url::CanonHostInfo host_info;
std::string canonicalized_host(net::CanonicalizeHost(host, &host_info));
if (host_info.IsIPAddress() ||
!net::IsCanonicalizedHostCompliant(canonicalized_host)) {
return false;
}
return true;
}
std::string GetStringForOptimizationGuideDecision(
OptimizationGuideDecision decision) {
switch (decision) {
case OptimizationGuideDecision::kUnknown:
return "Unknown";
case OptimizationGuideDecision::kTrue:
return "True";
case OptimizationGuideDecision::kFalse:
return "False";
}
NOTREACHED();
}
optimization_guide::proto::OriginInfo GetClientOriginInfo() {
optimization_guide::proto::OriginInfo origin_info;
origin_info.set_platform(GetPlatform());
return origin_info;
}
void LogFeatureFlagsInfo(OptimizationGuideLogger* optimization_guide_logger,
bool is_off_the_record,
PrefService* pref_service) {
if (!optimization_guide::switches::IsDebugLogsEnabled()) {
return;
}
if (!optimization_guide::features::IsOptimizationHintsEnabled()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::SERVICE_AND_SETTINGS,
optimization_guide_logger, "FEATURE_FLAG Hints component disabled");
}
if (!optimization_guide::features::IsRemoteFetchingEnabled()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::SERVICE_AND_SETTINGS,
optimization_guide_logger,
"FEATURE_FLAG remote fetching feature disabled");
}
if (!optimization_guide::IsUserPermittedToFetchFromRemoteOptimizationGuide(
is_off_the_record, pref_service)) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::SERVICE_AND_SETTINGS,
optimization_guide_logger,
"FEATURE_FLAG remote fetching user permission disabled");
}
if (!optimization_guide::features::IsPushNotificationsEnabled()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::SERVICE_AND_SETTINGS,
optimization_guide_logger,
"FEATURE_FLAG remote push notification feature disabled");
}
if (!optimization_guide::features::IsModelDownloadingEnabled()) {
OPTIMIZATION_GUIDE_LOG(
optimization_guide_common::mojom::LogSource::SERVICE_AND_SETTINGS,
optimization_guide_logger,
"FEATURE_FLAG model downloading feature disabled");
}
}
void PopulateAuthorizationRequestHeader(
network::ResourceRequest* resource_request,
std::string_view access_token) {
CHECK(!access_token.empty());
resource_request->headers.SetHeader(
net::HttpRequestHeaders::kAuthorization,
base::StrCat({kAuthHeaderBearer, access_token}));
}
void PopulateApiKeyRequestHeader(network::ResourceRequest* resource_request,
std::string_view api_key) {
CHECK(!api_key.empty());
google_apis::AddAPIKeyToRequest(*resource_request, api_key);
}
void PopulateServerTimeoutRequestHeader(
network::ResourceRequest* resource_request,
base::TimeDelta timeout) {
CHECK(timeout.is_positive());
resource_request->headers.SetHeader(
kServerTimeoutHeader, base::NumberToString(timeout.InSeconds()));
}
bool ShouldStartModelValidator() {
return switches::ShouldValidateModel() ||
switches::ShouldValidateModelExecution() ||
switches::GetOnDeviceValidationRequestOverride();
}
} // namespace optimization_guide
|