前言

chromium工程中, ::Value是一个数据的容器, 可以装double, string类型数据,数据的存取为key-value。
在chromium工程中, 对 :Value设置值,是原有框架的事情。接口上经常有 ::Value的值作为入参。
为了解送进接口的 ::Value数据是啥内容(一般会记录日志),对 ::Value取值是要做的。

实验

找了一段现有的chromium代码,入参有 ::Value, 尝试读取 ::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  ::Value& value, const char* psz_key, double& f_value)
{
	bool b_rc = false;
	const  ::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  ::Value& value, const char* psz_key, std::string& str_value)
{
	bool b_rc = false;
	const  ::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  ::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 (!Is Allowed())
    return;

  FireWebUIListener(\"service-download-changed\", service_download);

	/*
+		[0]	(\"bytes_downloaded\", unique_ptr DOUBLE 0.00000000000000000)	std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
+		[1]	(\"client\", unique_ptr STRING \"Debugging\")	std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
+		[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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
+		[3]	(\"guid\", unique_ptr STRING \"205d0b7a-b28e-4594-943c-ec8e5180eda6\")	std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
+		[4]	(\"state\", unique_ptr STRING \"ACTIVE\")	std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
+		[5]	(\"url\", unique_ptr STRING \"https://download.csdn.net/download/lostspeed/10834374\")	std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::unique_ptr< ::Value,std::default_delete< ::Value> > >
	*/

	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
收藏 打印