-
Notifications
You must be signed in to change notification settings - Fork 24k
/
ReactInstanceManagerInspectorTarget.cpp
120 lines (103 loc) · 4.03 KB
/
ReactInstanceManagerInspectorTarget.cpp
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
118
119
120
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ReactInstanceManagerInspectorTarget.h"
#include "SafeReleaseJniRef.h"
#include <fbjni/NativeRunnable.h>
#include <jsinspector-modern/InspectorFlags.h>
using namespace facebook::jni;
using namespace facebook::react::jsinspector_modern;
namespace facebook::react {
void ReactInstanceManagerInspectorTarget::TargetDelegate::onReload() const {
auto method = javaClassStatic()->getMethod<void()>("onReload");
method(self());
}
void ReactInstanceManagerInspectorTarget::TargetDelegate::
onSetPausedInDebuggerMessage(
const OverlaySetPausedInDebuggerMessageRequest& request) const {
auto method = javaClassStatic()->getMethod<void(local_ref<JString>)>(
"onSetPausedInDebuggerMessage");
method(self(), request.message ? make_jstring(*request.message) : nullptr);
}
ReactInstanceManagerInspectorTarget::ReactInstanceManagerInspectorTarget(
jni::alias_ref<ReactInstanceManagerInspectorTarget::jhybridobject> jobj,
jni::alias_ref<JExecutor::javaobject> executor,
jni::alias_ref<
ReactInstanceManagerInspectorTarget::TargetDelegate::javaobject>
delegate)
: delegate_(make_global(delegate)) {
auto& inspectorFlags = InspectorFlags::getInstance();
if (inspectorFlags.getEnableModernCDPRegistry()) {
inspectorTarget_ = HostTarget::create(
*this,
[javaExecutor =
// Use a SafeReleaseJniRef because this lambda may be copied to
// arbitrary threads.
SafeReleaseJniRef(make_global(executor))](auto callback) mutable {
auto jrunnable =
JNativeRunnable::newObjectCxxArgs(std::move(callback));
javaExecutor->execute(jrunnable);
});
inspectorPageId_ = getInspectorInstance().addPage(
"React Native Bridge (Experimental)",
/* vm */ "",
[inspectorTarget =
inspectorTarget_](std::unique_ptr<IRemoteConnection> remote)
-> std::unique_ptr<ILocalConnection> {
return inspectorTarget->connect(
std::move(remote),
{
.integrationName =
"Android Bridge (ReactInstanceManagerInspectorTarget)",
});
},
{.nativePageReloads = true, .prefersFuseboxFrontend = true});
}
}
ReactInstanceManagerInspectorTarget::~ReactInstanceManagerInspectorTarget() {
if (inspectorPageId_.has_value()) {
getInspectorInstance().removePage(*inspectorPageId_);
}
}
jni::local_ref<ReactInstanceManagerInspectorTarget::jhybriddata>
ReactInstanceManagerInspectorTarget::initHybrid(
jni::alias_ref<jhybridobject> jobj,
jni::alias_ref<JExecutor::javaobject> executor,
jni::alias_ref<
ReactInstanceManagerInspectorTarget::TargetDelegate::javaobject>
delegate) {
return makeCxxInstance(jobj, executor, delegate);
}
void ReactInstanceManagerInspectorTarget::sendDebuggerResumeCommand() {
if (inspectorTarget_) {
inspectorTarget_->sendCommand(HostCommand::DebuggerResume);
} else {
jni::throwNewJavaException(
"java/lang/IllegalStateException",
"Cannot send command while the Fusebox backend is not enabled");
}
}
void ReactInstanceManagerInspectorTarget::registerNatives() {
registerHybrid({
makeNativeMethod(
"initHybrid", ReactInstanceManagerInspectorTarget::initHybrid),
makeNativeMethod(
"sendDebuggerResumeCommand",
ReactInstanceManagerInspectorTarget::sendDebuggerResumeCommand),
});
}
void ReactInstanceManagerInspectorTarget::onReload(
const PageReloadRequest& /*request*/) {
delegate_->onReload();
}
void ReactInstanceManagerInspectorTarget::onSetPausedInDebuggerMessage(
const OverlaySetPausedInDebuggerMessageRequest& request) {
delegate_->onSetPausedInDebuggerMessage(request);
}
HostTarget* ReactInstanceManagerInspectorTarget::getInspectorTarget() {
return inspectorTarget_.get();
}
} // namespace facebook::react