Skip to content

Commit

Permalink
Implement new HostTargetMetadata fields (iOS) (#44893)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44893

Adds the following debugger metadata fields (sent over the `ReactNativeApplication.metadataUpdated` CDP event), and implements these on iOS (Bridge and Bridgeless).

- `appIdentifier`
- `deviceName`
- `platform`
- `reactNativeVersion`

Changelog: [Internal]

Reviewed By: robhogan

Differential Revision: D58288489

fbshipit-source-id: 7105ad3b70d409bcd98b232154ebd6b7c827fb2b
  • Loading branch information
huntie authored and facebook-github-bot committed Jun 12, 2024
1 parent aced407 commit 14914bd
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/react-native/React/Base/RCTBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <jsinspector-modern/ReactCdp.h>
#import <optional>
#import "RCTDevLoadingViewProtocol.h"
#import "RCTInspectorUtils.h"
#import "RCTJSThread.h"
#import "RCTLog.h"
#import "RCTModuleData.h"
Expand Down Expand Up @@ -199,8 +200,14 @@ void RCTUIManagerSetDispatchAccessibilityManagerInitOntoMain(BOOL enabled)

facebook::react::jsinspector_modern::HostTargetMetadata getMetadata() override
{
auto metadata = [RCTInspectorUtils getHostMetadata];

return {
.appIdentifier = metadata.appIdentifier,
.deviceName = metadata.deviceName,
.integrationName = "iOS Bridge (RCTBridge)",
.platform = metadata.platform,
.reactNativeVersion = metadata.reactNativeVersion,
};
}

Expand Down
23 changes: 23 additions & 0 deletions packages/react-native/React/DevSupport/RCTInspectorUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

#import <Foundation/Foundation.h>

#import <string>

struct CommonHostMetadata {
std::string appIdentifier;
std::string deviceName;
std::string platform;
std::string reactNativeVersion;
};

@interface RCTInspectorUtils : NSObject

+ (CommonHostMetadata)getHostMetadata;

@end
42 changes: 42 additions & 0 deletions packages/react-native/React/DevSupport/RCTInspectorUtils.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.
*/

#import "RCTInspectorUtils.h"

#import <React/RCTConstants.h>
#import <React/RCTVersion.h>
#import <UIKit/UIKit.h>

@implementation RCTInspectorUtils

+ (CommonHostMetadata)getHostMetadata
{
UIDevice *device = [UIDevice currentDevice];

NSString *appIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSString *platform = RCTPlatformName;
NSString *deviceName = [device name];

auto version = RCTGetReactNativeVersion();
NSString *reactNativeVersion =
[NSString stringWithFormat:@"%i.%i.%i%@",
[version[@"minor"] intValue],
[version[@"major"] intValue],
[version[@"patch"] intValue],
[version[@"prerelease"] isKindOfClass:[NSNull class]]
? @""
: [@"-" stringByAppendingString:[version[@"prerelease"] stringValue]]];

return {
.appIdentifier = [appIdentifier UTF8String],
.platform = [platform UTF8String],
.deviceName = [deviceName UTF8String],
.reactNativeVersion = [reactNativeVersion UTF8String],
};
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ bool HostTargetController::decrementPauseOverlayCounter() {
folly::dynamic hostMetadataToDynamic(const HostTargetMetadata& metadata) {
folly::dynamic result = folly::dynamic::object;

result["appIdentifier"] = metadata.appIdentifier.value_or(nullptr);
result["deviceName"] = metadata.deviceName.value_or(nullptr);
result["integrationName"] = metadata.integrationName.value_or(nullptr);
result["platform"] = metadata.platform.value_or(nullptr);
result["reactNativeVersion"] = metadata.reactNativeVersion.value_or(nullptr);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class HostCommandSender;
class HostTarget;

struct HostTargetMetadata {
std::optional<std::string> appIdentifier;
std::optional<std::string> deviceName;
std::optional<std::string> integrationName;
std::optional<std::string> platform;
std::optional<std::string> reactNativeVersion;
};

/**
Expand Down Expand Up @@ -90,7 +94,8 @@ class HostTargetDelegate {
virtual ~HostTargetDelegate();

/**
* Returns a metadata object describing the host.
* Returns a metadata object describing the host. This is called on an
* initial response to @cdp ReactNativeApplication.enable.
*/
virtual HostTargetMetadata getMetadata() = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <React/RCTConvert.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTInspectorDevServerHelper.h>
#import <React/RCTInspectorUtils.h>
#import <React/RCTJSThread.h>
#import <React/RCTLog.h>
#import <React/RCTMockDef.h>
Expand Down Expand Up @@ -42,8 +43,14 @@ @interface RCTHost () <RCTReloadListener, RCTInstanceDelegate>

jsinspector_modern::HostTargetMetadata getMetadata() override
{
auto metadata = [RCTInspectorUtils getHostMetadata];

return {
.appIdentifier = metadata.appIdentifier,
.deviceName = metadata.deviceName,
.integrationName = "iOS Bridgeless (RCTHost)",
.platform = metadata.platform,
.reactNativeVersion = metadata.reactNativeVersion,
};
}

Expand Down

0 comments on commit 14914bd

Please sign in to comment.