| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/main/browser_view_wrangler.h" | 5 #import "ios/chrome/browser/ui/main/browser_view_wrangler.h" |
| 6 | 6 |
| 7 #include "base/mac/objc_property_releaser.h" | |
| 8 #import "base/mac/scoped_nsobject.h" | |
| 9 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ios/chrome/browser/application_context.h" | 8 #include "ios/chrome/browser/application_context.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 9 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 #import "ios/chrome/browser/browsing_data/browsing_data_removal_controller.h" | 10 #import "ios/chrome/browser/browsing_data/browsing_data_removal_controller.h" |
| 13 #include "ios/chrome/browser/browsing_data/ios_chrome_browsing_data_remover.h" | 11 #include "ios/chrome/browser/browsing_data/ios_chrome_browsing_data_remover.h" |
| 14 #include "ios/chrome/browser/crash_report/crash_report_helper.h" | 12 #include "ios/chrome/browser/crash_report/crash_report_helper.h" |
| 15 #import "ios/chrome/browser/device_sharing/device_sharing_manager.h" | 13 #import "ios/chrome/browser/device_sharing/device_sharing_manager.h" |
| 16 #import "ios/chrome/browser/physical_web/start_physical_web_discovery.h" | 14 #import "ios/chrome/browser/physical_web/start_physical_web_discovery.h" |
| 17 #import "ios/chrome/browser/sessions/session_ios.h" | 15 #import "ios/chrome/browser/sessions/session_ios.h" |
| 18 #import "ios/chrome/browser/sessions/session_service_ios.h" | 16 #import "ios/chrome/browser/sessions/session_service_ios.h" |
| 19 #import "ios/chrome/browser/sessions/session_window_ios.h" | 17 #import "ios/chrome/browser/sessions/session_window_ios.h" |
| 20 #import "ios/chrome/browser/tabs/tab.h" | 18 #import "ios/chrome/browser/tabs/tab.h" |
| 21 #import "ios/chrome/browser/tabs/tab_model.h" | 19 #import "ios/chrome/browser/tabs/tab_model.h" |
| 22 #import "ios/chrome/browser/tabs/tab_model_observer.h" | 20 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
| 23 #import "ios/chrome/browser/ui/browser_view_controller.h" | 21 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 24 #import "ios/chrome/browser/ui/browser_view_controller_dependency_factory.h" | 22 #import "ios/chrome/browser/ui/browser_view_controller_dependency_factory.h" |
| 25 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 23 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 26 | 24 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." |
| 27 #endif |
| 28 |
| 27 @interface BrowserViewWrangler ()<TabModelObserver> { | 29 @interface BrowserViewWrangler ()<TabModelObserver> { |
| 28 ios::ChromeBrowserState* _browserState; | 30 ios::ChromeBrowserState* _browserState; |
| 29 __unsafe_unretained id<TabModelObserver> _tabModelObserver; | 31 __unsafe_unretained id<TabModelObserver> _tabModelObserver; |
| 30 BOOL _isShutdown; | 32 BOOL _isShutdown; |
| 31 | |
| 32 base::mac::ObjCPropertyReleaser _propertyReleaser_BrowserViewWrangler; | |
| 33 } | 33 } |
| 34 | 34 |
| 35 // Responsible for maintaining all state related to sharing to other devices. | 35 // Responsible for maintaining all state related to sharing to other devices. |
| 36 // Redeclared readwrite from the readonly declaration in the Testing interface. | 36 // Redeclared readwrite from the readonly declaration in the Testing interface. |
| 37 @property(nonatomic, retain, readwrite) | 37 @property(nonatomic, strong, readwrite) |
| 38 DeviceSharingManager* deviceSharingManager; | 38 DeviceSharingManager* deviceSharingManager; |
| 39 | 39 |
| 40 // Creates a new autoreleased tab model for |browserState|; if |empty| is NO, | 40 // Creates a new autoreleased tab model for |browserState|; if |empty| is NO, |
| 41 // then any existing tabs that have been saved for |browserState| will be | 41 // then any existing tabs that have been saved for |browserState| will be |
| 42 // loaded; otherwise, the tab model will be created empty. | 42 // loaded; otherwise, the tab model will be created empty. |
| 43 - (TabModel*)tabModelForBrowserState:(ios::ChromeBrowserState*)browserState | 43 - (TabModel*)tabModelForBrowserState:(ios::ChromeBrowserState*)browserState |
| 44 empty:(BOOL)empty; | 44 empty:(BOOL)empty; |
| 45 | 45 |
| 46 // Creates a new off-the-record ("incognito") browser state for |_browserState|, | 46 // Creates a new off-the-record ("incognito") browser state for |_browserState|, |
| 47 // then calls -tabModelForBrowserState:empty: and returns the (autoreleased) | 47 // then calls -tabModelForBrowserState:empty: and returns the (autoreleased) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 62 @synthesize mainTabModel = _mainTabModel; | 62 @synthesize mainTabModel = _mainTabModel; |
| 63 @synthesize otrBVC = _otrBVC; | 63 @synthesize otrBVC = _otrBVC; |
| 64 @synthesize otrTabModel = _otrTabModel; | 64 @synthesize otrTabModel = _otrTabModel; |
| 65 @synthesize currentBVC = _currentBVC; | 65 @synthesize currentBVC = _currentBVC; |
| 66 // Private properies. | 66 // Private properies. |
| 67 @synthesize deviceSharingManager = _deviceSharingManager; | 67 @synthesize deviceSharingManager = _deviceSharingManager; |
| 68 | 68 |
| 69 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | 69 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState |
| 70 tabModelObserver:(id<TabModelObserver>)tabModelObserver { | 70 tabModelObserver:(id<TabModelObserver>)tabModelObserver { |
| 71 if ((self = [super init])) { | 71 if ((self = [super init])) { |
| 72 _propertyReleaser_BrowserViewWrangler.Init(self, | |
| 73 [BrowserViewWrangler class]); | |
| 74 _browserState = browserState; | 72 _browserState = browserState; |
| 75 _tabModelObserver = tabModelObserver; | 73 _tabModelObserver = tabModelObserver; |
| 76 } | 74 } |
| 77 return self; | 75 return self; |
| 78 } | 76 } |
| 79 | 77 |
| 80 - (instancetype)init { | 78 - (instancetype)init { |
| 81 NOTREACHED(); | 79 NOTREACHED(); |
| 82 return nil; | 80 return nil; |
| 83 } | 81 } |
| 84 | 82 |
| 85 - (void)dealloc { | 83 - (void)dealloc { |
| 86 DCHECK(_isShutdown) << "-shutdown must be called before -dealloc"; | 84 DCHECK(_isShutdown) << "-shutdown must be called before -dealloc"; |
| 87 [super dealloc]; | |
| 88 } | 85 } |
| 89 | 86 |
| 90 #pragma mark - BrowserViewInformation property implementations | 87 #pragma mark - BrowserViewInformation property implementations |
| 91 | 88 |
| 92 - (BrowserViewController*)mainBVC { | 89 - (BrowserViewController*)mainBVC { |
| 93 if (!_mainBVC) { | 90 if (!_mainBVC) { |
| 94 // |_browserState| should always be set before trying to create | 91 // |_browserState| should always be set before trying to create |
| 95 // |_mainBVC|. | 92 // |_mainBVC|. |
| 96 DCHECK(_browserState); | 93 DCHECK(_browserState); |
| 97 self.mainBVC = | 94 self.mainBVC = |
| 98 [self bvcForBrowserState:_browserState tabModel:self.mainTabModel]; | 95 [self bvcForBrowserState:_browserState tabModel:self.mainTabModel]; |
| 99 DCHECK(_mainBVC); | 96 DCHECK(_mainBVC); |
| 100 } | 97 } |
| 101 return _mainBVC; | 98 return _mainBVC; |
| 102 } | 99 } |
| 103 | 100 |
| 104 - (void)setMainBVC:(BrowserViewController*)mainBVC { | 101 - (void)setMainBVC:(BrowserViewController*)mainBVC { |
| 105 if (_mainBVC == mainBVC) | 102 if (_mainBVC == mainBVC) |
| 106 return; | 103 return; |
| 107 | 104 |
| 108 if (_mainBVC) { | 105 if (_mainBVC) { |
| 109 [_mainBVC browserStateDestroyed]; | 106 [_mainBVC browserStateDestroyed]; |
| 110 [_mainBVC shutdown]; | 107 [_mainBVC shutdown]; |
| 111 [_mainBVC autorelease]; | |
| 112 } | 108 } |
| 113 | 109 |
| 114 _mainBVC = [mainBVC retain]; | 110 _mainBVC = mainBVC; |
| 115 } | 111 } |
| 116 | 112 |
| 117 - (TabModel*)mainTabModel { | 113 - (TabModel*)mainTabModel { |
| 118 if (!_mainTabModel) { | 114 if (!_mainTabModel) { |
| 119 self.mainTabModel = [self tabModelForBrowserState:_browserState empty:NO]; | 115 self.mainTabModel = [self tabModelForBrowserState:_browserState empty:NO]; |
| 120 // Follow loaded URLs in the main tab model to send those in case of | 116 // Follow loaded URLs in the main tab model to send those in case of |
| 121 // crashes. | 117 // crashes. |
| 122 ios_internal::breakpad::MonitorURLsForTabModel(_mainTabModel); | 118 ios_internal::breakpad::MonitorURLsForTabModel(_mainTabModel); |
| 123 ios::GetChromeBrowserProvider()->InitializeCastService(_mainTabModel); | 119 ios::GetChromeBrowserProvider()->InitializeCastService(_mainTabModel); |
| 124 } | 120 } |
| 125 return _mainTabModel; | 121 return _mainTabModel; |
| 126 } | 122 } |
| 127 | 123 |
| 128 - (void)setMainTabModel:(TabModel*)mainTabModel { | 124 - (void)setMainTabModel:(TabModel*)mainTabModel { |
| 129 if (_mainTabModel == mainTabModel) | 125 if (_mainTabModel == mainTabModel) |
| 130 return; | 126 return; |
| 131 | 127 |
| 132 if (_mainTabModel) { | 128 if (_mainTabModel) { |
| 133 [_mainTabModel browserStateDestroyed]; | 129 [_mainTabModel browserStateDestroyed]; |
| 134 if (_tabModelObserver) { | 130 if (_tabModelObserver) { |
| 135 [_mainTabModel removeObserver:_tabModelObserver]; | 131 [_mainTabModel removeObserver:_tabModelObserver]; |
| 136 } | 132 } |
| 137 [_mainTabModel removeObserver:self]; | 133 [_mainTabModel removeObserver:self]; |
| 138 [_mainTabModel autorelease]; | |
| 139 } | 134 } |
| 140 | 135 |
| 141 _mainTabModel = [mainTabModel retain]; | 136 _mainTabModel = mainTabModel; |
| 142 } | 137 } |
| 143 | 138 |
| 144 - (BrowserViewController*)otrBVC { | 139 - (BrowserViewController*)otrBVC { |
| 145 if (!_otrBVC) { | 140 if (!_otrBVC) { |
| 146 // |_browserState| should always be set before trying to create | 141 // |_browserState| should always be set before trying to create |
| 147 // |_otrBVC|. | 142 // |_otrBVC|. |
| 148 DCHECK(_browserState); | 143 DCHECK(_browserState); |
| 149 ios::ChromeBrowserState* otrBrowserState = | 144 ios::ChromeBrowserState* otrBrowserState = |
| 150 _browserState->GetOffTheRecordChromeBrowserState(); | 145 _browserState->GetOffTheRecordChromeBrowserState(); |
| 151 DCHECK(otrBrowserState); | 146 DCHECK(otrBrowserState); |
| 152 self.otrBVC = | 147 self.otrBVC = |
| 153 [self bvcForBrowserState:otrBrowserState tabModel:self.otrTabModel]; | 148 [self bvcForBrowserState:otrBrowserState tabModel:self.otrTabModel]; |
| 154 DCHECK(_otrBVC); | 149 DCHECK(_otrBVC); |
| 155 } | 150 } |
| 156 return _otrBVC; | 151 return _otrBVC; |
| 157 } | 152 } |
| 158 | 153 |
| 159 - (void)setOtrBVC:(BrowserViewController*)otrBVC { | 154 - (void)setOtrBVC:(BrowserViewController*)otrBVC { |
| 160 if (_otrBVC == otrBVC) | 155 if (_otrBVC == otrBVC) |
| 161 return; | 156 return; |
| 162 | 157 |
| 163 if (_otrBVC) { | 158 if (_otrBVC) { |
| 164 [_otrBVC browserStateDestroyed]; | 159 [_otrBVC browserStateDestroyed]; |
| 165 [_otrBVC shutdown]; | 160 [_otrBVC shutdown]; |
| 166 [_otrBVC autorelease]; | |
| 167 } | 161 } |
| 168 | 162 |
| 169 _otrBVC = [otrBVC retain]; | 163 _otrBVC = otrBVC; |
| 170 } | 164 } |
| 171 | 165 |
| 172 - (TabModel*)otrTabModel { | 166 - (TabModel*)otrTabModel { |
| 173 if (!_otrTabModel) { | 167 if (!_otrTabModel) { |
| 174 self.otrTabModel = [self buildOtrTabModel:NO]; | 168 self.otrTabModel = [self buildOtrTabModel:NO]; |
| 175 } | 169 } |
| 176 return _otrTabModel; | 170 return _otrTabModel; |
| 177 } | 171 } |
| 178 | 172 |
| 179 - (void)setOtrTabModel:(TabModel*)otrTabModel { | 173 - (void)setOtrTabModel:(TabModel*)otrTabModel { |
| 180 if (_otrTabModel == otrTabModel) | 174 if (_otrTabModel == otrTabModel) |
| 181 return; | 175 return; |
| 182 | 176 |
| 183 if (_otrTabModel) { | 177 if (_otrTabModel) { |
| 184 [_otrTabModel browserStateDestroyed]; | 178 [_otrTabModel browserStateDestroyed]; |
| 185 if (_tabModelObserver) { | 179 if (_tabModelObserver) { |
| 186 [_otrTabModel removeObserver:_tabModelObserver]; | 180 [_otrTabModel removeObserver:_tabModelObserver]; |
| 187 } | 181 } |
| 188 [_otrTabModel removeObserver:self]; | 182 [_otrTabModel removeObserver:self]; |
| 189 [_otrTabModel autorelease]; | |
| 190 } | 183 } |
| 191 | 184 |
| 192 _otrTabModel = [otrTabModel retain]; | 185 _otrTabModel = otrTabModel; |
| 193 } | 186 } |
| 194 | 187 |
| 195 - (void)setCurrentBVC:(BrowserViewController*)bvc | 188 - (void)setCurrentBVC:(BrowserViewController*)bvc |
| 196 storageSwitcher:(id<BrowserStateStorageSwitching>)storageSwitcher { | 189 storageSwitcher:(id<BrowserStateStorageSwitching>)storageSwitcher { |
| 197 DCHECK(bvc != nil); | 190 DCHECK(bvc != nil); |
| 198 // |bvc| should be one of the BrowserViewControllers this class already owns. | 191 // |bvc| should be one of the BrowserViewControllers this class already owns. |
| 199 DCHECK(_mainBVC == bvc || _otrBVC == bvc); | 192 DCHECK(_mainBVC == bvc || _otrBVC == bvc); |
| 200 if (self.currentBVC == bvc) { | 193 if (self.currentBVC == bvc) { |
| 201 return; | 194 return; |
| 202 } | 195 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 } | 245 } |
| 253 | 246 |
| 254 - (void)tabModel:(TabModel*)model didChangeTab:(Tab*)tab { | 247 - (void)tabModel:(TabModel*)model didChangeTab:(Tab*)tab { |
| 255 [self updateDeviceSharingManager]; | 248 [self updateDeviceSharingManager]; |
| 256 } | 249 } |
| 257 | 250 |
| 258 #pragma mark - Other public methods | 251 #pragma mark - Other public methods |
| 259 | 252 |
| 260 - (void)updateDeviceSharingManager { | 253 - (void)updateDeviceSharingManager { |
| 261 if (!self.deviceSharingManager) { | 254 if (!self.deviceSharingManager) { |
| 262 self.deviceSharingManager = | 255 self.deviceSharingManager = [[DeviceSharingManager alloc] init]; |
| 263 [[[DeviceSharingManager alloc] init] autorelease]; | |
| 264 } | 256 } |
| 265 [self.deviceSharingManager updateBrowserState:_browserState]; | 257 [self.deviceSharingManager updateBrowserState:_browserState]; |
| 266 | 258 |
| 267 GURL activeURL; | 259 GURL activeURL; |
| 268 Tab* currentTab = [self.currentBVC tabModel].currentTab; | 260 Tab* currentTab = [self.currentBVC tabModel].currentTab; |
| 269 // Set the active URL if there's a current tab and the current BVC is not OTR. | 261 // Set the active URL if there's a current tab and the current BVC is not OTR. |
| 270 if (currentTab && self.currentBVC != self.otrBVC) { | 262 if (currentTab && self.currentBVC != self.otrBVC) { |
| 271 activeURL = currentTab.visibleURL; | 263 activeURL = currentTab.visibleURL; |
| 272 } | 264 } |
| 273 [self.deviceSharingManager updateActiveURL:activeURL]; | 265 [self.deviceSharingManager updateActiveURL:activeURL]; |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 SessionIOS* session = | 360 SessionIOS* session = |
| 369 [[SessionServiceIOS sharedService] loadSessionFromDirectory:statePath]; | 361 [[SessionServiceIOS sharedService] loadSessionFromDirectory:statePath]; |
| 370 if (session) { | 362 if (session) { |
| 371 DCHECK_EQ(session.sessionWindows.count, 1u); | 363 DCHECK_EQ(session.sessionWindows.count, 1u); |
| 372 sessionWindow = session.sessionWindows[0]; | 364 sessionWindow = session.sessionWindows[0]; |
| 373 } | 365 } |
| 374 } | 366 } |
| 375 | 367 |
| 376 // Create tab model from saved session (nil is ok). | 368 // Create tab model from saved session (nil is ok). |
| 377 TabModel* tabModel = | 369 TabModel* tabModel = |
| 378 [[[TabModel alloc] initWithSessionWindow:sessionWindow | 370 [[TabModel alloc] initWithSessionWindow:sessionWindow |
| 379 sessionService:[SessionServiceIOS sharedService] | 371 sessionService:[SessionServiceIOS sharedService] |
| 380 browserState:browserState] autorelease]; | 372 browserState:browserState]; |
| 381 // Add observers. | 373 // Add observers. |
| 382 if (_tabModelObserver) { | 374 if (_tabModelObserver) { |
| 383 [tabModel addObserver:_tabModelObserver]; | 375 [tabModel addObserver:_tabModelObserver]; |
| 384 [tabModel addObserver:self]; | 376 [tabModel addObserver:self]; |
| 385 } | 377 } |
| 386 ios_internal::breakpad::MonitorTabStateForTabModel(tabModel); | 378 ios_internal::breakpad::MonitorTabStateForTabModel(tabModel); |
| 387 | 379 |
| 388 return tabModel; | 380 return tabModel; |
| 389 } | 381 } |
| 390 | 382 |
| 391 - (BrowserViewController*)bvcForBrowserState: | 383 - (BrowserViewController*)bvcForBrowserState: |
| 392 (ios::ChromeBrowserState*)browserState | 384 (ios::ChromeBrowserState*)browserState |
| 393 tabModel:(TabModel*)tabModel { | 385 tabModel:(TabModel*)tabModel { |
| 394 base::scoped_nsobject<BrowserViewControllerDependencyFactory> factory( | 386 BrowserViewControllerDependencyFactory* factory = |
| 395 [[BrowserViewControllerDependencyFactory alloc] | 387 [[BrowserViewControllerDependencyFactory alloc] |
| 396 initWithBrowserState:browserState]); | 388 initWithBrowserState:browserState]; |
| 397 return [[[BrowserViewController alloc] initWithTabModel:tabModel | 389 return [[BrowserViewController alloc] initWithTabModel:tabModel |
| 398 browserState:browserState | 390 browserState:browserState |
| 399 dependencyFactory:factory] autorelease]; | 391 dependencyFactory:factory]; |
| 400 } | 392 } |
| 401 | 393 |
| 402 @end | 394 @end |
| OLD | NEW |