2016-05-05 07:26:44 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/mac/dict_util.h"
|
2016-05-05 07:26:44 +00:00
|
|
|
|
2019-05-02 12:05:37 +00:00
|
|
|
#include <string>
|
|
|
|
|
2016-05-05 07:26:44 +00:00
|
|
|
#include "base/json/json_writer.h"
|
2016-05-18 06:49:33 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2016-05-05 07:26:44 +00:00
|
|
|
#include "base/values.h"
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2022-06-23 06:28:41 +00:00
|
|
|
NSArray* ListValueToNSArray(const base::Value::List& value) {
|
2024-02-02 18:25:58 +00:00
|
|
|
const auto json = base::WriteJson(value);
|
|
|
|
if (!json.has_value())
|
2016-11-28 23:08:22 +00:00
|
|
|
return nil;
|
2024-02-02 18:25:58 +00:00
|
|
|
NSData* jsonData = [NSData dataWithBytes:json->data() length:json->size()];
|
2019-05-01 19:27:55 +00:00
|
|
|
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
|
|
options:0
|
|
|
|
error:nil];
|
2016-11-28 23:08:22 +00:00
|
|
|
if (![obj isKindOfClass:[NSArray class]])
|
|
|
|
return nil;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:28:41 +00:00
|
|
|
base::Value::List NSArrayToValue(NSArray* arr) {
|
|
|
|
base::Value::List result;
|
2016-05-18 06:49:33 +00:00
|
|
|
if (!arr)
|
2019-10-30 05:30:59 +00:00
|
|
|
return result;
|
2016-05-18 06:49:33 +00:00
|
|
|
|
|
|
|
for (id value in arr) {
|
|
|
|
if ([value isKindOfClass:[NSString class]]) {
|
2021-10-06 02:21:00 +00:00
|
|
|
result.Append(base::SysNSStringToUTF8(value));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
const char* objc_type = [value objCType];
|
|
|
|
if (strcmp(objc_type, @encode(BOOL)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(char)) == 0)
|
2021-10-06 02:21:00 +00:00
|
|
|
result.Append([value boolValue]);
|
2016-05-18 06:49:33 +00:00
|
|
|
else if (strcmp(objc_type, @encode(double)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(float)) == 0)
|
2021-05-19 23:15:47 +00:00
|
|
|
result.Append([value doubleValue]);
|
2016-05-18 06:49:33 +00:00
|
|
|
else
|
2021-10-06 02:21:00 +00:00
|
|
|
result.Append([value intValue]);
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Append(NSArrayToValue(value));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Append(NSDictionaryToValue(value));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else {
|
2021-10-06 02:21:00 +00:00
|
|
|
result.Append(base::SysNSStringToUTF8([value description]));
|
2016-05-18 06:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:28:41 +00:00
|
|
|
NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value) {
|
2024-02-02 18:25:58 +00:00
|
|
|
const auto json = base::WriteJson(value);
|
|
|
|
if (!json.has_value())
|
2016-05-05 07:26:44 +00:00
|
|
|
return nil;
|
2024-02-02 18:25:58 +00:00
|
|
|
NSData* jsonData = [NSData dataWithBytes:json->data() length:json->size()];
|
2019-05-01 19:27:55 +00:00
|
|
|
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
|
|
options:0
|
|
|
|
error:nil];
|
2016-05-05 07:26:44 +00:00
|
|
|
if (![obj isKindOfClass:[NSDictionary class]])
|
|
|
|
return nil;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:28:41 +00:00
|
|
|
base::Value::Dict NSDictionaryToValue(NSDictionary* dict) {
|
|
|
|
base::Value::Dict result;
|
2016-05-18 06:49:33 +00:00
|
|
|
if (!dict)
|
2019-10-30 05:30:59 +00:00
|
|
|
return result;
|
2016-05-18 05:40:19 +00:00
|
|
|
|
2016-05-18 06:49:33 +00:00
|
|
|
for (id key in dict) {
|
2016-05-18 07:01:08 +00:00
|
|
|
std::string str_key = base::SysNSStringToUTF8(
|
|
|
|
[key isKindOfClass:[NSString class]] ? key : [key description]);
|
2016-05-18 06:49:33 +00:00
|
|
|
|
|
|
|
id value = [dict objectForKey:key];
|
|
|
|
if ([value isKindOfClass:[NSString class]]) {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, base::Value(base::SysNSStringToUTF8(value)));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
const char* objc_type = [value objCType];
|
|
|
|
if (strcmp(objc_type, @encode(BOOL)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(char)) == 0)
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, base::Value([value boolValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
else if (strcmp(objc_type, @encode(double)) == 0 ||
|
|
|
|
strcmp(objc_type, @encode(float)) == 0)
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, base::Value([value doubleValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
else
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, base::Value([value intValue]));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSArray class]]) {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, NSArrayToValue(value));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else if ([value isKindOfClass:[NSDictionary class]]) {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key, NSDictionaryToValue(value));
|
2016-05-18 06:49:33 +00:00
|
|
|
} else {
|
2022-06-23 06:28:41 +00:00
|
|
|
result.Set(str_key,
|
|
|
|
base::Value(base::SysNSStringToUTF8([value description])));
|
2016-05-18 06:49:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 07:26:44 +00:00
|
|
|
|
2016-05-18 06:49:33 +00:00
|
|
|
return result;
|
2016-05-05 07:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|