optimization_guide_switches.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/optimization_guide/core/optimization_guide_switches.h"
  5. #include "base/base64.h"
  6. #include "base/command_line.h"
  7. #include "base/logging.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "base/strings/string_split.h"
  10. #include "build/build_config.h"
  11. #include "components/optimization_guide/proto/hints.pb.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace optimization_guide {
  14. namespace switches {
  15. // Overrides the Hints Protobuf that would come from the component updater. If
  16. // the value of this switch is invalid, regular hint processing is used.
  17. // The value of this switch should be a base64 encoding of a binary
  18. // Configuration message, found in optimization_guide's hints.proto. Providing a
  19. // valid value to this switch causes Chrome startup to block on hints parsing.
  20. const char kHintsProtoOverride[] = "optimization_guide_hints_override";
  21. // Overrides scheduling and time delays for fetching hints and causes a hints
  22. // fetch immediately on start up using the provided comma separate lists of
  23. // hosts.
  24. const char kFetchHintsOverride[] = "optimization-guide-fetch-hints-override";
  25. // Overrides the hints fetch scheduling and delay, causing a hints fetch
  26. // immediately on start up using the TopHostProvider. This is meant for testing.
  27. const char kFetchHintsOverrideTimer[] =
  28. "optimization-guide-fetch-hints-override-timer";
  29. // Overrides the Optimization Guide Service URL that the HintsFetcher will
  30. // request remote hints from.
  31. const char kOptimizationGuideServiceGetHintsURL[] =
  32. "optimization-guide-service-get-hints-url";
  33. // Overrides the Optimization Guide Service URL that the PredictionModelFetcher
  34. // will request remote models and host features from.
  35. const char kOptimizationGuideServiceGetModelsURL[] =
  36. "optimization-guide-service-get-models-url";
  37. // Overrides the Optimization Guide Service API Key for remote requests to be
  38. // made.
  39. const char kOptimizationGuideServiceAPIKey[] =
  40. "optimization-guide-service-api-key";
  41. // Purges the store containing fetched and component hints on startup, so that
  42. // it's guaranteed to be using fresh data.
  43. const char kPurgeHintsStore[] = "purge-optimization-guide-store";
  44. // Purges the store containing prediction medels and host model features on
  45. // startup, so that it's guaranteed to be using fresh data.
  46. const char kPurgeModelAndFeaturesStore[] = "purge-model-and-features-store";
  47. const char kDisableFetchingHintsAtNavigationStartForTesting[] =
  48. "disable-fetching-hints-at-navigation-start";
  49. const char kDisableCheckingUserPermissionsForTesting[] =
  50. "disable-checking-optimization-guide-user-permissions";
  51. const char kDisableModelDownloadVerificationForTesting[] =
  52. "disable-model-download-verification";
  53. const char kDebugLoggingEnabled[] = "enable-optimization-guide-debug-logs";
  54. // Disables the fetching of models and overrides the file path and metadata to
  55. // be used for the session to use what's passed via command-line instead of what
  56. // is already stored.
  57. //
  58. // We expect that the string be a comma-separated string of model overrides with
  59. // each model override be: OPTIMIZATION_TARGET_STRING:file_path or
  60. // OPTIMIZATION_TARGET_STRING:file_path:base64_encoded_any_proto_model_metadata.
  61. //
  62. // It is possible this only works on Desktop since file paths are less easily
  63. // accessible on Android, but may work.
  64. const char kModelOverride[] = "optimization-guide-model-override";
  65. // Triggers validation of the model. Used for manual testing.
  66. const char kModelValidate[] = "optimization-guide-model-validate";
  67. const char kPageContentAnnotationsLoggingEnabled[] =
  68. "enable-page-content-annotations-logging";
  69. const char kPageContentAnnotationsValidationStartupDelaySeconds[] =
  70. "page-content-annotations-validation-startup-delay-seconds";
  71. const char kPageContentAnnotationsValidationBatchSizeOverride[] =
  72. "page-content-annotations-validation-batch-size";
  73. // Enables the specific annotation type to run validation at startup after a
  74. // delay. A comma separated list of inputs can be given as a value which will be
  75. // used as input for the validation job.
  76. const char kPageContentAnnotationsValidationPageTopics[] =
  77. "page-content-annotations-validation-page-topics";
  78. const char kPageContentAnnotationsValidationPageEntities[] =
  79. "page-content-annotations-validation-page-entities";
  80. const char kPageContentAnnotationsValidationContentVisibility[] =
  81. "page-content-annotations-validation-content-visibility";
  82. // Writes the output of page content annotation validations to the given file.
  83. const char kPageContentAnnotationsValidationWriteToFile[] =
  84. "page-content-annotations-validation-write-to-file";
  85. bool IsHintComponentProcessingDisabled() {
  86. return base::CommandLine::ForCurrentProcess()->HasSwitch(kHintsProtoOverride);
  87. }
  88. bool ShouldPurgeOptimizationGuideStoreOnStartup() {
  89. base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  90. return cmd_line->HasSwitch(kHintsProtoOverride) ||
  91. cmd_line->HasSwitch(kPurgeHintsStore);
  92. }
  93. bool ShouldPurgeModelAndFeaturesStoreOnStartup() {
  94. base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  95. return cmd_line->HasSwitch(kPurgeModelAndFeaturesStore);
  96. }
  97. bool IsDebugLogsEnabled() {
  98. base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  99. return cmd_line->HasSwitch(kDebugLoggingEnabled);
  100. }
  101. // Parses a list of hosts to have hints fetched for. This overrides scheduling
  102. // of the first hints fetch and forces it to occur immediately. If no hosts are
  103. // provided, nullopt is returned.
  104. absl::optional<std::vector<std::string>>
  105. ParseHintsFetchOverrideFromCommandLine() {
  106. base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  107. if (!cmd_line->HasSwitch(kFetchHintsOverride))
  108. return absl::nullopt;
  109. std::string override_hosts_value =
  110. cmd_line->GetSwitchValueASCII(kFetchHintsOverride);
  111. std::vector<std::string> hosts =
  112. base::SplitString(override_hosts_value, ",", base::TRIM_WHITESPACE,
  113. base::SPLIT_WANT_NONEMPTY);
  114. if (hosts.size() == 0)
  115. return absl::nullopt;
  116. return hosts;
  117. }
  118. bool ShouldOverrideFetchHintsTimer() {
  119. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  120. kFetchHintsOverrideTimer);
  121. }
  122. std::unique_ptr<optimization_guide::proto::Configuration>
  123. ParseComponentConfigFromCommandLine() {
  124. base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  125. if (!cmd_line->HasSwitch(kHintsProtoOverride))
  126. return nullptr;
  127. std::string b64_pb = cmd_line->GetSwitchValueASCII(kHintsProtoOverride);
  128. std::string binary_pb;
  129. if (!base::Base64Decode(b64_pb, &binary_pb)) {
  130. LOG(ERROR) << "Invalid base64 encoding of the Hints Proto Override";
  131. return nullptr;
  132. }
  133. std::unique_ptr<optimization_guide::proto::Configuration>
  134. proto_configuration =
  135. std::make_unique<optimization_guide::proto::Configuration>();
  136. if (!proto_configuration->ParseFromString(binary_pb)) {
  137. LOG(ERROR) << "Invalid proto provided to the Hints Proto Override";
  138. return nullptr;
  139. }
  140. return proto_configuration;
  141. }
  142. bool DisableFetchingHintsAtNavigationStartForTesting() {
  143. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  144. return command_line->HasSwitch(
  145. kDisableFetchingHintsAtNavigationStartForTesting);
  146. }
  147. bool ShouldOverrideCheckingUserPermissionsToFetchHintsForTesting() {
  148. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  149. return command_line->HasSwitch(kDisableCheckingUserPermissionsForTesting);
  150. }
  151. bool ShouldSkipModelDownloadVerificationForTesting() {
  152. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  153. return command_line->HasSwitch(kDisableModelDownloadVerificationForTesting);
  154. }
  155. bool IsModelOverridePresent() {
  156. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  157. return command_line->HasSwitch(kModelOverride);
  158. }
  159. bool ShouldValidateModel() {
  160. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  161. return command_line->HasSwitch(kModelValidate);
  162. }
  163. absl::optional<std::string> GetModelOverride() {
  164. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  165. if (!command_line->HasSwitch(kModelOverride))
  166. return absl::nullopt;
  167. return command_line->GetSwitchValueASCII(kModelOverride);
  168. }
  169. bool ShouldLogPageContentAnnotationsInput() {
  170. return base::CommandLine::ForCurrentProcess()->HasSwitch(
  171. kPageContentAnnotationsLoggingEnabled);
  172. }
  173. absl::optional<base::TimeDelta> PageContentAnnotationsValidationStartupDelay() {
  174. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  175. if (!command_line->HasSwitch(
  176. kPageContentAnnotationsValidationStartupDelaySeconds)) {
  177. return absl::nullopt;
  178. }
  179. std::string value = command_line->GetSwitchValueASCII(
  180. kPageContentAnnotationsValidationStartupDelaySeconds);
  181. size_t seconds = 0;
  182. if (base::StringToSizeT(value, &seconds)) {
  183. return base::Seconds(seconds);
  184. }
  185. return absl::nullopt;
  186. }
  187. absl::optional<size_t> PageContentAnnotationsValidationBatchSize() {
  188. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  189. if (!command_line->HasSwitch(
  190. kPageContentAnnotationsValidationBatchSizeOverride)) {
  191. return absl::nullopt;
  192. }
  193. std::string value = command_line->GetSwitchValueASCII(
  194. kPageContentAnnotationsValidationBatchSizeOverride);
  195. size_t size = 0;
  196. if (base::StringToSizeT(value, &size)) {
  197. return size;
  198. }
  199. return absl::nullopt;
  200. }
  201. bool LogPageContentAnnotationsValidationToConsole() {
  202. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  203. return command_line->HasSwitch(kPageContentAnnotationsValidationPageTopics) ||
  204. command_line->HasSwitch(
  205. kPageContentAnnotationsValidationPageEntities) ||
  206. command_line->HasSwitch(
  207. kPageContentAnnotationsValidationContentVisibility);
  208. }
  209. absl::optional<std::vector<std::string>>
  210. PageContentAnnotationsValidationInputForType(AnnotationType type) {
  211. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  212. std::string value;
  213. switch (type) {
  214. case AnnotationType::kPageTopics:
  215. value = command_line->GetSwitchValueASCII(
  216. kPageContentAnnotationsValidationPageTopics);
  217. break;
  218. case AnnotationType::kPageEntities:
  219. value = command_line->GetSwitchValueASCII(
  220. kPageContentAnnotationsValidationPageEntities);
  221. break;
  222. case AnnotationType::kContentVisibility:
  223. value = command_line->GetSwitchValueASCII(
  224. kPageContentAnnotationsValidationContentVisibility);
  225. break;
  226. default:
  227. break;
  228. }
  229. if (value.empty()) {
  230. return absl::nullopt;
  231. }
  232. return base::SplitString(value, ",", base::KEEP_WHITESPACE,
  233. base::SPLIT_WANT_ALL);
  234. }
  235. absl::optional<base::FilePath> PageContentAnnotationsValidationWriteToFile() {
  236. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  237. if (!command_line->HasSwitch(kPageContentAnnotationsValidationWriteToFile)) {
  238. return absl::nullopt;
  239. }
  240. return command_line->GetSwitchValuePath(
  241. kPageContentAnnotationsValidationWriteToFile);
  242. }
  243. } // namespace switches
  244. } // namespace optimization_guide