summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-js/UniFFICallbacks.h
blob: 34faeda6a298597da69cbe50f0e11fe97c5b17ba (plain)
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
/* -*- 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/dom/UniFFIRust.h"
#include "mozilla/dom/UniFFIScaffolding.h"

namespace mozilla::uniffi {

/**
 * UniFFI-generated scaffolding function to initialize a callback interface
 *
 * The Rust code expests us to pass it a ForeignCallback entrypoint for each
 * callback interface.
 */
typedef void (*CallbackInitFunc)(ForeignCallback, RustCallStatus*);

/**
 * All the information needed to handle a callback interface.
 *
 * The generated code includes a function that maps interface ids to one of
 * these structs
 */
struct CallbackInterfaceInfo {
  // Human-friendly name
  const char* mName;
  // JS handler
  StaticRefPtr<dom::UniFFICallbackHandler>* mHandler;
  // Handler function, defined in the generated C++ code
  ForeignCallback mForeignCallback;
  // Init function, defined in the generated Rust scaffolding.  mForeignCallback
  // should be passed to this.
  CallbackInitFunc mInitForeignCallback;
};

Maybe<CallbackInterfaceInfo> UniFFIGetCallbackInterfaceInfo(
    uint64_t aInterfaceId);

#ifdef MOZ_UNIFFI_FIXTURES
Maybe<CallbackInterfaceInfo> UniFFIFixturesGetCallbackInterfaceInfo(
    uint64_t aInterfaceId);
#endif

/**
 * Register the JS handler for a callback interface
 */
void RegisterCallbackHandler(uint64_t aInterfaceId,
                             dom::UniFFICallbackHandler& aCallbackHandler,
                             ErrorResult& aError);

/**
 * Deregister the JS handler for a callback interface
 */
void DeregisterCallbackHandler(uint64_t aInterfaceId, ErrorResult& aError);

/**
 * Queue a UniFFI callback interface function to run at some point
 *
 * This function 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.
 */
MOZ_CAN_RUN_SCRIPT
void QueueCallback(size_t aInterfaceId, uint64_t aHandle, uint32_t aMethod,
                   RustBuffer aArgs);

}  // namespace mozilla::uniffi

#endif  // mozilla_UniFFICallbacks_h