web_engine_config.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2022 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 "fuchsia_web/webengine/browser/web_engine_config.h"
  5. #include "base/base_switches.h"
  6. #include "base/command_line.h"
  7. #include "base/containers/contains.h"
  8. #include "base/logging.h"
  9. #include "base/metrics/field_trial.h"
  10. #include "base/strings/strcat.h"
  11. #include "base/values.h"
  12. #include "cc/base/switches.h"
  13. #include "components/embedder_support/switches.h"
  14. #include "components/viz/common/features.h"
  15. #include "components/viz/common/switches.h"
  16. #include "content/public/common/content_switches.h"
  17. #include "fuchsia_web/webengine/switches.h"
  18. #include "gpu/command_buffer/service/gpu_switches.h"
  19. #include "gpu/config/gpu_switches.h"
  20. #include "media/base/media_switches.h"
  21. #include "third_party/blink/public/common/switches.h"
  22. #include "ui/display/display_switches.h"
  23. #include "ui/gl/gl_switches.h"
  24. #include "ui/ozone/public/ozone_switches.h"
  25. namespace {
  26. // Appends `value` to the value of `switch_name` in the `command_line`.
  27. // The switch is assumed to consist of comma-separated values. If `switch_name`
  28. // is already set in `command_line` then a comma will be appended, followed by
  29. // `value`, otherwise the switch will be set to `value`.
  30. void AppendToSwitch(base::StringPiece switch_name,
  31. base::StringPiece value,
  32. base::CommandLine* command_line) {
  33. if (!command_line->HasSwitch(switch_name)) {
  34. command_line->AppendSwitchNative(switch_name, value);
  35. return;
  36. }
  37. std::string new_value = base::StrCat(
  38. {command_line->GetSwitchValueASCII(switch_name), ",", value});
  39. command_line->RemoveSwitch(switch_name);
  40. command_line->AppendSwitchNative(switch_name, new_value);
  41. }
  42. bool AddCommandLineArgsFromConfig(const base::Value& config,
  43. base::CommandLine* command_line) {
  44. const base::Value::Dict* args =
  45. config.GetDict().FindDict("command-line-args");
  46. if (!args)
  47. return true;
  48. static const base::StringPiece kAllowedArgs[] = {
  49. blink::switches::kSharedArrayBufferAllowedOrigins,
  50. blink::switches::kGpuRasterizationMSAASampleCount,
  51. blink::switches::kMinHeightForGpuRasterTile,
  52. cc::switches::kEnableClippedImageScaling,
  53. cc::switches::kEnableGpuBenchmarking,
  54. embedder_support::kOriginTrialPublicKey,
  55. embedder_support::kOriginTrialDisabledFeatures,
  56. embedder_support::kOriginTrialDisabledTokens,
  57. switches::kDisableFeatures,
  58. switches::kDisableGpuWatchdog,
  59. switches::kDisableMipmapGeneration,
  60. // TODO(crbug.com/1082821): Remove this switch from the allow-list.
  61. switches::kEnableCastStreamingReceiver,
  62. switches::kEnableFeatures,
  63. switches::kEnableLowEndDeviceMode,
  64. switches::kForceDeviceScaleFactor,
  65. switches::kForceGpuMemAvailableMb,
  66. switches::kForceGpuMemDiscardableLimitMb,
  67. switches::kForceMaxTextureSize,
  68. switches::kGoogleApiKey,
  69. switches::kMaxDecodedImageSizeMb,
  70. switches::kOzonePlatform,
  71. switches::kRendererProcessLimit,
  72. switches::kUseCmdDecoder,
  73. switches::kV,
  74. switches::kVModule,
  75. switches::kVulkanHeapMemoryLimitMb,
  76. switches::kVulkanSyncCpuMemoryLimitMb,
  77. switches::kWebglAntialiasingMode,
  78. switches::kWebglMSAASampleCount,
  79. };
  80. for (const auto arg : *args) {
  81. if (!base::Contains(kAllowedArgs, arg.first)) {
  82. // TODO(https://crbug.com/1032439): Increase severity and return false
  83. // once we have a mechanism for soft transitions of supported arguments.
  84. LOG(WARNING) << "Unknown command-line arg: '" << arg.first
  85. << "'. Config file and WebEngine version may not match.";
  86. continue;
  87. }
  88. if (arg.first == switches::kEnableFeatures ||
  89. arg.first == switches::kDisableFeatures) {
  90. if (!arg.second.is_string()) {
  91. LOG(ERROR) << "Config command-line arg must be a string: " << arg.first;
  92. return false;
  93. }
  94. // Merge the features.
  95. AppendToSwitch(arg.first, arg.second.GetString(), command_line);
  96. continue;
  97. }
  98. if (command_line->HasSwitch(arg.first)) {
  99. // Use the existing command line value rather than override it.
  100. continue;
  101. }
  102. if (arg.second.is_none()) {
  103. command_line->AppendSwitch(arg.first);
  104. continue;
  105. }
  106. if (arg.second.is_string()) {
  107. command_line->AppendSwitchNative(arg.first, arg.second.GetString());
  108. continue;
  109. }
  110. LOG(ERROR) << "Config command-line arg must be a string: " << arg.first;
  111. return false;
  112. }
  113. return true;
  114. }
  115. } // namespace
  116. bool UpdateCommandLineFromConfigFile(const base::Value& config,
  117. base::CommandLine* command_line) {
  118. // The FieldTrialList should be initialized only after config is loaded.
  119. CHECK(!base::FieldTrialList::GetInstance());
  120. if (!AddCommandLineArgsFromConfig(config, command_line))
  121. return false;
  122. // The following two args are set by calling component. They are used to set
  123. // other flags below.
  124. const bool playready_enabled =
  125. command_line->HasSwitch(switches::kPlayreadyKeySystem);
  126. const bool widevine_enabled =
  127. command_line->HasSwitch(switches::kEnableWidevine);
  128. const base::Value::Dict& dict = config.GetDict();
  129. const bool allow_protected_graphics =
  130. dict.FindBool("allow-protected-graphics").value_or(false);
  131. const bool force_protected_graphics =
  132. dict.FindBool("force-protected-graphics").value_or(false);
  133. const bool enable_protected_graphics =
  134. ((playready_enabled || widevine_enabled) && allow_protected_graphics) ||
  135. force_protected_graphics;
  136. const bool use_overlays_for_video =
  137. dict.FindBool("use-overlays-for-video").value_or(false);
  138. if (enable_protected_graphics) {
  139. command_line->AppendSwitch(switches::kEnableVulkanProtectedMemory);
  140. command_line->AppendSwitch(switches::kEnableProtectedVideoBuffers);
  141. const bool force_protected_video_buffers =
  142. dict.FindBool("force-protected-video-buffers").value_or(false);
  143. if (force_protected_video_buffers) {
  144. command_line->AppendSwitch(switches::kForceProtectedVideoOutputBuffers);
  145. }
  146. }
  147. if (use_overlays_for_video) {
  148. // Overlays are only available if OutputPresenterFuchsia is in use.
  149. AppendToSwitch(switches::kEnableFeatures,
  150. features::kUseSkiaOutputDeviceBufferQueue.name,
  151. command_line);
  152. AppendToSwitch(switches::kEnableFeatures,
  153. features::kUseRealBuffersForPageFlipTest.name, command_line);
  154. command_line->AppendSwitchASCII(switches::kEnableHardwareOverlays,
  155. "underlay");
  156. command_line->AppendSwitch(switches::kUseOverlaysForVideo);
  157. }
  158. return true;
  159. }