Commit 47045ab2 authored by Daniel Classon's avatar Daniel Classon Committed by Commit Bot

[PrintingPage] Fix "Saved printers" search tag when no saved printers

Fixes the behavior of the "Saved Printers" search tag so it is
dynamically added/removed when the number of saved printers is greater
than 0.

Bug: 1116553
Change-Id: I37f2d3c1612ec58feb01a4dcdf426807fd442040
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2424518
Commit-Queue: Daniel Classon <dclasson@google.com>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarJimmy Gong <jimmyxgong@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809985}
parent f3435d9d
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chromeos/constants/chromeos_features.h" #include "chromeos/constants/chromeos_features.h"
#include "chromeos/printing/printer_configuration.h"
#include "content/public/browser/web_ui_data_source.h" #include "content/public/browser/web_ui_data_source.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/webui/web_ui_util.h" #include "ui/base/webui/web_ui_util.h"
...@@ -29,12 +30,6 @@ const std::vector<SearchConcept>& GetPrintingSearchConcepts() { ...@@ -29,12 +30,6 @@ const std::vector<SearchConcept>& GetPrintingSearchConcepts() {
mojom::SearchResultDefaultRank::kMedium, mojom::SearchResultDefaultRank::kMedium,
mojom::SearchResultType::kSetting, mojom::SearchResultType::kSetting,
{.setting = mojom::Setting::kAddPrinter}}, {.setting = mojom::Setting::kAddPrinter}},
{IDS_OS_SETTINGS_TAG_PRINTING_SAVED_PRINTERS,
mojom::kPrintingDetailsSubpagePath,
mojom::SearchResultIcon::kPrinter,
mojom::SearchResultDefaultRank::kMedium,
mojom::SearchResultType::kSetting,
{.setting = mojom::Setting::kSavedPrinters}},
{IDS_OS_SETTINGS_TAG_PRINTING, {IDS_OS_SETTINGS_TAG_PRINTING,
mojom::kPrintingDetailsSubpagePath, mojom::kPrintingDetailsSubpagePath,
mojom::SearchResultIcon::kPrinter, mojom::SearchResultIcon::kPrinter,
...@@ -47,6 +42,18 @@ const std::vector<SearchConcept>& GetPrintingSearchConcepts() { ...@@ -47,6 +42,18 @@ const std::vector<SearchConcept>& GetPrintingSearchConcepts() {
return *tags; return *tags;
} }
const std::vector<SearchConcept>& GetSavedPrintersSearchConcepts() {
static const base::NoDestructor<std::vector<SearchConcept>> tags({
{IDS_OS_SETTINGS_TAG_PRINTING_SAVED_PRINTERS,
mojom::kPrintingDetailsSubpagePath,
mojom::SearchResultIcon::kPrinter,
mojom::SearchResultDefaultRank::kMedium,
mojom::SearchResultType::kSetting,
{.setting = mojom::Setting::kSavedPrinters}},
});
return *tags;
}
const std::vector<SearchConcept>& GetPrintingManagementSearchConcepts() { const std::vector<SearchConcept>& GetPrintingManagementSearchConcepts() {
static const base::NoDestructor<std::vector<SearchConcept>> tags({ static const base::NoDestructor<std::vector<SearchConcept>> tags({
{IDS_OS_SETTINGS_TAG_PRINT_MANAGEMENT, {IDS_OS_SETTINGS_TAG_PRINT_MANAGEMENT,
...@@ -77,9 +84,19 @@ PrintingSection::PrintingSection(Profile* profile, ...@@ -77,9 +84,19 @@ PrintingSection::PrintingSection(Profile* profile,
updater.AddSearchTags(GetPrintingSearchConcepts()); updater.AddSearchTags(GetPrintingSearchConcepts());
if (IsPrintManagementEnabled()) if (IsPrintManagementEnabled())
updater.AddSearchTags(GetPrintingManagementSearchConcepts()); updater.AddSearchTags(GetPrintingManagementSearchConcepts());
// Saved Printers search tags are added/removed dynamically.
if (printers_manager_) {
printers_manager_->AddObserver(this);
UpdateSavedPrintersSearchTags();
}
} }
PrintingSection::~PrintingSection() = default; PrintingSection::~PrintingSection() {
if (printers_manager_) {
printers_manager_->RemoveObserver(this);
}
}
void PrintingSection::AddLoadTimeData(content::WebUIDataSource* html_source) { void PrintingSection::AddLoadTimeData(content::WebUIDataSource* html_source) {
static constexpr webui::LocalizedString kLocalizedStrings[] = { static constexpr webui::LocalizedString kLocalizedStrings[] = {
...@@ -274,5 +291,22 @@ void PrintingSection::RegisterHierarchy(HierarchyGenerator* generator) const { ...@@ -274,5 +291,22 @@ void PrintingSection::RegisterHierarchy(HierarchyGenerator* generator) const {
kPrintingDetailsSettings, generator); kPrintingDetailsSettings, generator);
} }
void PrintingSection::OnPrintersChanged(PrinterClass printer_class,
const std::vector<Printer>& printers) {
UpdateSavedPrintersSearchTags();
}
void PrintingSection::UpdateSavedPrintersSearchTags() {
// Start with no saved printers search tags.
SearchTagRegistry::ScopedTagUpdater updater = registry()->StartUpdate();
updater.RemoveSearchTags(GetSavedPrintersSearchConcepts());
std::vector<Printer> saved_printers =
printers_manager_->GetPrinters(PrinterClass::kSaved);
if (!saved_printers.empty()) {
updater.AddSearchTags(GetSavedPrintersSearchConcepts());
}
}
} // namespace settings } // namespace settings
} // namespace chromeos } // namespace chromeos
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_PRINTING_SECTION_H_ #ifndef CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_PRINTING_SECTION_H_
#define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_PRINTING_SECTION_H_ #define CHROME_BROWSER_UI_WEBUI_SETTINGS_CHROMEOS_PRINTING_SECTION_H_
#include "chrome/browser/chromeos/printing/cups_printers_manager.h"
#include "chrome/browser/ui/webui/settings/chromeos/os_settings_section.h" #include "chrome/browser/ui/webui/settings/chromeos/os_settings_section.h"
namespace content { namespace content {
...@@ -20,7 +21,8 @@ namespace settings { ...@@ -20,7 +21,8 @@ namespace settings {
class SearchTagRegistry; class SearchTagRegistry;
// Provides UI strings and search tags for Printing settings. // Provides UI strings and search tags for Printing settings.
class PrintingSection : public OsSettingsSection { class PrintingSection : public OsSettingsSection,
public CupsPrintersManager::Observer {
public: public:
PrintingSection(Profile* profile, PrintingSection(Profile* profile,
SearchTagRegistry* search_tag_registry, SearchTagRegistry* search_tag_registry,
...@@ -37,6 +39,12 @@ class PrintingSection : public OsSettingsSection { ...@@ -37,6 +39,12 @@ class PrintingSection : public OsSettingsSection {
std::string GetSectionPath() const override; std::string GetSectionPath() const override;
void RegisterHierarchy(HierarchyGenerator* generator) const override; void RegisterHierarchy(HierarchyGenerator* generator) const override;
// CupsPrintersManager::Observer
void OnPrintersChanged(PrinterClass printer_class,
const std::vector<Printer>& printers) override;
void UpdateSavedPrintersSearchTags();
CupsPrintersManager* printers_manager_; CupsPrintersManager* printers_manager_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment