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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
// Copyright 2012 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/ui/webui/theme_source.h"
#include <algorithm>
#include <string_view>
#include "base/functional/bind.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/timer/elapsed_timer.h"
#include "build/branding_buildflags.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resources_util.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/themes/browser_theme_pack.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/color/chrome_color_provider_utils.h"
#include "chrome/browser/ui/webui/current_channel_logo.h"
#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
#include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/theme_resources.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "net/base/url_util.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/resource_scale_factor.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_utils.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/grit/cros_styles_resources.h" // nogncheck crbug.com/1113869
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace {
GURL GetThemeUrl(const std::string& path) {
return GURL(std::string(content::kChromeUIScheme) + "://" +
std::string(chrome::kChromeUIThemeHost) + "/" + path);
}
bool IsNewTabCssPath(const std::string& path) {
static const char kNewTabThemeCssPath[] = "css/new_tab_theme.css";
static const char kIncognitoTabThemeCssPath[] = "css/incognito_tab_theme.css";
return path == kNewTabThemeCssPath || path == kIncognitoTabThemeCssPath;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ThemeSource, public:
ThemeSource::ThemeSource(Profile* profile)
: profile_(profile), serve_untrusted_(false) {}
ThemeSource::ThemeSource(Profile* profile, bool serve_untrusted)
: profile_(profile), serve_untrusted_(serve_untrusted) {}
ThemeSource::~ThemeSource() = default;
std::string ThemeSource::GetSource() {
return serve_untrusted_ ? chrome::kChromeUIUntrustedThemeURL
: chrome::kChromeUIThemeHost;
}
void ThemeSource::StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) {
// TODO(crbug.com/40050262): Simplify usages of |path| since |url| is
// available.
const std::string path = content::URLDataSource::URLToRequestPath(url);
// Default scale factor if not specified.
float scale = 1.0f;
// All frames by default if not specified.
int frame = -1;
std::string parsed_path;
webui::ParsePathAndImageSpec(GetThemeUrl(path), &parsed_path, &scale, &frame);
if (IsNewTabCssPath(parsed_path)) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
NTPResourceCache::WindowType type =
NTPResourceCache::GetWindowType(profile_);
NTPResourceCache* cache = NTPResourceCacheFactory::GetForProfile(profile_);
std::move(callback).Run(cache->GetNewTabCSS(type, wc_getter));
return;
}
// kColorsCssPath should stay consistent with COLORS_CSS_SELECTOR in
// colors_css_updater.js.
constexpr char kColorsCssPath[] = "colors.css";
if (parsed_path == kColorsCssPath) {
SendColorsCss(url, wc_getter, std::move(callback));
return;
}
#if BUILDFLAG(IS_CHROMEOS)
constexpr char kTypographyCssPath[] = "typography.css";
if (parsed_path == kTypographyCssPath) {
SendTypographyCss(std::move(callback));
return;
}
#endif
int resource_id = -1;
if (parsed_path == "current-channel-logo") {
resource_id = webui::CurrentChannelLogoResourceId();
} else {
resource_id = ResourcesUtil::GetThemeResourceId(parsed_path);
}
// Limit the maximum scale we'll respond to. Very large scale factors can
// take significant time to serve or, at worst, crash the browser due to OOM.
// We don't want to clamp to the max scale factor, though, for devices that
// use 2x scale without 2x data packs, as well as omnibox requests for larger
// (but still reasonable) scales (see below).
const float max_scale = ui::GetScaleForResourceScaleFactor(
ui::ResourceBundle::GetSharedInstance().GetMaxResourceScaleFactor());
const float unreasonable_scale = max_scale * 32;
// TODO(reveman): Add support frames beyond 0 (crbug.com/750064).
if ((resource_id == -1) || (scale >= unreasonable_scale) || (frame > 0)) {
// Either we have no data to send back, or the requested scale is
// unreasonably large. This shouldn't happen normally, as chrome://theme/
// URLs are only used by WebUI pages and component extensions. However, the
// user can also enter these into the omnibox, so we need to fail
// gracefully.
std::move(callback).Run(nullptr);
} else if ((GetMimeType(url) == "image/png") &&
((scale > max_scale) || (frame != -1))) {
// This will extract and scale frame 0 of animated images.
// TODO(reveman): Support scaling of animated images and avoid scaling and
// re-encode when specific frame is specified (crbug.com/750064).
DCHECK_LE(frame, 0);
SendThemeImage(std::move(callback), resource_id, scale);
} else {
SendThemeBitmap(std::move(callback), resource_id, scale);
}
}
std::string ThemeSource::GetMimeType(const GURL& url) {
const std::string_view file_path = url.path_piece();
if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) {
return "text/css";
}
return "image/png";
}
bool ThemeSource::AllowCaching() {
return false;
}
bool ThemeSource::ShouldServiceRequest(const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) {
return url.SchemeIs(chrome::kChromeSearchScheme)
? InstantService::ShouldServiceRequest(url, browser_context,
render_process_id)
: URLDataSource::ShouldServiceRequest(url, browser_context,
render_process_id);
}
////////////////////////////////////////////////////////////////////////////////
// ThemeSource, private:
void ThemeSource::SendThemeBitmap(
content::URLDataSource::GotDataCallback callback,
int resource_id,
float scale) {
ui::ResourceScaleFactor scale_factor =
ui::GetSupportedResourceScaleFactor(scale);
if (BrowserThemePack::IsPersistentImageID(resource_id)) {
scoped_refptr<base::RefCountedMemory> image_data(
ThemeService::GetThemeProviderForProfile(profile_->GetOriginalProfile())
.GetRawData(resource_id, scale_factor));
std::move(callback).Run(image_data.get());
} else {
const ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
std::move(callback).Run(
rb.LoadDataResourceBytesForScale(resource_id, scale_factor));
}
}
void ThemeSource::SendThemeImage(
content::URLDataSource::GotDataCallback callback,
int resource_id,
float scale) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
gfx::ImageSkia* image;
if (BrowserThemePack::IsPersistentImageID(resource_id)) {
const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile(
profile_->GetOriginalProfile());
image = tp.GetImageSkiaNamed(resource_id);
} else {
image =
ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
}
const gfx::ImageSkiaRep& rep = image->GetRepresentation(scale);
std::optional<std::vector<uint8_t>> result =
gfx::PNGCodec::EncodeBGRASkBitmap(rep.GetBitmap(),
/*discard_transparency=*/false);
if (result) {
std::move(callback).Run(
base::MakeRefCounted<base::RefCountedBytes>(std::move(result.value())));
} else {
std::move(callback).Run(base::MakeRefCounted<base::RefCountedBytes>());
}
}
void ThemeSource::SendColorsCss(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) {
base::ElapsedTimer timer;
const ui::ColorProvider& color_provider = wc_getter.Run()->GetColorProvider();
std::string sets_param;
std::vector<std::string_view> color_id_sets;
bool generate_rgb_vars = false;
std::string generate_rgb_vars_query_value;
if (net::GetValueForKeyInQuery(url, "generate_rgb_vars",
&generate_rgb_vars_query_value)) {
generate_rgb_vars =
base::ToLowerASCII(generate_rgb_vars_query_value) == "true";
}
bool shadow_host = false;
std::string shadow_host_query_value;
if (net::GetValueForKeyInQuery(url, "shadow_host",
&shadow_host_query_value)) {
shadow_host = base::ToLowerASCII(shadow_host_query_value) == "true";
}
if (!net::GetValueForKeyInQuery(url, "sets", &sets_param)) {
LOG(ERROR)
<< "colors.css requires a 'sets' query parameter to specify the color "
"id sets returned e.g chrome://theme/colors.css?sets=ui,chrome";
std::move(callback).Run(nullptr);
return;
}
color_id_sets = base::SplitStringPiece(sets_param, ",", base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL);
using ColorIdCSSCallback = base::RepeatingCallback<std::string(ui::ColorId)>;
auto generate_color_mapping =
[&color_id_sets, &color_provider, &generate_rgb_vars](
std::string set_name, ui::ColorId start, ui::ColorId end,
ColorIdCSSCallback color_css_name) {
// Only return these mappings if specified in the query parameter.
auto it = std::ranges::find(color_id_sets, set_name);
if (it == color_id_sets.end()) {
return std::string();
}
color_id_sets.erase(it);
std::string css_string;
for (ui::ColorId id = start; id < end; ++id) {
const SkColor color = color_provider.GetColor(id);
std::string css_id_to_color_mapping =
base::StringPrintf("%s:%s;", color_css_name.Run(id).c_str(),
ui::ConvertSkColorToCSSColor(color).c_str());
base::StrAppend(&css_string, {css_id_to_color_mapping});
if (generate_rgb_vars) {
// Also generate a r,g,b string for each color so apps can construct
// colors with their own opacities in css.
const std::string css_rgb_color_str =
color_utils::SkColorToRgbString(color);
const std::string css_id_to_rgb_color_mapping =
base::StringPrintf("%s-rgb:%s;", color_css_name.Run(id).c_str(),
css_rgb_color_str.c_str());
base::StrAppend(&css_string, {css_id_to_rgb_color_mapping});
}
}
return css_string;
};
// Convenience lambda for wrapping
// |ConvertColorProviderColorIdToCSSColorId|.
auto generate_color_provider_mapping = [&generate_color_mapping](
std::string set_name,
ui::ColorId start, ui::ColorId end,
std::string (*color_id_name)(
ui::ColorId)) {
auto color_id_to_css_name = base::BindRepeating(
[](std::string (*color_id_name)(ui::ColorId), ui::ColorId id) {
return ui::ConvertColorProviderColorIdToCSSColorId(color_id_name(id));
},
color_id_name);
return generate_color_mapping(set_name, start, end, color_id_to_css_name);
};
std::string css_selector;
if (shadow_host) {
css_selector = ":host";
} else {
// This selector requires more specificity than other existing CSS
// selectors that define variables. We increase the specifity by adding
// a pseudoselector.
css_selector = "html:not(#z)";
}
const auto* theme_service =
ThemeServiceFactory::GetForProfile(profile_->GetOriginalProfile());
std::string theme_id;
if (theme_service->GetIsGrayscale()) {
theme_id = "--user-color-source: baseline-grayscale;";
} else if (theme_service->GetIsBaseline()) {
theme_id = "--user-color-source: baseline-default;";
}
std::string css_string = base::StrCat(
{css_selector, "{", theme_id,
generate_color_provider_mapping("ui", ui::kUiColorsStart,
ui::kUiColorsEnd, ui::ColorIdName),
generate_color_provider_mapping("chrome", kChromeColorsStart,
kChromeColorsEnd, &ChromeColorIdName),
#if BUILDFLAG(IS_CHROMEOS)
generate_color_mapping("ref", cros_tokens::kCrosRefColorsStart,
cros_tokens::kCrosRefColorsEnd,
base::BindRepeating(cros_tokens::ColorIdName)),
generate_color_mapping("sys", cros_tokens::kCrosSysColorsStart,
cros_tokens::kCrosSysColorsEnd,
base::BindRepeating(cros_tokens::ColorIdName)),
generate_color_mapping("legacy", cros_tokens::kLegacySemanticColorsStart,
cros_tokens::kLegacySemanticColorsEnd,
base::BindRepeating(cros_tokens::ColorIdName)),
#endif // BUILDFLAG(IS_CHROMEOS)
"}"});
if (!color_id_sets.empty()) {
LOG(ERROR)
<< "Unrecognized color set(s) specified for chrome://theme/colors.css: "
<< base::JoinString(color_id_sets, ",");
std::move(callback).Run(nullptr);
return;
}
std::move(callback).Run(
base::MakeRefCounted<base::RefCountedString>(std::move(css_string)));
// Measures the time it takes to generate the colors.css and queue it for the
// renderer.
UmaHistogramTimes("WebUI.ColorsStylesheetServingDuration", timer.Elapsed());
}
std::string ThemeSource::GetAccessControlAllowOriginForOrigin(
const std::string& origin) {
std::string allowed_origin_prefix = content::kChromeUIScheme;
allowed_origin_prefix += "://";
if (base::StartsWith(origin, allowed_origin_prefix,
base::CompareCase::SENSITIVE)) {
return origin;
}
return content::URLDataSource::GetAccessControlAllowOriginForOrigin(origin);
}
#if BUILDFLAG(IS_CHROMEOS)
void ThemeSource::SendTypographyCss(
content::URLDataSource::GotDataCallback callback) {
const ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
std::move(callback).Run(rb.LoadDataResourceBytesForScale(
IDR_CROS_STYLES_UI_CHROMEOS_STYLES_CROS_TYPOGRAPHY_CSS,
ui::kScaleFactorNone));
}
#endif
std::string ThemeSource::GetContentSecurityPolicy(
network::mojom::CSPDirectiveName directive) {
if (directive == network::mojom::CSPDirectiveName::DefaultSrc &&
serve_untrusted_) {
// TODO(crbug.com/40693568): Audit and tighten CSP.
return std::string();
}
return content::URLDataSource::GetContentSecurityPolicy(directive);
}
|