forked from project-chip/connectedhomeip
-
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.
[Darwin] MTRDevice attribute cache persistent storage local test faci…
…lity
- Loading branch information
1 parent
d4a89c9
commit 664cd44
Showing
8 changed files
with
293 additions
and
44 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
33 changes: 33 additions & 0 deletions
33
src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.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,33 @@ | ||
// | ||
/** | ||
* 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. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <Matter/Matter.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
MTR_EXTERN @interface MTRDeviceControllerLocalTestStorage : NSObject<MTRDeviceControllerStorageDelegate> | ||
|
||
// Setting this variable only affects subsequent MTRDeviceController initializations | ||
@property (class, nonatomic, assign) BOOL localTestStorageEnabled; | ||
|
||
// This storage persists items to NSUserDefaults for MTRStorageSharingTypeNotShared data. Items with other sharing types will be droppped, or stored/fetched with the "passthrough storage" if one is specified. | ||
- (instancetype)initWithPassThroughStorage:(id<MTRDeviceControllerStorageDelegate> _Nullable)passThroughStorage; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
93 changes: 93 additions & 0 deletions
93
src/darwin/Framework/CHIP/MTRDeviceControllerLocalTestStorage.m
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,93 @@ | ||
// | ||
/** | ||
* 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. | ||
*/ | ||
|
||
#import "MTRDeviceControllerLocalTestStorage.h" | ||
|
||
static NSString * const kLocalTestUserDefaultDomain = @"org.csa-iot.matter.darwintest"; | ||
static NSString * const kLocalTestUserDefaultEnabledKey = @"enableTestStorage"; | ||
|
||
@implementation MTRDeviceControllerLocalTestStorage { | ||
id<MTRDeviceControllerStorageDelegate> _passThroughStorage; | ||
} | ||
|
||
+ (BOOL)localTestStorageEnabled | ||
{ | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
return [defaults boolForKey:kLocalTestUserDefaultEnabledKey]; | ||
} | ||
|
||
+ (void)setLocalTestStorageEnabled:(BOOL)localTestStorageEnabled | ||
{ | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults setBool:localTestStorageEnabled forKey:kLocalTestUserDefaultEnabledKey]; | ||
} | ||
|
||
- (instancetype)initWithPassThroughStorage:(id<MTRDeviceControllerStorageDelegate>)passThroughStorage | ||
{ | ||
if (self = [super init]) { | ||
_passThroughStorage = passThroughStorage; | ||
} | ||
return self; | ||
} | ||
|
||
- (nullable id<NSSecureCoding>)controller:(MTRDeviceController *)controller | ||
valueForKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
NSData * storedData = [defaults dataForKey:key]; | ||
NSError * error; | ||
id value = [NSKeyedUnarchiver unarchivedObjectOfClasses:MTRDeviceControllerStorageClasses() fromData:storedData error:&error]; | ||
return value; | ||
} else { | ||
return [_passThroughStorage controller:controller valueForKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
|
||
- (BOOL)controller:(MTRDeviceController *)controller | ||
storeValue:(id<NSSecureCoding>)value | ||
forKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSError * error; | ||
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:value requiringSecureCoding:YES error:&error]; | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults setObject:data forKey:key]; | ||
return YES; | ||
} else { | ||
return [_passThroughStorage controller:controller storeValue:value forKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
|
||
- (BOOL)controller:(MTRDeviceController *)controller | ||
removeValueForKey:(NSString *)key | ||
securityLevel:(MTRStorageSecurityLevel)securityLevel | ||
sharingType:(MTRStorageSharingType)sharingType | ||
{ | ||
if (sharingType == MTRStorageSharingTypeNotShared) { | ||
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:kLocalTestUserDefaultDomain]; | ||
[defaults removeObjectForKey:key]; | ||
return YES; | ||
} else { | ||
return [_passThroughStorage controller:controller removeValueForKey:key securityLevel:securityLevel sharingType:sharingType]; | ||
} | ||
} | ||
@end |
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
Oops, something went wrong.