Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 1 | <style> |
| 2 | .note::before { |
| 3 | content: 'Note: '; |
| 4 | font-variant: small-caps; |
| 5 | font-style: italic; |
| 6 | } |
| 7 | |
| 8 | .doc h1 { |
| 9 | margin: 0; |
| 10 | } |
| 11 | </style> |
| 12 | |
| 13 | # WebUI Explainer |
| 14 | |
| 15 | [TOC] |
| 16 | |
| 17 | <a name="What_is_webui"></a> |
| 18 | ## What is "WebUI"? |
| 19 | |
| 20 | "WebUI" is a term used to loosely describe **parts of Chrome's UI |
| 21 | implemented with web technologies** (i.e. HTML, CSS, JavaScript). |
| 22 | |
| 23 | Examples of WebUI in Chromium: |
| 24 | |
| 25 | * Settings (chrome://settings) |
| 26 | * History (chrome://history) |
| 27 | * Downloads (chrome://downloads) |
| 28 | |
| 29 | <div class="note"> |
| 30 | Not all web-based UIs in Chrome have chrome:// URLs. |
| 31 | </div> |
| 32 | |
| 33 | This document explains how WebUI works. |
| 34 | |
| 35 | <a name="bindings"></a> |
| 36 | ## What's different from a web page? |
| 37 | |
| 38 | WebUIs are granted super powers so that they can manage Chrome itself. For |
| 39 | example, it'd be very hard to implement the Settings UI without access to many |
| 40 | different privacy and security sensitive services. Access to these services are |
| 41 | not granted by default. |
| 42 | |
| 43 | Only special URLs are granted WebUI "bindings" via the child security process. |
| 44 | |
| 45 | Specifically, these bindings: |
| 46 | |
| 47 | * give a renderer access to load [`chrome:`](#chrome_urls) URLS |
| 48 | * this is helpful for shared libraries, i.e. `chrome://resources/` |
| 49 | * allow the browser to execute arbitrary JavaScript in that renderer via |
| 50 | [`CallJavascriptFunction()`](#CallJavascriptFunction) |
| 51 | * allow communicating from the renderer to the browser with |
| 52 | [`chrome.send()`](#chrome_send) and friends |
| 53 | * ignore content settings regarding showing images or executing JavaScript |
| 54 | |
| 55 | <a name="chrome_urls"></a> |
| 56 | ## How `chrome:` URLs work |
| 57 | |
| 58 | <div class="note"> |
| 59 | A URL is of the format <protocol>://<host>/<path>. |
| 60 | </div> |
| 61 | |
| 62 | A `chrome:` URL loads a file from disk, memory, or can respond dynamically. |
| 63 | |
| 64 | Because Chrome UIs generally need access to the browser (not just the current |
| 65 | tab), much of the C++ that handles requests or takes actions lives in the |
| 66 | browser process. The browser has many more privileges than a renderer (which is |
| 67 | sandboxed and doesn't have file access), so access is only granted for certain |
| 68 | URLs. |
| 69 | |
| 70 | ### `chrome:` protocol |
| 71 | |
| 72 | Chrome recognizes a list of special protocols, which it registers while starting |
| 73 | up. |
| 74 | |
| 75 | Examples: |
| 76 | |
James Lissiak | 28b21a6 | 2019-05-15 15:32:04 | [diff] [blame] | 77 | * devtools: |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 78 | * chrome-extensions: |
Adam Langley | 81be073 | 2019-03-06 18:38:45 | [diff] [blame] | 79 | * chrome: |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 80 | * file: |
| 81 | * view-source: |
| 82 | |
| 83 | This document mainly cares about the **chrome:** protocol, but others can also |
| 84 | be granted [WebUI bindings](#bindings) or have special |
| 85 | properties. |
| 86 | |
| 87 | ### `chrome:` hosts |
| 88 | |
| 89 | After registering the `chrome:` protocol, a set of factories are created. These |
| 90 | factories contain a list of valid host names. A valid hostname generates a |
| 91 | controller. |
| 92 | |
| 93 | In the case of `chrome:` URLs, these factories are registered early in the |
| 94 | browser process lifecycle. |
| 95 | |
| 96 | ```c++ |
| 97 | // ChromeBrowserMainParts::PreMainMessageLoopRunImpl(): |
| 98 | content::WebUIControllerFactory::RegisterFactory( |
| 99 | ChromeWebUIControllerFactory::GetInstance()); |
| 100 | ``` |
| 101 | |
| 102 | When a URL is requested, a new renderer is created to load the URL, and a |
| 103 | corresponding class in the browser is set up to handle messages from the |
| 104 | renderer to the browser (a `RenderFrameHost`). |
| 105 | |
| 106 | The URL of the request is inspected: |
| 107 | |
| 108 | ```c++ |
| 109 | if (url.SchemeIs("chrome") && url.host_piece() == "donuts") // chrome://donuts |
| 110 | return &NewWebUI<DonutsUI>; |
| 111 | return nullptr; // Not a known host; no special access. |
| 112 | ``` |
| 113 | |
| 114 | and if a factory knows how to handle a host (returns a `WebUIFactoryFunction`), |
| 115 | the navigation machinery [grants the renderer process WebUI |
| 116 | bindings](#bindings) via the child security policy. |
| 117 | |
| 118 | ```c++ |
| 119 | // RenderFrameHostImpl::AllowBindings(): |
| 120 | if (bindings_flags & BINDINGS_POLICY_WEB_UI) { |
dbeam | 8b52edff | 2017-06-16 22:36:18 | [diff] [blame] | 121 | ChildProcessSecurityPolicyImpl::GetInstance()->GrantWebUIBindings( |
| 122 | GetProcess()->GetID()); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 123 | } |
| 124 | ``` |
| 125 | |
| 126 | The factory creates a [`WebUIController`](#WebUIController) for a tab. |
| 127 | Here's an example: |
| 128 | |
| 129 | ```c++ |
| 130 | // Controller for chrome://donuts. |
| 131 | class DonutsUI : public content::WebUIController { |
| 132 | public: |
| 133 | DonutsUI(content::WebUI* web_ui) : content::WebUIController(web_ui) { |
| 134 | content::WebUIDataSource* source = |
| 135 | content::WebUIDataSource::Create("donuts"); // "donuts" == hostname |
| 136 | source->AddString("mmmDonuts", "Mmm, donuts!"); // Translations. |
| 137 | source->SetDefaultResource(IDR_DONUTS_HTML); // Home page. |
| 138 | content::WebUIDataSource::Add(source); |
| 139 | |
| 140 | // Handles messages from JavaScript to C++ via chrome.send(). |
Jeremy Roman | e0760a40 | 2018-03-02 18:19:40 | [diff] [blame] | 141 | web_ui->AddMessageHandler(std::make_unique<OvenHandler>()); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 142 | } |
| 143 | }; |
| 144 | ``` |
| 145 | |
| 146 | If we assume the contents of `IDR_DONUTS_HTML` yields: |
| 147 | |
| 148 | ```html |
| 149 | <h1>$i18n{mmmDonuts}</h1> |
| 150 | ``` |
| 151 | |
| 152 | Visiting `chrome://donuts` should show in something like: |
| 153 | |
| 154 | <div style="border: 1px solid black; padding: 10px;"> |
| 155 | <h1>Mmmm, donuts!</h1> |
| 156 | </div> |
| 157 | |
| 158 | Delicious success. |
| 159 | |
| 160 | ## C++ classes |
| 161 | |
| 162 | ### WebUI |
| 163 | |
| 164 | `WebUI` is a high-level class and pretty much all HTML-based Chrome UIs have |
| 165 | one. `WebUI` lives in the browser process, and is owned by a `RenderFrameHost`. |
| 166 | `WebUI`s have a concrete implementation (`WebUIImpl`) in `content/` and are |
| 167 | created in response to navigation events. |
| 168 | |
| 169 | A `WebUI` knows very little about the page it's showing, and it owns a |
| 170 | [`WebUIController`](#WebUIController) that is set after creation based on the |
| 171 | hostname of a requested URL. |
| 172 | |
| 173 | A `WebUI` *can* handle messages itself, but often defers these duties to |
| 174 | separate [`WebUIMessageHandler`](#WebUIMessageHandler)s, which are generally |
| 175 | designed for handling messages on certain topics. |
| 176 | |
| 177 | A `WebUI` can be created speculatively, and are generally fairly lightweight. |
| 178 | Heavier duty stuff like hard initialization logic or accessing services that may |
| 179 | have side effects are more commonly done in a |
| 180 | [`WebUIController`](#WebUIController) or |
| 181 | [`WebUIMessageHandler`s](#WebUIMessageHandler). |
| 182 | |
| 183 | `WebUI` are created synchronously on the UI thread in response to a URL request, |
| 184 | and are re-used where possible between navigations (i.e. refreshing a page). |
| 185 | Because they run in a separate process and can exist before a corresponding |
| 186 | renderer process has been created, special care is required to communicate with |
| 187 | the renderer if reliable message passing is required. |
| 188 | |
| 189 | <a name="WebUIController"></a> |
| 190 | ### WebUIController |
| 191 | |
| 192 | A `WebUIController` is the brains of the operation, and is responsible for |
| 193 | application-specific logic, setting up translations and resources, creating |
| 194 | message handlers, and potentially responding to requests dynamically. In complex |
| 195 | pages, logic is often split across multiple |
| 196 | [`WebUIMessageHandler`s](#WebUIMessageHandler) instead of solely in the |
| 197 | controller for organizational benefits. |
| 198 | |
| 199 | A `WebUIController` is owned by a [`WebUI`](#WebUI), and is created and set on |
| 200 | an existing [`WebUI`](#WebUI) when the correct one is determined via URL |
| 201 | inspection (i.e. chrome://settings creates a generic [`WebUI`](#WebUI) with a |
| 202 | settings-specific `WebUIController`). |
| 203 | |
| 204 | ### WebUIDataSource |
| 205 | |
| 206 | <a name="WebUIMessageHandler"></a> |
| 207 | ### WebUIMessageHandler |
| 208 | |
| 209 | Because some pages have many messages or share code that sends messages, message |
| 210 | handling is often split into discrete classes called `WebUIMessageHandler`s. |
| 211 | These handlers respond to specific invocations from JavaScript. |
| 212 | |
| 213 | So, the given C++ code: |
| 214 | |
| 215 | ```c++ |
| 216 | void OvenHandler::RegisterMessages() { |
| 217 | web_ui()->RegisterMessageHandler("bakeDonuts", |
| 218 | base::Bind(&OvenHandler::HandleBakeDonuts, base::Unretained(this))); |
| 219 | } |
| 220 | |
| 221 | void OverHandler::HandleBakeDonuts(const base::ListValue* args) { |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 222 | AllowJavascript(); |
| 223 | |
| 224 | CHECK_EQ(1u, args->GetSize()); |
| 225 | // JavaScript numbers are doubles. |
| 226 | double num_donuts = args->GetList()[0].GetDouble(); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 227 | GetOven()->BakeDonuts(static_cast<int>(num_donuts)); |
| 228 | } |
| 229 | ``` |
| 230 | |
| 231 | Can be triggered in JavaScript with this example code: |
| 232 | |
| 233 | ```js |
| 234 | $('bakeDonutsButton').onclick = function() { |
| 235 | chrome.send('bakeDonuts', [5]); // bake 5 donuts! |
| 236 | }; |
| 237 | ``` |
| 238 | |
| 239 | ## Browser (C++) → Renderer (JS) |
| 240 | |
| 241 | <a name="AllowJavascript"></a> |
| 242 | ### WebUIMessageHandler::AllowJavascript() |
| 243 | |
Adam Langley | 81be073 | 2019-03-06 18:38:45 | [diff] [blame] | 244 | A tab that has been used for settings UI may be reloaded, or may navigate to an |
| 245 | external origin. In both cases, one does not want callbacks from C++ to |
| 246 | Javascript to run. In the former case, the callbacks will occur when the |
| 247 | Javascript doesn't expect them. In the latter case, sensitive information may be |
| 248 | delivered to an untrusted origin. |
| 249 | |
| 250 | Therefore each message handler maintains |
| 251 | [a boolean](https://cs.chromium.org/search/?q=WebUIMessageHandler::javascript_allowed_) |
| 252 | that describes whether delivering callbacks to Javascript is currently |
| 253 | appropriate. This boolean is set by calling `AllowJavascript`, which should be |
| 254 | done when handling a call from Javascript, because that indicates that the page |
| 255 | is ready for the subsequent callback. (See |
| 256 | [design doc](https://drive.google.com/open?id=1z1diKvwgMmn4YFzlW1kss0yHmo8yy68TN_FUhUzRz7Q).) |
| 257 | If the tab navigates or reloads, |
| 258 | [`DisallowJavascript`](https://cs.chromium.org/search/?q=WebUIMessageHandler::DisallowJavascript) |
| 259 | is called to clear the flag. |
| 260 | |
| 261 | Therefore, before each callback from C++ to Javascript, the flag must be tested |
| 262 | by calling |
| 263 | [`IsJavascriptAllowed`](https://cs.chromium.org/search/?q=WebUIMessageHandler::IsJavascriptAllowed). |
| 264 | If false, then the callback must be dropped. (When the flag is false, calling |
| 265 | [`ResolveJavascriptCallback`](https://cs.chromium.org/search/?q=WebUIMessageHandler::ResolveJavascriptCallback) |
| 266 | will crash. See |
| 267 | [design doc](https://docs.google.com/document/d/1udXoW3aJL0-l5wrbsOg5bpYWB0qOCW5K7yXpv4tFeA8).) |
| 268 | |
| 269 | Also beware of [ABA](https://en.wikipedia.org/wiki/ABA_problem) issues: Consider |
| 270 | the case where an asynchronous operation is started, the settings page is |
| 271 | reloaded, and the user triggers another operation using the original message |
| 272 | handler. The `javascript_allowed_` boolean will be true, but the original |
| 273 | callback should still be dropped because it relates to a operation that was |
| 274 | discarded by the reload. (Reloading settings UI does _not_ cause message handler |
| 275 | objects to be deleted.) |
| 276 | |
| 277 | Thus a message handler may override |
| 278 | [`OnJavascriptDisallowed`](https://cs.chromium.org/search/?q=WebUIMessageHandler::OnJavascriptDisallowed) |
| 279 | to learn when pending callbacks should be canceled. |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 280 | |
| 281 | In the JS: |
| 282 | |
| 283 | ```js |
| 284 | window.onload = function() { |
| 285 | app.initialize(); |
| 286 | chrome.send('startPilotLight'); |
| 287 | }; |
| 288 | ``` |
| 289 | |
| 290 | In the C++: |
| 291 | |
| 292 | ```c++ |
| 293 | void OvenHandler::HandleStartPilotLight(cont base::ListValue* /*args*/) { |
| 294 | AllowJavascript(); |
| 295 | // CallJavascriptFunction() and FireWebUIListener() are now safe to do. |
| 296 | GetOven()->StartPilotLight(); |
| 297 | } |
| 298 | ``` |
| 299 | |
| 300 | <div class="note"> |
| 301 | Relying on the <code>'load'</code> event or browser-side navigation callbacks to |
| 302 | detect page readiness omits <i>application-specific</i> initialization, and a |
| 303 | custom <code>'initialized'</code> message is often necessary. |
| 304 | </div> |
| 305 | |
| 306 | <a name="CallJavascriptFunction"></a> |
| 307 | ### WebUIMessageHandler::CallJavascriptFunction() |
| 308 | |
| 309 | When the browser process needs to tell the renderer/JS of an event or otherwise |
| 310 | execute code, it can use `CallJavascriptFunction()`. |
| 311 | |
| 312 | <div class="note"> |
| 313 | Javascript must be <a href="#AllowJavascript">allowed</a> to use |
| 314 | <code>CallJavscriptFunction()</code>. |
| 315 | </div> |
| 316 | |
| 317 | ```c++ |
| 318 | void OvenHandler::OnPilotLightExtinguished() { |
| 319 | CallJavascriptFunction("app.pilotLightExtinguished"); |
| 320 | } |
| 321 | ``` |
| 322 | |
| 323 | This works by crafting a string to be evaluated in the renderer. Any arguments |
| 324 | to the call are serialized to JSON and the parameter list is wrapped with |
| 325 | |
| 326 | ``` |
| 327 | // See WebUI::GetJavascriptCall() for specifics: |
| 328 | "functionCallName(" + argumentsAsJson + ")" |
| 329 | ``` |
| 330 | |
| 331 | and sent to the renderer via a `FrameMsg_JavaScriptExecuteRequest` IPC message. |
| 332 | |
| 333 | While this works, it implies that: |
| 334 | |
| 335 | * a global method must exist to successfully run the Javascript request |
| 336 | * any method can be called with any parameter (far more access than required in |
| 337 | practice) |
| 338 | |
| 339 | ^ These factors have resulted in less use of `CallJavascriptFunction()` in the |
| 340 | webui codebase. This functionality can easily be accomplished with the following |
| 341 | alternatives: |
| 342 | |
| 343 | * [`FireWebUIListener()`](#FireWebUIListener) allows easily notifying the page |
| 344 | when an event occurs in C++ and is more loosely coupled (nothing blows up if |
| 345 | the event dispatch is ignored). JS subscribes to notifications via |
| 346 | [`cr.addWebUIListener`](#cr_addWebUIListener). |
| 347 | * [`ResolveJavascriptCallback`](#ResolveJavascriptCallback) and |
| 348 | [`RejectJavascriptCallback`](#RejectJavascriptCallback) are useful |
| 349 | when Javascript requires a response to an inquiry about C++-canonical state |
| 350 | (i.e. "Is Autofill enabled?", "Is the user incognito?") |
| 351 | |
| 352 | <a name="FireWebUIListener"></a> |
| 353 | ### WebUIMessageHandler::FireWebUIListener() |
| 354 | |
| 355 | `FireWebUIListener()` is used to notify a registered set of listeners that an |
| 356 | event has occurred. This is generally used for events that are not guaranteed to |
| 357 | happen in timely manner, or may be caused to happen by unpredictable events |
| 358 | (i.e. user actions). |
| 359 | |
| 360 | Here's some example to detect a change to Chrome's theme: |
| 361 | |
| 362 | ```js |
| 363 | cr.addWebUIListener("theme-changed", refreshThemeStyles); |
| 364 | ``` |
| 365 | |
| 366 | This Javascript event listener can be triggered in C++ via: |
| 367 | |
| 368 | ```c++ |
| 369 | void MyHandler::OnThemeChanged() { |
| 370 | FireWebUIListener("theme-changed"); |
| 371 | } |
| 372 | ``` |
| 373 | |
| 374 | Because it's not clear when a user might want to change their theme nor what |
| 375 | theme they'll choose, this is a good candidate for an event listener. |
| 376 | |
| 377 | If you simply need to get a response in Javascript from C++, consider using |
| 378 | [`cr.sendWithPromise()`](#cr_sendWithPromise) and |
| 379 | [`ResolveJavascriptCallback`](#ResolveJavascriptCallback). |
| 380 | |
| 381 | <a name="OnJavascriptAllowed"></a> |
| 382 | ### WebUIMessageHandler::OnJavascriptAllowed() |
| 383 | |
| 384 | `OnJavascriptDisallowed()` is a lifecycle method called in response to |
| 385 | [`AllowJavascript()`](#AllowJavascript). It is a good place to register |
| 386 | observers of global services or other callbacks that might call at unpredictable |
| 387 | times. |
| 388 | |
| 389 | For example: |
| 390 | |
| 391 | ```c++ |
| 392 | class MyHandler : public content::WebUIMessageHandler { |
| 393 | MyHandler() { |
| 394 | GetGlobalService()->AddObserver(this); // <-- DON'T DO THIS. |
| 395 | } |
| 396 | void OnGlobalServiceEvent() { |
| 397 | FireWebUIListener("global-thing-happened"); |
| 398 | } |
| 399 | }; |
| 400 | ``` |
| 401 | |
| 402 | Because browser-side C++ handlers are created before a renderer is ready, the |
| 403 | above code may result in calling [`FireWebUIListener`](#FireWebUIListener) |
| 404 | before the renderer is ready, which may result in dropped updates or |
| 405 | accidentally running Javascript in a renderer that has navigated to a new URL. |
| 406 | |
| 407 | A safer way to set up communication is: |
| 408 | |
| 409 | ```c++ |
| 410 | class MyHandler : public content::WebUIMessageHandler { |
| 411 | public: |
| 412 | MyHandler() : observer_(this) {} |
| 413 | void OnJavascriptAllowed() override { |
| 414 | observer_.Add(GetGlobalService()); // <-- DO THIS. |
| 415 | } |
| 416 | void OnJavascriptDisallowed() override { |
| 417 | observer_.RemoveAll(); // <-- AND THIS. |
| 418 | } |
| 419 | ScopedObserver<MyHandler, GlobalService> observer_; // <-- ALSO HANDY. |
| 420 | ``` |
| 421 | when a renderer has been created and the |
| 422 | document has loaded enough to signal to the C++ that it's ready to respond to |
| 423 | messages. |
| 424 | |
| 425 | <a name="OnJavascriptDisallowed"></a> |
| 426 | ### WebUIMessageHandler::OnJavascriptDisallowed() |
| 427 | |
| 428 | `OnJavascriptDisallowed` is a lifecycle method called when it's unclear whether |
| 429 | it's safe to send JavaScript messsages to the renderer. |
| 430 | |
| 431 | There's a number of situations that result in this method being called: |
| 432 | |
| 433 | * renderer doesn't exist yet |
| 434 | * renderer exists but isn't ready |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 435 | * renderer is ready but application-specific JS isn't ready yet |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 436 | * tab refresh |
| 437 | * renderer crash |
| 438 | |
| 439 | Though it's possible to programmatically disable Javascript, it's uncommon to |
| 440 | need to do so. |
| 441 | |
| 442 | Because there's no single strategy that works for all cases of a renderer's |
| 443 | state (i.e. queueing vs dropping messages), these lifecycle methods were |
| 444 | introduced so a WebUI application can implement these decisions itself. |
| 445 | |
| 446 | Often, it makes sense to disconnect from observers in |
| 447 | `OnJavascriptDisallowed()`: |
| 448 | |
| 449 | ```c++ |
| 450 | void OvenHandler::OnJavascriptDisallowed() { |
| 451 | scoped_oven_observer_.RemoveAll() |
| 452 | } |
| 453 | ``` |
| 454 | |
| 455 | Because `OnJavascriptDisallowed()` is not guaranteed to be called before a |
| 456 | `WebUIMessageHandler`'s destructor, it is often advisable to use some form of |
| 457 | scoped observer that automatically unsubscribes on destruction but can also |
| 458 | imperatively unsubscribe in `OnJavascriptDisallowed()`. |
| 459 | |
| 460 | <a name="RejectJavascriptCallback"></a> |
| 461 | ### WebUIMessageHandler::RejectJavascriptCallback() |
| 462 | |
| 463 | This method is called in response to |
| 464 | [`cr.sendWithPromise()`](#cr_sendWithPromise) to reject the issued Promise. This |
| 465 | runs the rejection (second) callback in the [Promise's |
| 466 | executor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) |
| 467 | and any |
| 468 | [`catch()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) |
| 469 | callbacks in the chain. |
| 470 | |
| 471 | ```c++ |
| 472 | void OvenHandler::HandleBakeDonuts(const base::ListValue* args) { |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 473 | AllowJavascript(); |
| 474 | if (!GetOven()->HasGas()) { |
| 475 | RejectJavascriptCallback(args->GetList()[0], |
| 476 | base::StringValue("need gas to cook the donuts!")); |
| 477 | } |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 478 | ``` |
| 479 | |
| 480 | This method is basically just a |
| 481 | [`CallJavascriptFunction()`](#CallJavascriptFunction) wrapper that calls a |
| 482 | global "cr.webUIResponse" method with a success value of false. |
| 483 | |
| 484 | ```c++ |
| 485 | // WebUIMessageHandler::RejectJavascriptCallback(): |
| 486 | CallJavascriptFunction("cr.webUIResponse", callback_id, base::Value(false), |
dbeam | 8b52edff | 2017-06-16 22:36:18 | [diff] [blame] | 487 | response); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 488 | ``` |
| 489 | |
| 490 | See also: [`ResolveJavascriptCallback`](#ResolveJavascriptCallback) |
| 491 | |
| 492 | <a name="ResolveJavascriptCallback"></a> |
| 493 | ### WebUIMessageHandler::ResolveJavascriptCallback() |
| 494 | |
| 495 | This method is called in response to |
| 496 | [`cr.sendWithPromise()`](#cr_sendWithPromise) to fulfill an issued Promise, |
| 497 | often with a value. This results in runnings any fulfillment (first) callbacks |
| 498 | in the associate Promise executor and any registered |
| 499 | [`then()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) |
| 500 | callbacks. |
| 501 | |
| 502 | So, given this JS code: |
| 503 | |
| 504 | ```js |
| 505 | cr.sendWithPromise('bakeDonuts').then(function(numDonutsBaked) { |
| 506 | shop.donuts += numDonutsBaked; |
| 507 | }); |
| 508 | ``` |
| 509 | |
| 510 | Some handling C++ might do this: |
| 511 | |
| 512 | ```c++ |
| 513 | void OvenHandler::HandleBakeDonuts(const base::ListValue* args) { |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 514 | AllowJavascript(); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 515 | double num_donuts_baked = GetOven()->BakeDonuts(); |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 516 | ResolveJavascriptCallback(args->GetList()[0], num_donuts_baked); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 517 | } |
| 518 | ``` |
| 519 | |
| 520 | ## Renderer (JS) → Browser (C++) |
| 521 | |
| 522 | <a name="chrome_send"></a> |
| 523 | ### chrome.send() |
| 524 | |
| 525 | When the JavaScript `window` object is created, a renderer is checked for [WebUI |
| 526 | bindings](#bindings). |
| 527 | |
| 528 | ```c++ |
| 529 | // RenderFrameImpl::DidClearWindowObject(): |
| 530 | if (enabled_bindings_ & BINDINGS_POLICY_WEB_UI) |
| 531 | WebUIExtension::Install(frame_); |
| 532 | ``` |
| 533 | |
| 534 | If the bindings exist, a global `chrome.send()` function is exposed to the |
| 535 | renderer: |
| 536 | |
| 537 | ```c++ |
| 538 | // WebUIExtension::Install(): |
Dan Elphick | 258bbaf | 2019-02-01 17:37:35 | [diff] [blame] | 539 | v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, context); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 540 | chrome->Set(gin::StringToSymbol(isolate, "send"), |
dbeam | 8b52edff | 2017-06-16 22:36:18 | [diff] [blame] | 541 | gin::CreateFunctionTemplate( |
| 542 | isolate, base::Bind(&WebUIExtension::Send))->GetFunction()); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 543 | ``` |
| 544 | |
| 545 | The `chrome.send()` method takes a message name and argument list. |
| 546 | |
| 547 | ```js |
| 548 | chrome.send('messageName', [arg1, arg2, ...]); |
| 549 | ``` |
| 550 | |
| 551 | The message name and argument list are serialized to JSON and sent via the |
Lukasz Anforowicz | 0292310 | 2017-10-09 18:11:37 | [diff] [blame] | 552 | `FrameHostMsg_WebUISend` IPC message from the renderer to the browser. |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 553 | |
| 554 | ```c++ |
| 555 | // In the renderer (WebUIExtension::Send()): |
Lukasz Anforowicz | 0292310 | 2017-10-09 18:11:37 | [diff] [blame] | 556 | render_frame->Send(new FrameHostMsg_WebUISend(render_frame->GetRoutingID(), |
| 557 | frame->GetDocument().Url(), |
| 558 | message, *content)); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 559 | ``` |
| 560 | ```c++ |
| 561 | // In the browser (WebUIImpl::OnMessageReceived()): |
Lukasz Anforowicz | 0292310 | 2017-10-09 18:11:37 | [diff] [blame] | 562 | IPC_MESSAGE_HANDLER(FrameHostMsg_WebUISend, OnWebUISend) |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 563 | ``` |
| 564 | |
| 565 | The browser-side code does a map lookup for the message name and calls the found |
| 566 | callback with the deserialized arguments: |
| 567 | |
| 568 | ```c++ |
| 569 | // WebUIImpl::ProcessWebUIMessage(): |
| 570 | message_callbacks_.find(message)->second.Run(&args); |
| 571 | ``` |
| 572 | |
| 573 | <a name="cr_addWebUIListener"> |
| 574 | ### cr.addWebUIListener() |
| 575 | |
| 576 | WebUI listeners are a convenient way for C++ to inform JavaScript of events. |
| 577 | |
| 578 | Older WebUI code exposed public methods for event notification, similar to how |
| 579 | responses to [chrome.send()](#chrome_send) used to work. They both |
| 580 | resulted in global namespace polution, but it was additionally hard to stop |
| 581 | listening for events in some cases. **cr.addWebUIListener** is preferred in new |
| 582 | code. |
| 583 | |
| 584 | Adding WebUI listeners creates and inserts a unique ID into a map in JavaScript, |
| 585 | just like [cr.sendWithPromise()](#cr_sendWithPromise). |
| 586 | |
| 587 | ```js |
| 588 | // addWebUIListener(): |
| 589 | webUIListenerMap[eventName] = webUIListenerMap[eventName] || {}; |
| 590 | webUIListenerMap[eventName][createUid()] = callback; |
| 591 | ``` |
| 592 | |
| 593 | The C++ responds to a globally exposed function (`cr.webUIListenerCallback`) |
| 594 | with an event name and a variable number of arguments. |
| 595 | |
| 596 | ```c++ |
| 597 | // WebUIMessageHandler: |
| 598 | template <typename... Values> |
| 599 | void FireWebUIListener(const std::string& event_name, const Values&... values) { |
| 600 | CallJavascriptFunction("cr.webUIListenerCallback", base::Value(event_name), |
| 601 | values...); |
| 602 | } |
| 603 | ``` |
| 604 | |
| 605 | C++ handlers call this `FireWebUIListener` method when an event occurs that |
| 606 | should be communicated to the JavaScript running in a tab. |
| 607 | |
| 608 | ```c++ |
| 609 | void OvenHandler::OnBakingDonutsFinished(size_t num_donuts) { |
| 610 | FireWebUIListener("donuts-baked", base::FundamentalValue(num_donuts)); |
| 611 | } |
| 612 | ``` |
| 613 | |
| 614 | JavaScript can listen for WebUI events via: |
| 615 | |
| 616 | ```js |
| 617 | var donutsReady = 0; |
| 618 | cr.addWebUIListener('donuts-baked', function(numFreshlyBakedDonuts) { |
| 619 | donutsReady += numFreshlyBakedDonuts; |
| 620 | }); |
| 621 | ``` |
| 622 | |
| 623 | <a name="cr_sendWithPromise"></a> |
| 624 | ### cr.sendWithPromise() |
| 625 | |
| 626 | `cr.sendWithPromise()` is a wrapper around `chrome.send()`. It's used when |
| 627 | triggering a message requires a response: |
| 628 | |
| 629 | ```js |
| 630 | chrome.send('getNumberOfDonuts'); // No easy way to get response! |
| 631 | ``` |
| 632 | |
| 633 | In older WebUI pages, global methods were exposed simply so responses could be |
| 634 | sent. **This is discouraged** as it pollutes the global namespace and is harder |
| 635 | to make request specific or do from deeply nested code. |
| 636 | |
| 637 | In newer WebUI pages, you see code like this: |
| 638 | |
| 639 | ```js |
| 640 | cr.sendWithPromise('getNumberOfDonuts').then(function(numDonuts) { |
| 641 | alert('Yay, there are ' + numDonuts + ' delicious donuts left!'); |
| 642 | }); |
| 643 | ``` |
| 644 | |
| 645 | On the C++ side, the message registration is similar to |
| 646 | [`chrome.send()`](#chrome_send) except that the first argument in the |
| 647 | message handler's list is a callback ID. That ID is passed to |
| 648 | `ResolveJavascriptCallback()`, which ends up resolving the `Promise` in |
| 649 | JavaScript and calling the `then()` function. |
| 650 | |
| 651 | ```c++ |
| 652 | void DonutHandler::HandleGetNumberOfDonuts(const base::ListValue* args) { |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 653 | AllowJavascript(); |
| 654 | |
| 655 | const base::Value& callback_id = args->GetList()[0]; |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 656 | size_t num_donuts = GetOven()->GetNumberOfDonuts(); |
Michael Giuffrida | 1493829 | 2019-05-31 21:30:23 | [diff] [blame^] | 657 | ResolveJavascriptCallback(callback_id, base::FundamentalValue(num_donuts)); |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 658 | } |
| 659 | ``` |
| 660 | |
| 661 | Under the covers, a map of `Promise`s are kept in JavaScript. |
| 662 | |
| 663 | The callback ID is just a namespaced, ever-increasing number. It's used to |
| 664 | insert a `Promise` into the JS-side map when created. |
| 665 | |
| 666 | ```js |
| 667 | // cr.sendWithPromise(): |
| 668 | var id = methodName + '_' + uidCounter++; |
| 669 | chromeSendResolverMap[id] = new PromiseResolver; |
| 670 | chrome.send(methodName, [id].concat(args)); |
| 671 | ``` |
| 672 | |
| 673 | The corresponding number is used to look up a `Promise` and reject or resolve it |
| 674 | when the outcome is known. |
| 675 | |
| 676 | ```js |
| 677 | // cr.webUIResponse(): |
| 678 | var resolver = chromeSendResolverMap[id]; |
| 679 | if (success) |
| 680 | resolver.resolve(response); |
| 681 | else |
| 682 | resolver.reject(response); |
| 683 | ``` |
| 684 | |
| 685 | This approach still relies on the C++ calling a globally exposed method, but |
| 686 | reduces the surface to only a single global (`cr.webUIResponse`) instead of |
| 687 | many. It also makes per-request responses easier, which is helpful when multiple |
| 688 | are in flight. |
| 689 | |
Lukasz Anforowicz | 11e5953 | 2018-10-23 22:46:21 | [diff] [blame] | 690 | |
| 691 | ## Security considerations |
| 692 | |
| 693 | Because WebUI pages are highly privileged, they are often targets for attack, |
| 694 | since taking control of a WebUI page can sometimes be sufficient to escape |
| 695 | Chrome's sandbox. To make sure that the special powers granted to WebUI pages |
| 696 | are safe, WebUI pages are restricted in what they can do: |
| 697 | |
| 698 | * WebUI pages cannot embed http/https resources or frames |
| 699 | * WebUI pages cannot issue http/https fetches |
| 700 | |
| 701 | In the rare case that a WebUI page really needs to include web content, the safe |
| 702 | way to do this is by using a `<webview>` tag. Using a `<webview>` tag is more |
| 703 | secure than using an iframe for multiple reasons, even if Site Isolation and |
| 704 | out-of-process iframes keep the web content out of the privileged WebUI process. |
| 705 | |
| 706 | First, the content inside the `<webview>` tag has a much reduced attack surface, |
| 707 | since it does not have a window reference to its embedder or any other frames. |
| 708 | Only postMessage channel is supported, and this needs to be initiated by the |
| 709 | embedder, not the guest. |
| 710 | |
| 711 | Second, the content inside the `<webview>` tag is hosted in a separate |
| 712 | StoragePartition. Thus, cookies and other persistent storage for both the WebUI |
| 713 | page and other browser tabs are inaccessible to it. |
| 714 | |
| 715 | This greater level of isolation makes it safer to load possibly untrustworthy or |
| 716 | compromised web content, reducing the risk of sandbox escapes. |
| 717 | |
| 718 | For an example of switching from iframe to webview tag see |
| 719 | https://crrev.com/c/710738. |
| 720 | |
| 721 | |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 722 | ## See also |
| 723 | |
Amos Lim | f916d57 | 2018-05-21 23:10:35 | [diff] [blame] | 724 | * WebUI's C++ code follows the [Chromium C++ styleguide](../styleguide/c++/c++.md). |
Dan Beam | 079d5c1 | 2017-06-16 19:23:30 | [diff] [blame] | 725 | * WebUI's HTML/CSS/JS code follows the [Chromium Web |
| 726 | Development Style Guide](../styleguide/web/web.md) |
| 727 | |
| 728 | |
| 729 | <script> |
| 730 | let nameEls = Array.from(document.querySelectorAll('[id], a[name]')); |
| 731 | let names = nameEls.map(nameEl => nameEl.name || nameEl.id); |
| 732 | |
| 733 | let localLinks = Array.from(document.querySelectorAll('a[href^="#"]')); |
| 734 | let hrefs = localLinks.map(a => a.href.split('#')[1]); |
| 735 | |
| 736 | hrefs.forEach(href => { |
| 737 | if (names.includes(href)) |
| 738 | console.info('found: ' + href); |
| 739 | else |
| 740 | console.error('broken href: ' + href); |
| 741 | }) |
| 742 | </script> |