chore: modernize some devtools code using deprecated ListValue (#34655)

This commit is contained in:
Jeremy Rose 2022-06-29 09:39:48 -07:00 committed by GitHub
parent 7c12baccab
commit 4ddd03b1b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 100 additions and 112 deletions

View file

@ -547,14 +547,13 @@ FileSystem CreateFileSystemStruct(content::WebContents* web_contents,
return FileSystem(type, file_system_name, root_url, file_system_path); return FileSystem(type, file_system_name, root_url, file_system_path);
} }
std::unique_ptr<base::DictionaryValue> CreateFileSystemValue( base::Value::Dict CreateFileSystemValue(const FileSystem& file_system) {
const FileSystem& file_system) { base::Value::Dict value;
auto file_system_value = std::make_unique<base::DictionaryValue>(); value.Set("type", file_system.type);
file_system_value->SetString("type", file_system.type); value.Set("fileSystemName", file_system.file_system_name);
file_system_value->SetString("fileSystemName", file_system.file_system_name); value.Set("rootURL", file_system.root_url);
file_system_value->SetString("rootURL", file_system.root_url); value.Set("fileSystemPath", file_system.file_system_path);
file_system_value->SetString("fileSystemPath", file_system.file_system_path); return value;
return file_system_value;
} }
void WriteToFile(const base::FilePath& path, const std::string& content) { void WriteToFile(const base::FilePath& path, const std::string& content) {
@ -1986,9 +1985,8 @@ void WebContents::DevToolsOpened() {
devtools_web_contents_.Reset(isolate, handle.ToV8()); devtools_web_contents_.Reset(isolate, handle.ToV8());
// Set inspected tabID. // Set inspected tabID.
base::Value tab_id(ID()); inspectable_web_contents_->CallClientFunction(
inspectable_web_contents_->CallClientFunction("DevToolsAPI.setInspectedTabId", "DevToolsAPI", "setInspectedTabId", base::Value(ID()));
&tab_id, nullptr, nullptr);
// Inherit owner window in devtools when it doesn't have one. // Inherit owner window in devtools when it doesn't have one.
auto* devtools = inspectable_web_contents_->GetDevToolsWebContents(); auto* devtools = inspectable_web_contents_->GetDevToolsWebContents();
@ -3595,19 +3593,17 @@ void WebContents::DevToolsSaveToFile(const std::string& url,
settings.title = url; settings.title = url;
settings.default_path = base::FilePath::FromUTF8Unsafe(url); settings.default_path = base::FilePath::FromUTF8Unsafe(url);
if (!file_dialog::ShowSaveDialogSync(settings, &path)) { if (!file_dialog::ShowSaveDialogSync(settings, &path)) {
base::Value url_value(url);
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.canceledSaveURL", &url_value, nullptr, nullptr); "DevToolsAPI", "canceledSaveURL", base::Value(url));
return; return;
} }
} }
saved_files_[url] = path; saved_files_[url] = path;
// Notify DevTools. // Notify DevTools.
base::Value url_value(url);
base::Value file_system_path_value(path.AsUTF8Unsafe());
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.savedURL", &url_value, &file_system_path_value, nullptr); "DevToolsAPI", "savedURL", base::Value(url),
base::Value(path.AsUTF8Unsafe()));
file_task_runner_->PostTask(FROM_HERE, file_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&WriteToFile, path, content)); base::BindOnce(&WriteToFile, path, content));
} }
@ -3619,9 +3615,8 @@ void WebContents::DevToolsAppendToFile(const std::string& url,
return; return;
// Notify DevTools. // Notify DevTools.
base::Value url_value(url); inspectable_web_contents_->CallClientFunction("DevToolsAPI", "appendedToURL",
inspectable_web_contents_->CallClientFunction("DevToolsAPI.appendedToURL", base::Value(url));
&url_value, nullptr, nullptr);
file_task_runner_->PostTask( file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&AppendToFile, it->second, content)); FROM_HERE, base::BindOnce(&AppendToFile, it->second, content));
} }
@ -3629,10 +3624,8 @@ void WebContents::DevToolsAppendToFile(const std::string& url,
void WebContents::DevToolsRequestFileSystems() { void WebContents::DevToolsRequestFileSystems() {
auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents()); auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents());
if (file_system_paths.empty()) { if (file_system_paths.empty()) {
base::ListValue empty_file_system_value;
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.fileSystemsLoaded", &empty_file_system_value, nullptr, "DevToolsAPI", "fileSystemsLoaded", base::Value(base::Value::List()));
nullptr);
return; return;
} }
@ -3648,12 +3641,12 @@ void WebContents::DevToolsRequestFileSystems() {
file_systems.push_back(file_system); file_systems.push_back(file_system);
} }
base::ListValue file_system_value; base::Value::List file_system_value;
for (const auto& file_system : file_systems) for (const auto& file_system : file_systems)
file_system_value.Append( file_system_value.Append(CreateFileSystemValue(file_system));
base::Value::FromUniquePtrValue(CreateFileSystemValue(file_system)));
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.fileSystemsLoaded", &file_system_value, nullptr, nullptr); "DevToolsAPI", "fileSystemsLoaded",
base::Value(std::move(file_system_value)));
} }
void WebContents::DevToolsAddFileSystem( void WebContents::DevToolsAddFileSystem(
@ -3679,14 +3672,15 @@ void WebContents::DevToolsAddFileSystem(
FileSystem file_system = CreateFileSystemStruct( FileSystem file_system = CreateFileSystemStruct(
GetDevToolsWebContents(), file_system_id, path.AsUTF8Unsafe(), type); GetDevToolsWebContents(), file_system_id, path.AsUTF8Unsafe(), type);
std::unique_ptr<base::DictionaryValue> file_system_value( base::Value::Dict file_system_value = CreateFileSystemValue(file_system);
CreateFileSystemValue(file_system));
auto* pref_service = GetPrefService(GetDevToolsWebContents()); auto* pref_service = GetPrefService(GetDevToolsWebContents());
DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths); DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths);
update.Get()->SetKey(path.AsUTF8Unsafe(), base::Value(type)); update.Get()->SetKey(path.AsUTF8Unsafe(), base::Value(type));
std::string error = ""; // No error
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.fileSystemAdded", nullptr, file_system_value.get(), nullptr); "DevToolsAPI", "fileSystemAdded", base::Value(error),
base::Value(std::move(file_system_value)));
} }
void WebContents::DevToolsRemoveFileSystem( void WebContents::DevToolsRemoveFileSystem(
@ -3702,10 +3696,8 @@ void WebContents::DevToolsRemoveFileSystem(
DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths); DictionaryPrefUpdate update(pref_service, prefs::kDevToolsFileSystemPaths);
update.Get()->RemoveKey(path); update.Get()->RemoveKey(path);
base::Value file_system_path_value(path); inspectable_web_contents_->CallClientFunction(
inspectable_web_contents_->CallClientFunction("DevToolsAPI.fileSystemRemoved", "DevToolsAPI", "fileSystemRemoved", base::Value(path));
&file_system_path_value,
nullptr, nullptr);
} }
void WebContents::DevToolsIndexPath( void WebContents::DevToolsIndexPath(
@ -3781,13 +3773,13 @@ void WebContents::DevToolsSetEyeDropperActive(bool active) {
} }
void WebContents::ColorPickedInEyeDropper(int r, int g, int b, int a) { void WebContents::ColorPickedInEyeDropper(int r, int g, int b, int a) {
base::DictionaryValue color; base::Value::Dict color;
color.SetInteger("r", r); color.Set("r", r);
color.SetInteger("g", g); color.Set("g", g);
color.SetInteger("b", b); color.Set("b", b);
color.SetInteger("a", a); color.Set("a", a);
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.eyeDropperPickedColor", &color, nullptr, nullptr); "DevToolsAPI", "eyeDropperPickedColor", base::Value(std::move(color)));
} }
#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC) #if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
@ -3808,48 +3800,37 @@ void WebContents::OnDevToolsIndexingWorkCalculated(
int request_id, int request_id,
const std::string& file_system_path, const std::string& file_system_path,
int total_work) { int total_work) {
base::Value request_id_value(request_id);
base::Value file_system_path_value(file_system_path);
base::Value total_work_value(total_work);
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.indexingTotalWorkCalculated", &request_id_value, "DevToolsAPI", "indexingTotalWorkCalculated", base::Value(request_id),
&file_system_path_value, &total_work_value); base::Value(file_system_path), base::Value(total_work));
} }
void WebContents::OnDevToolsIndexingWorked(int request_id, void WebContents::OnDevToolsIndexingWorked(int request_id,
const std::string& file_system_path, const std::string& file_system_path,
int worked) { int worked) {
base::Value request_id_value(request_id);
base::Value file_system_path_value(file_system_path);
base::Value worked_value(worked);
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.indexingWorked", &request_id_value, &file_system_path_value, "DevToolsAPI", "indexingWorked", base::Value(request_id),
&worked_value); base::Value(file_system_path), base::Value(worked));
} }
void WebContents::OnDevToolsIndexingDone(int request_id, void WebContents::OnDevToolsIndexingDone(int request_id,
const std::string& file_system_path) { const std::string& file_system_path) {
devtools_indexing_jobs_.erase(request_id); devtools_indexing_jobs_.erase(request_id);
base::Value request_id_value(request_id); inspectable_web_contents_->CallClientFunction("DevToolsAPI", "indexingDone",
base::Value file_system_path_value(file_system_path); base::Value(request_id),
inspectable_web_contents_->CallClientFunction( base::Value(file_system_path));
"DevToolsAPI.indexingDone", &request_id_value, &file_system_path_value,
nullptr);
} }
void WebContents::OnDevToolsSearchCompleted( void WebContents::OnDevToolsSearchCompleted(
int request_id, int request_id,
const std::string& file_system_path, const std::string& file_system_path,
const std::vector<std::string>& file_paths) { const std::vector<std::string>& file_paths) {
base::ListValue file_paths_value; base::Value::List file_paths_value;
for (const auto& file_path : file_paths) { for (const auto& file_path : file_paths)
file_paths_value.Append(file_path); file_paths_value.Append(file_path);
}
base::Value request_id_value(request_id);
base::Value file_system_path_value(file_system_path);
inspectable_web_contents_->CallClientFunction( inspectable_web_contents_->CallClientFunction(
"DevToolsAPI.searchCompleted", &request_id_value, &file_system_path_value, "DevToolsAPI", "searchCompleted", base::Value(request_id),
&file_paths_value); base::Value(file_system_path), base::Value(std::move(file_paths_value)));
} }
void WebContents::SetHtmlApiFullscreen(bool enter_fullscreen) { void WebContents::SetHtmlApiFullscreen(bool enter_fullscreen) {

View file

@ -271,8 +271,9 @@ class InspectableWebContents::NetworkResourceLoader
base::Value id(stream_id_); base::Value id(stream_id_);
base::Value encodedValue(encoded); base::Value encodedValue(encoded);
bindings_->CallClientFunction("DevToolsAPI.streamWrite", &id, &chunkValue, bindings_->CallClientFunction("DevToolsAPI", "streamWrite", std::move(id),
&encodedValue); std::move(chunkValue),
std::move(encodedValue));
std::move(resume).Run(); std::move(resume).Run();
} }
@ -510,30 +511,29 @@ void InspectableWebContents::Reattach(DispatchCallback callback) {
} }
void InspectableWebContents::CallClientFunction( void InspectableWebContents::CallClientFunction(
const std::string& function_name, const std::string& object_name,
const base::Value* arg1, const std::string& method_name,
const base::Value* arg2, base::Value arg1,
const base::Value* arg3) { base::Value arg2,
base::Value arg3,
base::OnceCallback<void(base::Value)> cb) {
if (!GetDevToolsWebContents()) if (!GetDevToolsWebContents())
return; return;
std::string javascript = function_name + "("; base::Value::List arguments;
if (arg1) { if (!arg1.is_none()) {
std::string json; arguments.Append(std::move(arg1));
base::JSONWriter::Write(*arg1, &json); if (!arg2.is_none()) {
javascript.append(json); arguments.Append(std::move(arg2));
if (arg2) { if (!arg3.is_none()) {
base::JSONWriter::Write(*arg2, &json); arguments.Append(std::move(arg3));
javascript.append(", ").append(json);
if (arg3) {
base::JSONWriter::Write(*arg3, &json);
javascript.append(", ").append(json);
} }
} }
} }
javascript.append(");");
GetDevToolsWebContents()->GetPrimaryMainFrame()->ExecuteJavaScript( GetDevToolsWebContents()->GetPrimaryMainFrame()->ExecuteJavaScriptMethod(
base::UTF8ToUTF16(javascript), base::NullCallback()); base::ASCIIToUTF16(object_name), base::ASCIIToUTF16(method_name),
std::move(arguments), std::move(cb));
} }
gfx::Rect InspectableWebContents::GetDevToolsBounds() const { gfx::Rect InspectableWebContents::GetDevToolsBounds() const {
@ -601,7 +601,7 @@ void InspectableWebContents::AddDevToolsExtensionsToClient() {
if (!registry) if (!registry)
return; return;
base::ListValue results; base::Value::List results;
for (auto& extension : registry->enabled_extensions()) { for (auto& extension : registry->enabled_extensions()) {
auto devtools_page_url = auto devtools_page_url =
extensions::chrome_manifest_urls::GetDevToolsPage(extension.get()); extensions::chrome_manifest_urls::GetDevToolsPage(extension.get());
@ -615,17 +615,17 @@ void InspectableWebContents::AddDevToolsExtensionsToClient() {
web_contents_->GetPrimaryMainFrame()->GetProcess()->GetID(), web_contents_->GetPrimaryMainFrame()->GetProcess()->GetID(),
url::Origin::Create(extension->url())); url::Origin::Create(extension->url()));
auto extension_info = std::make_unique<base::DictionaryValue>(); base::Value::Dict extension_info;
extension_info->SetString("startPage", devtools_page_url.spec()); extension_info.Set("startPage", devtools_page_url.spec());
extension_info->SetString("name", extension->name()); extension_info.Set("name", extension->name());
extension_info->SetBoolean( extension_info.Set("exposeExperimentalAPIs",
"exposeExperimentalAPIs", extension->permissions_data()->HasAPIPermission(
extension->permissions_data()->HasAPIPermission( extensions::mojom::APIPermissionID::kExperimental));
extensions::mojom::APIPermissionID::kExperimental)); results.Append(base::Value(std::move(extension_info)));
results.Append(base::Value::FromUniquePtrValue(std::move(extension_info)));
} }
CallClientFunction("DevToolsAPI.addExtensions", &results, NULL, NULL); CallClientFunction("DevToolsAPI", "addExtensions",
base::Value(std::move(results)));
} }
#endif #endif
@ -943,22 +943,21 @@ void InspectableWebContents::DispatchProtocolMessage(
base::StringPiece str_message(reinterpret_cast<const char*>(message.data()), base::StringPiece str_message(reinterpret_cast<const char*>(message.data()),
message.size()); message.size());
if (str_message.size() < kMaxMessageChunkSize) { if (str_message.length() < kMaxMessageChunkSize) {
std::string param; CallClientFunction("DevToolsAPI", "dispatchMessage",
base::EscapeJSONString(str_message, true, &param); base::Value(std::string(str_message)));
std::u16string javascript = } else {
base::UTF8ToUTF16("DevToolsAPI.dispatchMessage(" + param + ");"); size_t total_size = str_message.length();
GetDevToolsWebContents()->GetPrimaryMainFrame()->ExecuteJavaScript( for (size_t pos = 0; pos < str_message.length();
javascript, base::NullCallback()); pos += kMaxMessageChunkSize) {
return; base::StringPiece str_message_chunk =
} str_message.substr(pos, kMaxMessageChunkSize);
base::Value total_size(static_cast<int>(str_message.length())); CallClientFunction(
for (size_t pos = 0; pos < str_message.length(); "DevToolsAPI", "dispatchMessageChunk",
pos += kMaxMessageChunkSize) { base::Value(std::string(str_message_chunk)),
base::Value message_value(str_message.substr(pos, kMaxMessageChunkSize)); base::Value(base::NumberToString(pos ? 0 : total_size)));
CallClientFunction("DevToolsAPI.dispatchMessageChunk", &message_value, }
pos ? nullptr : &total_size, nullptr);
} }
} }
@ -1070,8 +1069,13 @@ void InspectableWebContents::DidFinishNavigation(
void InspectableWebContents::SendMessageAck(int request_id, void InspectableWebContents::SendMessageAck(int request_id,
const base::Value* arg) { const base::Value* arg) {
base::Value id_value(request_id); if (arg) {
CallClientFunction("DevToolsAPI.embedderMessageAck", &id_value, arg, nullptr); CallClientFunction("DevToolsAPI", "embedderMessageAck",
base::Value(request_id), arg->Clone());
} else {
CallClientFunction("DevToolsAPI", "embedderMessageAck",
base::Value(request_id));
}
} }
} // namespace electron } // namespace electron

View file

@ -70,10 +70,13 @@ class InspectableWebContents
bool IsDevToolsViewShowing(); bool IsDevToolsViewShowing();
void AttachTo(scoped_refptr<content::DevToolsAgentHost>); void AttachTo(scoped_refptr<content::DevToolsAgentHost>);
void Detach(); void Detach();
void CallClientFunction(const std::string& function_name, void CallClientFunction(
const base::Value* arg1, const std::string& object_name,
const base::Value* arg2, const std::string& method_name,
const base::Value* arg3); const base::Value arg1 = {},
const base::Value arg2 = {},
const base::Value arg3 = {},
base::OnceCallback<void(base::Value)> cb = base::NullCallback());
void InspectElement(int x, int y); void InspectElement(int x, int y);
// Return the last position and size of devtools window. // Return the last position and size of devtools window.