Pranav Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 1 | // 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 Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 9 | #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 Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 13 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Pranav Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 14 | #include "ui/gfx/geometry/size.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | constexpr char kPrintingPaperSizeDefault[] = "printing.paper_size_default"; |
| 19 | |
| 20 | } // namespace |
| 21 | |
| 22 | namespace printing { |
| 23 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 24 | absl::optional<gfx::Size> ParsePaperSizeDefault(const PrefService& prefs) { |
Pranav Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 25 | if (!prefs.HasPrefPath(kPrintingPaperSizeDefault)) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 26 | return absl::nullopt; |
Pranav Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 27 | |
| 28 | const base::Value* paper_size_value = prefs.Get(kPrintingPaperSizeDefault); |
| 29 | if (!paper_size_value || paper_size_value->DictEmpty()) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 30 | return absl::nullopt; |
Pranav Batra | 91824d98 | 2021-04-22 08:48:34 | [diff] [blame] | 31 | |
| 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 |