当前位置: 首页 > article >正文

Chromium webui如何与c++接口通信

参考谷歌浏览器设置页面下载为例:

1、前端js  lazy_load.js 
需要在chrome\browser\resources\settings\BUILD.gn里面加进来
 
 if (optimize_webui) {
  build_manifest = "build_manifest.json"

  optimize_webui("build") {
    host = "settings"
    input = rebase_path("$target_gen_dir/tsc", root_build_dir)
    js_module_in_files = [
      "settings.js",
      "lazy_load.js", #追加自己的js即可
    ]
    js_out_files = [
      "settings.rollup.js",
      "lazy_load.rollup.js",
      "shared.rollup.js",
    ]
    out_manifest = "$target_gen_dir/$build_manifest"

    deps = [
      ":build_ts",
      "//ui/webui/resources:preprocess",
    ]

    if (!is_chromeos_ash) {
      deps += [ "//ui/webui/resources/cr_components/customize_themes:build_ts" ]
    }

    if (use_nss_certs) {
      deps +=
          [ "//ui/webui/resources/cr_components/certificate_manager:build_ts" ]
    }

    excludes = [
      "chrome://resources/js/cr.m.js",
      "chrome://resources/mojo/mojo/public/js/bindings.js",
      "chrome://resources/mojo/skia/public/mojom/skcolor.mojom-webui.js",
    ]
  }
}
 if (optimize_webui) {
    deps = [
      ":build",
      "privacy_sandbox:build_grdp",
    ]
    manifest_files = [ "$target_gen_dir/$build_manifest" ]
    resource_path_rewrites = [
      "settings.rollup.js|settings.js",
      "lazy_load.rollup.js|lazy_load.js",
    ]
    grdp_files = [ "$target_gen_dir/privacy_sandbox/resources.grdp" ]
  } else {
    deps = [ ":build_ts" ]
    manifest_files = [ "$target_gen_dir/tsconfig.manifest" ]
  }

 
方法:
  selectDownloadLocation() {
        chrome.send("selectDownloadLocation")
  }

================================================================

2、chrome\browser\ui\webui\settings\settings_ui.cc
在SettingsUI::SettingsUI(content::WebUI* web_ui) 注册下
{

  AddSettingsPageUIHandler(std::make_unique<DownloadsHandler>(profile));
}

3、自定义页面按照这个复制即可
chrome\browser\ui\webui\settings\downloads_handler.cc
chrome\browser\ui\webui\settings\downloads_handler.h

namespace settings {

// Chrome "Downloads" settings page UI handler.
class DownloadsHandler : public SettingsPageUIHandler,
                         public ui::SelectFileDialog::Listener {
 public:
  explicit DownloadsHandler(Profile* profile);

  DownloadsHandler(const DownloadsHandler&) = delete;
  DownloadsHandler& operator=(const DownloadsHandler&) = delete;

  ~DownloadsHandler() override;

  // SettingsPageUIHandler implementation.
  void RegisterMessages() override; //需要重载这个注册回调函数
  void OnJavascriptAllowed() override;
  void OnJavascriptDisallowed() override;
.............
 };

}

4、在DownloadsHandler 里面添加webui注册函数
void DownloadsHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "initializeDownloads",
      base::BindRepeating(&DownloadsHandler::HandleInitialize,
                          base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "resetAutoOpenFileTypes",
      base::BindRepeating(&DownloadsHandler::HandleResetAutoOpenFileTypes,
                          base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "selectDownloadLocation",
      base::BindRepeating(&DownloadsHandler::HandleSelectDownloadLocation,
                          base::Unretained(this)));
#if BUILDFLAG(IS_CHROMEOS_ASH)
  web_ui()->RegisterMessageCallback(
      "getDownloadLocationText",
      base::BindRepeating(&DownloadsHandler::HandleGetDownloadLocationText,
                          base::Unretained(this)));
#endif

  web_ui()->RegisterMessageCallback(
      "setDownloadsConnectionAccountLink",
      base::BindRepeating(
          &DownloadsHandler::HandleSetDownloadsConnectionAccountLink,
          base::Unretained(this)));
}

5、对应实现
void DownloadsHandler::HandleSelectDownloadLocation(
    const base::Value::List& args) {
  // Early return if the select folder dialog is already active.
  if (select_folder_dialog_)
    return;

  PrefService* pref_service = profile_->GetPrefs();
  select_folder_dialog_ = ui::SelectFileDialog::Create(
      this,
      std::make_unique<ChromeSelectFilePolicy>(web_ui()->GetWebContents()));
  ui::SelectFileDialog::FileTypeInfo info;
  info.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH;
  select_folder_dialog_->SelectFile(
      ui::SelectFileDialog::SELECT_FOLDER,
      l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION),
      pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0,
      base::FilePath::StringType(),
      web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL);
}

6、webui调用c++代码执行结果效果如图:

 


http://www.kler.cn/a/322964.html

相关文章:

  • VMD + CEEMDAN 二次分解,CNN-LSTM预测模型
  • 基于opencv制作GUI界面
  • ADS项目笔记 1. 低噪声放大器LNA天线一体化设计
  • IDC 报告:百度智能云 VectorDB 优势数量 TOP 1
  • 一个win32 / WTL下多线程库(CThread类)的使用心得
  • 查询DBA_FREE_SPACE缓慢问题
  • 幕后魔术:掌握 PyTorch 中延迟初始化的精妙艺术
  • 五子棋双人对战项目(2)——登录模块
  • 【IoT-NTN】系统消息SIB31信令分析
  • 宝塔centOs添加node环境变量
  • WPF入门教学十五 ViewModel的设计与实现
  • 供应QCA8337-AL3C-R芯片
  • HTTP 请求方法
  • OpenAI o1-preview:详细分析
  • 边缘计算网关在工业中的应用
  • 关于贪心算法
  • 2024年7月大众点评天津美食店铺基础信息
  • 【Python】Daphne:Django 异步服务的桥梁
  • Docker仓库搭建
  • Python软体中使用Keras进行图像分类:从数据准备到模型部署
  • WebSocket和Http的server send event(sse)/EventSource
  • 嵌入式边缘计算软硬件开发“1+X”考证建设方案
  • 高校教师成果管理小程序的设计与实现springboot(lw+演示+源码+运行)
  • WebSocket消息防丢ACK和心跳机制对信息安全性的作用及实现方法
  • Avalonia开发
  • 在新ARM板上移植U-Boot和Linux指南