-
Notifications
You must be signed in to change notification settings - Fork 24k
/
JReactHostInspectorTarget.cpp
101 lines (90 loc) · 3.48 KB
/
JReactHostInspectorTarget.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
/*
* 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 "JReactHostInspectorTarget.h"
#include <fbjni/NativeRunnable.h>
#include <jsinspector-modern/InspectorFlags.h>
#include <react/jni/SafeReleaseJniRef.h>
using namespace facebook::jni;
using namespace facebook::react::jsinspector_modern;
namespace facebook::react {
JReactHostInspectorTarget::JReactHostInspectorTarget(
alias_ref<JReactHostImpl::javaobject> reactHostImpl,
alias_ref<JExecutor::javaobject> executor)
: javaReactHostImpl_(make_global(reactHostImpl)),
javaExecutor_(make_global(executor)) {
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(javaExecutor_)](
std::function<void()>&& callback) mutable {
auto jrunnable =
JNativeRunnable::newObjectCxxArgs(std::move(callback));
javaExecutor->execute(jrunnable);
});
inspectorPageId_ = getInspectorInstance().addPage(
"React Native Bridgeless (Experimental)",
/* vm */ "",
[inspectorTargetWeak = std::weak_ptr(inspectorTarget_)](
std::unique_ptr<IRemoteConnection> remote)
-> std::unique_ptr<ILocalConnection> {
if (auto inspectorTarget = inspectorTargetWeak.lock()) {
return inspectorTarget->connect(
std::move(remote),
{
.integrationName = "Android Bridgeless (ReactHostImpl)",
});
}
// Reject the connection.
return nullptr;
},
{.nativePageReloads = true, .prefersFuseboxFrontend = true});
}
}
JReactHostInspectorTarget::~JReactHostInspectorTarget() {
if (inspectorPageId_.has_value()) {
getInspectorInstance().removePage(*inspectorPageId_);
}
}
local_ref<JReactHostInspectorTarget::jhybriddata>
JReactHostInspectorTarget::initHybrid(
alias_ref<JReactHostInspectorTarget::jhybridobject> self,
jni::alias_ref<JReactHostImpl::javaobject> reactHostImpl,
jni::alias_ref<JExecutor::javaobject> executor) {
return makeCxxInstance(reactHostImpl, executor);
}
void JReactHostInspectorTarget::sendDebuggerResumeCommand() {
if (inspectorTarget_) {
inspectorTarget_->sendCommand(HostCommand::DebuggerResume);
} else {
jni::throwNewJavaException(
"java/lang/IllegalStateException",
"Cannot send command while the Fusebox backend is not enabled");
}
}
void JReactHostInspectorTarget::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JReactHostInspectorTarget::initHybrid),
makeNativeMethod(
"sendDebuggerResumeCommand",
JReactHostInspectorTarget::sendDebuggerResumeCommand),
});
}
void JReactHostInspectorTarget::onReload(const PageReloadRequest& request) {
javaReactHostImpl_->reload("CDP Page.reload");
}
void JReactHostInspectorTarget::onSetPausedInDebuggerMessage(
const OverlaySetPausedInDebuggerMessageRequest& request) {
javaReactHostImpl_->setPausedInDebuggerMessage(request.message);
}
HostTarget* JReactHostInspectorTarget::getInspectorTarget() {
return inspectorTarget_ ? inspectorTarget_.get() : nullptr;
}
} // namespace facebook::react