chromium/chrome/browser/themes/browser_theme_pack.cc

// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/themes/browser_theme_pack.h"

#include <limits.h>
#include <stddef.h>

#include <limits>
#include <memory>
#include <optional>
#include <string_view>
#include <utility>

#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/heap_array.h"
#include "base/containers/span.h"
#include "base/files/file.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/frame/window_frame_util.h"
#include "chrome/common/extensions/manifest_handlers/theme_handler.h"
#include "chrome/common/themes/autogenerated_theme_util.h"
#include "chrome/grit/theme_resources.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/resource/data_pack.h"
#include "ui/color/color_mixer.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_recipe.h"
#include "ui/color/color_transform.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_analysis.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"

BrowserThread;
Extension;
TP;

// Persistent constants for the main images that we need. These have the same
// names as their IDR_* counterparts but these values will always stay the
// same.
enum BrowserThemePack::PersistentID : int {};

namespace {

PRS;

// The tallest tab height in any mode.
constexpr int kTallestTabHeight =;

// The tallest height above the tabs in any mode is 19 DIP.
constexpr int kTallestFrameHeight =;

// Version number of the current theme pack. We just throw out and rebuild
// theme packs that aren't int-equal to this. Increment this number if you
// changed default theme assets, if you need themes to recreate their generated
// images (which are cached), if you changed how missing values are
// generated, or if you changed any constants.
const int kThemePackVersion =;

// IDs that are in the DataPack won't clash with the positive integer
// uint16_t. kHeaderID should always have the maximum value because we want the
// "header" to be written last. That way we can detect whether the pack was
// successfully written and ignore and regenerate if it was only partially
// written (i.e. chrome crashed on a different thread while writing the pack).
const int kMaxID =;  // Max unsigned 16-bit int.
const int kHeaderID =;
const int kTintsID =;
const int kColorsID =;
const int kDisplayPropertiesID =;
const int kSourceImagesID =;
const int kScaleFactorsID =;

struct PersistingImagesTable {};

// IDR_* resource names change whenever new resources are added; use persistent
// IDs when storing to a cached pack.
constexpr PersistingImagesTable kPersistingImages[] =;

BrowserThemePack::PersistentID GetPersistentIDByName(const std::string& key) {}

BrowserThemePack::PersistentID GetPersistentIDByIDR(int idr) {}

// Returns true if the scales in |input| match those in |expected|.
// The order must match as the index is used in determining the raw id.
bool InputScalesValid(std::string_view input,
                      base::span<ui::ResourceScaleFactor> expected) {}

// Returns |scale_factors| as a string to be written to disk.
std::string GetResourceScaleFactorsAsString(
    base::span<const ui::ResourceScaleFactor> scale_factors) {}

struct StringToIntTable {};

// Strings used by themes to identify tints in the JSON.
const StringToIntTable kTintTable[] =;
const size_t kTintTableLength =;

// Strings used by themes to identify colors in the JSON.
constexpr StringToIntTable kOverwritableColorTable[] =;
constexpr size_t kOverwritableColorTableLength =;

// Colors generated based on the theme, but not overwritable in the theme file.
constexpr int kNonOverwritableColorTable[] =;
constexpr size_t kNonOverwritableColorTableLength =;

// The maximum number of colors we may need to store (includes ones that can be
// specified by the theme, and ones that we calculate but can't be specified).
constexpr size_t kColorsArrayLength =;

// Strings used by themes to identify display properties keys in JSON.
const StringToIntTable kDisplayProperties[] =;
const size_t kDisplayPropertiesSize =;

int GetIntForString(const std::string& key,
                    const StringToIntTable* table,
                    size_t table_length) {}

struct CropEntry {};

// The images which should be cropped before being saved to the data pack. The
// maximum heights are meant to be conservative as to give room for the UI to
// change without the maximum heights having to be modified.
// |kThemePackVersion| must be incremented if any of the maximum heights below
// are modified.
const struct CropEntry kImagesToCrop[] =;

// A list of images that don't need tinting or any other modification and can
// be byte-copied directly into the finished DataPack. This should contain the
// persistent IDs for all themeable image IDs that aren't in kFrameValues,
// kTabBackgroundMap or kImagesToCrop.
const BrowserThemePack::PersistentID kPreloadIDs[] =;

// Returns a piece of memory with the contents of the file |path|.
scoped_refptr<base::RefCountedMemory> ReadFileData(const base::FilePath& path) {}

// Computes a bitmap at one scale from a bitmap at a different scale.
SkBitmap CreateLowQualityResizedBitmap(
    const SkBitmap& source_bitmap,
    ui::ResourceScaleFactor source_scale_factor,
    ui::ResourceScaleFactor desired_scale_factor) {}

// A ImageSkiaSource that scales 100P image to the target scale factor
// if the ImageSkiaRep for the target scale factor isn't available.
class ThemeImageSource : public gfx::ImageSkiaSource {};

// An ImageSkiaSource that delays decoding PNG data into bitmaps until
// needed. Missing data for a scale factor is computed by scaling data for an
// available scale factor. Computed bitmaps are stored for future look up.
class ThemeImagePngSource : public gfx::ImageSkiaSource {};

class TabBackgroundImageSource : public gfx::CanvasImageSource {};

class ControlButtonBackgroundImageSource : public gfx::CanvasImageSource {};

// Returns whether the color is grayscale.
bool IsColorGrayscale(SkColor color) {}

// The minimum contrast the omnibox background must have against the toolbar.
constexpr float kMinOmniboxToolbarContrast =;

ui::ColorTransform ChooseOmniboxBgBlendTarget() {}

}  // namespace

BrowserThemePack::~BrowserThemePack() {}

void BrowserThemePack::SetColor(int id, SkColor color) {}

void BrowserThemePack::SetColorIfUnspecified(int id, SkColor color) {}

void BrowserThemePack::SetTint(int id, color_utils::HSL tint) {}

void BrowserThemePack::SetDisplayProperty(int id, int value) {}

SkColor BrowserThemePack::ComputeImageColor(const gfx::Image& image,
                                            int height) {}

// static
void BrowserThemePack::BuildFromExtension(
    const extensions::Extension* extension,
    BrowserThemePack* pack) {}

// static
scoped_refptr<BrowserThemePack> BrowserThemePack::BuildFromDataPack(
    const base::FilePath& path, const std::string& expected_id) {}

// static
bool BrowserThemePack::IsPersistentImageID(int id) {}

// static
void BrowserThemePack::BuildFromColor(SkColor color, BrowserThemePack* pack) {}

// static
void BrowserThemePack::BuildFromColors(AutogeneratedThemeColors colors,
                                       BrowserThemePack* pack) {}

void BrowserThemePack::BuildFromWebAppColors(SkColor theme_color,
                                             SkColor background_color,
                                             BrowserThemePack* pack) {}

BrowserThemePack::BrowserThemePack(ThemeType theme_type)
    :{}

bool BrowserThemePack::WriteToDisk(const base::FilePath& path) const {}

bool BrowserThemePack::GetTint(int id, color_utils::HSL* hsl) const {}

bool BrowserThemePack::GetColor(int id, SkColor* color) const {}

bool BrowserThemePack::GetDisplayProperty(int id, int* result) const {}

gfx::Image BrowserThemePack::GetImageNamed(int idr_id) const {}

base::RefCountedMemory* BrowserThemePack::GetRawData(
    int idr_id,
    ui::ResourceScaleFactor scale_factor) const {}

bool BrowserThemePack::HasCustomImage(int idr_id) const {}

void BrowserThemePack::AddColorMixers(ui::ColorProvider* provider,
                                      const ui::ColorProviderKey& key) const {}

// private:

void BrowserThemePack::AdjustThemePack() {}

void BrowserThemePack::InitEmptyPack() {}

void BrowserThemePack::InitEmptyPackFromColors() {}

void BrowserThemePack::InitHeader() {}

void BrowserThemePack::InitTints() {}

void BrowserThemePack::InitColors() {}

void BrowserThemePack::InitDisplayProperties() {}

void BrowserThemePack::InitSourceImages() {}

void BrowserThemePack::FinalizePackFromColors() {}

void BrowserThemePack::SetHeaderId(const Extension* extension) {}

void BrowserThemePack::SetTintsFromJSON(const base::Value::Dict* tints_value) {}

void BrowserThemePack::SetColorsFromJSON(
    const base::Value::Dict* colors_value) {}

void BrowserThemePack::ReadColorsFromJSON(const base::Value::Dict& colors_value,
                                          std::map<int, SkColor>* temp_colors) {}

void BrowserThemePack::SetDisplayPropertiesFromJSON(
    const base::Value::Dict* display_properties_value) {}

void BrowserThemePack::ParseImageNamesFromJSON(
    const base::Value::Dict* images_value,
    const base::FilePath& images_path,
    FilePathMap* file_paths) const {}

void BrowserThemePack::AddFileAtScaleToMap(const std::string& image_name,
                                           ui::ResourceScaleFactor scale_factor,
                                           const base::FilePath& image_path,
                                           FilePathMap* file_paths) const {}

void BrowserThemePack::BuildSourceImagesArray(const FilePathMap& file_paths) {}

bool BrowserThemePack::LoadRawBitmapsTo(
    const FilePathMap& file_paths,
    ImageCache* image_cache) {}

void BrowserThemePack::CropImages(ImageCache* images) const {}

void BrowserThemePack::SetFrameAndToolbarRelatedColors() {}

void BrowserThemePack::CreateToolbarImageAndColors(ImageCache* images) {}

void BrowserThemePack::CreateFrameImagesAndColors(ImageCache* images) {}

void BrowserThemePack::GenerateFrameColorsFromTints() {}

void BrowserThemePack::GenerateWindowControlButtonColor(ImageCache* images) {}

void BrowserThemePack::CreateTabBackgroundImagesAndColors(ImageCache* images) {}

void BrowserThemePack::GenerateMissingNtpColors() {}

void BrowserThemePack::RepackImages(const ImageCache& images,
                                    RawImages* reencoded_images) const {}

void BrowserThemePack::MergeImageCaches(
    const ImageCache& source, ImageCache* destination) const {}

void BrowserThemePack::AddRawImagesTo(const RawImages& images,
                                      RawDataForWriting* out) const {}

color_utils::HSL BrowserThemePack::GetTintInternal(int id) const {}

int BrowserThemePack::GetRawIDByPersistentID(
    PersistentID prs_id,
    ui::ResourceScaleFactor scale_factor) const {}

bool BrowserThemePack::GetScaleFactorFromManifestKey(
    const std::string& key,
    ui::ResourceScaleFactor* scale_factor) const {}

void BrowserThemePack::GenerateRawImageForAllSupportedScales(
    PersistentID prs_id) {}