File: web_print_job.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,790 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/printing/web_print_job.h"

#include "third_party/blink/renderer/bindings/modules/v8/v8_web_print_job_attributes.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_web_print_job_state.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/event_target_names.h"
#include "third_party/blink/renderer/core/event_type_names.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/printing/web_printing_type_converters.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

namespace {

using V8JobStateEnum = V8WebPrintJobState::Enum;

bool AreFurtherStateUpdatesPossible(V8JobStateEnum state) {
  switch (state) {
    case V8JobStateEnum::kCompleted:
    case V8JobStateEnum::kAborted:
    case V8JobStateEnum::kCanceled:
      // These states are terminal -- no more updates from the browser.
      return false;
    case V8JobStateEnum::kPreliminary:
    case V8JobStateEnum::kPending:
    case V8JobStateEnum::kProcessing:
      return true;
  }
}

}  // namespace

WebPrintJob::WebPrintJob(ExecutionContext* execution_context,
                         mojom::blink::WebPrintJobInfoPtr print_job_info)
    : ActiveScriptWrappable<WebPrintJob>({}),
      ExecutionContextClient(execution_context),
      attributes_(MakeGarbageCollected<WebPrintJobAttributes>()),
      observer_(this, execution_context),
      controller_(execution_context) {
  attributes_->setJobName(print_job_info->job_name);
  attributes_->setJobPages(print_job_info->job_pages);
  attributes_->setJobPagesCompleted(0);
  attributes_->setJobState(V8JobStateEnum::kPreliminary);

  observer_.Bind(std::move(print_job_info->observer),
                 execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI));
  controller_.Bind(
      std::move(print_job_info->controller),
      execution_context->GetTaskRunner(TaskType::kMiscPlatformAPI));
}

WebPrintJob::~WebPrintJob() = default;

void WebPrintJob::cancel() {
  // There's no sense in cancelling a job that has either already been cancelled
  // or is in a terminal state.
  if (cancel_called_ ||
      !AreFurtherStateUpdatesPossible(attributes_->jobState().AsEnum())) {
    return;
  }
  cancel_called_ = true;
  controller_->Cancel();
}

ExecutionContext* WebPrintJob::GetExecutionContext() const {
  return ExecutionContextClient::GetExecutionContext();
}

const AtomicString& WebPrintJob::InterfaceName() const {
  return event_target_names::kWebPrintJob;
}

void WebPrintJob::OnWebPrintJobUpdate(
    mojom::blink::WebPrintJobUpdatePtr update) {
  auto state = mojo::ConvertTo<V8JobStateEnum>(update->state);
  // Discard the update if nothing has actually changed.
  if (state == attributes_->jobState().AsEnum() &&
      update->pages_printed == attributes_->jobPagesCompleted()) {
    return;
  }
  attributes_->setJobState(state);
  attributes_->setJobPagesCompleted(update->pages_printed);
  DispatchEvent(*Event::Create(event_type_names::kJobstatechange));
}

bool WebPrintJob::HasPendingActivity() const {
  // The job is kept alive for as long as there are more updates to be reported
  // and at least one listener to catch them.
  return AreFurtherStateUpdatesPossible(attributes_->jobState().AsEnum()) &&
         HasEventListeners();
}

void WebPrintJob::Trace(Visitor* visitor) const {
  visitor->Trace(attributes_);
  visitor->Trace(observer_);
  visitor->Trace(controller_);
  ExecutionContextClient::Trace(visitor);
  EventTarget::Trace(visitor);
}

}  // namespace blink