Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(839)

Side by Side Diff: ios/chrome/browser/tabs/tab_model.mm

Issue 2885803002: [ios] Remove TabModel cleanup from -dealloc to -browserStateDestroyed. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/chrome/browser/tabs/tab.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 184
185 @implementation TabModel 185 @implementation TabModel
186 186
187 @synthesize browserState = _browserState; 187 @synthesize browserState = _browserState;
188 @synthesize sessionID = _sessionID; 188 @synthesize sessionID = _sessionID;
189 @synthesize webUsageEnabled = webUsageEnabled_; 189 @synthesize webUsageEnabled = webUsageEnabled_;
190 190
191 #pragma mark - Overriden 191 #pragma mark - Overriden
192 192
193 - (void)dealloc { 193 - (void)dealloc {
194 // Make sure the tabs do clean after themselves. It is important for
195 // removeObserver: to be called first otherwise a lot of unecessary work will
rohitrao (ping after 24h) 2017/05/16 13:28:10 We are changing the semantics here. It seems like
196 // happen on -closeAllTabs.
197 DCHECK([_observers empty]);
198
199 // browserStateDestroyed should always have been called before destruction. 194 // browserStateDestroyed should always have been called before destruction.
200 DCHECK(!_browserState); 195 DCHECK(!_browserState);
201 196
202 [[NSNotificationCenter defaultCenter] removeObserver:self]; 197 // Make sure the observers do clean after themselves.
203 198 DCHECK([_observers empty]);
204 // Clear weak pointer to WebStateListMetricsObserver before destroying it.
205 _webStateListMetricsObserver = nullptr;
206
207 // Close all tabs. Do this in an @autoreleasepool as WebStateList observers
208 // will be notified (they are unregistered later). As some of them may be
209 // implemented in Objective-C and unregister themselves in their -dealloc
210 // method, ensure they -autorelease introduced by ARC are processed before
211 // the WebStateList destructor is called.
212 @autoreleasepool {
213 [self closeAllTabs];
214 }
215
216 // Unregister all observers after closing all the tabs as some of them are
217 // required to properly clean up the Tabs.
218 for (const auto& webStateListObserver : _webStateListObservers)
219 _webStateList->RemoveObserver(webStateListObserver.get());
220 _webStateListObservers.clear();
221 _retainedWebStateListObservers = nil;
222
223 _clearPoliciesTaskTracker.TryCancelAll();
224 } 199 }
225 200
226 #pragma mark - Public methods 201 #pragma mark - Public methods
227 202
228 - (Tab*)currentTab { 203 - (Tab*)currentTab {
229 web::WebState* webState = _webStateList->GetActiveWebState(); 204 web::WebState* webState = _webStateList->GetActiveWebState();
230 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil; 205 return webState ? LegacyTabHelper::GetTabForWebState(webState) : nil;
231 } 206 }
232 207
233 - (void)setCurrentTab:(Tab*)newTab { 208 - (void)setCurrentTab:(Tab*)newTab {
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 if (UrlIsExternalFileReference(URL)) { 599 if (UrlIsExternalFileReference(URL)) {
625 NSString* fileName = base::SysUTF8ToNSString(URL.ExtractFileName()); 600 NSString* fileName = base::SysUTF8ToNSString(URL.ExtractFileName());
626 [referencedFiles addObject:fileName]; 601 [referencedFiles addObject:fileName];
627 } 602 }
628 } 603 }
629 return referencedFiles; 604 return referencedFiles;
630 } 605 }
631 606
632 // NOTE: This can be called multiple times, so must be robust against that. 607 // NOTE: This can be called multiple times, so must be robust against that.
633 - (void)browserStateDestroyed { 608 - (void)browserStateDestroyed {
609 if (!_browserState)
610 return;
611
634 [[NSNotificationCenter defaultCenter] removeObserver:self]; 612 [[NSNotificationCenter defaultCenter] removeObserver:self];
635 if (_browserState) { 613 UnregisterTabModelFromChromeBrowserState(_browserState, self);
636 UnregisterTabModelFromChromeBrowserState(_browserState, self); 614 _browserState = nullptr;
615
616 // Clear weak pointer to WebStateListMetricsObserver before destroying it.
617 _webStateListMetricsObserver = nullptr;
618
619 // Close all tabs. Do this in an @autoreleasepool as WebStateList observers
620 // will be notified (they are unregistered later). As some of them may be
621 // implemented in Objective-C and unregister themselves in their -dealloc
622 // method, ensure they -autorelease introduced by ARC are processed before
623 // the WebStateList destructor is called.
624 @autoreleasepool {
625 [self closeAllTabs];
637 } 626 }
638 _browserState = nullptr; 627
628 // Unregister all observers after closing all the tabs as some of them are
629 // required to properly clean up the Tabs.
630 for (const auto& webStateListObserver : _webStateListObservers)
631 _webStateList->RemoveObserver(webStateListObserver.get());
632 _webStateListObservers.clear();
633 _retainedWebStateListObservers = nil;
634
635 _clearPoliciesTaskTracker.TryCancelAll();
639 } 636 }
640 637
641 - (void)navigationCommittedInTab:(Tab*)tab 638 - (void)navigationCommittedInTab:(Tab*)tab
642 previousItem:(web::NavigationItem*)previousItem { 639 previousItem:(web::NavigationItem*)previousItem {
643 if (self.offTheRecord) 640 if (self.offTheRecord)
644 return; 641 return;
645 if (![tab navigationManager]) 642 if (![tab navigationManager])
646 return; 643 return;
647 644
648 // See if the navigation was within a page; if so ignore it. 645 // See if the navigation was within a page; if so ignore it.
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 } 785 }
789 786
790 // Called when UIApplicationWillEnterForegroundNotification is received. 787 // Called when UIApplicationWillEnterForegroundNotification is received.
791 - (void)applicationWillEnterForeground:(NSNotification*)notify { 788 - (void)applicationWillEnterForeground:(NSNotification*)notify {
792 if (_tabUsageRecorder) { 789 if (_tabUsageRecorder) {
793 _tabUsageRecorder->AppWillEnterForeground(); 790 _tabUsageRecorder->AppWillEnterForeground();
794 } 791 }
795 } 792 }
796 793
797 @end 794 @end
OLDNEW
« no previous file with comments | « ios/chrome/browser/tabs/tab.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698