chore: modernize ListValue usage in dict_util.mm and related files (#34661)

* chore: modernize ListValue usage in dict_util.mm and related files

* use base::Value::{Dict,List} instead of raw base::Value

* fix compile

* fix build

* fix build again
This commit is contained in:
Jeremy Rose 2022-06-22 23:28:41 -07:00 committed by GitHub
parent cd19a741b1
commit 11924bdbb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 106 additions and 107 deletions

View file

@ -706,13 +706,13 @@ void App::OnWillFinishLaunching() {
Emit("will-finish-launching");
}
void App::OnFinishLaunching(const base::DictionaryValue& launch_info) {
void App::OnFinishLaunching(base::Value::Dict launch_info) {
#if BUILDFLAG(IS_LINUX)
// Set the application name for audio streams shown in external
// applications. Only affects pulseaudio currently.
media::AudioManager::SetGlobalAppName(Browser::Get()->GetName());
#endif
Emit("ready", launch_info);
Emit("ready", base::Value(std::move(launch_info)));
}
void App::OnPreMainMessageLoopRun() {
@ -756,22 +756,23 @@ void App::OnDidFailToContinueUserActivity(const std::string& type,
void App::OnContinueUserActivity(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info,
const base::DictionaryValue& details) {
if (Emit("continue-activity", type, user_info, details)) {
base::Value::Dict user_info,
base::Value::Dict details) {
if (Emit("continue-activity", type, base::Value(std::move(user_info)),
base::Value(std::move(details)))) {
*prevent_default = true;
}
}
void App::OnUserActivityWasContinued(const std::string& type,
const base::DictionaryValue& user_info) {
Emit("activity-was-continued", type, user_info);
base::Value::Dict user_info) {
Emit("activity-was-continued", type, base::Value(std::move(user_info)));
}
void App::OnUpdateUserActivityState(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info) {
if (Emit("update-activity-state", type, user_info)) {
base::Value::Dict user_info) {
if (Emit("update-activity-state", type, base::Value(std::move(user_info)))) {
*prevent_default = true;
}
}

View file

@ -96,7 +96,7 @@ class App : public ElectronBrowserClient::Delegate,
void OnOpenURL(const std::string& url) override;
void OnActivate(bool has_visible_windows) override;
void OnWillFinishLaunching() override;
void OnFinishLaunching(const base::DictionaryValue& launch_info) override;
void OnFinishLaunching(base::Value::Dict launch_info) override;
void OnAccessibilitySupportChanged() override;
void OnPreMainMessageLoopRun() override;
void OnPreCreateThreads() override;
@ -107,15 +107,13 @@ class App : public ElectronBrowserClient::Delegate,
const std::string& error) override;
void OnContinueUserActivity(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info,
const base::DictionaryValue& details) override;
void OnUserActivityWasContinued(
base::Value::Dict user_info,
base::Value::Dict details) override;
void OnUserActivityWasContinued(const std::string& type,
base::Value::Dict user_info) override;
void OnUpdateUserActivityState(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info) override;
void OnUpdateUserActivityState(
bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info) override;
base::Value::Dict user_info) override;
void OnNewWindowForTab() override;
void OnDidBecomeActive() override;
#endif

View file

@ -67,11 +67,11 @@ class SystemPreferences
void OnSysColorChange() override;
// BrowserObserver:
void OnFinishLaunching(const base::DictionaryValue& launch_info) override;
void OnFinishLaunching(base::Value::Dict launch_info) override;
#elif BUILDFLAG(IS_MAC)
using NotificationCallback = base::RepeatingCallback<
void(const std::string&, base::DictionaryValue, const std::string&)>;
void(const std::string&, base::Value, const std::string&)>;
void PostNotification(const std::string& name,
base::DictionaryValue user_info,

View file

@ -147,9 +147,10 @@ void SystemPreferences::PostNotification(const std::string& name,
NSDistributedNotificationCenter* center =
[NSDistributedNotificationCenter defaultCenter];
[center postNotificationName:base::SysUTF8ToNSString(name)
[center
postNotificationName:base::SysUTF8ToNSString(name)
object:nil
userInfo:DictionaryValueToNSDictionary(user_info)
userInfo:DictionaryValueToNSDictionary(user_info.GetDict())
deliverImmediately:immediate];
}
@ -169,9 +170,10 @@ void SystemPreferences::UnsubscribeNotification(int request_id) {
void SystemPreferences::PostLocalNotification(const std::string& name,
base::DictionaryValue user_info) {
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center postNotificationName:base::SysUTF8ToNSString(name)
[center
postNotificationName:base::SysUTF8ToNSString(name)
object:nil
userInfo:DictionaryValueToNSDictionary(user_info)];
userInfo:DictionaryValueToNSDictionary(user_info.GetDict())];
}
int SystemPreferences::SubscribeLocalNotification(
@ -191,9 +193,10 @@ void SystemPreferences::PostWorkspaceNotification(
base::DictionaryValue user_info) {
NSNotificationCenter* center =
[[NSWorkspace sharedWorkspace] notificationCenter];
[center postNotificationName:base::SysUTF8ToNSString(name)
[center
postNotificationName:base::SysUTF8ToNSString(name)
object:nil
userInfo:DictionaryValueToNSDictionary(user_info)];
userInfo:DictionaryValueToNSDictionary(user_info.GetDict())];
}
int SystemPreferences::SubscribeWorkspaceNotification(
@ -240,7 +243,7 @@ int SystemPreferences::DoSubscribeNotification(
if (notification.userInfo) {
copied_callback.Run(
base::SysNSStringToUTF8(notification.name),
NSDictionaryToDictionaryValue(notification.userInfo),
base::Value(NSDictionaryToValue(notification.userInfo)),
object);
} else {
copied_callback.Run(
@ -282,11 +285,12 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
return gin::ConvertToV8(isolate,
net::GURLWithNSURL([defaults URLForKey:key]));
} else if (type == "array") {
return gin::ConvertToV8(isolate,
NSArrayToListValue([defaults arrayForKey:key]));
return gin::ConvertToV8(
isolate, base::Value(NSArrayToValue([defaults arrayForKey:key])));
} else if (type == "dictionary") {
return gin::ConvertToV8(isolate, NSDictionaryToDictionaryValue(
[defaults dictionaryForKey:key]));
return gin::ConvertToV8(
isolate,
base::Value(NSDictionaryToValue([defaults dictionaryForKey:key])));
} else {
return v8::Undefined(isolate);
}
@ -299,7 +303,7 @@ void SystemPreferences::RegisterDefaults(gin::Arguments* args) {
args->ThrowError();
} else {
@try {
NSDictionary* dict = DictionaryValueToNSDictionary(value);
NSDictionary* dict = DictionaryValueToNSDictionary(value.GetDict());
for (id key in dict) {
id value = [dict objectForKey:key];
if ([value isKindOfClass:[NSNull class]] || value == nil) {
@ -359,17 +363,17 @@ void SystemPreferences::SetUserDefault(const std::string& name,
}
}
} else if (type == "array") {
base::ListValue value;
if (args->GetNext(&value)) {
if (NSArray* array = ListValueToNSArray(value)) {
base::Value value;
if (args->GetNext(&value) && value.is_list()) {
if (NSArray* array = ListValueToNSArray(value.GetList())) {
[defaults setObject:array forKey:key];
return;
}
}
} else if (type == "dictionary") {
base::DictionaryValue value;
if (args->GetNext(&value)) {
if (NSDictionary* dict = DictionaryValueToNSDictionary(value)) {
base::Value value;
if (args->GetNext(&value) && value.is_dict()) {
if (NSDictionary* dict = DictionaryValueToNSDictionary(value.GetDict())) {
[defaults setObject:dict forKey:key];
return;
}

View file

@ -258,8 +258,7 @@ void SystemPreferences::OnSysColorChange() {
Emit("color-changed");
}
void SystemPreferences::OnFinishLaunching(
const base::DictionaryValue& launch_info) {
void SystemPreferences::OnFinishLaunching(base::Value::Dict launch_info) {
color_change_listener_ =
std::make_unique<gfx::ScopedSysColorChangeListener>(this);
}

View file

@ -184,7 +184,7 @@ void Browser::WillFinishLaunching() {
observer.OnWillFinishLaunching();
}
void Browser::DidFinishLaunching(base::DictionaryValue launch_info) {
void Browser::DidFinishLaunching(base::Value::Dict launch_info) {
// Make sure the userData directory is created.
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::FilePath user_data;
@ -196,7 +196,7 @@ void Browser::DidFinishLaunching(base::DictionaryValue launch_info) {
ready_promise_->Resolve();
}
for (BrowserObserver& observer : observers_)
observer.OnFinishLaunching(launch_info);
observer.OnFinishLaunching(launch_info.Clone());
}
v8::Local<v8::Value> Browser::WhenReady(v8::Isolate* isolate) {

10
shell/browser/browser.h Executable file → Normal file
View file

@ -191,16 +191,16 @@ class Browser : public WindowListObserver {
// Resumes an activity via hand-off.
bool ContinueUserActivity(const std::string& type,
base::DictionaryValue user_info,
base::DictionaryValue details);
base::Value::Dict user_info,
base::Value::Dict details);
// Indicates that an activity was continued on another device.
void UserActivityWasContinued(const std::string& type,
base::DictionaryValue user_info);
base::Value::Dict user_info);
// Gives an opportunity to update the Handoff payload.
bool UpdateUserActivityState(const std::string& type,
base::DictionaryValue user_info);
base::Value::Dict user_info);
// Bounce the dock icon.
enum class BounceType{
@ -288,7 +288,7 @@ class Browser : public WindowListObserver {
// Tell the application the loading has been done.
void WillFinishLaunching();
void DidFinishLaunching(base::DictionaryValue launch_info);
void DidFinishLaunching(base::Value::Dict launch_info);
void OnAccessibilitySupportChanged();

View file

@ -242,7 +242,7 @@ void Browser::SetUserActivity(const std::string& type,
[[AtomApplication sharedApplication]
setCurrentActivity:base::SysUTF8ToNSString(type)
withUserInfo:DictionaryValueToNSDictionary(user_info)
withUserInfo:DictionaryValueToNSDictionary(user_info.GetDict())
withWebpageURL:net::NSURLWithGURL(GURL(url_string))];
}
@ -264,7 +264,7 @@ void Browser::UpdateCurrentActivity(const std::string& type,
base::DictionaryValue user_info) {
[[AtomApplication sharedApplication]
updateCurrentActivity:base::SysUTF8ToNSString(type)
withUserInfo:DictionaryValueToNSDictionary(user_info)];
withUserInfo:DictionaryValueToNSDictionary(user_info.GetDict())];
}
bool Browser::WillContinueUserActivity(const std::string& type) {
@ -281,25 +281,27 @@ void Browser::DidFailToContinueUserActivity(const std::string& type,
}
bool Browser::ContinueUserActivity(const std::string& type,
base::DictionaryValue user_info,
base::DictionaryValue details) {
base::Value::Dict user_info,
base::Value::Dict details) {
bool prevent_default = false;
for (BrowserObserver& observer : observers_)
observer.OnContinueUserActivity(&prevent_default, type, user_info, details);
observer.OnContinueUserActivity(&prevent_default, type, user_info.Clone(),
details.Clone());
return prevent_default;
}
void Browser::UserActivityWasContinued(const std::string& type,
base::DictionaryValue user_info) {
base::Value::Dict user_info) {
for (BrowserObserver& observer : observers_)
observer.OnUserActivityWasContinued(type, user_info);
observer.OnUserActivityWasContinued(type, user_info.Clone());
}
bool Browser::UpdateUserActivityState(const std::string& type,
base::DictionaryValue user_info) {
base::Value::Dict user_info) {
bool prevent_default = false;
for (BrowserObserver& observer : observers_)
observer.OnUpdateUserActivityState(&prevent_default, type, user_info);
observer.OnUpdateUserActivityState(&prevent_default, type,
user_info.Clone());
return prevent_default;
}
@ -486,7 +488,8 @@ void Browser::DockSetIcon(v8::Isolate* isolate, v8::Local<v8::Value> icon) {
}
void Browser::ShowAboutPanel() {
NSDictionary* options = DictionaryValueToNSDictionary(about_panel_options_);
NSDictionary* options =
DictionaryValueToNSDictionary(about_panel_options_.GetDict());
// Credits must be a NSAttributedString instead of NSString
NSString* credits = (NSString*)options[@"Credits"];

View file

@ -47,7 +47,7 @@ class BrowserObserver : public base::CheckedObserver {
// The browser has finished loading.
virtual void OnWillFinishLaunching() {}
virtual void OnFinishLaunching(const base::DictionaryValue& launch_info) {}
virtual void OnFinishLaunching(base::Value::Dict launch_info) {}
// The browser's accessibility support has changed.
virtual void OnAccessibilitySupportChanged() {}
@ -70,17 +70,15 @@ class BrowserObserver : public base::CheckedObserver {
// The browser wants to resume a user activity via handoff. (macOS only)
virtual void OnContinueUserActivity(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info,
const base::DictionaryValue& details) {}
base::Value::Dict user_info,
base::Value::Dict details) {}
// The browser wants to notify that an user activity was resumed. (macOS only)
virtual void OnUserActivityWasContinued(
const std::string& type,
const base::DictionaryValue& user_info) {}
virtual void OnUserActivityWasContinued(const std::string& type,
base::Value::Dict user_info) {}
// The browser wants to update an user activity payload. (macOS only)
virtual void OnUpdateUserActivityState(
bool* prevent_default,
virtual void OnUpdateUserActivityState(bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info) {}
base::Value::Dict user_info) {}
// User clicked the native macOS new tab button. (macOS only)
virtual void OnNewWindowForTab() {}

View file

@ -453,7 +453,7 @@ int ElectronBrowserMainParts::PreMainMessageLoopRun() {
#if !BUILDFLAG(IS_MAC)
// The corresponding call in macOS is in ElectronApplicationDelegate.
Browser::Get()->WillFinishLaunching();
Browser::Get()->DidFinishLaunching(base::DictionaryValue());
Browser::Get()->DidFinishLaunching(base::Value::Dict());
#endif
// Notify observers that main thread message loop was initialized.

View file

@ -7,17 +7,14 @@
#import <Foundation/Foundation.h>
namespace base {
class ListValue;
class DictionaryValue;
} // namespace base
#include "base/values.h"
namespace electron {
NSArray* ListValueToNSArray(const base::ListValue& value);
base::ListValue NSArrayToListValue(NSArray* arr);
NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value);
base::DictionaryValue NSDictionaryToDictionaryValue(NSDictionary* dict);
NSArray* ListValueToNSArray(const base::Value::List& value);
base::Value::List NSArrayToValue(NSArray* arr);
NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value);
base::Value::Dict NSDictionaryToValue(NSDictionary* dict);
} // namespace electron

View file

@ -12,9 +12,9 @@
namespace electron {
NSArray* ListValueToNSArray(const base::ListValue& value) {
NSArray* ListValueToNSArray(const base::Value::List& value) {
std::string json;
if (!base::JSONWriter::Write(value, &json))
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json))
return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
@ -25,8 +25,8 @@ NSArray* ListValueToNSArray(const base::ListValue& value) {
return obj;
}
base::ListValue NSArrayToListValue(NSArray* arr) {
base::ListValue result;
base::Value::List NSArrayToValue(NSArray* arr) {
base::Value::List result;
if (!arr)
return result;
@ -44,9 +44,9 @@ base::ListValue NSArrayToListValue(NSArray* arr) {
else
result.Append([value intValue]);
} else if ([value isKindOfClass:[NSArray class]]) {
result.Append(NSArrayToListValue(value));
result.Append(NSArrayToValue(value));
} else if ([value isKindOfClass:[NSDictionary class]]) {
result.Append(NSDictionaryToDictionaryValue(value));
result.Append(NSDictionaryToValue(value));
} else {
result.Append(base::SysNSStringToUTF8([value description]));
}
@ -55,10 +55,9 @@ base::ListValue NSArrayToListValue(NSArray* arr) {
return result;
}
NSDictionary* DictionaryValueToNSDictionary(
const base::DictionaryValue& value) {
NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value) {
std::string json;
if (!base::JSONWriter::Write(value, &json))
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json))
return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
@ -69,8 +68,8 @@ NSDictionary* DictionaryValueToNSDictionary(
return obj;
}
base::DictionaryValue NSDictionaryToDictionaryValue(NSDictionary* dict) {
base::DictionaryValue result;
base::Value::Dict NSDictionaryToValue(NSDictionary* dict) {
base::Value::Dict result;
if (!dict)
return result;
@ -80,23 +79,23 @@ base::DictionaryValue NSDictionaryToDictionaryValue(NSDictionary* dict) {
id value = [dict objectForKey:key];
if ([value isKindOfClass:[NSString class]]) {
result.SetKey(str_key, base::Value(base::SysNSStringToUTF8(value)));
result.Set(str_key, base::Value(base::SysNSStringToUTF8(value)));
} 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)
result.SetKey(str_key, base::Value([value boolValue]));
result.Set(str_key, base::Value([value boolValue]));
else if (strcmp(objc_type, @encode(double)) == 0 ||
strcmp(objc_type, @encode(float)) == 0)
result.SetKey(str_key, base::Value([value doubleValue]));
result.Set(str_key, base::Value([value doubleValue]));
else
result.SetKey(str_key, base::Value([value intValue]));
result.Set(str_key, base::Value([value intValue]));
} else if ([value isKindOfClass:[NSArray class]]) {
result.SetKey(str_key, NSArrayToListValue(value));
result.Set(str_key, NSArrayToValue(value));
} else if ([value isKindOfClass:[NSDictionary class]]) {
result.SetKey(str_key, NSDictionaryToDictionaryValue(value));
result.Set(str_key, NSDictionaryToValue(value));
} else {
result.SetKey(str_key,
result.Set(str_key,
base::Value(base::SysNSStringToUTF8([value description])));
}
}

View file

@ -120,8 +120,8 @@ inline void dispatch_sync_main(dispatch_block_t block) {
dispatch_sync_main(^{
std::string activity_type(
base::SysNSStringToUTF8(userActivity.activityType));
base::DictionaryValue user_info =
electron::NSDictionaryToDictionaryValue(userActivity.userInfo);
base::Value::Dict user_info =
electron::NSDictionaryToValue(userActivity.userInfo);
electron::Browser* browser = electron::Browser::Get();
shouldWait =
@ -149,8 +149,8 @@ inline void dispatch_sync_main(dispatch_block_t block) {
dispatch_async(dispatch_get_main_queue(), ^{
std::string activity_type(
base::SysNSStringToUTF8(userActivity.activityType));
base::DictionaryValue user_info =
electron::NSDictionaryToDictionaryValue(userActivity.userInfo);
base::Value::Dict user_info =
electron::NSDictionaryToValue(userActivity.userInfo);
electron::Browser* browser = electron::Browser::Get();
browser->UserActivityWasContinued(activity_type, std::move(user_info));

View file

@ -84,7 +84,7 @@ static NSDictionary* UNNotificationResponseToNSDictionary(
}
electron::Browser::Get()->DidFinishLaunching(
electron::NSDictionaryToDictionaryValue(notification_info));
electron::NSDictionaryToValue(notification_info));
}
- (void)applicationDidBecomeActive:(NSNotification*)notification {
@ -128,8 +128,8 @@ static NSDictionary* UNNotificationResponseToNSDictionary(
electron::Browser* browser = electron::Browser::Get();
return browser->ContinueUserActivity(
activity_type,
electron::NSDictionaryToDictionaryValue(userActivity.userInfo),
electron::NSDictionaryToDictionaryValue(details))
electron::NSDictionaryToValue(userActivity.userInfo),
electron::NSDictionaryToValue(details))
? YES
: NO;
}