Commit 26222d10 authored by Nikita Podguzov's avatar Nikita Podguzov Committed by Commit Bot

Printing: Move StickySettings to c/b/printing

As far as we want to reuse StickySettings in Printing API for
extracting information about recent printer activity we should place it
in more appropriate component.

Also create GetInstance() function to support the assumption that only
one instance of the class is instantiated.

Background: crrev.com/c/1893206

Bug: 996785
Change-Id: I6c5205ef181e56836954f83990a007ee04f351ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1947738
Commit-Queue: Nikita Podguzov <nikitapodguzov@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#721430}
parent ca475fdb
......@@ -4369,6 +4369,8 @@ jumbo_static_library("browser") {
"printing/print_preview_dialog_controller.h",
"printing/print_preview_message_handler.cc",
"printing/print_preview_message_handler.h",
"printing/print_preview_sticky_settings.cc",
"printing/print_preview_sticky_settings.h",
"printing/print_view_manager.cc",
"printing/print_view_manager.h",
"printing/printing_service.cc",
......
......@@ -54,6 +54,7 @@
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/previews/previews_lite_page_redirect_decider.h"
#include "chrome/browser/previews/previews_offline_helper.h"
#include "chrome/browser/printing/print_preview_sticky_settings.h"
#include "chrome/browser/profiles/chrome_version_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
......@@ -80,7 +81,6 @@
#include "chrome/browser/ui/webui/flags_ui.h"
#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
#include "chrome/browser/ui/webui/print_preview/policy_settings.h"
#include "chrome/browser/ui/webui/print_preview/sticky_settings.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/webauthn/chrome_authenticator_request_delegate.h"
#include "chrome/common/buildflags.h"
......@@ -876,7 +876,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry,
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
printing::PolicySettings::RegisterProfilePrefs(registry);
printing::StickySettings::RegisterProfilePrefs(registry);
printing::PrintPreviewStickySettings::RegisterProfilePrefs(registry);
#endif
#if BUILDFLAG(ENABLE_SERVICE_DISCOVERY)
......
......@@ -2,8 +2,9 @@
// 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/print_preview/sticky_settings.h"
#include "chrome/browser/printing/print_preview_sticky_settings.h"
#include "base/no_destructor.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
......@@ -17,26 +18,32 @@ const char kSettingAppState[] = "appState";
} // namespace
StickySettings::StickySettings() {}
// static
PrintPreviewStickySettings* PrintPreviewStickySettings::GetInstance() {
static base::NoDestructor<PrintPreviewStickySettings> instance;
return instance.get();
}
PrintPreviewStickySettings::PrintPreviewStickySettings() = default;
StickySettings::~StickySettings() {}
PrintPreviewStickySettings::~PrintPreviewStickySettings() = default;
const std::string* StickySettings::printer_app_state() const {
const std::string* PrintPreviewStickySettings::printer_app_state() const {
return printer_app_state_ ? &printer_app_state_.value() : nullptr;
}
void StickySettings::StoreAppState(const std::string& data) {
void PrintPreviewStickySettings::StoreAppState(const std::string& data) {
printer_app_state_ = base::make_optional(data);
}
void StickySettings::SaveInPrefs(PrefService* prefs) const {
void PrintPreviewStickySettings::SaveInPrefs(PrefService* prefs) const {
base::Value dict(base::Value::Type::DICTIONARY);
if (printer_app_state_)
dict.SetKey(kSettingAppState, base::Value(*printer_app_state_));
prefs->Set(prefs::kPrintPreviewStickySettings, dict);
}
void StickySettings::RestoreFromPrefs(PrefService* prefs) {
void PrintPreviewStickySettings::RestoreFromPrefs(PrefService* prefs) {
const base::DictionaryValue* value =
prefs->GetDictionary(prefs::kPrintPreviewStickySettings);
const base::Value* app_state =
......@@ -46,7 +53,7 @@ void StickySettings::RestoreFromPrefs(PrefService* prefs) {
}
// static
void StickySettings::RegisterProfilePrefs(
void PrintPreviewStickySettings::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kPrintPreviewStickySettings);
}
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_STICKY_SETTINGS_H_
#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_STICKY_SETTINGS_H_
#ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_STICKY_SETTINGS_H_
#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_STICKY_SETTINGS_H_
#include <string>
......@@ -21,10 +21,12 @@ namespace printing {
// Holds all the settings that should be remembered (sticky) in print preview.
// A sticky setting will be restored next time the user launches print preview.
// Only one instance of this class is instantiated.
class StickySettings {
class PrintPreviewStickySettings {
public:
StickySettings();
~StickySettings();
static PrintPreviewStickySettings* GetInstance();
PrintPreviewStickySettings();
~PrintPreviewStickySettings();
const std::string* printer_app_state() const;
......@@ -42,4 +44,4 @@ class StickySettings {
} // namespace printing
#endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_STICKY_SETTINGS_H_
#endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_STICKY_SETTINGS_H_
......@@ -3973,8 +3973,6 @@ jumbo_static_library("ui") {
"webui/print_preview/print_preview_utils.h",
"webui/print_preview/printer_handler.cc",
"webui/print_preview/printer_handler.h",
"webui/print_preview/sticky_settings.cc",
"webui/print_preview/sticky_settings.h",
]
deps += [
"//services/device/public/cpp/usb",
......
......@@ -24,9 +24,9 @@
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#include "chrome/browser/printing/print_preview_sticky_settings.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/browser/ui/webui/print_preview/sticky_settings.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/grit/generated_resources.h"
#include "components/cloud_devices/common/printer_description.h"
......@@ -168,9 +168,10 @@ void ConstructCapabilitiesAndCompleteCallback(
} // namespace
PdfPrinterHandler::PdfPrinterHandler(Profile* profile,
content::WebContents* preview_web_contents,
StickySettings* sticky_settings)
PdfPrinterHandler::PdfPrinterHandler(
Profile* profile,
content::WebContents* preview_web_contents,
PrintPreviewStickySettings* sticky_settings)
: preview_web_contents_(preview_web_contents),
profile_(profile),
sticky_settings_(sticky_settings) {}
......
......@@ -23,21 +23,19 @@ namespace content {
class WebContents;
}
namespace printing {
class StickySettings;
}
class GURL;
class Profile;
namespace printing {
class PrintPreviewStickySettings;
class PdfPrinterHandler : public PrinterHandler,
public ui::SelectFileDialog::Listener {
public:
PdfPrinterHandler(Profile* profile,
content::WebContents* preview_web_contents,
StickySettings* sticky_settings);
PrintPreviewStickySettings* sticky_settings);
~PdfPrinterHandler() override;
......@@ -91,7 +89,7 @@ class PdfPrinterHandler : public PrinterHandler,
const base::FilePath& directory);
Profile* const profile_;
StickySettings* const sticky_settings_;
PrintPreviewStickySettings* const sticky_settings_;
// Holds the path to the print to pdf request. It is empty if no such request
// exists.
......
......@@ -43,7 +43,7 @@ class FakePdfPrinterHandler : public PdfPrinterHandler {
public:
FakePdfPrinterHandler(Profile* profile,
content::WebContents* contents,
StickySettings* sticky_settings)
PrintPreviewStickySettings* sticky_settings)
: PdfPrinterHandler(profile, contents, sticky_settings),
save_failed_(false) {}
......
......@@ -33,6 +33,7 @@
#include "chrome/browser/printing/print_error_dialog.h"
#include "chrome/browser/printing/print_job_manager.h"
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#include "chrome/browser/printing/print_preview_sticky_settings.h"
#include "chrome/browser/printing/print_view_manager.h"
#include "chrome/browser/printing/printer_manager_dialog.h"
#include "chrome/browser/profiles/profile.h"
......@@ -46,7 +47,6 @@
#include "chrome/browser/ui/webui/print_preview/policy_settings.h"
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
#include "chrome/browser/ui/webui/print_preview/printer_handler.h"
#include "chrome/browser/ui/webui/print_preview/sticky_settings.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
......@@ -423,13 +423,6 @@ UserActionBuckets DetermineUserAction(const base::Value& settings) {
return PRINT_TO_PRINTER;
}
base::LazyInstance<StickySettings>::DestructorAtExit g_sticky_settings =
LAZY_INSTANCE_INITIALIZER;
StickySettings* GetStickySettings() {
return g_sticky_settings.Pointer();
}
base::Value GetPolicies(const PrefService& prefs) {
base::Value policies(base::Value::Type::DICTIONARY);
......@@ -892,7 +885,8 @@ void PrintPreviewHandler::HandleCancelPendingPrintRequest(
void PrintPreviewHandler::HandleSaveAppState(const base::ListValue* args) {
std::string data_to_save;
StickySettings* sticky_settings = GetStickySettings();
PrintPreviewStickySettings* sticky_settings =
PrintPreviewStickySettings::GetInstance();
if (args->GetString(0, &data_to_save) && !data_to_save.empty())
sticky_settings->StoreAppState(data_to_save);
sticky_settings->SaveInPrefs(GetPrefs());
......@@ -1081,7 +1075,8 @@ void PrintPreviewHandler::SendInitialSettings(
initial_settings.SetBoolKey(kSettingShouldPrintSelectionOnly,
print_preview_ui()->print_selection_only());
PrefService* prefs = GetPrefs();
StickySettings* sticky_settings = GetStickySettings();
PrintPreviewStickySettings* sticky_settings =
PrintPreviewStickySettings::GetInstance();
sticky_settings->RestoreFromPrefs(prefs);
if (sticky_settings->printer_app_state()) {
initial_settings.SetStringKey(kAppState,
......@@ -1359,7 +1354,7 @@ PrinterHandler* PrintPreviewHandler::GetPrinterHandler(
if (!pdf_printer_handler_) {
pdf_printer_handler_ = PrinterHandler::CreateForPdfPrinter(
Profile::FromWebUI(web_ui()), preview_web_contents(),
GetStickySettings());
PrintPreviewStickySettings::GetInstance());
}
return pdf_printer_handler_.get();
}
......
......@@ -49,7 +49,7 @@ std::unique_ptr<PrinterHandler> PrinterHandler::CreateForLocalPrinters(
std::unique_ptr<PrinterHandler> PrinterHandler::CreateForPdfPrinter(
Profile* profile,
content::WebContents* preview_web_contents,
StickySettings* sticky_settings) {
PrintPreviewStickySettings* sticky_settings) {
return std::make_unique<PdfPrinterHandler>(profile, preview_web_contents,
sticky_settings);
}
......
......@@ -23,7 +23,7 @@ class Profile;
namespace printing {
class StickySettings;
class PrintPreviewStickySettings;
// Wrapper around PrinterProviderAPI to be used by print preview.
// It makes request lifetime management easier, and hides details of more
......@@ -62,7 +62,7 @@ class PrinterHandler {
static std::unique_ptr<PrinterHandler> CreateForPdfPrinter(
Profile* profile,
content::WebContents* preview_web_contents,
StickySettings* sticky_settings);
PrintPreviewStickySettings* sticky_settings);
static std::unique_ptr<PrinterHandler> CreateForLocalPrinters(
content::WebContents* preview_web_contents,
......
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