File: message_port_fuchsia.h

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 (97 lines) | stat: -rw-r--r-- 3,674 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
// 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.

#ifndef COMPONENTS_CAST_MESSAGE_PORT_FUCHSIA_MESSAGE_PORT_FUCHSIA_H_
#define COMPONENTS_CAST_MESSAGE_PORT_FUCHSIA_MESSAGE_PORT_FUCHSIA_H_

#include <fuchsia/web/cpp/fidl.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/interface_handle.h>
#include <lib/fidl/cpp/interface_request.h>

#include <optional>
#include <string_view>

#include "base/containers/circular_deque.h"
#include "base/memory/raw_ptr.h"
#include "components/cast/message_port/message_port.h"

namespace cast_api_bindings {

// Implements the MessagePort abstraction for the FIDL interface
// fuchsia::web::WebMessagePort.
class MessagePortFuchsia : public cast_api_bindings::MessagePort {
 public:
  ~MessagePortFuchsia() override;

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

  // Creates a pair of message ports. Clients must respect |client| and
  // |server| semantics because they matter for some implementations.
  static void CreatePair(std::unique_ptr<MessagePort>* client,
                         std::unique_ptr<MessagePort>* server);
  static std::unique_ptr<MessagePort> Create(
      fidl::InterfaceHandle<::fuchsia::web::MessagePort> port);
  static std::unique_ptr<MessagePort> Create(
      fidl::InterfaceRequest<::fuchsia::web::MessagePort> port);

  // Gets the implementation of |port| for callers who know its platform type.
  static MessagePortFuchsia* FromMessagePort(MessagePort* port);

  // Returns the platform-specific port resources and invalidates this object.
  // The caller is responsible for choosing the take method which is appropriate
  // to the underlying FIDL resource. Attempting to take the wrong resource will
  // produce a DCHECK failure.
  virtual fidl::InterfaceHandle<::fuchsia::web::MessagePort>
  TakeClientHandle() = 0;
  virtual fidl::InterfaceRequest<::fuchsia::web::MessagePort>
  TakeServiceRequest() = 0;

 protected:
  // Represents whether a MessagePortFuchsia was created from an InterfaceHandle
  // (PortType::HANDLE) or InterfaceRequest (PortType::REQUEST)
  enum class PortType {
    HANDLE = 1,
    REQUEST = 2,
  };

  explicit MessagePortFuchsia(PortType port_type);

  // Creates a fuchsia::web::WebMessage containing |message| and transferring
  // |ports|
  static fuchsia::web::WebMessage CreateWebMessage(
      std::string_view message,
      std::vector<std::unique_ptr<MessagePort>> ports);

  // Delivers a message to FIDL from |message_queue_|.
  virtual void DeliverMessageToFidl() = 0;

  // Extracts the message and transferrables from |message_queue_| and invokes
  // |receiver_.OnMessage()| to process it. Returns a FrameError if extracting
  // or handling the message fails.
  // Note that handling the message may result in |this| being deleted before
  // the call returns.
  std::optional<fuchsia::web::FrameError> ExtractAndHandleMessageFromFidl(
      fuchsia::web::WebMessage message);

  void OnZxError(zx_status_t status);
  void ReportPipeError();

  // cast_api_bindings::MessagePort implementation
  bool PostMessage(std::string_view message) final;
  bool PostMessageWithTransferables(
      std::string_view message,
      std::vector<std::unique_ptr<MessagePort>> ports) final;

  raw_ptr<cast_api_bindings::MessagePort::Receiver> receiver_ = nullptr;
  base::circular_deque<fuchsia::web::WebMessage> message_queue_;

 private:
  const PortType port_type_;
};

}  // namespace cast_api_bindings

#endif  // COMPONENTS_CAST_MESSAGE_PORT_FUCHSIA_MESSAGE_PORT_FUCHSIA_H_