File: mime_handler_private.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 (117 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/api/mime_handler_private/mime_handler_private.h"

#include <cmath>
#include <utility>

#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
#include "extensions/common/api/mime_handler.mojom.h"
#include "extensions/common/constants.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/http/http_response_headers.h"

namespace extensions {
namespace {

base::flat_map<std::string, std::string> CreateResponseHeadersMap(
    const net::HttpResponseHeaders* headers) {
  base::flat_map<std::string, std::string> result;
  if (!headers)
    return result;

  size_t iter = 0;
  std::string header_name;
  std::string header_value;
  while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
    // mojo strings must be UTF-8 and headers might not be, so drop any headers
    // that aren't ASCII. The PDF plugin does not use any headers with non-ASCII
    // names and non-ASCII values are never useful for the headers the plugin
    // does use.
    //
    // TODO(sammc): Send as bytes instead of a string and let the client decide
    // how to decode.
    if (!base::IsStringASCII(header_name) || !base::IsStringASCII(header_value))
      continue;
    auto& current_value = result[header_name];
    if (!current_value.empty())
      current_value += ", ";
    current_value += header_value;
  }
  return result;
}

}  // namespace

MimeHandlerServiceImpl::MimeHandlerServiceImpl(
    base::WeakPtr<StreamContainer> stream_container)
    : stream_(stream_container) {}

MimeHandlerServiceImpl::~MimeHandlerServiceImpl() = default;

// static
void MimeHandlerServiceImpl::Create(
    base::WeakPtr<StreamContainer> stream_container,
    mojo::PendingReceiver<mime_handler::MimeHandlerService> receiver) {
  mojo::MakeSelfOwnedReceiver(
      std::make_unique<MimeHandlerServiceImpl>(stream_container),
      std::move(receiver));
}

void MimeHandlerServiceImpl::GetStreamInfo(GetStreamInfoCallback callback) {
  if (!stream_) {
    std::move(callback).Run(mime_handler::StreamInfoPtr());
    return;
  }
  std::move(callback).Run(
      mojo::ConvertTo<mime_handler::StreamInfoPtr>(*stream_));
}

void MimeHandlerServiceImpl::SetPdfPluginAttributes(
    mime_handler::PdfPluginAttributesPtr pdf_plugin_attributes) {
  if (!stream_)
    return;

  // Check the `background_color` is an integer.
  double whole = 0.0;
  if (std::modf(pdf_plugin_attributes->background_color, &whole) != 0.0)
    return;

  // Check the `background_color` is within the range of a uint32_t.
  if (!base::IsValueInRangeForNumericType<uint32_t>(
          pdf_plugin_attributes->background_color)) {
    return;
  }

  stream_->set_pdf_plugin_attributes(std::move(pdf_plugin_attributes));
}

}  // namespace extensions

namespace mojo {

extensions::mime_handler::StreamInfoPtr TypeConverter<
    extensions::mime_handler::StreamInfoPtr,
    extensions::StreamContainer>::Convert(const extensions::StreamContainer&
                                              stream) {
  if (stream.stream_url().is_empty())
    return extensions::mime_handler::StreamInfoPtr();

  auto result = extensions::mime_handler::StreamInfo::New();
  result->embedded = stream.embedded();
  result->tab_id = stream.tab_id();
  result->mime_type = stream.mime_type();
  result->original_url = stream.original_url().spec();
  result->stream_url = stream.stream_url().spec();
  result->response_headers =
      extensions::CreateResponseHeadersMap(stream.response_headers());
  return result;
}

}  // namespace mojo