| OLD | NEW |
| (Empty) | |
| 1 # `Tab` lifecycle |
| 2 |
| 3 `LegacyTabHelper` creates `Tab` and keep it retained. `AttachTabHelpers` |
| 4 creates `LegacyTabHelper` first and then all the other tab helpers. The |
| 5 `AttachTabHelpers` method can be invoked on a newly created `WebState`. |
| 6 |
| 7 From that point, `LegacyTabHelper::GetTabFromWebState()` will return the |
| 8 `Tab` associated with `WebState`. That method will return `nil` before |
| 9 the call to `AttachTabHelpers`. |
| 10 |
| 11 ````cpp |
| 12 web::WebState::CreateParams params{...}; |
| 13 std::unique_ptr<web::WebState> web_state = web::WebState::Create(params); |
| 14 AttachTabHelper(web_state.get()); |
| 15 |
| 16 Tab* tab = LegacyTabHelper::GetFromWebState(web_state.get()); |
| 17 DCHECK(tab != nil); |
| 18 ```` |
| 19 |
| 20 When a `WebState` is added to a `TabModel`'s `WebStateList`, |
| 21 `TabModelWebStateListDelegate` will invoke `AttachTabHelpers` if necessary. |
| 22 |
| 23 ```cpp |
| 24 TabModel* tab_model = ...; |
| 25 std::unique_ptr<web::WebState> web_state = ...; |
| 26 [tab_model webStateList]->InsertWebState(0, std::move(web_state)); |
| 27 Tab* tab = LegacyTabHelper::GetFromWebState( |
| 28 [tab_model webStateList]->GetWebStateAt(0)); |
| 29 DCHECK(tab != nil); |
| 30 ``` |
| 31 |
| 32 `Tab` register itself as a `WebStateObserver`. When `-webStateDestroyed:` |
| 33 is invoked as part of `WebState` destruction, `Tab` destroys its state and |
| 34 should no longer be used (as `-webState` will return `nullptr`). |
| 35 |
| 36 `LegacyTabHelper` is a `WebStateUserData` thus it is destroyed after the |
| 37 `WebState` destructor completes. `LegacyTabHelper` release its reference |
| 38 to `Tab` when destroyed. |
| 39 |
| 40 It is better to only use `WebState` and to access the `Tab` via |
| 41 `LegacyTabHelper` as `Tab` will be removed in the new architecture. |
| OLD | NEW |