chromium - post task for web-ui
发布日期:2021-06-30 22:19:30
浏览次数:2
分类:技术文章
本文共 3178 字,大约阅读时间需要 10 分钟。
前言
web-ui和js交互,为了避免阻塞web-ui的消息循环,不能直接PostTask或直接bind来干活,需要用和UI相关的BrowserThread::PostTask.
通过实验可知,即使用BrowserThread::PostTask,发起的任务很频繁时,web-ui也卡的很。
查了chromium工程中的已有实现,看看BrowserThread都怎么用。发现人家都是用BrowserThread::PostDelayedTask,我也用BrowserThread::PostDelayedTask来发起任务,在任务指定的本类属回调中,再用BrowserThread::PostDelayedTask发起下一个任务。这时,web-ui很流畅,一点都不卡。实验
BrowserThread::PostDelayedTask的用法例子
web-ui中代码很少,只有初始化的代码。 干活都时绑定一个WebUIMessageHandler子类来干活。// Class acting as a controller of the chrome://download-internals WebUI.class DownloadInternalsUIMessageHandler : public content::WebUIMessageHandler, public download::Logger::Observer { public: DownloadInternalsUIMessageHandler(); ~DownloadInternalsUIMessageHandler() override; // content::WebUIMessageHandler implementation. void RegisterMessages() override;
在WebUIMessageHandler子类中,找一个只有初始化时才执行的成员函数,PostDelayedTask即可。PostDelayedTask非常适合在消息循环中绑定自己类的成员函数来干活。
void DownloadInternalsUIMessageHandler::HandleGetServiceStatus( const base::ListValue* args) { AllowJavascript(); const base::Value* callback_id; CHECK(args->Get(0, &callback_id)); ResolveJavascriptCallback(*callback_id, download_service_->GetLogger()->GetServiceStatus()); // 这里是服务下载有效, 启动一次本web-ui, 这里只进入一次,很适合自动启动任务 post_ui_task_for_download();}void DownloadInternalsUIMessageHandler::post_ui_task_for_download(){ // http://192.168.2.222/a.txt // 可以用http下载的测试文件 m_str_url_by_js_first = "http://192.168.2.222/a.txt"; BrowserThread::PostDelayedTask( BrowserThread::UI, FROM_HERE, base::BindOnce(&DownloadInternalsUIMessageHandler::HandleStartDownload_url_again, base::Unretained(this)), base::TimeDelta::FromSeconds(2)); // 延时是以秒为单位的, 都是约数设置10秒延时,有时7秒发起任务,有时11秒发起任务}
void DownloadInternalsUIMessageHandler::HandleStartDownload_url_again(){ if (!m_str_url_by_js_first.empty()) { GURL url_now = GURL(m_str_url_by_js_first); HandleStartDownload_url(url_now); }}void DownloadInternalsUIMessageHandler::HandleStartDownload_url(GURL& url){ if (!url.is_valid()) { LOG(WARNING) << "Can't parse download URL, try to enter a valid URL."; return; } download::DownloadParams params; params.guid = base::GenerateGUID(); params.client = download::DownloadClient::DEBUGGING; params.request_params.method = "GET"; params.request_params.url = url; net::NetworkTrafficAnnotationTag traffic_annotation = net::DefineNetworkTrafficAnnotation("download_internals_webui_source", R"( semantics { sender: "Download Internals Page" description: "Starts a download with background download service in WebUI." trigger: "User clicks on the download button in " "chrome://download-internals." data: "None" destination: WEBSITE } policy { cookies_allowed: YES cookies_store: "user" setting: "This feature cannot be disabled by settings." policy_exception_justification: "Not implemented." })"); params.traffic_annotation = net::MutableNetworkTrafficAnnotationTag(traffic_annotation); DCHECK(download_service_); download_service_->StartDownload(params);}
转载地址:https://lostspeed.blog.csdn.net/article/details/85095737 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月15日 08时21分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
电信物联网平台插件开发相关总结
2019-05-01
ARM之一 分散加载文件(scatter)详述
2019-05-01
centos6 python3.6 安装配置jupyer
2019-05-01
CDH5.14 spark2.4.0配置python3 以及读取hive表
2019-05-01
VMware安装centos7.2后配置网络并设置固定ip
2019-05-01
在 centos7.2安装docker 并设置阿里云源
2019-05-01
centos7.2 将mysql添加到服务中
2019-05-01
【linux用户模块】/etc/passwd的字段含义
2019-05-01
【linux用户模块】用户/用户组的管理
2019-05-01
【大数据】【调度】Airflow 和 Azkaban的选型
2019-05-01
用zabbix监控nginx
2019-05-01
计算机网络 —— 数据链路层 3.
2019-05-01
计算机网络 —— 网络层 1.
2019-05-01
29. 两数相除
2019-05-01
1833. 雪糕的最大数量
2019-05-01
55. 跳跃游戏
2019-05-01
dubbo+zookeeper构建高可用分布式集群
2019-05-01