Add support for chrome extensions

Issue #1947 new
Marshall Greenblatt
created an issue

Chrome supports an extension system [1] which generally functions as follows:

  1. Load a pre-packaged .crx file (zip archive) that contains the extension source code.
  2. Run the extension in an isolated JavaScript context.
  3. Expose chrome.* JavaScript APIs [2] that the extension can use to perform actions, show UI and interact with other browser content.

CEF will add support for Chrome extensions as a multi-step process:

  1. Add support for loading and running existing chrome extensions (note: they will not work until the required JavaScript APIs have been implemented).
  2. Incrementally implement JavaScript APIs over time.

The first round of implementation will include:

  • Extension - The chrome.extension API has utilities that can be used by any extension page. It includes support for exchanging messages between an extension and its content scripts or between extensions, as described in detail in Message Passing.
  • Runtime - Use the chrome.runtime API to retrieve the background page, return details about the manifest, and listen for and respond to events in the app or extension lifecycle. You can also use this API to convert the relative path of URLs to fully-qualified URLs.
  • Storage - Use the chrome.storage API to store, retrieve, and track changes to user data.
  • Tabs - Use the chrome.tabs API to interact with the browser's tab system. You can use this API to create, modify, and rearrange tabs in the browser. (For CEF this will operate on browser windows instead of "tabs".)
  • Web Navigation - Use the chrome.webNavigation API to receive notifications about the status of navigation requests in-flight.
  • Web Request - Use the chrome.webRequest API to observe and analyze traffic and to intercept, block, or modify requests in-flight.
  • Windows - Use the chrome.windows API to interact with browser windows. You can use this API to create, modify, and rearrange windows in the browser. (For CEF this will operate on browser windows instead of "tabs".)

Other APIs under consideration for early implementation include:

  • Browser Action - Use browser actions to put icons in the main Google Chrome toolbar, to the right of the address bar. In addition to its icon, a browser action can also have a tooltip, a badge, and a popup.
  • Context Menus - Use the chrome.contextMenus API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.
  • DevTools - A DevTools extension adds functionality to the Chrome DevTools. It can add new UI panels and sidebars, interact with the inspected page, get information about network requests, and more.
  • I18n - Use the chrome.i18n infrastructure to implement internationalization across your whole app or extension.

Any APIs that require user interface components, such as Browser Action, will be implemented as new CEF APIs that the client application will be responsible for implementing.

This is expected to be a long term project.

[1] https://developer.chrome.com/extensions

[2] https://developer.chrome.com/extensions/api_index

Comments (28)

  1. Czarek Tomczak

    You might consider mocking some of the APIs that are non-essential, but which would stop the extension from working. For example when Browser Action is implemented, you could mock the Page Action API which is related and many extensions that use Browser Action may use Page Action as well. Page Action API is not essential for an extension, it would still work fine if the API was mocked.

    The Cookies API seem to be important to be implemented in early stages. This API was available since early Chrome v5 and we know that all websites use cookies.

    If there was a way to gather statistics about Chrome Extensions to get know which APIs are most commonly used and implement them first, so that CEF supports greatest number of extensions with the least amount of work. Maybe some scraper that downloads all Chrome extensions, unpacks the crx (zip) files and analyzes the source code.

  2. Eivind Arvesen

    Both Vivaldi and Opera is built on Chromium and supports Chrome-extensions. Thus, they must have implemented the API.

    Both Vivaldi and Opera make their source code available (though Vivaldi does not include code for the GUI).

    Would it be possible to reach out to them or to use some of their code?

    Edit: It seems Opera uses an adaptor to install extensions.

  3. Marshall Greenblatt reporter

    The Chromium code base has multiple layers. CEF currently uses the Content API layer. Chrome, Vivaldi and Opera use the chrome/ layer which sits on top of the Content API later. Extension support is implemented in the chrome/ layer. Consequently it's not a question of missing code but instead an architectural issue that needs to be resolved. We want to do this selectively to avoid exposing untested code paths in CEF.

  4. Marshall Greenblatt reporter

    The process for adding extension support is documented at https://bitbucket.org/chromiumembedded/cef/src/master/libcef/common/extensions/api/README.txt?at=master&fileviewer=file-view-default.

    For example, compare the tabs API implementation in Chrome and CEF. The Chrome implementation of GetTabById uses the Browser and TabStripModel objects that do not exist in CEF. This code can be converted to the CEF equivalent that uses CefBrowserHostImpl.

    Similar conversions will be required when implementing other extension APIs.

  5. Marshall Greenblatt reporter

    Master revision 9cff99d adds support for loading extensions.

    • Add CefRequestContext::LoadExtension, CefExtension, CefExtensionHandler and related methods/interfaces.
    • Add chrome://extensions-support that lists supported Chrome APIs.
    • Add CefBrowserHost::SetAutoResizeEnabled and CefDisplayHandler::OnAutoResize to support browser resize based on preferred web contents size.
    • views: Add support for custom CefMenuButton popups.
    • cefclient: Run with --load-extension=set_page_color command-line flag for an extension loading example. Add --use-views on Windows and Linux for an even better example.
  6. fengxingren

    out\Debug_GN_x86\cefclient.exe crashed when launched with "--single-process --load-extension=set_page_color". would it be possible to support extensions in single process mode? thanks.

  7. fengxingren

    @Marshall Greenblatt Thanks for your reply. As I understand it, the extension process is like the devtool process, and the latter can be supported in single-process mode. So How much trouble would it be to support it? Or is it just technically not feasible? I want to use the React Developer Tools extension within CEF3 in single-process mode. It would be appreciated if you could give me some suggestions about how to implement the extension support of single-process mode.

  8. Marshall Greenblatt reporter

    To support the React Developer Tools extension (v2.5.1) we need to add:

    • chrome.browserAction
      • setIcon
      • setPopup
    • chrome.devtools
      • inspectedWindow.eval
      • inspectedWindow.tabId
      • panels.create
      • network.onNavigated.addListener
      • network.onNavigated.removeListener
      • panels.themeName
    • chrome.runtime
      • getURL
      • connect
      • onConnect.addListener
      • onMessage.addListener
      • sendMessage
  9. Andrew Warnick

    I noticed this message in the client app: "Cannot mix --load-extension and --request-context-per-browser"

    We use different request contexts in most browsers. I assume that's going to be a problem. Are you able to explain why, and is there a possibility of a work-around?

  10. Log in to comment