File: Callbacks.h

package info (click to toggle)
firefox-esr 140.3.1esr-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,539,016 kB
  • sloc: cpp: 7,380,478; javascript: 6,388,099; ansic: 3,710,142; python: 1,393,715; xml: 628,165; asm: 426,918; java: 184,025; sh: 65,742; makefile: 19,302; objc: 13,059; perl: 12,912; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,226; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (94 lines) | stat: -rw-r--r-- 3,665 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
90
91
92
93
94
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_UniFFICallbacks_h
#define mozilla_UniFFICallbacks_h

#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/UniFFIScaffolding.h"
#include "mozilla/uniffi/Rust.h"

namespace mozilla::uniffi {

/**
 * Generated code to register a callback handler.
 *
 * This stores a reference to JS callback handler.  When Rust wants to invoke a
 * callback method, we will use this reference.
 *
 * Also, call the Rust FFI function to initialize the callback interface.
 */
void RegisterCallbackHandler(uint64_t aInterfaceId,
                             dom::UniFFICallbackHandler& aCallbackHandler,
                             ErrorResult& aError);

/**
 * Generated code to deregister a callback handler.
 *
 * This releases the reference to the JS callback handler. After this, our
 * vtable will still be registered with Rust, but all method calls will fail.
 */
void DeregisterCallbackHandler(uint64_t aInterfaceId, ErrorResult& aError);

/**
 * Implemented by generated code for each callback interface.
 *
 * The generated subclass handles the specifics of each call, while the code in
 * the base class handles generic aspects of the call
 *
 * The generated subclass stores all data needed to make the call, including the
 * arguments passed from Rust internally. MakeCall must only be called
 * once-per-object, since it may consume some of the arguments. This means that
 * we create a new UniffiCallbackMethodHandlerBase subclass instance for each
 * callback interface call from Rust.
 */
class UniffiCallbackMethodHandlerBase {
 protected:
  // Name of the callback interface
  const char* mInterfaceName;
  uint64_t mObjectHandle;

  // Invoke the callback method using a JS handler
  MOZ_CAN_RUN_SCRIPT
  virtual void MakeCall(JSContext* aCx, dom::UniFFICallbackHandler* aJsHandler,
                        ErrorResult& aError) = 0;

 public:
  UniffiCallbackMethodHandlerBase(const char* aInterfaceName,
                                  uint64_t aObjectHandle)
      : mInterfaceName(aInterfaceName), mObjectHandle(aObjectHandle) {}

  virtual ~UniffiCallbackMethodHandlerBase() = default;

  // ---- Generic entry points ----

  // Queue the method to be called asynchronously and ignore the return value.
  //
  // This is for fire-and-forget callbacks where the caller doesn't care about
  // the return value and doesn't want to wait for the call to finish.  A good
  // use case for this is logging.
  //
  // FireAndForget is responsible for checking that the aJsHandler is non-null,
  // this way we don't need to duplicate the null check in the generated code.
  static void FireAndForget(
      UniquePtr<UniffiCallbackMethodHandlerBase> aHandler,
      StaticRefPtr<dom::UniFFICallbackHandler>* aJsHandler);
};

// Class to handle the free method, this is an implicit method for each callback
// interface. In inputs no arguments and has index=0.
class UniffiCallbackFreeHandler : public UniffiCallbackMethodHandlerBase {
 public:
  UniffiCallbackFreeHandler(const char* aInterfaceName, uint64_t aObjectHandle)
      : UniffiCallbackMethodHandlerBase(aInterfaceName, aObjectHandle) {}
  void MakeCall(JSContext* aCx, dom::UniFFICallbackHandler* aJsHandler,
                ErrorResult& aError) override;
};

}  // namespace mozilla::uniffi

#endif  // mozilla_UniFFICallbacks_h