File: print_preview_ui_wrapper.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 (148 lines) | stat: -rw-r--r-- 5,060 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
141
142
143
144
145
146
147
148
// Copyright 2024 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/chromeos/printing/print_preview/print_preview_ui_wrapper.h"

#include "base/containers/flat_map.h"
#include "base/containers/id_map.h"
#include "base/lazy_instance.h"
#include "base/no_destructor.h"
#include "base/unguessable_token.h"
#include "base/values.h"
#include "components/printing/common/print.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "printing/print_job_constants.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"

namespace chromeos {

namespace {

// IDMap instance for this particular instance.
base::LazyInstance<base::IDMap<PrintPreviewUiWrapper*>>::DestructorAtExit
    g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER;

// Mapping from PrintPreviewUI ID to print preview request ID.
// This mapping is used in static accessors to track which print preview
// instance is available and which requests are associated with it.
// It's possible that a print preview instance is destroyed by the time
// preview generation is completed, so this also serves as a way to keep track
// of the available instances.
using PrintPreviewRequestIdMap = base::flat_map<int, int>;

PrintPreviewRequestIdMap& GetPrintPreviewRequestIdMap() {
  static base::NoDestructor<PrintPreviewRequestIdMap> map;
  return *map;
}

}  // namespace

PrintPreviewUiWrapper::PrintPreviewUiWrapper() {
  SetPreviewUIId();
}

// Clean up of member variables should be done via Reset() prior to destruction.
PrintPreviewUiWrapper::~PrintPreviewUiWrapper() = default;

void PrintPreviewUiWrapper::Reset() {
  render_frame_host_ = nullptr;
  receiver_.reset();
  print_render_frame_.reset();

  if (!id_) {
    // `id_` can be empty if no preview generation was requested.
    return;
  }

  g_print_preview_ui_id_map.Get().Remove(*id_);
  GetPrintPreviewRequestIdMap().erase(*id_);
}

void PrintPreviewUiWrapper::BindPrintPreviewUI(content::RenderFrameHost* rfh) {
  CHECK(!render_frame_host_);
  render_frame_host_ = rfh;
  if (!print_render_frame_.is_bound()) {
    render_frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(
        &print_render_frame_);
  }

  if (!receiver_.is_bound()) {
    print_render_frame_->SetPrintPreviewUI(
        receiver_.BindNewEndpointAndPassRemote());
  }
}

// static
bool PrintPreviewUiWrapper::ShouldCancelRequest(
    const std::optional<int32_t>& print_preview_ui_id,
    int request_id) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // A null `print_preview_ui_id` indicates that the preview request for the
  // associated ID is not in progress and can be cancelled.
  if (!print_preview_ui_id) {
    return true;
  }

  auto& map = GetPrintPreviewRequestIdMap();
  auto it = map.find(*print_preview_ui_id);
  return it == map.end() || request_id != it->second;
}

void PrintPreviewUiWrapper::GeneratePrintPreview(base::Value::Dict settings) {
  CHECK(print_render_frame_.is_bound());
  // TODO(jimmyxgong): Store settings for sticky settings support.
  // Store the request ID.
  const int request_id = settings.FindInt(printing::kPreviewRequestID).value();
  GetPrintPreviewRequestIdMap()[*id_] = request_id;
  print_render_frame_->PrintPreview(settings.Clone());
}

// TODO(jimmyxgong): Implement stub.
void PrintPreviewUiWrapper::SetOptionsFromDocument(
    ::printing::mojom::OptionsFromDocumentParamsPtr params,
    int32_t request_id) {}

void PrintPreviewUiWrapper::DidPrepareDocumentForPreview(
    int32_t document_cookie,
    int32_t request_id) {}

void PrintPreviewUiWrapper::DidPreviewPage(
    ::printing::mojom::DidPreviewPageParamsPtr params,
    int32_t request_id) {}

void PrintPreviewUiWrapper::MetafileReadyForPrinting(
    ::printing::mojom::DidPreviewDocumentParamsPtr params,
    int32_t request_id) {}

void PrintPreviewUiWrapper::PrintPreviewFailed(int32_t document_cookie,
                                               int32_t request_id) {}

void PrintPreviewUiWrapper::PrintPreviewCancelled(int32_t document_cookie,
                                                  int32_t request_id) {}

void PrintPreviewUiWrapper::PrinterSettingsInvalid(int32_t document_cookie,
                                                   int32_t request_id) {}

void PrintPreviewUiWrapper::DidGetDefaultPageLayout(
    ::printing::mojom::PageSizeMarginsPtr page_layout_in_points,
    const gfx::RectF& printable_area_in_points,
    bool all_pages_have_custom_size,
    bool all_pages_have_custom_orientation,
    int32_t request_id) {}

void PrintPreviewUiWrapper::DidStartPreview(
    ::printing::mojom::DidStartPreviewParamsPtr params,
    int32_t request_id) {}

void PrintPreviewUiWrapper::SetPreviewUIId() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(!id_);

  id_ = g_print_preview_ui_id_map.Get().Add(this);
  GetPrintPreviewRequestIdMap()[*id_] = -1;
}

}  // namespace chromeos