File: printing_hooks_delegate.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 (99 lines) | stat: -rw-r--r-- 3,455 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
// Copyright 2020 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/renderer/extensions/api/printing_hooks_delegate.h"

#include "extensions/renderer/bindings/api_signature.h"
#include "extensions/renderer/v8_helpers.h"
#include "gin/dictionary.h"
#include "third_party/blink/public/web/web_blob.h"
#include "v8/include/v8-primitive.h"

namespace extensions {

namespace {

constexpr char kSubmitJobMethod[] = "printing.submitJob";

constexpr char kJobKey[] = "job";
constexpr char kDocumentKey[] = "document";
constexpr char kDocumentBlobUuidKey[] = "documentBlobUuid";

}  // namespace

using RequestResult = APIBindingHooks::RequestResult;

PrintingHooksDelegate::PrintingHooksDelegate() = default;

PrintingHooksDelegate::~PrintingHooksDelegate() = default;

RequestResult PrintingHooksDelegate::HandleRequest(
    const std::string& method_name,
    const APISignature* signature,
    v8::Local<v8::Context> context,
    v8::LocalVector<v8::Value>* arguments,
    const APITypeReferenceMap& refs) {
  // Error checks.
  // Ensure we would like to call the SubmitJob function.
  if (method_name != kSubmitJobMethod)
    return RequestResult(RequestResult::NOT_HANDLED);
  // Ensure arguments are successfully parsed and converted.
  APISignature::V8ParseResult parse_result =
      signature->ParseArgumentsToV8(context, *arguments, refs);
  if (!parse_result.succeeded()) {
    RequestResult result(RequestResult::INVALID_INVOCATION);
    result.error = std::move(parse_result.error.value());
    return result;
  }

  return HandleSubmitJob(context->GetIsolate(), arguments);
}

RequestResult PrintingHooksDelegate::HandleSubmitJob(
    v8::Isolate* isolate,
    v8::LocalVector<v8::Value>* arguments) {
  // If being called without the callback parameter (i.e. a promise based API
  // call) the bindings require the final argument to be filled out with a null
  // argument instead.
  // TODO(tjudkins): It would be good to fix the logic to not require this. For
  // more details see the comment in APIBindingJSUtil::SendRequest.
  if (arguments->size() == 1u) {
    arguments->push_back(v8::Null(isolate));
  }
  DCHECK_EQ(2u, arguments->size());
  DCHECK((*arguments)[0]->IsObject());

  v8::Local<v8::Object> v8_submit_job_request =
      (*arguments)[0].As<v8::Object>();

  gin::Dictionary submit_job_request_dict(isolate, v8_submit_job_request);
  v8::Local<v8::Value> v8_print_job;
  if (!submit_job_request_dict.Get(kJobKey, &v8_print_job)) {
    NOTREACHED();
  }
  DCHECK(v8_print_job->IsObject());

  gin::Dictionary print_job_dict(isolate, v8_print_job.As<v8::Object>());
  v8::Local<v8::Value> v8_document;
  if (!print_job_dict.Get(kDocumentKey, &v8_document)) {
    NOTREACHED();
  }
  DCHECK(!v8_document.IsEmpty());
  DCHECK(!v8_document->IsNull());
  DCHECK(!v8_document->IsUndefined());

  blink::WebBlob document_blob =
      blink::WebBlob::FromV8Value(isolate, v8_document);
  v8::Local<v8::String> document_blob_uuid;
  v8_helpers::ToV8String(isolate, document_blob.Uuid().Utf8().data(),
                         &document_blob_uuid);
  if (!submit_job_request_dict.Set(kDocumentBlobUuidKey,
                                   document_blob_uuid.As<v8::Value>())) {
    NOTREACHED() << "Unexpected exception: couldn't update arguments";
  }

  return RequestResult(RequestResult::ARGUMENTS_UPDATED);
}

}  // namespace extensions