forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register ReactInstanceManager with modern CDP backend (1/2) (facebook…
…#43250) Summary: Pull Request resolved: facebook#43250 Integrates the modern CDP backend with `ReactInstanceManager` on Android. `ReactInstanceManager` is equivalent to the CDP page / `HostTarget` concept, therefore we register the `addPage`/`removePage` calls with this object's lifecycle. Implementation notes: - `ReactInstanceManagerInspectorTarget` is created to avoid converting `ReactInstanceManager` to JNI (impacting tests). - Its constructor receives a `TargetDelegate` object, so that we avoid passing the entire `ReactInstanceManager` class through (avoids cyclic dependency from `com.facebook.react.bridge` to `com.facebook.react`). Changelog: [Internal] - Register `ReactInstanceManager` with modern CDP backend Reviewed By: motiz88 Differential Revision: D51456960 fbshipit-source-id: 942255bb2487fdc581eb4fa0903c8e68106f8b35
- Loading branch information
1 parent
6f956af
commit fcddb16
Showing
6 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...tAndroid/src/main/java/com/facebook/react/bridge/ReactInstanceManagerInspectorTarget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.facebook.react.bridge; | ||
|
||
import com.facebook.jni.HybridData; | ||
import com.facebook.proguard.annotations.DoNotStripAny; | ||
import java.util.concurrent.Executor; | ||
|
||
@DoNotStripAny | ||
public class ReactInstanceManagerInspectorTarget implements AutoCloseable { | ||
public interface TargetDelegate { | ||
public void onReload(); | ||
} | ||
|
||
private final HybridData mHybridData; | ||
|
||
public ReactInstanceManagerInspectorTarget(TargetDelegate delegate) { | ||
mHybridData = | ||
initHybrid( | ||
new Executor() { | ||
@Override | ||
public void execute(Runnable command) { | ||
if (UiThreadUtil.isOnUiThread()) { | ||
command.run(); | ||
} else { | ||
UiThreadUtil.runOnUiThread(command); | ||
} | ||
} | ||
}, | ||
delegate); | ||
} | ||
|
||
private native HybridData initHybrid(Executor executor, TargetDelegate delegate); | ||
|
||
public void close() { | ||
mHybridData.resetNative(); | ||
} | ||
|
||
static { | ||
ReactBridge.staticInit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../react-native/ReactAndroid/src/main/jni/react/jni/ReactInstanceManagerInspectorTarget.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 <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()); | ||
} | ||
|
||
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 = 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}); | ||
} | ||
} | ||
|
||
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::registerNatives() { | ||
registerHybrid({ | ||
makeNativeMethod( | ||
"initHybrid", ReactInstanceManagerInspectorTarget::initHybrid), | ||
}); | ||
} | ||
|
||
void ReactInstanceManagerInspectorTarget::onReload( | ||
const PageReloadRequest& /*request*/) { | ||
delegate_->onReload(); | ||
} | ||
|
||
HostTarget* ReactInstanceManagerInspectorTarget::getInspectorTarget() { | ||
return inspectorTarget_.get(); | ||
} | ||
|
||
} // namespace facebook::react |
67 changes: 67 additions & 0 deletions
67
...es/react-native/ReactAndroid/src/main/jni/react/jni/ReactInstanceManagerInspectorTarget.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <fb/fbjni.h> | ||
#include <jsinspector-modern/HostTarget.h> | ||
#include <react/jni/JExecutor.h> | ||
|
||
namespace facebook::react { | ||
|
||
class ReactInstanceManagerInspectorTarget | ||
: public jni::HybridClass<ReactInstanceManagerInspectorTarget>, | ||
public jsinspector_modern::HostTargetDelegate { | ||
private: | ||
struct TargetDelegate : public facebook::jni::JavaClass<TargetDelegate> { | ||
static constexpr auto kJavaDescriptor = | ||
"Lcom/facebook/react/bridge/ReactInstanceManagerInspectorTarget$TargetDelegate;"; | ||
|
||
void onReload() const; | ||
}; | ||
|
||
public: | ||
static constexpr auto kJavaDescriptor = | ||
"Lcom/facebook/react/bridge/ReactInstanceManagerInspectorTarget;"; | ||
|
||
ReactInstanceManagerInspectorTarget( | ||
const ReactInstanceManagerInspectorTarget&) = delete; | ||
ReactInstanceManagerInspectorTarget& operator=( | ||
const ReactInstanceManagerInspectorTarget&) = delete; | ||
|
||
~ReactInstanceManagerInspectorTarget() override; | ||
|
||
static jni::local_ref<jhybriddata> initHybrid( | ||
jni::alias_ref<jhybridobject> jobj, | ||
jni::alias_ref<JExecutor::javaobject> executor, | ||
jni::alias_ref< | ||
ReactInstanceManagerInspectorTarget::TargetDelegate::javaobject> | ||
delegate); | ||
|
||
static void registerNatives(); | ||
|
||
void onReload(const PageReloadRequest& request) override; | ||
jsinspector_modern::HostTarget* getInspectorTarget(); | ||
|
||
private: | ||
friend HybridBase; | ||
|
||
ReactInstanceManagerInspectorTarget( | ||
jni::alias_ref<ReactInstanceManagerInspectorTarget::jhybridobject> jobj, | ||
jni::alias_ref<JExecutor::javaobject> executor, | ||
jni::alias_ref< | ||
ReactInstanceManagerInspectorTarget::TargetDelegate::javaobject> | ||
delegate); | ||
|
||
jni::global_ref< | ||
ReactInstanceManagerInspectorTarget::TargetDelegate::javaobject> | ||
delegate_; | ||
std::shared_ptr<jsinspector_modern::HostTarget> inspectorTarget_; | ||
std::optional<int> inspectorPageId_; | ||
}; | ||
|
||
} // namespace facebook::react |