chromium/chrome/browser/ui/webui/side_panel/customize_chrome/wallpaper_search/wallpaper_search_handler.cc

// Copyright 2023 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/side_panel/customize_chrome/wallpaper_search/wallpaper_search_handler.h"

#include <optional>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>

#include "base/barrier_callback.h"
#include "base/base64.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "base/token.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/feedback/show_feedback_page.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/background/wallpaper_search/wallpaper_search_background_manager.h"
#include "chrome/browser/search/background/wallpaper_search/wallpaper_search_data.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/hats/survey_config.h"
#include "chrome/browser/ui/webui/cr_components/theme_color_picker/customize_chrome_colors.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/wallpaper_search/wallpaper_search_string_map.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/image_fetcher/core/image_decoder.h"
#include "components/optimization_guide/core/model_execution/optimization_guide_model_execution_error.h"
#include "components/optimization_guide/core/model_quality/feature_type_map.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/features/wallpaper_search.pb.h"
#include "components/optimization_guide/proto/model_execution.pb.h"
#include "components/optimization_guide/proto/model_quality_service.pb.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/search/ntp_features.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/skia_utils_base.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"

DescriptorDName;
DescriptorDValue;
UserFeedback;
WallpaperSearchResult;
WallpaperSearchResultPtr;
WallpaperSearchStatus;

namespace {

const char kGstaticBaseURL[] =;
// Calculate new dimensions given the width and height that will make the
// smaller dimension equal to goal_size but keep the current aspect ratio.
// The first value in the pair is the width and the second is the height.
std::pair<int, int> CalculateResizeDimensions(int width,
                                              int height,
                                              int goal_size) {}

std::string ReadFile(const base::FilePath& path) {}

optimization_guide::proto::UserFeedback
OptimizationFeedbackFromWallpaperSearchFeedback(UserFeedback feedback) {}

side_panel::customize_chrome::mojom::KeyLabelPtr MakeKeyLabel(
    const std::string& key,
    const std::string& label) {}
}  // namespace

WallpaperSearchHandler::WallpaperSearchHandler(
    mojo::PendingReceiver<
        side_panel::customize_chrome::mojom::WallpaperSearchHandler>
        pending_handler,
    mojo::PendingRemote<
        side_panel::customize_chrome::mojom::WallpaperSearchClient>
        pending_client,
    Profile* profile,
    image_fetcher::ImageDecoder* image_decoder,
    WallpaperSearchBackgroundManager* wallpaper_search_background_manager,
    int64_t session_id,
    WallpaperSearchStringMap* string_map)
    :{}

WallpaperSearchHandler::~WallpaperSearchHandler() {}

void WallpaperSearchHandler::GetDescriptors(GetDescriptorsCallback callback) {}

void WallpaperSearchHandler::GetInspirations(GetInspirationsCallback callback) {}

void WallpaperSearchHandler::GetWallpaperSearchResults(
    side_panel::customize_chrome::mojom::ResultDescriptorsPtr
        result_descriptors,
    GetWallpaperSearchResultsCallback callback) {}

void WallpaperSearchHandler::SetBackgroundToHistoryImage(
    const base::Token& result_id,
    side_panel::customize_chrome::mojom::ResultDescriptorsPtr descriptors) {}

void WallpaperSearchHandler::SetBackgroundToWallpaperSearchResult(
    const base::Token& result_id,
    double time,
    side_panel::customize_chrome::mojom::ResultDescriptorsPtr descriptors) {}

void WallpaperSearchHandler::SetBackgroundToInspirationImage(
    const base::Token& id,
    const GURL& background_url) {}

void WallpaperSearchHandler::UpdateHistory() {}

void WallpaperSearchHandler::SetUserFeedback(UserFeedback selected_option) {}

void WallpaperSearchHandler::OpenHelpArticle() {}

void WallpaperSearchHandler::LaunchHatsSurvey() {}

void WallpaperSearchHandler::ShowFeedbackPage() {}

void WallpaperSearchHandler::OnHistoryUpdated() {}

// This function is a wrapper around image_fetcher::ImageDecoder::DecodeImage()
// because of argument order when working with PostTaskAndReplyWithResult().
void WallpaperSearchHandler::DecodeHistoryImage(
    image_fetcher::ImageDecodedCallback callback,
    std::string image) {}

void WallpaperSearchHandler::OnDescriptorsRetrieved(
    GetDescriptorsCallback callback,
    std::unique_ptr<std::string> response_body) {}

void WallpaperSearchHandler::OnDescriptorsJsonParsed(
    GetDescriptorsCallback callback,
    data_decoder::DataDecoder::ValueOrError result) {}

void WallpaperSearchHandler::OnHistoryDecoded(
    std::vector<HistoryEntry> history,
    std::vector<std::pair<SkBitmap, base::Token>> results) {}

void WallpaperSearchHandler::OnInspirationImageDownloaded(
    const base::Token& id,
    base::ElapsedTimer timer,
    std::unique_ptr<std::string> response_body) {}

void WallpaperSearchHandler::OnInspirationImageDecoded(
    const base::Token& id,
    base::ElapsedTimer timer,
    const gfx::Image& image) {}

void WallpaperSearchHandler::OnInspirationsRetrieved(
    GetInspirationsCallback callback,
    std::unique_ptr<std::string> response_body) {}

void WallpaperSearchHandler::OnInspirationsJsonParsed(
    GetInspirationsCallback callback,
    data_decoder::DataDecoder::ValueOrError result) {}

void WallpaperSearchHandler::SelectHistoryImage(
    const base::Token& id,
    base::ElapsedTimer timer,
    side_panel::customize_chrome::mojom::ResultDescriptorsPtr descriptors,
    const gfx::Image& image) {}

void WallpaperSearchHandler::OnWallpaperSearchResultsRetrieved(
    GetWallpaperSearchResultsCallback callback,
    base::ElapsedTimer request_timer,
    optimization_guide::OptimizationGuideModelExecutionResult result,
    std::unique_ptr<optimization_guide::ModelQualityLogEntry> log_entry) {}

void WallpaperSearchHandler::SetResultRenderTime(
    const std::vector<base::Token>& result_ids,
    double time) {}

// Save the full sized bitmaps and create a much smaller image version of each
// for sending back to the UI through the callback. Re-encode the bitmap and
// make it base64 for easy reading by the UI.
void WallpaperSearchHandler::OnWallpaperSearchResultsDecoded(
    GetWallpaperSearchResultsCallback callback,
    base::ElapsedTimer processing_timer,
    std::vector<
        std::pair<optimization_guide::proto::WallpaperSearchImageQuality*,
                  SkBitmap>> bitmaps) {}

void WallpaperSearchHandler::LaunchDelayedHatsSurvey() {}