Add a config define for Darwin per-controller storage. (#29037)

It's not quite ready yet; make sure we don't accidentally ship it.
diff --git a/.github/workflows/darwin.yaml b/.github/workflows/darwin.yaml
index 1eddaec..99306fe 100644
--- a/.github/workflows/darwin.yaml
+++ b/.github/workflows/darwin.yaml
@@ -116,6 +116,8 @@
                   # -enableUndefinedBehaviorSanitizer instruments the code in Matter.framework,
                   # but to instrument the code in the underlying libCHIP we need to pass CHIP_IS_UBSAN=YES
                   TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO > >(tee /tmp/darwin/framework-tests/darwin-tests-asan.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-err.log >&2)
+                  # And the same thing, but with MTR_PER_CONTROLLER_STORAGE_ENABLED turned on.
+                  TEST_RUNNER_ASAN_OPTIONS=__CURRENT_VALUE__:detect_stack_use_after_return=1 xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_UBSAN=YES CHIP_IS_BLE=NO GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_PER_CONTROLLER_STORAGE_ENABLED=1' > >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-asan-provisional-err.log >&2)
                   # -enableThreadSanitizer instruments the code in Matter.framework,
                   # but to instrument the code in the underlying libCHIP we need to pass CHIP_IS_TSAN=YES
                   xcodebuild test -target "Matter" -scheme "Matter Framework Tests" -sdk macosx -enableThreadSanitizer YES OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new' CHIP_IS_TSAN=YES CHIP_IS_BLE=NO > >(tee /tmp/darwin/framework-tests/darwin-tests-tsan.log) 2> >(tee /tmp/darwin/framework-tests/darwin-tests-tsan-err.log >&2)
diff --git a/src/darwin/Framework/CHIP/MTRDefines.h b/src/darwin/Framework/CHIP/MTRDefines.h
index dab6250..206ae4d 100644
--- a/src/darwin/Framework/CHIP/MTRDefines.h
+++ b/src/darwin/Framework/CHIP/MTRDefines.h
@@ -53,12 +53,20 @@
 #define MTR_NEWLY_AVAILABLE
 #endif
 
-#if defined(MTR_ENABLE_PROVISIONAL) && MTR_ENABLE_PROVISIONAL
+#if !defined(MTR_ENABLE_PROVISIONAL)
+#define MTR_ENABLE_PROVISIONAL 0
+#endif
+
+#if MTR_ENABLE_PROVISIONAL
 #define MTR_PROVISIONALLY_AVAILABLE MTR_NEWLY_AVAILABLE
 #else
 #define MTR_PROVISIONALLY_AVAILABLE NS_UNAVAILABLE MTR_HIDDEN
 #endif
 
+#ifndef MTR_PER_CONTROLLER_STORAGE_ENABLED
+#define MTR_PER_CONTROLLER_STORAGE_ENABLED 0
+#endif
+
 #pragma mark - Types
 
 typedef NSData * MTRTLVBytes;
diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.h b/src/darwin/Framework/CHIP/MTRDeviceController.h
index 670e12b..d863679 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceController.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceController.h
@@ -47,10 +47,12 @@
  */
 @property (readonly, nonatomic, getter=isRunning) BOOL running;
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 /**
  * The ID assigned to this controller at creation time.
  */
 @property (readonly, nonatomic) NSUUID * uniqueIdentifier MTR_NEWLY_AVAILABLE;
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 /**
  * Return the Node ID assigned to the controller.  Will return nil if the
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h b/src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
index 8d10c7a..3abab9e 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h
@@ -17,7 +17,11 @@
 #import <Foundation/Foundation.h>
 #import <Matter/MTRDefines.h>
 #import <Matter/MTRDeviceController.h>
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTRDeviceControllerStorageDelegate.h>
+#else
+#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 #include <lib/core/CHIPError.h>
 
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h
index 6471db8..5cb4b7c 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.h
@@ -22,7 +22,12 @@
 
 #import <Foundation/Foundation.h>
 #import <Matter/MTRCertificates.h>
+#import <Matter/MTRDefines.h>
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTRDeviceControllerStartupParameters.h>
+#else
+@class MTRDeviceControllerStartupParameters;
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 NS_ASSUME_NONNULL_BEGIN
 
@@ -93,12 +98,15 @@
  */
 - (instancetype)initWithStorage:(id<MTRStorage>)storage;
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 /*
  * Initialize the device controller factory without storage.  In this mode,
  * device controllers will need to have per-controller storage provided to allow
  * storing controller-specific information.
  */
 - (instancetype)init MTR_NEWLY_AVAILABLE;
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 @end
 
 API_AVAILABLE(ios(16.4), macos(13.3), watchos(9.4), tvos(16.4))
@@ -180,6 +188,7 @@
 - (MTRDeviceController * _Nullable)createControllerOnNewFabric:(MTRDeviceControllerStartupParams *)startupParams
                                                          error:(NSError * __autoreleasing *)error;
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 /**
  * Create an MTRDeviceController.  Returns nil on failure.
  *
@@ -191,6 +200,7 @@
  */
 - (MTRDeviceController * _Nullable)createController:(MTRDeviceControllerStartupParameters *)startupParameters
                                               error:(NSError * __autoreleasing *)error MTR_NEWLY_AVAILABLE;
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 @end
 
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
index 195fb61..31b261a 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
@@ -34,6 +34,9 @@
 #import "MTRPersistentStorageDelegateBridge.h"
 #import "MTRSessionResumptionStorageBridge.h"
 #import "NSDataSpanConversion.h"
+#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
+#import "MTRDeviceControllerStartupParameters_Wrapper.h"
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 #import <os/lock.h>
 
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters.h
index 0fca319..7b69f0f 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters.h
@@ -15,13 +15,22 @@
  */
 
 #import <Foundation/Foundation.h>
-
 #import <Matter/MTRDefines.h>
+
+#if defined(MTR_INTERNAL_INCLUDE) && defined(MTR_INCLUDED_FROM_UMBRELLA_HEADER)
+#error Internal includes should not happen from the umbrella header
+#endif
+
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
+
 #import <Matter/MTRDeviceControllerStorageDelegate.h>
 #import <Matter/MTROTAProviderDelegate.h>
 
 NS_ASSUME_NONNULL_BEGIN
 
+#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
+MTR_HIDDEN
+#endif
 MTR_NEWLY_AVAILABLE
 @interface MTRDeviceControllerStartupParameters : NSObject
 
@@ -68,6 +77,9 @@
 
 @end
 
+#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
+MTR_HIDDEN
+#endif
 MTR_NEWLY_AVAILABLE
 @interface MTRDeviceControllerExternalCertificateStartupParameters : MTRDeviceControllerStartupParameters
 
@@ -119,3 +131,5 @@
 @end
 
 NS_ASSUME_NONNULL_END
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters_Wrapper.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters_Wrapper.h
new file mode 100644
index 0000000..537958d
--- /dev/null
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParameters_Wrapper.h
@@ -0,0 +1,25 @@
+/**
+ *    Copyright (c) 2023 Project CHIP Authors
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <Matter/MTRDefines.h>
+
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+#error Should be including Matter/MTRDeviceControllerStartupParameters.h
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
+
+#define MTR_INTERNAL_INCLUDE
+#import <Matter/MTRDeviceControllerStartupParameters.h>
+#undef MTR_INTERNAL_INCLUDE
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm
index 7bbcea9..3da8a90 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams.mm
@@ -23,7 +23,11 @@
 #import "MTRP256KeypairBridge.h"
 #import "NSDataSpanConversion.h"
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTRDeviceControllerStorageDelegate.h>
+#else
+#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 #include <controller/OperationalCredentialsDelegate.h>
 #include <credentials/CHIPCert.h>
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h
index 734197e..9d52067 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStartupParams_Internal.h
@@ -20,7 +20,11 @@
 #import <Foundation/Foundation.h>
 #import <Matter/MTRDefines.h>
 #import <Matter/MTRDeviceController.h>
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTRDeviceControllerStartupParameters.h>
+#else
+#import "MTRDeviceControllerStartupParameters_Wrapper.h"
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 
 #include <crypto/CHIPCryptoPAL.h>
 #include <lib/core/DataModelTypes.h>
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate.h
index 1fee9c6..2a322eb 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate.h
@@ -15,8 +15,15 @@
  */
 
 #import <Foundation/Foundation.h>
+#import <Matter/MTRDefines.h>
 #import <Matter/MTRDeviceController.h>
 
+#if defined(MTR_INTERNAL_INCLUDE) && defined(MTR_INCLUDED_FROM_UMBRELLA_HEADER)
+#error Internal includes should not happen from the umbrella header
+#endif
+
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
+
 NS_ASSUME_NONNULL_BEGIN
 
 typedef NS_ENUM(NSUInteger, MTRStorageSecurityLevel) {
@@ -105,6 +112,13 @@
 @end
 
 // TODO: FIXME: Is this a sane place to put this API?
-MTR_EXTERN MTR_NEWLY_AVAILABLE NSSet<Class> * MTRDeviceControllerStorageClasses(void);
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+MTR_EXTERN
+#else
+MTR_HIDDEN
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
+MTR_NEWLY_AVAILABLE NSSet<Class> * MTRDeviceControllerStorageClasses(void);
 
 NS_ASSUME_NONNULL_END
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED || defined(MTR_INTERNAL_INCLUDE)
diff --git a/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate_Wrapper.h b/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate_Wrapper.h
new file mode 100644
index 0000000..774fff9
--- /dev/null
+++ b/src/darwin/Framework/CHIP/MTRDeviceControllerStorageDelegate_Wrapper.h
@@ -0,0 +1,25 @@
+/**
+ *    Copyright (c) 2023 Project CHIP Authors
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <Matter/MTRDefines.h>
+
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+#error Should be including Matter/MTRDeviceControllerStorageDelegate.h
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
+
+#define MTR_INTERNAL_INCLUDE
+#import <Matter/MTRDeviceControllerStorageDelegate.h>
+#undef MTR_INTERNAL_INCLUDE
diff --git a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
index 6ff5843..2f86a4e 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
+++ b/src/darwin/Framework/CHIP/MTRDeviceController_Internal.h
@@ -32,7 +32,11 @@
 #import "MTRDeviceControllerDataStore.h"
 
 #import <Matter/MTRDeviceControllerStartupParams.h>
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTRDeviceControllerStorageDelegate.h>
+#else
+#import "MTRDeviceControllerStorageDelegate_Wrapper.h"
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
 #import <Matter/MTROTAProviderDelegate.h>
 
 @class MTRDeviceControllerStartupParamsInternal;
@@ -51,6 +55,13 @@
 
 @interface MTRDeviceController ()
 
+#if !MTR_PER_CONTROLLER_STORAGE_ENABLED
+/**
+ * The ID assigned to this controller at creation time.
+ */
+@property (readonly, nonatomic) NSUUID * uniqueIdentifier MTR_NEWLY_AVAILABLE;
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 #pragma mark - MTRDeviceControllerFactory methods
 
 /**
diff --git a/src/darwin/Framework/CHIP/Matter.h b/src/darwin/Framework/CHIP/Matter.h
index 26c74cd..63c067f 100644
--- a/src/darwin/Framework/CHIP/Matter.h
+++ b/src/darwin/Framework/CHIP/Matter.h
@@ -17,6 +17,8 @@
 
 #import <Foundation/Foundation.h>
 
+#define MTR_INCLUDED_FROM_UMBRELLA_HEADER
+
 #import <Matter/MTRAsyncCallbackWorkQueue.h>
 #import <Matter/MTRBackwardsCompatShims.h>
 #import <Matter/MTRBaseClusters.h>
@@ -57,3 +59,5 @@
 #import <Matter/MTRStorage.h>
 #import <Matter/MTRStructsObjc.h>
 #import <Matter/MTRThreadOperationalDataset.h>
+
+#undef MTR_INCLUDED_FROM_UMBRELLA_HEADER
diff --git a/src/darwin/Framework/CHIPTests/MTRControllerAdvertisingTests.m b/src/darwin/Framework/CHIPTests/MTRControllerAdvertisingTests.m
index 1276e0c..549d66e 100644
--- a/src/darwin/Framework/CHIPTests/MTRControllerAdvertisingTests.m
+++ b/src/darwin/Framework/CHIPTests/MTRControllerAdvertisingTests.m
@@ -24,6 +24,8 @@
 #import "MTRTestKeys.h"
 #import "MTRTestPerControllerStorage.h"
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 static const uint16_t kTestVendorId = 0xFFF1u;
 static const uint16_t kTimeoutInSeconds = 3;
 
@@ -295,3 +297,5 @@
 }
 
 @end
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
diff --git a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
index 894eb5f..f81cc23 100644
--- a/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
+++ b/src/darwin/Framework/CHIPTests/MTRPerControllerStorageTests.m
@@ -25,6 +25,8 @@
 #import "MTRTestPerControllerStorage.h"
 #import "MTRTestResetCommissioneeHelper.h"
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 static const uint16_t kPairingTimeoutInSeconds = 10;
 static const uint16_t kTimeoutInSeconds = 3;
 static NSString * kOnboardingPayload = @"MT:-24J0AFN00KA0648G00";
@@ -1055,3 +1057,5 @@
 }
 
 @end
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h
index b3052a9..427d537 100644
--- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h
+++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.h
@@ -17,6 +17,8 @@
 #import <Foundation/Foundation.h>
 #import <Matter/Matter.h>
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 NS_ASSUME_NONNULL_BEGIN
 
 @interface MTRTestPerControllerStorage : NSObject <MTRDeviceControllerStorageDelegate>
@@ -41,3 +43,5 @@
 @end
 
 NS_ASSUME_NONNULL_END
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED
diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m
index 1898bd1..1a453d5 100644
--- a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m
+++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestPerControllerStorage.m
@@ -18,6 +18,8 @@
 
 #import "MTRTestPerControllerStorage.h"
 
+#if MTR_PER_CONTROLLER_STORAGE_ENABLED
+
 @interface MTRTestPerControllerStorage ()
 @property (nonatomic, readonly) NSMutableDictionary<NSString *, NSData *> * storage;
 @end
@@ -83,3 +85,5 @@
 }
 
 @end
+
+#endif // MTR_PER_CONTROLLER_STORAGE_ENABLED