Programmatically enabling extensions

525 views
Skip to first unread message

Sebastian M.D.

unread,
May 3, 2021, 5:11:58 PM5/3/21
to Chromium Extensions
I've managed to install an extension automatically by adding the following 
snippet to "chrome\browser\chrome_browser_main.cc":

void InstallMyExtension () {
  LOG(INFO) << "InstallMyExtension():";
  base::FilePath path(
      L"C:\\my_extension_dir\");
  LOG(INFO) << "InstallMyExtension(): Installing extension from path"
            << path;
  base::FilePath kiwi_extension_crx_name(L"my_extension.crx");

  scoped_refptr<extensions::CrxInstaller> crx_installer(
      extensions::CrxInstaller::CreateSilent(extension_service));
  crx_installer->set_install_immediately(true);

    LOG(INFO) << "InstallMyExtension(): Installing from path: "
              << path.Append(kiwi_extension_crx_name);

    crx_installer->InstallCrx(path.Append(kiwi_extension_crx_name));
}

Profile* CreatePrimaryProfile(...) {
 ...
 InstallMyExtension(profile);
return profile;
}

This works nicely; the extension is installed but not enabled. 
I tried enabling it by changing: "chrome\browser\extensions\crx_installer.cc":

in the last step (intuitively since the extension is assumed installed):

void CrxInstaller::CleanupTempFiles() {
ExtensionService* service = service_weak_.get();
      if (!service || service->browser_terminating())
        return;
service->GrantPermissionsAndEnableExtension(extension());
...
}

but I get an error like:


[ERROR:socket_posix.cc(144)] bind() returned an error, errno=98: Address already in use (98)
 [ERROR:unix_domain_server_socket_posix.cc(98)] Could not bind unix domain socket to chrome_devtools_remote (with abstract namespace): Address already in use (98)
 [ERROR:devtools_http_handler.cc(292)] Cannot start http server for devtools. Stop devtools.

I'm not exactly sure what address is already in use. Is there any other way of enabling extensions programmatically?

I also tried setting the completion callback:

  scoped_refptr<extensions::CrxInstaller> crx_installer(
      extensions::CrxInstaller::CreateSilent(extension_service));
  crx_installer->set_install_immediately(true);
  crx_installer->set_installer_callback(
      base::BindOnce([](const base::Optional<extensions::CrxInstallError>&
      error) {
  
      }));

but I can't send parameters to it; it needs a browser context/profile in order to get the extension service.

Any thoughts?

Simeon Vincent

unread,
May 4, 2021, 1:31:31 PM5/4/21
to Chromium Extensions, mdnsse...@gmail.com
To my knowledge there are 2 main classes you'll want to look at
  • ComponentLoader for component extensions. These are extension that are bundled with the browser and cannot be uninstalled by the user. They're effectively part of the browser.
  • ExternalProviderImpl for default-installed extensions. These are effectively "normal" extensions that ship with the browser and can be removed by the user. 

Simeon - @dotproto
Chrome Extensions DevRel
Reply all
Reply to author
Forward
0 new messages