Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 1 | # Lifetime annotations |
| 2 | |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 3 | ## Overview |
| 4 | |
| 5 | As part of the WebView Multi-profile project, classes and state in |
| 6 | `//android_webview/`` will be annotated to make their lifetimes explicit. |
Wayne Jackson Jr | b5ad3710 | 2023-06-03 14:57:03 | [diff] [blame] | 7 | There are broadly 5 different lifetimes that a class can have: |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 8 | |
Nate Fischer | e7f39eba | 2024-01-03 11:06:10 | [diff] [blame] | 9 | - Singleton - there is one instance of this class per WebView browser process. |
| 10 | As WebView is run in the embedding app's process, this means there is one |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 11 | instance per embedding app. |
| 12 | - Scoped to Profile - this class is specific to a Profile. It is likely to do |
| 13 | with the user's browsing state, such as cookies or permissions. |
| 14 | - Scoped to WebView - this class is specific to a WebView (the Android View), |
| 15 | for example the AwZoomControls. |
Nate Fischer | e7f39eba | 2024-01-03 11:06:10 | [diff] [blame] | 16 | - Temporary - this class has a shorter lifetime than a WebView. Examples include |
| 17 | objects that live within a single call stack, objects associated with |
| 18 | a single HTTP request or page navigation, etc. |
| 19 | - Renderer - this class has a lifetime tied to specific to a Renderer. |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 20 | |
| 21 | There is a many to one relationship between WebViews and Profiles. A single |
| 22 | Profile can support multiple WebViews, each WebView will only have a single |
| 23 | (constant) Profile. |
| 24 | |
| 25 | These annotations are purely for documentation, there is no static analysis to |
| 26 | check them. |
| 27 | |
| 28 | ## Annotations |
| 29 | |
| 30 | In Java, use the following annotations: |
| 31 | |
| 32 | - `@Lifetime.Singleton` |
| 33 | - `@Lifetime.Profile` |
| 34 | - `@Lifetime.WebView` |
| 35 | - `@Lifetime.Temporary` |
Wayne Jackson Jr | b5ad3710 | 2023-06-03 14:57:03 | [diff] [blame] | 36 | - `@Lifetime.Renderer` |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 37 | |
Nate Fischer | e7f39eba | 2024-01-03 11:06:10 | [diff] [blame] | 38 | In C++, use the following comment format as the last line of the class |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 39 | documentation: |
| 40 | |
| 41 | ``` |
Nate Fischer | e7f39eba | 2024-01-03 11:06:10 | [diff] [blame] | 42 | // Lifetime: Singleton |
| 43 | // Lifetime: Profile |
| 44 | // Lifetime: WebView |
| 45 | // Lifetime: Temporary |
| 46 | // Lifetime: Renderer |
Peter E Conn | 5b603ca | 2023-05-18 14:44:27 | [diff] [blame] | 47 | ``` |