OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/tabs/tab_model.h" | 5 #import "ios/chrome/browser/tabs/tab_model.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 | 197 |
198 @implementation TabModel | 198 @implementation TabModel |
199 | 199 |
200 @synthesize browserState = _browserState; | 200 @synthesize browserState = _browserState; |
201 @synthesize sessionID = _sessionID; | 201 @synthesize sessionID = _sessionID; |
202 @synthesize webUsageEnabled = _webUsageEnabled; | 202 @synthesize webUsageEnabled = _webUsageEnabled; |
203 | 203 |
204 #pragma mark - Overriden | 204 #pragma mark - Overriden |
205 | 205 |
206 - (void)dealloc { | 206 - (void)dealloc { |
207 // Make sure the tabs do clean after themselves. It is important for | |
208 // removeObserver: to be called first otherwise a lot of unecessary work will | |
209 // happen on -closeAllTabs. | |
210 DCHECK([_observers empty]); | |
211 | |
212 // browserStateDestroyed should always have been called before destruction. | 207 // browserStateDestroyed should always have been called before destruction. |
213 DCHECK(!_browserState); | 208 DCHECK(!_browserState); |
214 | 209 |
215 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 210 // Make sure the observers do clean after themselves. |
216 | 211 DCHECK([_observers empty]); |
217 // Clear weak pointer to WebStateListMetricsObserver before destroying it. | |
218 _webStateListMetricsObserver = nullptr; | |
219 | |
220 // Close all tabs. Do this in an @autoreleasepool as WebStateList observers | |
221 // will be notified (they are unregistered later). As some of them may be | |
222 // implemented in Objective-C and unregister themselves in their -dealloc | |
223 // method, ensure they -autorelease introduced by ARC are processed before | |
224 // the WebStateList destructor is called. | |
225 @autoreleasepool { | |
226 [self closeAllTabs]; | |
227 } | |
228 | |
229 // Unregister all observers after closing all the tabs as some of them are | |
230 // required to properly clean up the Tabs. | |
231 for (const auto& webStateListObserver : _webStateListObservers) | |
232 _webStateList->RemoveObserver(webStateListObserver.get()); | |
233 _webStateListObservers.clear(); | |
234 _retainedWebStateListObservers = nil; | |
235 | |
236 _clearPoliciesTaskTracker.TryCancelAll(); | |
237 } | 212 } |
238 | 213 |
239 #pragma mark - Public methods | 214 #pragma mark - Public methods |
240 | 215 |
241 - (Tab*)currentTab { | 216 - (Tab*)currentTab { |
242 web::WebState* webState = _webStateList->GetActiveWebState(); | 217 web::WebState* webState = _webStateList->GetActiveWebState(); |
243 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil; | 218 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil; |
244 } | 219 } |
245 | 220 |
246 - (void)setCurrentTab:(Tab*)newTab { | 221 - (void)setCurrentTab:(Tab*)newTab { |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 if (UrlIsExternalFileReference(URL)) { | 612 if (UrlIsExternalFileReference(URL)) { |
638 NSString* fileName = base::SysUTF8ToNSString(URL.ExtractFileName()); | 613 NSString* fileName = base::SysUTF8ToNSString(URL.ExtractFileName()); |
639 [referencedFiles addObject:fileName]; | 614 [referencedFiles addObject:fileName]; |
640 } | 615 } |
641 } | 616 } |
642 return referencedFiles; | 617 return referencedFiles; |
643 } | 618 } |
644 | 619 |
645 // NOTE: This can be called multiple times, so must be robust against that. | 620 // NOTE: This can be called multiple times, so must be robust against that. |
646 - (void)browserStateDestroyed { | 621 - (void)browserStateDestroyed { |
| 622 if (!_browserState) |
| 623 return; |
| 624 |
647 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 625 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
648 if (_browserState) { | 626 UnregisterTabModelFromChromeBrowserState(_browserState, self); |
649 UnregisterTabModelFromChromeBrowserState(_browserState, self); | 627 _browserState = nullptr; |
| 628 |
| 629 // Clear weak pointer to WebStateListMetricsObserver before destroying it. |
| 630 _webStateListMetricsObserver = nullptr; |
| 631 |
| 632 // Close all tabs. Do this in an @autoreleasepool as WebStateList observers |
| 633 // will be notified (they are unregistered later). As some of them may be |
| 634 // implemented in Objective-C and unregister themselves in their -dealloc |
| 635 // method, ensure they -autorelease introduced by ARC are processed before |
| 636 // the WebStateList destructor is called. |
| 637 @autoreleasepool { |
| 638 [self closeAllTabs]; |
650 } | 639 } |
651 _browserState = nullptr; | 640 |
| 641 // Unregister all observers after closing all the tabs as some of them are |
| 642 // required to properly clean up the Tabs. |
| 643 for (const auto& webStateListObserver : _webStateListObservers) |
| 644 _webStateList->RemoveObserver(webStateListObserver.get()); |
| 645 _webStateListObservers.clear(); |
| 646 _retainedWebStateListObservers = nil; |
| 647 |
| 648 _clearPoliciesTaskTracker.TryCancelAll(); |
652 } | 649 } |
653 | 650 |
654 - (void)navigationCommittedInTab:(Tab*)tab | 651 - (void)navigationCommittedInTab:(Tab*)tab |
655 previousItem:(web::NavigationItem*)previousItem { | 652 previousItem:(web::NavigationItem*)previousItem { |
656 if (self.offTheRecord) | 653 if (self.offTheRecord) |
657 return; | 654 return; |
658 if (![tab navigationManager]) | 655 if (![tab navigationManager]) |
659 return; | 656 return; |
660 | 657 |
661 // See if the navigation was within a page; if so ignore it. | 658 // See if the navigation was within a page; if so ignore it. |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 } | 797 } |
801 | 798 |
802 // Called when UIApplicationWillEnterForegroundNotification is received. | 799 // Called when UIApplicationWillEnterForegroundNotification is received. |
803 - (void)applicationWillEnterForeground:(NSNotification*)notify { | 800 - (void)applicationWillEnterForeground:(NSNotification*)notify { |
804 if (_tabUsageRecorder) { | 801 if (_tabUsageRecorder) { |
805 _tabUsageRecorder->AppWillEnterForeground(); | 802 _tabUsageRecorder->AppWillEnterForeground(); |
806 } | 803 } |
807 } | 804 } |
808 | 805 |
809 @end | 806 @end |
OLD | NEW |