Skip to content

Commit

Permalink
set wallpaper search result as background
Browse files Browse the repository at this point in the history
Steps:
1. On click of result tile, pass id of result to handler.
2. Handler uses the id to get the image SkBitmap from map of search
   results that were saved during the fetch and passes it to
   |NtpCustomBackgroundService|.
   * Note: The whole SkBitmap is passed rather than encoding so that we
     can skip decoding the image again in the service.
3. The same steps as the rest of the SelectLocalBackgroundImage()
   methods except we skip over the image decoding step and encode before
   writing to file.

Note: Updated the get method for search results and the frontend to use
an object that includes a Token id for each image along with the
thumbnail.

Bug: b/300966546
Change-Id: I8e72c48e739ea758c3b6349b92cfb42a55c22420
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4936353
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: Alex Gough <ajgo@chromium.org>
Commit-Queue: Riley Tatum <rtatum@google.com>
Cr-Commit-Position: refs/heads/main@{#1209657}
  • Loading branch information
Riley Tatum authored and Chromium LUCI CQ committed Oct 13, 2023
1 parent 5ad43d1 commit 363b723
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ <h2 slot="heading">Wallpaper Search</h2>
<hr class="sp-hr">
<cr-grid columns="3" disable-arrow-navigation>
<template is="dom-repeat" id="resultsRepeat" items="[[results_]]">
<div class="tile result" tabindex="0" role="button">
<div class="tile result" tabindex="0" role="button"
on-click="onResultClick_">
<div class="image-container">
<img src="data:image/png;base64,[[item]]">
<img src="data:image/png;base64,[[item.image]]">
</img>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {SpHeading} from 'chrome://customize-chrome-side-panel.top-chrome/shared/
import {CrActionMenuElement} from 'chrome://resources/cr_elements/cr_action_menu/cr_action_menu.js';
import {CrButtonElement} from 'chrome://resources/cr_elements/cr_button/cr_button.js';
import {CrInputElement} from 'chrome://resources/cr_elements/cr_input/cr_input.js';
import {PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {DomRepeatEvent, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

import {CustomizeChromePageHandlerInterface, Descriptors} from './customize_chrome.mojom-webui.js';
import {CustomizeChromePageHandlerInterface, Descriptors, WallpaperSearchResult} from './customize_chrome.mojom-webui.js';
import {CustomizeChromeApiProxy} from './customize_chrome_api_proxy.js';
import {getTemplate} from './wallpaper_search.html.js';

Expand Down Expand Up @@ -52,7 +52,7 @@ export class WallpaperSearchElement extends PolymerElement {
private descriptors_: Descriptors|null;
private emptyContainers_: number[];
private query_: string;
private results_: string[];
private results_: WallpaperSearchResult[];

private pageHandler_: CustomizeChromePageHandlerInterface;

Expand Down Expand Up @@ -95,6 +95,10 @@ export class WallpaperSearchElement extends PolymerElement {
this.$.queryInput.invalid = !results.length;
this.$.queryInput.errorMessage = 'Error';
}

private async onResultClick_(e: DomRepeatEvent<WallpaperSearchResult>) {
this.pageHandler_.setBackgroundToWallpaperSearchResult(e.model.item.id);
}
}

declare global {
Expand Down
32 changes: 32 additions & 0 deletions chrome/browser/search/background/ntp_custom_background_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/mojom/themes.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/color_analysis.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -387,6 +388,37 @@ void NtpCustomBackgroundService::SelectLocalBackgroundImage(
}
}

void NtpCustomBackgroundService::SetBackgroundToLocalResourceAndExtractColor(
const SkBitmap& bitmap) {
NtpCustomBackgroundService::SetBackgroundToLocalResource();
NtpCustomBackgroundService::UpdateCustomLocalBackgroundColorAsync(
gfx::Image::CreateFrom1xBitmap(bitmap));
}

void NtpCustomBackgroundService::SelectLocalBackgroundImage(
const SkBitmap& bitmap) {
if (IsCustomBackgroundDisabledByPolicy()) {
return;
}

previous_background_info_.reset();
previous_local_background_ = true;

std::vector<unsigned char> encoded;
const bool success = gfx::PNGCodec::EncodeBGRASkBitmap(
bitmap, /*discard_transparency=*/false, &encoded);
if (success) {
base::ThreadPool::PostTaskAndReply(
FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
base::BindOnce(&WriteFileToProfilePath,
std::string(encoded.begin(), encoded.end()),
profile_->GetPath()),
base::BindOnce(&NtpCustomBackgroundService::
SetBackgroundToLocalResourceAndExtractColor,
weak_ptr_factory_.GetWeakPtr(), bitmap));
}
}

void NtpCustomBackgroundService::RefreshBackgroundIfNeeded() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Do not refresh background & color if extension theme is in use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/color_utils.h"

class NtpCustomBackgroundServiceObserver;
Expand Down Expand Up @@ -73,6 +74,10 @@ class NtpCustomBackgroundService : public KeyedService,
// Invoked by Wallpaper Search to set background image.
virtual void SelectLocalBackgroundImage(const std::string& data);

// Invoked by Wallpaper Search to set background image with already decoded
// data.
virtual void SelectLocalBackgroundImage(const SkBitmap& bitmap);

// Virtual for testing.
virtual void RefreshBackgroundIfNeeded();

Expand Down Expand Up @@ -143,6 +148,10 @@ class NtpCustomBackgroundService : public KeyedService,
const GURL& verified_custom_background_url,
int headers_response_code);

// Set background pref info to say that the current background is a local
// resource and start color extraction of the associated bitmap.
void SetBackgroundToLocalResourceAndExtractColor(const SkBitmap& bitmap);

const raw_ptr<Profile> profile_;
raw_ptr<PrefService, DanglingUntriaged> pref_service_;
raw_ptr<ThemeService, DanglingUntriaged> theme_service_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,3 +1071,50 @@ TEST_F(NtpCustomBackgroundServiceTest, LocalImageURLsDoNotGetVerified) {
EXPECT_TRUE(custom_background_service_->IsCustomBackgroundSet());
EXPECT_EQ(true, custom_background->is_uploaded_image);
}

class NtpCustomBackgroundServiceWithWallpaperSearchTest
: public NtpCustomBackgroundServiceTest {
public:
void SetUp() override {
feature_list_.InitWithFeatures(
/*enabled_features=*/{ntp_features::kCustomizeChromeWallpaperSearch},
/*disabled_features=*/{});
NtpCustomBackgroundServiceTest::SetUp();
}

private:
base::test::ScopedFeatureList feature_list_;
};

TEST_F(NtpCustomBackgroundServiceWithWallpaperSearchTest,
SetLocalImageWithSkBitmap) {
EXPECT_CALL(observer_, OnCustomBackgroundImageUpdated).Times(1);
ASSERT_FALSE(custom_background_service_->IsCustomBackgroundSet());

SkColor color = SK_ColorBLUE;
EXPECT_CALL(mock_theme_service(), SetUserColorAndBrowserColorVariant)
.WillOnce(SaveArg<0>(&color));

sync_preferences::TestingPrefServiceSyncable* pref_service =
profile().GetTestingPrefService();
SkBitmap bitmap;
bitmap.allocN32Pixels(32, 32);
bitmap.eraseColor(SK_ColorRED);

custom_background_service_->SelectLocalBackgroundImage(bitmap);
task_environment_.RunUntilIdle();

// Check that local background image was set.
auto custom_background = custom_background_service_->GetCustomBackground();
EXPECT_TRUE(
base::StartsWith(custom_background->custom_background_url.spec(),
chrome::kChromeUIUntrustedNewTabPageBackgroundUrl,
base::CompareCase::SENSITIVE));
EXPECT_TRUE(
pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
EXPECT_TRUE(custom_background_service_->IsCustomBackgroundSet());
EXPECT_EQ(true, custom_background->is_uploaded_image);

// Check that the color is correct.
EXPECT_EQ(SK_ColorRED, color);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module side_panel.mojom;

import "mojo/public/mojom/base/token.mojom";
import "skia/public/mojom/skcolor.mojom";
import "url/mojom/url.mojom";

Expand Down Expand Up @@ -104,6 +105,11 @@ struct Descriptors {
array<string> descriptor_c;
};

struct WallpaperSearchResult {
mojo_base.mojom.Token id;
string image;
};

// Used by the WebUI page to bootstrap bidirectional communication.
interface CustomizeChromePageHandlerFactory {
// The WebUI calls this method when the page is first initialized.
Expand Down Expand Up @@ -174,7 +180,11 @@ interface CustomizeChromePageHandler {
GetDescriptors() => (Descriptors? descriptors);

// Searches NTP wallpaper for `query`. Returns list of sanitized image data.
GetWallpaperSearchResults(string query) => (array<string> results);
GetWallpaperSearchResults(string query) =>
(array<WallpaperSearchResult> results);

// Sets wallpaper search result of index to background image.
SetBackgroundToWallpaperSearchResult(mojo_base.mojom.Token result_id);
};

// WebUI-side handler for requests from the browser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/token.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/new_tab_page/modules/new_tab_page_modules.h"
#include "chrome/browser/new_tab_page/new_tab_page_util.h"
Expand Down Expand Up @@ -477,7 +478,8 @@ void CustomizeChromePageHandler::GetWallpaperSearchResults(
const std::string& query,
GetWallpaperSearchResultsCallback callback) {
callback = mojo::WrapCallbackWithDefaultInvokeIfNotRun(
std::move(callback), std::vector<std::string>());
std::move(callback),
std::vector<side_panel::mojom::WallpaperSearchResultPtr>());
if (!base::FeatureList::IsEnabled(
ntp_features::kCustomizeChromeWallpaperSearch) ||
!base::FeatureList::IsEnabled(
Expand All @@ -500,6 +502,13 @@ void CustomizeChromePageHandler::GetWallpaperSearchResults(
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void CustomizeChromePageHandler::SetBackgroundToWallpaperSearchResult(
const base::Token& result_id) {
CHECK(base::Contains(wallpaper_search_results_, result_id));
ntp_custom_background_service_->SelectLocalBackgroundImage(
wallpaper_search_results_[result_id]);
}

void CustomizeChromePageHandler::OnDescriptorsRetrieved(
std::unique_ptr<std::string> response_body) {
if (!response_body) {
Expand Down Expand Up @@ -648,8 +657,7 @@ void CustomizeChromePageHandler::OnWallpaperSearchResultsRetrieved(
void CustomizeChromePageHandler::OnWallpaperSearchResultsDecoded(
GetWallpaperSearchResultsCallback callback,
std::vector<SkBitmap> bitmaps) {
std::vector<std::string> thumbnails;
wallpaper_search_results_.clear();
std::vector<side_panel::mojom::WallpaperSearchResultPtr> thumbnails;

for (auto& bitmap : bitmaps) {
SkBitmap small_bitmap = skia::ImageOperations::Resize(
Expand All @@ -658,12 +666,16 @@ void CustomizeChromePageHandler::OnWallpaperSearchResultsDecoded(
const bool success = gfx::PNGCodec::EncodeBGRASkBitmap(
small_bitmap, /*discard_transparency=*/false, &encoded);
if (success) {
thumbnails.push_back(base::Base64Encode(encoded));
wallpaper_search_results_.push_back(bitmap);
auto thumbnail = side_panel::mojom::WallpaperSearchResult::New();
auto id = base::Token::CreateRandom();
wallpaper_search_results_[id] = std::move(bitmap);
thumbnail->image = base::Base64Encode(encoded);
thumbnail->id = std::move(id);
thumbnails.push_back(std::move(thumbnail));
}
}

std::move(callback).Run(thumbnails);
std::move(callback).Run(std::move(thumbnails));
}

void CustomizeChromePageHandler::LogEvent(NTPLoggingEventType event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/token.h"
#include "chrome/browser/search/background/ntp_background_service.h"
#include "chrome/browser/search/background/ntp_background_service_observer.h"
#include "chrome/browser/search/background/ntp_custom_background_service.h"
Expand Down Expand Up @@ -113,6 +115,8 @@ class CustomizeChromePageHandler
void GetWallpaperSearchResults(
const std::string& query,
GetWallpaperSearchResultsCallback callback) override;
void SetBackgroundToWallpaperSearchResult(
const base::Token& result_id) override;

private:
void OnDescriptorsRetrieved(std::unique_ptr<std::string> response_body);
Expand Down Expand Up @@ -171,7 +175,7 @@ class CustomizeChromePageHandler
std::unique_ptr<network::SimpleURLLoader> simple_url_loader_;
std::unique_ptr<data_decoder::DataDecoder> data_decoder_;
const raw_ref<image_fetcher::ImageDecoder> image_decoder_;
std::vector<SkBitmap> wallpaper_search_results_;
base::flat_map<base::Token, SkBitmap> wallpaper_search_results_;
// Caches a request to scroll to a section in case the front-end queries the
// last requested section, e.g. during load.
CustomizeChromeSection last_requested_section_ =
Expand Down

0 comments on commit 363b723

Please sign in to comment.