|
9 | 9 | #include <vector>
|
10 | 10 |
|
11 | 11 | #include "base/bind.h"
|
| 12 | +#include "base/callback_helpers.h" |
12 | 13 | #include "base/logging.h"
|
13 | 14 | #import "base/mac/foundation_util.h"
|
14 | 15 | #include "base/metrics/histogram_macros.h"
|
|
42 | 43 | #import "ios/chrome/browser/tabs/tab_model_selected_tab_observer.h"
|
43 | 44 | #import "ios/chrome/browser/tabs/tab_model_synced_window_delegate.h"
|
44 | 45 | #import "ios/chrome/browser/tabs/tab_model_web_state_list_delegate.h"
|
| 46 | +#import "ios/chrome/browser/tabs/tab_model_web_usage_enabled_observer.h" |
45 | 47 | #import "ios/chrome/browser/tabs/tab_parenting_observer.h"
|
46 | 48 | #import "ios/chrome/browser/web/page_placeholder_tab_helper.h"
|
47 | 49 | #import "ios/chrome/browser/web_state_list/web_state_list.h"
|
@@ -156,6 +158,9 @@ @interface TabModel () {
|
156 | 158 | // The delegate for sync.
|
157 | 159 | std::unique_ptr<TabModelSyncedWindowDelegate> _syncedWindowDelegate;
|
158 | 160 |
|
| 161 | + // The observer that sends kTabModelNewTabWillOpenNotification notifications. |
| 162 | + TabModelNotificationObserver* _tabModelNotificationObserver; |
| 163 | + |
159 | 164 | // Counters for metrics.
|
160 | 165 | WebStateListMetricsObserver* _webStateListMetricsObserver;
|
161 | 166 |
|
@@ -313,7 +318,12 @@ - (instancetype)initWithSessionWindow:(SessionWindowIOS*)window
|
313 | 318 | _webStateListObservers.push_back(std::move(webStateListMetricsObserver));
|
314 | 319 |
|
315 | 320 | _webStateListObservers.push_back(
|
316 |
| - base::MakeUnique<TabModelNotificationObserver>(self)); |
| 321 | + base::MakeUnique<TabModelWebUsageEnabledObserver>(self)); |
| 322 | + |
| 323 | + auto tabModelNotificationObserver = |
| 324 | + base::MakeUnique<TabModelNotificationObserver>(self); |
| 325 | + _tabModelNotificationObserver = tabModelNotificationObserver.get(); |
| 326 | + _webStateListObservers.push_back(std::move(tabModelNotificationObserver)); |
317 | 327 |
|
318 | 328 | for (const auto& webStateListObserver : _webStateListObservers)
|
319 | 329 | _webStateList->AddObserver(webStateListObserver.get());
|
@@ -569,7 +579,8 @@ - (void)browserStateDestroyed {
|
569 | 579 | UnregisterTabModelFromChromeBrowserState(_browserState, self);
|
570 | 580 | _browserState = nullptr;
|
571 | 581 |
|
572 |
| - // Clear weak pointer to WebStateListMetricsObserver before destroying it. |
| 582 | + // Clear weak pointer to observers before destroying them. |
| 583 | + _tabModelNotificationObserver = nullptr; |
573 | 584 | _webStateListMetricsObserver = nullptr;
|
574 | 585 |
|
575 | 586 | // Close all tabs. Do this in an @autoreleasepool as WebStateList observers
|
@@ -643,6 +654,16 @@ - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window
|
643 | 654 | DCHECK(_browserState);
|
644 | 655 | DCHECK(window);
|
645 | 656 |
|
| 657 | + // Disable sending the kTabModelNewTabWillOpenNotification notification |
| 658 | + // while restoring a session as it breaks the BVC (see crbug.com/763964). |
| 659 | + base::ScopedClosureRunner enableTabModelNotificationObserver; |
| 660 | + if (_tabModelNotificationObserver) { |
| 661 | + _tabModelNotificationObserver->SetDisabled(true); |
| 662 | + enableTabModelNotificationObserver.ReplaceClosure( |
| 663 | + base::BindOnce(&TabModelNotificationObserver::SetDisabled, |
| 664 | + base::Unretained(_tabModelNotificationObserver), false)); |
| 665 | + } |
| 666 | + |
646 | 667 | if (!window.sessions.count)
|
647 | 668 | return NO;
|
648 | 669 |
|
@@ -695,6 +716,7 @@ - (BOOL)restoreSessionWindow:(SessionWindowIOS*)window
|
695 | 716 | _tabUsageRecorder->InitialRestoredTabs(self.currentTab.webState,
|
696 | 717 | restoredWebStates);
|
697 | 718 | }
|
| 719 | + |
698 | 720 | return closedNTPTab;
|
699 | 721 | }
|
700 | 722 |
|
|
0 commit comments