blob: 689e8adad3b5a8ffccf367420b4ea416daa449af [file] [log] [blame]
Pranav Batra91824d982021-04-22 08:48:341// Copyright 2021 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/printing/browser/prefs_util.h"
6
7#include <string>
8
Pranav Batra91824d982021-04-22 08:48:349#include "base/values.h"
10#include "components/prefs/pref_service.h"
11#include "printing/backend/print_backend_utils.h"
12#include "printing/backend/printing_restrictions.h"
Anton Bikineev1156b5f2021-05-15 22:35:3613#include "third_party/abseil-cpp/absl/types/optional.h"
Pranav Batra91824d982021-04-22 08:48:3414#include "ui/gfx/geometry/size.h"
15
16namespace {
17
18constexpr char kPrintingPaperSizeDefault[] = "printing.paper_size_default";
19
20} // namespace
21
22namespace printing {
23
Anton Bikineev1156b5f2021-05-15 22:35:3624absl::optional<gfx::Size> ParsePaperSizeDefault(const PrefService& prefs) {
Pranav Batra91824d982021-04-22 08:48:3425 if (!prefs.HasPrefPath(kPrintingPaperSizeDefault))
Anton Bikineev1156b5f2021-05-15 22:35:3626 return absl::nullopt;
Pranav Batra91824d982021-04-22 08:48:3427
28 const base::Value* paper_size_value = prefs.Get(kPrintingPaperSizeDefault);
29 if (!paper_size_value || paper_size_value->DictEmpty())
Anton Bikineev1156b5f2021-05-15 22:35:3630 return absl::nullopt;
Pranav Batra91824d982021-04-22 08:48:3431
32 const base::Value* custom_size =
33 paper_size_value->FindKey(kPaperSizeCustomSize);
34 if (custom_size) {
35 return gfx::Size(*custom_size->FindIntKey(kPaperSizeWidth),
36 *custom_size->FindIntKey(kPaperSizeHeight));
37 }
38
39 const std::string* name = paper_size_value->FindStringKey(kPaperSizeName);
40 DCHECK(name);
41 return ParsePaper(*name).size_um;
42}
43
44} // namespace printing