electron/shell/browser/ui/devtools_ui_theme_data_source.cc
electron-roller[bot] 3b421ef77f
chore: bump chromium to 134.0.6998.3 (35-x-y) (#45460)
* chore: bump chromium in DEPS to 134.0.6998.1

* chore: bump chromium in DEPS to 134.0.6998.5

* chore: bump chromium in DEPS to 134.0.6998.3

* chore: bump chromium to 134.0.6988.0 (main) (#45334)

* chore: bump chromium in DEPS to 134.0.6976.0

* chore: update mas_avoid_private_macos_api_usage.patch.patch
https://chromium-review.googlesource.com/c/chromium/src/+/6171046
process_info_mac.cc -> process_info_mac.mm

* chore: update build_do_not_depend_on_packed_resource_integrity.patch
https://chromium-review.googlesource.com/c/chromium/src/+/6196857

* chore: update feat_add_support_for_missing_dialog_features_to_shell_dialogs.patch
https://chromium-review.googlesource.com/c/chromium/src/+/6182296
https://chromium-review.googlesource.com/c/chromium/src/+/6183404
https://chromium-review.googlesource.com/c/chromium/src/+/6187853

A lot changed in the upstream implementation. There's a good chance I got
this wrong as threading has changed and moved some variables into globals.

* chore: remove build_remove_vr_directx_helpers_dependency.patch
https://chromium-review.googlesource.com/c/chromium/src/+/6186102
This landed upstream

* chore: e patches all

* chore: update net::CookieInclusionStatus::ExclusionReason enum
https://chromium-review.googlesource.com/c/chromium/src/+/6183252
https://chromium-review.googlesource.com/c/chromium/src/+/6185544

* chore: update content::WebAuthenticationDelegate import
https://chromium-review.googlesource.com/c/chromium/src/+/6189769

* Revert "chore: disable focus handling test due to win32/ia32 regression"

This reverts commit 1a57ba5d59848d0c841ddda59c9299a4f957452a.

* chore: bump chromium in DEPS to 134.0.6978.0

* chore: bump chromium in DEPS to 134.0.6980.0

* chore: bump chromium in DEPS to 134.0.6982.0

* chore: bump chromium in DEPS to 134.0.6984.0

* 6196281: Allow direct embedder IsPdfInternalPluginAllowedOrigin() interaction
https://chromium-review.googlesource.com/c/chromium/src/+/6196281

* 6196283: Delete PdfInternalPluginDelegate
https://chromium-review.googlesource.com/c/chromium/src/+/6196283

* chore: update patches

* chore: bump chromium in DEPS to 134.0.6986.0

* chore: update patches

* 6205762: Support option to use window.showSaveFilePicker() in PDF attachment code
https://chromium-review.googlesource.com/c/chromium/src/+/6205762

See also:
* https://issues.chromium.org/issues/373852607
* 5939153: [PDF] Add PdfUseShowSaveFilePicker feature flag | https://chromium-review.googlesource.com/c/chromium/src/+/5939153
* 6205761: Delete spurious Ink-specific code in pdf_viewer.ts | https://chromium-review.googlesource.com/c/chromium/src/+/6205761

* 6209609: Remove WebVector: Automatic changes
https://chromium-review.googlesource.com/c/chromium/src/+/6209609

* 6205488: UI: make QT5 optional
https://chromium-review.googlesource.com/c/chromium/src/+/6205488

* 6178281: Rename pak files from branding strings
https://chromium-review.googlesource.com/c/chromium/src/+/6178281

* fixup! 6209609: Remove WebVector: Automatic changes https://chromium-review.googlesource.com/c/chromium/src/+/6209609

* 6193249: Switch from safe_browsing::EventResult to enterprise_connectors:EventResult
https://chromium-review.googlesource.com/c/chromium/src/+/6193249

* 6197457: Remove Pause/ResumeReadingBodyFromNet IPCs
https://chromium-review.googlesource.com/c/chromium/src/+/6197457

* 6191230: Record total time spent on a picture in picture window
https://chromium-review.googlesource.com/c/chromium/src/+/6191230

* chore: bump chromium in DEPS to 134.0.6988.0

* chore: update patches

* 6215440: Remove base/ranges/.
https://chromium-review.googlesource.com/c/chromium/src/+/6215440

* Disable unsafe buffers error

Not sure what changed, but we're now seeing unsafe buffer errors in Chromium code, at least when using reclient. Will update this comment if we find out the cause.

* 6187853: SelectFileDialogLinuxPortal: Use dbus_xdg::Request and DbusType
https://chromium-review.googlesource.com/c/chromium/src/+/6187853

* fix `setDisplayMediaRequestHandler` test

Given how this test is written, I would expect this assertion to be false. It seems the oppositue was true before, but that was also acknowledged to be suprising. Seems that the underlying implementation is now fixed and works as expected.

* fixup! 6187853: SelectFileDialogLinuxPortal: Use dbus_xdg::Request and DbusType https://chromium-review.googlesource.com/c/chromium/src/+/6187853

* chore: udpate patches

* Multiple PRS: https://chromium-review.googlesource.com/c/chromium/src/+/6185544 | https://chromium-review.googlesource.com/c/chromium/src/+/6183252

* fix: cast enum class to numeric type

* fix: add 1 to MAX_EXCLUSION_REASON because enum values are zero-based, and we want the total count of reasons.

* Reapply "chore: disable focus handling test due to win32/ia32 regression"

This reverts commit 760b1a519b5919b483c66bc3096eeefb4d7011f4.

* refactor: use ExclusionReasonBitset::kValueCount for size

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
Co-authored-by: clavin <clavin@electronjs.org>
Co-authored-by: alice <alice@makenotion.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
(cherry picked from commit 213165a467b84b3fb979a869d9bf10ad21e2d78e)

---------

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
2025-02-07 11:14:12 -05:00

221 lines
8.7 KiB
C++

// Copyright (c) 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/devtools_ui_theme_data_source.h"
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#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 "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/color/chrome_color_provider_utils.h"
#include "chrome/common/webui_url_constants.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "net/base/url_util.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_utils.h"
namespace electron {
namespace {
GURL GetThemeUrl(const std::string& path) {
return GURL(std::string(content::kChromeDevToolsScheme) +
url::kStandardSchemeSeparator +
std::string(chrome::kChromeUIThemeHost) + "/" + path);
}
scoped_refptr<base::RefCountedMemory> CreateNotFoundResponse() {
const char kHttpNotFound[] = "HTTP/1.1 404 Not Found\n\n";
return base::MakeRefCounted<base::RefCountedStaticMemory>(
base::byte_span_from_cstring(kHttpNotFound));
}
std::string GetMimeTypeForUrl(const GURL& url) {
std::string filename = url.ExtractFileName();
if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) {
return "text/html";
} else if (base::EndsWith(filename, ".css",
base::CompareCase::INSENSITIVE_ASCII)) {
return "text/css";
} else if (base::EndsWith(filename, ".js",
base::CompareCase::INSENSITIVE_ASCII) ||
base::EndsWith(filename, ".mjs",
base::CompareCase::INSENSITIVE_ASCII)) {
return "application/javascript";
} else if (base::EndsWith(filename, ".png",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/png";
} else if (base::EndsWith(filename, ".map",
base::CompareCase::INSENSITIVE_ASCII)) {
return "application/json";
} else if (base::EndsWith(filename, ".ts",
base::CompareCase::INSENSITIVE_ASCII)) {
return "application/x-typescript";
} else if (base::EndsWith(filename, ".gif",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/gif";
} else if (base::EndsWith(filename, ".svg",
base::CompareCase::INSENSITIVE_ASCII)) {
return "image/svg+xml";
} else if (base::EndsWith(filename, ".manifest",
base::CompareCase::INSENSITIVE_ASCII)) {
return "text/cache-manifest";
}
return "text/html";
}
} // namespace
std::string ThemeDataSource::GetSource() {
// kChromeUIThemeHost
return chrome::kChromeUIThemeHost;
}
void ThemeDataSource::StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
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);
// kColorsCssPath should stay consistent with COLORS_CSS_SELECTOR in
// colors_css_updater.js.
constexpr std::string_view kColorsCssPath = "colors.css";
if (parsed_path == kColorsCssPath) {
SendColorsCss(url, wc_getter, std::move(callback));
return;
}
std::move(callback).Run(CreateNotFoundResponse());
}
std::string ThemeDataSource::GetMimeType(const GURL& url) {
return GetMimeTypeForUrl(url);
}
void ThemeDataSource::SendColorsCss(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) {
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)";
}
std::string css_string = base::StrCat(
{css_selector, "{", "--user-color-source: baseline-default;",
generate_color_provider_mapping("ui", ui::kUiColorsStart,
ui::kUiColorsEnd, ui::ColorIdName),
generate_color_provider_mapping("chrome", kChromeColorsStart,
kChromeColorsEnd, &ChromeColorIdName),
"}"});
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)));
}
} // namespace electron