File: print_preview_data_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 4,648 bytes parent folder | download | duplicates (6)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 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_data_service.h"

#include <utility>

#include "base/containers/contains.h"
#include "base/dcheck_is_on.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/singleton.h"
#include "build/build_config.h"
#include "printing/print_job_constants.h"
#include "printing/printing_utils.h"

#if BUILDFLAG(IS_WIN)
#include "chrome/browser/printing/xps_features.h"
#endif

// PrintPreviewDataStore stores data for preview workflow and preview printing
// workflow.
//
// NOTE:
//   This class stores a list of PDFs. The list `index` is zero-based and can
// be `printing::COMPLETE_PREVIEW_DOCUMENT_INDEX` to represent complete preview
// document. The PDF stored at `printing::COMPLETE_PREVIEW_DOCUMENT_INDEX` is
// optimized with font subsetting, compression, etc. PDF's stored at all other
// indices are unoptimized.
//
// PrintPreviewDataStore owns the data and is responsible for freeing it when
// either:
//    a) There is a new data.
//    b) When PrintPreviewDataStore is destroyed.
//
class PrintPreviewDataStore {
 public:
  PrintPreviewDataStore() = default;

  PrintPreviewDataStore(const PrintPreviewDataStore&) = delete;
  PrintPreviewDataStore& operator=(const PrintPreviewDataStore&) = delete;

  ~PrintPreviewDataStore() = default;

  // Get the preview page for the specified `index`.
  void GetPreviewDataForIndex(
      int index,
      scoped_refptr<base::RefCountedMemory>* data) const {
    if (IsInvalidIndex(index))
      return;

    auto it = page_data_map_.find(index);
    if (it != page_data_map_.end())
      *data = it->second.get();
  }

  // Set/Update the preview data entry for the specified `index`.
  void SetPreviewDataForIndex(int index,
                              scoped_refptr<base::RefCountedMemory> data) {
    if (IsInvalidIndex(index))
      return;

    DCHECK(data);
#if DCHECK_IS_ON()
    DCHECK(IsValidData(index, *data));
#endif

    page_data_map_[index] = std::move(data);
  }

 private:
  // 1:1 relationship between page index and its associated preview data.
  // Key: Page index is zero-based and can be
  // `printing::COMPLETE_PREVIEW_DOCUMENT_INDEX` to represent complete preview
  // document.
  // Value: Preview data.
  using PreviewPageDataMap =
      std::map<int, scoped_refptr<base::RefCountedMemory>>;

#if DCHECK_IS_ON()
  bool IsValidData(int index, base::span<const uint8_t> data) const {
#if BUILDFLAG(IS_WIN)
    // Do not have access here whether this print document is from a modifiable
    // source or not, so next best restriction is if some kind of XPS data
    // generation is to be expected.
    if (index == printing::COMPLETE_PREVIEW_DOCUMENT_INDEX &&
        printing::IsXpsPrintCapabilityRequired()) {
      // A valid Windows document could be PDF or XPS.
      printing::DocumentDataType data_type =
          printing::DetermineDocumentDataType(data);
      return data_type == printing::DocumentDataType::kPdf ||
             data_type == printing::DocumentDataType::kXps;
    }
#endif

    // Non-Windows and all individual pages are only ever supposed to be PDF.
    return printing::LooksLikePdf(data);
  }
#endif  // DCHECK_IS_ON()

  static bool IsInvalidIndex(int index) {
    return (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX &&
            index < printing::FIRST_PAGE_INDEX);
  }

  PreviewPageDataMap page_data_map_;
};

// static
PrintPreviewDataService* PrintPreviewDataService::GetInstance() {
  return base::Singleton<PrintPreviewDataService>::get();
}

PrintPreviewDataService::PrintPreviewDataService() = default;

PrintPreviewDataService::~PrintPreviewDataService() = default;

void PrintPreviewDataService::GetDataEntry(
    int32_t preview_ui_id,
    int index,
    scoped_refptr<base::RefCountedMemory>* data_bytes) const {
  *data_bytes = nullptr;
  auto it = data_store_map_.find(preview_ui_id);
  if (it != data_store_map_.end())
    it->second->GetPreviewDataForIndex(index, data_bytes);
}

void PrintPreviewDataService::SetDataEntry(
    int32_t preview_ui_id,
    int index,
    scoped_refptr<base::RefCountedMemory> data_bytes) {
  if (!base::Contains(data_store_map_, preview_ui_id))
    data_store_map_[preview_ui_id] = std::make_unique<PrintPreviewDataStore>();
  data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index,
                                                         std::move(data_bytes));
}

void PrintPreviewDataService::RemoveEntry(int32_t preview_ui_id) {
  data_store_map_.erase(preview_ui_id);
}