File: message.h

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,680 bytes parent folder | download | duplicates (4)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_COMMON_API_MESSAGING_MESSAGE_H_
#define EXTENSIONS_COMMON_API_MESSAGING_MESSAGE_H_

#include <string>
#include <variant>

#include "extensions/common/mojom/message_port.mojom-shared.h"
#include "mojo/public/cpp/base/big_buffer.h"

namespace extensions {

// TODO(crbug.com/40321352): `mojo_base::BigBuffer`, by itself, doesn't support
// JS `Blob`s because it doesn't have the `Blob`'s metadata. Switch to
// `blink::mojom::CloneableMessage` to get `Blob` support.
using StructuredCloneMessageWireData = mojo_base::BigBuffer;
// C++ type-safe representation of the `extensions::mojom::MessageData` `union`.
using MessageData = std::variant<std::string, StructuredCloneMessageWireData>;

// Represents a message sent between extension components, encapsulating the
// data payload and associated metadata. This class is represented in mojom as
// the `extensions::mojom::Message` struct in `message_port.mojom`.
//
// Data Payload: A `Message` can hold one of two types of data, distinguished by
// the `format()` field:
//
// 1. JSON-serialized data: For backward compatibility and simple messages, the
//    payload can be a JSON string stored in the `data_` member. The `format()`
//    will be `mojom::SerializationFormat::kJson`.
//
// 2. Structure-cloned data: For complex, non-JSON-serializable objects, this
//    class can hold data serialized by Blink's `(Web)SerializedScriptValue`.
//    This wire data is stored in the `structured_data_` member as a
//    `mojo_base::BigBuffer`, and the `format()` will be
//    `mojom::SerializationFormat::kStructuredClone`.
//
// Metadata:
// - `user_gesture`: This boolean indicates whether the message was sent as a
//   direct result of a user action (e.g., a button click). This is important
//   for determining if an action can be performed without requiring a temporary
//   user activation.
//
// - `from_privileged_context`: This boolean indicates whether the message
//   originated from a trusted, extension-specific context (like a background
//   script) rather than a potentially untrusted context (like a content
//   script).
class Message {
 public:
  Message();
  Message(MessageData data,
          mojom::SerializationFormat format,
          bool user_gesture,
          bool from_privileged_context = false);
  Message(const Message& other);
  Message(Message&& other);
  ~Message();

  Message& operator=(const Message& other);
  Message& operator=(Message&& other);

  bool operator==(const Message& other) const;

  // TODO(crbug.com/40321352): Merge `data()` and `structured_data()` into
  // `message_data()` once the feature is complete and callers are updated to
  // handle the variant.
  const std::string& data() const;
  const StructuredCloneMessageWireData& structured_data() const;
  const MessageData& message_data() const { return data_; }
  mojom::SerializationFormat format() const { return format_; }
  bool user_gesture() const { return user_gesture_; }
  bool from_privileged_context() const { return from_privileged_context_; }

 private:
  MessageData data_;
  // TODO(crbug.com/40321352): Convert `format_` to an unknown value since we
  // shouldn't assume JSON by default anymore.
  mojom::SerializationFormat format_ = mojom::SerializationFormat::kJson;
  bool user_gesture_ = false;
  // The equality check skips `from_privileged_context` because this field is
  // used only for histograms.
  bool from_privileged_context_ = false;
};

}  // namespace extensions

#endif  // EXTENSIONS_COMMON_API_MESSAGING_MESSAGE_H_