Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/clean/chrome/browser/ui/web_contents/web_contents_mediator.h" | 5 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/scoped_observer.h" | |
| 8 #include "ios/chrome/browser/chrome_url_constants.h" | 9 #include "ios/chrome/browser/chrome_url_constants.h" |
| 9 #import "ios/chrome/browser/web_state_list/web_state_list.h" | 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" | 11 #import "ios/chrome/browser/web_state_list/web_state_list_observer_bridge.h" |
| 11 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_consumer.h" | 12 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_consumer.h" |
| 12 #import "ios/web/public/navigation_manager.h" | 13 #import "ios/web/public/navigation_manager.h" |
| 13 #include "ios/web/public/web_state/web_state.h" | 14 #include "ios/web/public/web_state/web_state.h" |
| 14 #include "ui/base/page_transition_types.h" | 15 #include "ui/base/page_transition_types.h" |
| 15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 16 | 17 |
| 17 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 18 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 @interface WebContentsMediator ()<WebStateListObserving> | 22 @interface WebContentsMediator ()<WebStateListObserving> |
| 22 @end | 23 @end |
| 23 | 24 |
| 24 @implementation WebContentsMediator { | 25 @implementation WebContentsMediator { |
| 25 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; | 26 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 27 std::unique_ptr<ScopedObserver<WebStateList, WebStateListObserverBridge>> | |
| 28 _scopedWebStateListObserver; | |
| 26 } | 29 } |
| 27 @synthesize webStateList = _webStateList; | 30 @synthesize webStateList = _webStateList; |
| 28 @synthesize consumer = _consumer; | 31 @synthesize consumer = _consumer; |
| 29 | 32 |
| 30 - (void)dealloc { | 33 - (void)dealloc { |
| 31 [self disconnect]; | 34 [self disconnect]; |
| 32 } | 35 } |
| 33 | 36 |
| 34 #pragma mark - Public | 37 #pragma mark - Public |
| 35 | 38 |
| 36 - (void)disconnect { | 39 - (void)disconnect { |
| 37 if (!self.webStateList) { | 40 if (!self.webStateList) { |
| 38 return; | 41 return; |
| 39 } | 42 } |
| 40 [self disableWebUsage:self.webStateList->GetActiveWebState()]; | 43 [self disableWebUsage:self.webStateList->GetActiveWebState()]; |
| 41 self.webStateList = nullptr; | 44 self.webStateList = nullptr; |
| 42 } | 45 } |
| 43 | 46 |
| 44 #pragma mark - Properties | 47 #pragma mark - Properties |
| 45 | 48 |
| 46 - (void)setWebStateList:(WebStateList*)webStateList { | 49 - (void)setWebStateList:(WebStateList*)webStateList { |
| 47 if (_webStateList) { | 50 _scopedWebStateListObserver.reset(); |
|
sdefresne
2017/04/20 10:02:07
ditto, use RemoveAll/Add to avoid creating destroy
lpromero
2017/04/20 16:25:13
Done.
| |
| 48 _webStateList->RemoveObserver(_webStateListObserver.get()); | 51 _webStateListObserver.reset(); |
|
sdefresne
2017/04/20 10:02:07
Why don't we call "[self disableWebUsage:self.webS
lpromero
2017/04/20 16:25:13
No idea. I filed http://crbug.com/713757.
| |
| 49 _webStateListObserver.reset(); | |
| 50 } | |
| 51 _webStateList = webStateList; | 52 _webStateList = webStateList; |
| 52 if (!_webStateList) { | 53 if (!_webStateList) { |
| 53 return; | 54 return; |
| 54 } | 55 } |
| 55 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 56 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 56 _webStateList->AddObserver(_webStateListObserver.get()); | 57 _scopedWebStateListObserver = base::MakeUnique< |
| 58 ScopedObserver<WebStateList, WebStateListObserverBridge>>( | |
| 59 _webStateListObserver.get()); | |
| 60 _scopedWebStateListObserver->Add(_webStateList); | |
| 57 if (_webStateList->GetActiveWebState()) { | 61 if (_webStateList->GetActiveWebState()) { |
| 58 [self updateConsumerWithWebState:_webStateList->GetActiveWebState()]; | 62 [self updateConsumerWithWebState:_webStateList->GetActiveWebState()]; |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 - (void)setConsumer:(id<WebContentsConsumer>)consumer { | 66 - (void)setConsumer:(id<WebContentsConsumer>)consumer { |
| 63 _consumer = consumer; | 67 _consumer = consumer; |
| 64 if (self.webStateList && self.webStateList->GetActiveWebState()) { | 68 if (self.webStateList && self.webStateList->GetActiveWebState()) { |
| 65 [self updateConsumerWithWebState:self.webStateList->GetActiveWebState()]; | 69 [self updateConsumerWithWebState:self.webStateList->GetActiveWebState()]; |
| 66 } | 70 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 // PLACEHOLDER: This navigates an empty webstate to the NTP. | 107 // PLACEHOLDER: This navigates an empty webstate to the NTP. |
| 104 - (void)navigateToDefaultPage:(web::WebState*)webState { | 108 - (void)navigateToDefaultPage:(web::WebState*)webState { |
| 105 if (!webState->GetNavigationManager()->GetItemCount()) { | 109 if (!webState->GetNavigationManager()->GetItemCount()) { |
| 106 web::NavigationManager::WebLoadParams params((GURL(kChromeUINewTabURL))); | 110 web::NavigationManager::WebLoadParams params((GURL(kChromeUINewTabURL))); |
| 107 params.transition_type = ui::PAGE_TRANSITION_TYPED; | 111 params.transition_type = ui::PAGE_TRANSITION_TYPED; |
| 108 webState->GetNavigationManager()->LoadURLWithParams(params); | 112 webState->GetNavigationManager()->LoadURLWithParams(params); |
| 109 } | 113 } |
| 110 } | 114 } |
| 111 | 115 |
| 112 @end | 116 @end |
| OLD | NEW |