From 6152763398efe60521fc86fcf992b6a84361df12 Mon Sep 17 00:00:00 2001 From: Robert Balicki Date: Mon, 7 Nov 2022 07:40:21 -0800 Subject: [PATCH] Buck, etc. changes that enable persisted, sync settings for DevTools (#35163) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35163 # What This diff contains all the changes from D40333083 (https://github.com/facebook/react-native/commit/0fac9817df403e31d8256befe52409c948614706) (aka https://github.com/facebook/react-native/pull/34964), **except** the change to `setUpReactDevTools.js`, which actually uses the new files. # Why * We want to ship the Buck, C++, etc. changes before the JavaScript changes that depend on those files. * Otherwise, apps can fail at startup with the message: ``` `TurboModuleRegistry.getEnforcing(...): '${name}' could not be found. ` + 'Verify that a module by this name is registered in the native binary.', ``` * Note that this only occurs if you are using a previously-built version of the C++, Obj C, etc. files in RN, but a more recent version of the JavaScript files. If you are building from matching sources, this does not occur. * After a few days, we can land the JS files. ## Changelog Changelog [General][Added] Add, but don't use, DevTools Settings Manager. Reviewed By: NickGerleman Differential Revision: D40873390 fbshipit-source-id: c7bac6ae65f85666b8616443db278ebb175b691b --- .../DevToolsSettingsManager.android.js | 13 +++++ .../DevToolsSettingsManager.ios.js | 33 +++++++++++++ .../NativeDevToolsSettingsManager.js | 22 +++++++++ React/CoreModules/BUCK | 1 + .../src/main/java/com/facebook/react/BUCK | 1 + .../react/modules/devtoolssettings/BUCK | 28 +++++++++++ .../DevToolsSettingsManagerModule.java | 49 +++++++++++++++++++ .../main/java/com/facebook/react/shell/BUCK | 1 + .../react/shell/MainReactPackage.java | 7 ++- .../test/java/com/facebook/react/modules/BUCK | 1 + 10 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Libraries/DevToolsSettings/DevToolsSettingsManager.android.js create mode 100644 Libraries/DevToolsSettings/DevToolsSettingsManager.ios.js create mode 100644 Libraries/DevToolsSettings/NativeDevToolsSettingsManager.js create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java diff --git a/Libraries/DevToolsSettings/DevToolsSettingsManager.android.js b/Libraries/DevToolsSettings/DevToolsSettingsManager.android.js new file mode 100644 index 00000000000000..262aafbcb0d69b --- /dev/null +++ b/Libraries/DevToolsSettings/DevToolsSettingsManager.android.js @@ -0,0 +1,13 @@ +/** + * 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. + * + * @flow strict + * @format + */ + +import NativeDevToolsSettingsManager from './NativeDevToolsSettingsManager'; + +module.exports = NativeDevToolsSettingsManager; diff --git a/Libraries/DevToolsSettings/DevToolsSettingsManager.ios.js b/Libraries/DevToolsSettings/DevToolsSettingsManager.ios.js new file mode 100644 index 00000000000000..fdc61730166bf4 --- /dev/null +++ b/Libraries/DevToolsSettings/DevToolsSettingsManager.ios.js @@ -0,0 +1,33 @@ +/** + * 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. + * + * @flow strict-local + * @format + */ + +import type {Spec} from './NativeDevToolsSettingsManager'; + +import Settings from '../Settings/Settings'; + +const CONSOLE_PATCH_SETTINGS_KEY = 'ReactDevTools::ConsolePatchSettings'; + +const DevToolsSettingsManager = { + setConsolePatchSettings: (newConsolePatchSettings: string) => { + Settings.set({ + [CONSOLE_PATCH_SETTINGS_KEY]: newConsolePatchSettings, + }); + }, + getConsolePatchSettings: () => { + const value = Settings.get(CONSOLE_PATCH_SETTINGS_KEY); + if (typeof value === 'string') { + // $FlowFixMe[unclear-type] + return ((value: any): string); + } + return null; + }, +}; + +module.exports = (DevToolsSettingsManager: Spec); diff --git a/Libraries/DevToolsSettings/NativeDevToolsSettingsManager.js b/Libraries/DevToolsSettings/NativeDevToolsSettingsManager.js new file mode 100644 index 00000000000000..904e8d6701c756 --- /dev/null +++ b/Libraries/DevToolsSettings/NativeDevToolsSettingsManager.js @@ -0,0 +1,22 @@ +/** + * 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. + * + * @flow strict + * @format + */ + +import type {TurboModule} from '../TurboModule/RCTExport'; + +import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +setConsolePatchSettings: (newConsolePatchSettings: string) => void; + +getConsolePatchSettings: () => ?string; +} + +export default (TurboModuleRegistry.get( + 'DevToolsSettingsManager', +): ?Spec); diff --git a/React/CoreModules/BUCK b/React/CoreModules/BUCK index 143a840eff4138..9820c22ed4b943 100644 --- a/React/CoreModules/BUCK +++ b/React/CoreModules/BUCK @@ -134,6 +134,7 @@ rn_apple_library( "//xplat/js/react-native-github:FBReactNativeSpecApple", "//xplat/js/react-native-github:RCTLinkingApple", "//xplat/js/react-native-github:RCTPushNotificationApple", + "//xplat/js/react-native-github:RCTSettingsApple", "//xplat/js/react-native-github:ReactInternalApple", ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/react/BUCK b/ReactAndroid/src/main/java/com/facebook/react/BUCK index dd21088699dab5..08f8c1176bc528 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/BUCK @@ -41,6 +41,7 @@ rn_android_library( react_native_target("java/com/facebook/react/modules/appearance:appearance"), react_native_target("java/com/facebook/react/modules/bundleloader:bundleloader"), react_native_target("java/com/facebook/react/modules/debug:debug"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/fabric:fabric"), react_native_target("java/com/facebook/react/modules/debug:interfaces"), react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK new file mode 100644 index 00000000000000..50f0d888a9cdf4 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/BUCK @@ -0,0 +1,28 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_root_target", "react_native_target", "rn_android_library") + +rn_android_library( + name = "devtoolssettings", + srcs = glob(["**/*.java"]), + autoglob = False, + labels = [ + "pfh:ReactNative_CommonInfrastructurePlaceholder", + "supermodule:xplat/default/public.react_native.infra", + ], + language = "JAVA", + provided_deps = [ + react_native_dep("third-party/android/androidx:annotation"), + ], + visibility = [ + "PUBLIC", + ], + deps = [ + react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"), + react_native_dep("third-party/java/infer-annotations:infer-annotations"), + react_native_dep("third-party/java/jsr-305:jsr-305"), + react_native_target("java/com/facebook/react/bridge:bridge"), + react_native_target("java/com/facebook/react/common:common"), + react_native_target("java/com/facebook/react/module/annotations:annotations"), + react_native_target("java/com/facebook/react/modules/core:core"), + ], + exported_deps = [react_native_root_target(":FBReactNativeSpec")], +) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java new file mode 100644 index 00000000000000..0164de2788292f --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/devtoolssettings/DevToolsSettingsManagerModule.java @@ -0,0 +1,49 @@ +/* + * 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.modules.devtoolssettings; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import androidx.annotation.Nullable; +import com.facebook.fbreact.specs.NativeDevToolsSettingsManagerSpec; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.module.annotations.ReactModule; + +@ReactModule(name = DevToolsSettingsManagerModule.NAME) +public class DevToolsSettingsManagerModule extends NativeDevToolsSettingsManagerSpec { + public static final String NAME = "DevToolsSettingsManager"; + + private static final String SHARED_PREFERENCES_PREFIX = "ReactNative__DevToolsSettings"; + private static final String KEY_CONSOLE_PATCH_SETTINGS = "ConsolePatchSettings"; + + private final SharedPreferences mSharedPreferences; + + public DevToolsSettingsManagerModule(ReactApplicationContext reactContext) { + super(reactContext); + mSharedPreferences = + reactContext.getSharedPreferences(SHARED_PREFERENCES_PREFIX, Context.MODE_PRIVATE); + } + + @Override + public String getName() { + return NAME; + } + + @Override + public @Nullable String getConsolePatchSettings() { + return mSharedPreferences.getString(KEY_CONSOLE_PATCH_SETTINGS, null); + } + + @Override + public void setConsolePatchSettings(String newSettings) { + Editor editor = mSharedPreferences.edit(); + editor.putString(KEY_CONSOLE_PATCH_SETTINGS, newSettings); + editor.apply(); + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK index f6d575f9aff635..5ea6716012c739 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/BUCK @@ -37,6 +37,7 @@ rn_android_library( react_native_target("java/com/facebook/react/modules/clipboard:clipboard"), react_native_target("java/com/facebook/react/modules/core:core"), react_native_target("java/com/facebook/react/modules/debug:debug"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/dialog:dialog"), react_native_target("java/com/facebook/react/modules/fresco:fresco"), react_native_target("java/com/facebook/react/modules/i18nmanager:i18nmanager"), diff --git a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java index f845a46118cfd8..df4a46adacf91e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java @@ -23,6 +23,7 @@ import com.facebook.react.modules.blob.FileReaderModule; import com.facebook.react.modules.camera.ImageStoreManager; import com.facebook.react.modules.clipboard.ClipboardModule; +import com.facebook.react.modules.devtoolssettings.DevToolsSettingsManagerModule; import com.facebook.react.modules.dialog.DialogModule; import com.facebook.react.modules.fresco.FrescoModule; import com.facebook.react.modules.i18nmanager.I18nManagerModule; @@ -141,6 +142,8 @@ public MainReactPackage(MainPackageConfig config) { return new VibrationModule(context); case WebSocketModule.NAME: return new WebSocketModule(context); + case DevToolsSettingsManagerModule.NAME: + return new DevToolsSettingsManagerModule(context); default: return null; } @@ -181,7 +184,8 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() { Class.forName("com.facebook.react.shell.MainReactPackage$$ReactModuleInfoProvider"); return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); } catch (ClassNotFoundException e) { - // In OSS case, the annotation processor does not run. We fall back on creating this byhand + // In the OSS case, the annotation processor does not run. We fall back to creating this by + // hand Class[] moduleList = new Class[] { AccessibilityInfoModule.class, @@ -199,6 +203,7 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() { NativeAnimatedModule.class, NetworkingModule.class, PermissionsModule.class, + DevToolsSettingsManagerModule.class, ShareModule.class, StatusBarModule.class, SoundManagerModule.class, diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK b/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK index 235079472f328d..555b08c3863fff 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/BUCK @@ -34,6 +34,7 @@ rn_robolectric_test( react_native_target("java/com/facebook/react/modules/core:core"), react_native_target("java/com/facebook/react/modules/debug:debug"), react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"), + react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"), react_native_target("java/com/facebook/react/modules/dialog:dialog"), react_native_target("java/com/facebook/react/modules/network:network"), react_native_target("java/com/facebook/react/modules/share:share"),