1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
// 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.
#include "chrome/browser/printing/print_preview_sticky_settings.h"
#include "base/json/json_reader.h"
#include "base/no_destructor.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
namespace printing {
namespace {
constexpr char kId[] = "id";
constexpr char kRecentDestinations[] = "recentDestinations";
constexpr char kSettingAppState[] = "appState";
} // namespace
// static
PrintPreviewStickySettings* PrintPreviewStickySettings::GetInstance() {
static base::NoDestructor<PrintPreviewStickySettings> instance;
return instance.get();
}
PrintPreviewStickySettings::PrintPreviewStickySettings() = default;
PrintPreviewStickySettings::~PrintPreviewStickySettings() = default;
const std::string* PrintPreviewStickySettings::printer_app_state() const {
return printer_app_state_ ? &printer_app_state_.value() : nullptr;
}
void PrintPreviewStickySettings::StoreAppState(const std::string& data) {
printer_app_state_ = std::make_optional(data);
}
void PrintPreviewStickySettings::SaveInPrefs(PrefService* prefs) const {
base::Value::Dict dict;
if (printer_app_state_)
dict.Set(kSettingAppState, *printer_app_state_);
prefs->SetDict(prefs::kPrintPreviewStickySettings, std::move(dict));
}
void PrintPreviewStickySettings::RestoreFromPrefs(PrefService* prefs) {
const base::Value::Dict& value =
prefs->GetDict(prefs::kPrintPreviewStickySettings);
const std::string* app_state = value.FindString(kSettingAppState);
if (app_state)
StoreAppState(*app_state);
}
base::flat_map<std::string, int>
PrintPreviewStickySettings::GetPrinterRecentlyUsedRanks() {
auto recently_used_printers = GetRecentlyUsedPrinters();
int current_rank = 0;
std::vector<std::pair<std::string, int>> recently_used_ranks;
recently_used_ranks.reserve(recently_used_printers.size());
for (std::string& printer_id : recently_used_printers)
recently_used_ranks.emplace_back(std::move(printer_id), current_rank++);
return recently_used_ranks;
}
std::vector<std::string> PrintPreviewStickySettings::GetRecentlyUsedPrinters() {
const std::string* sticky_settings_state = printer_app_state();
if (!sticky_settings_state)
return {};
std::optional<base::Value::Dict> sticky_settings_state_value =
base::JSONReader::ReadDict(*sticky_settings_state);
if (!sticky_settings_state_value) {
return {};
}
base::Value::List* recent_destinations =
sticky_settings_state_value->FindList(kRecentDestinations);
if (!recent_destinations)
return {};
std::vector<std::string> printers;
printers.reserve(recent_destinations->size());
for (const auto& recent_destination : *recent_destinations) {
if (!recent_destination.is_dict())
continue;
const std::string* printer_id =
recent_destination.GetDict().FindString(kId);
if (!printer_id)
continue;
printers.push_back(*printer_id);
}
return printers;
}
// static
void PrintPreviewStickySettings::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kPrintPreviewStickySettings);
}
} // namespace printing
|