chromium - get value from base:Value
发布日期:2021-06-30 22:19:28 浏览次数:2 分类:技术文章

本文共 3790 字,大约阅读时间需要 12 分钟。

前言

chromium工程中,base::Value是一个数据的容器, 可以装double, string类型数据,数据的存取为key-value。

在chromium工程中, 对base:Value设置值,是原有框架的事情。接口上经常有base::Value的值作为入参。
为了解送进接口的base::Value数据是啥内容(一般会记录日志),对base::Value取值是要做的。

实验

找了一段现有的chromium代码,入参有base::Value, 尝试读取base::Value入参的内容。

Z:\chromium\src\chrome\browser\ui\webui\download_internals\download_internals_ui_message_handler.cc

bool DownloadInternalsUIMessageHandler::get_double_value_by_key_from_value(const base::Value& value, const char* psz_key, double& f_value){	bool b_rc = false;	const base::Value* p_bv_find_key = NULL;	do {		f_value = 0.0f;		if (NULL == psz_key) {			break;		}		p_bv_find_key = value.FindKey(psz_key);		if (NULL == p_bv_find_key) {			break;		}		if (!p_bv_find_key->is_double()) {			break;		}		f_value = p_bv_find_key->GetDouble();		b_rc = true;	} while (0);	return b_rc;}bool DownloadInternalsUIMessageHandler::get_string_value_by_key_from_value(const base::Value& value, const char* psz_key, std::string& str_value){	bool b_rc = false;	const base::Value* p_bv_find_key = NULL;	do {		str_value = "";		if (NULL == psz_key) {			break;		}		p_bv_find_key = value.FindKey(psz_key);		if (NULL == p_bv_find_key) {			break;		}		if (!p_bv_find_key->is_string()) {			break;		}		str_value = p_bv_find_key->GetString();		b_rc = true;	} while (0);	return b_rc;}void DownloadInternalsUIMessageHandler::OnServiceDownloadChanged(    const base::Value& service_download) {	double f_bytes_downloaded = 0.0f;	std::string str_client = "";	std::string str_state = "";	std::string str_file_path = "";	std::string str_guid = "";	std::string str_url = "";  if (!IsJavascriptAllowed())    return;  FireWebUIListener("service-download-changed", service_download);	/*+		[0]	("bytes_downloaded", unique_ptr DOUBLE 0.00000000000000000)	std::pair
,std::allocator
>,std::unique_ptr
> >+ [1] ("client", unique_ptr STRING "Debugging") std::pair
,std::allocator
>,std::unique_ptr
> >+ [2] ("file_path", unique_ptr STRING "C:\\Users\\LostSpeed\\AppData\\Local\\Chromium\\User Data\\Default\\Download Service\\Files\\205d0b7a-b28e-4594-943c-ec8e5180eda6") std::pair
,std::allocator
>,std::unique_ptr
> >+ [3] ("guid", unique_ptr STRING "205d0b7a-b28e-4594-943c-ec8e5180eda6") std::pair
,std::allocator
>,std::unique_ptr
> >+ [4] ("state", unique_ptr STRING "ACTIVE") std::pair
,std::allocator
>,std::unique_ptr
> >+ [5] ("url", unique_ptr STRING "https://download.csdn.net/download/lostspeed/10834374") std::pair
,std::allocator
>,std::unique_ptr
> > */ do { if (!get_double_value_by_key_from_value(service_download, "bytes_downloaded", f_bytes_downloaded)) { break; } if (!get_string_value_by_key_from_value(service_download, "client", str_client)) { break; } if (!get_string_value_by_key_from_value(service_download, "file_path", str_file_path)) { break; } if (!get_string_value_by_key_from_value(service_download, "guid", str_guid)) { break; } if (!get_string_value_by_key_from_value(service_download, "state", str_state)) { break; } if (!get_string_value_by_key_from_value(service_download, "url", str_url)) { break; } // COMPLETE DLOG(INFO) << "service_download info :" << "bytes_downloaded = " << f_bytes_downloaded << ", client = " << str_client << ", file_path = " << str_file_path << ", guid = " << str_guid << ", down_state = " << str_state << ", url = " << str_url; } while (0);}

记录的日志效果

[223200:240192:1218/130225.050:INFO:download_internals_ui_message_handler.cc(172)] service_download info :bytes_downloaded = 0, client = Debugging, file_path = C:\Users\LostSpeed\AppData\Local\Chromium\User Data\Default\Download Service\Files\b345452c-992c-47db-83ad-43606b7632ac, guid = b345452c-992c-47db-83ad-43606b7632ac, down_state = ACTIVE, url = https://download.csdn.net/download/lostspeed/10834374

转载地址:https://lostspeed.blog.csdn.net/article/details/85063350 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:chromium - using special class
下一篇:chromium - base::Bind take parameter

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月22日 03时27分45秒