How to Add New Features (without bloating RenderView/RenderViewHost/WebContents)

Problem

Historically, new features (i.e. autofill to pick an example) have been added by bolting on their code to RenderViewin the renderer process, and RenderViewHostin the browser process. If a feature was handled on the IO thread in the browser process, then its IPC messages were dispatched in BrowserMessageFilter. RenderViewHostwould often dispatch the IPC message only to call WebContents(through the RenderViewHostDelegateinterface), which would then call to another piece of code. All the IPC messages between the browser and renderer were declared in a massive render_messages_internal.h. Touching each of these files for every feature meant that the classes became bloated.

Solution

We have added helper classes and mechanisms for filtering IPC messages on each of the above threads. This makes it easier to write self contained features.

Renderer side:

If you want to filter and send IPC messages, implement the RenderViewObserverinterface (content/renderer/render_view_observer.h). The RenderViewObserverbase class takes a RenderViewand manages the object's lifetime so that it's tied to RenderView(this is overridable). The class can then filter and send IPC messages, and additionally gets notification about frame loading and closing, which many features need. As an example, see ChromeExtensionHelper (chrome/renderer/extensions/chrome_extension_helper.h).

If your feature has part of the code in WebKit, avoid having callbacks go through WebViewClientinterface so that we don't bloat it. Consider creating a new WebKit interface that the WebKit code calls, and have the renderer side class implement it. As an example, see WebAutoFillClient (WebKit/chromium/public/WebAutoFillClient.h).

Browser UI thread:

The WebContentsObserver (content/public/browser/web_contents_observer.h) interface allows objects on the UI thread to filter IPC messages and also gives notifications about frame navigation. As an example, see TabHelper (chrome/browser/extensions/tab_helper.h).

Browser other threads:

To filter and send IPC messages on other browser threads, such as IO/FILE/WEBKIT etc, implement BrowserMessageFilterinterface (content/browser/browser_message_filter.h). The BrowserRenderProcessHost object creates and adds the filters in its CreateMessageFilters function.

In general, if a feature has more than a few IPC messages, they should be moved into a separate file (i.e. not be added to render_messages_internal.h). This also helps with filtering messages on a thread other than the IO thread. As an example, see content/common/pepper_file_messages.h. This allows their filter, PepperFileMessageFilter, to easily send the messages to the file thread without having to specify their IDs in multiple places.