Daniel Murphy | f296b36 | 2022-11-17 22:06:16 | [diff] [blame] | 1 | # [Web Apps](README.md) - Operating System Integration |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 2 | |
Daniel Murphy | f296b36 | 2022-11-17 22:06:16 | [diff] [blame] | 3 | The WebAppProvider system has to provide a lot of integrations with operating system surfaces for web apps. This functionality is usually different per operating system, and is usually invoked through the [`OsIntegrationManager`][2]. |
Daniel Murphy | 11adede8 | 2021-06-03 15:05:06 | [diff] [blame] | 4 | |
Daniel Murphy | f296b36 | 2022-11-17 22:06:16 | [diff] [blame] | 5 | The [`OsIntegrationManager`][2]'s main responsibility is support the following operations: |
Daniel Murphy | 11adede8 | 2021-06-03 15:05:06 | [diff] [blame] | 6 | 1. Install operating system integration for a given web app. |
| 7 | 1. Update operating system integration for a given web app. |
| 8 | 1. Uninstall/remove operating system integration for a given web app. |
| 9 | |
Daniel Murphy | f296b36 | 2022-11-17 22:06:16 | [diff] [blame] | 10 | It owns sub-managers who are responsible for each individual operating system integration functionality (e.g. [`web_app_file_handler_manager.h`][1] which owns the file handling feature). That manager will implement the non-os-specific logic, and then call into functions that have os-specific implementations (e.g. `web_app_file_handler_registration.h/_mac.h/_win.h/_linux.h` files). |
Daniel Murphy | 11adede8 | 2021-06-03 15:05:06 | [diff] [blame] | 11 | |
| 12 | Below are sections describing how each OS integration works. |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 13 | |
| 14 | ## Protocol Handler |
| 15 | |
| 16 | The Protocol Handler component is responsible for handling the registration and |
| 17 | unregistration of custom protocols for web apps ([explainer](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/URLProtocolHandler/explainer.md)). |
| 18 | |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 19 | ### WebAppProtocolHandlerManager |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 20 | |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 21 | The entrypoint to the component is `web_app_protocol_handler_manager.cc`, which contains |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 22 | methods for registering and unregistering custom protocol handlers for web apps, |
| 23 | as well as any other logic related to protocol handling that should be |
| 24 | OS-agnostic - such as URL translation (translating from a protocol URL to a web |
| 25 | app URL) and interactions with the `ProtocolHandlerRegistry`, the browser |
| 26 | component that stores information about handlers internally. |
| 27 | |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 28 | `WebAppProtocolHandlerManager` is also responsible for leveraging the |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 29 | web_app_protocol_handler_registration API to register and unregister protocols |
| 30 | with the underlying OS, so that custom protocols for a web app can be used |
| 31 | from outside the browser. |
| 32 | |
| 33 | ### web_app_protocol_handler_registration |
| 34 | `web_app_protocol_handler_registration` exposes a simple API that has |
| 35 | OS-specific files / implementations. This API communicates with other components |
| 36 | responsible for directly managing OS integrations. For example, on Windows this |
| 37 | interacts with the `ShellUtil` class, which manages the interactions with the |
| 38 | Windows registry for us. |
| 39 | |
| 40 | Since some of these interactions with the OSes can be a bit costly, we try to do |
| 41 | as much as possible off the main thread. For the Windows implementation, we |
| 42 | post a task and wait for confirmation before proceeding with other steps (such |
| 43 | as updating the browser internal registry). |
| 44 | |
| 45 | ### ProtocolHandlerRegistry |
| 46 | Protocol handlers, like other Web App features, interact both with the OS and |
| 47 | the browser. On the browser, `protocol_handler_registry.cc` stores and manages |
| 48 | all protocol handler registrations, for both web apps and web sites (registered |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 49 | via the HTML5 `registerProtocolHandler` API). The `WebAppProtocolHandlerManager` is |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 50 | responsible for keeping both the OS and the browser in sync, by ensuring OS |
| 51 | changes are reflected in the browser registry accordingly. |
| 52 | |
| 53 | ### Flow of execution for a protocol registration call (Windows) |
| 54 | |
| 55 | The flow of execution is similar for both registrations and unregistrations, so |
| 56 | we only describe registrations below. |
| 57 | |
| 58 | 1) Other components (typically the `OSIntegrationManager`) call into |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 59 | WebAppProtocolHandlerManager: |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 60 | |
| 61 | ```cpp |
| 62 | protocol_handler_manager_->RegisterOsProtocolHandlers(app_id); |
| 63 | ``` |
| 64 | |
Song Fangzhen | 8333d7b | 2021-09-03 08:23:26 | [diff] [blame] | 65 | 2) `WebAppProtocolHandlerManager` forwards that to |
Fabio Rocha | 1c34619 | 2021-02-12 05:07:16 | [diff] [blame] | 66 | `web_app_protocol_handler_registration`, which is OS specific. That API registers |
| 67 | protocols with the OS via `ShellUtil` utilities off the main thread: |
| 68 | |
| 69 | ```cpp |
| 70 | base::ThreadPool::PostTaskAndReply( |
| 71 | FROM_HERE, |
| 72 | {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}, |
| 73 | base::BindOnce(&RegisterProtocolHandlersWithOSInBackground, app_id, |
| 74 | base::UTF8ToUTF16(app_name), profile, profile->GetPath(), |
| 75 | protocol_handlers, app_name_extension), |
| 76 | base::BindOnce(&CheckAndUpdateExternalInstallations, profile->GetPath(), |
| 77 | app_id)); |
| 78 | ``` |
| 79 | |
| 80 | 3) Once the protocols are registered successfully, a callback is invoked inside |
| 81 | `RegisterProtocolHandlersWithOSInBackground` which then completes the |
| 82 | registration with the `ProtocolHandlerRegistry`. |
| 83 | |
| 84 | ```cpp |
| 85 | ProtocolHandlerRegistry* registry = |
| 86 | ProtocolHandlerRegistryFactory::GetForBrowserContext(profile); |
| 87 | |
| 88 | registry->RegisterAppProtocolHandlers(app_id, protocol_handlers); |
| 89 | ``` |
| 90 | |
| 91 | 4) `CheckAndUpdateExternalInstallations` is then called as the reply to the task |
| 92 | in 2) and checks if there is an installation of this app in another profile that |
| 93 | needs to be updated with a profile specific name and executes required update. |
| 94 | |
| 95 | |
Daniel Murphy | f296b36 | 2022-11-17 22:06:16 | [diff] [blame] | 96 | [1]: /chrome/browser/web_applications/os_integration/web_app_file_handler_manager.h |
| 97 | [2]: /chrome/browser/web_applications/os_integration/os_integration_manager.h |