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 |
| 33 - (instancetype)init { |
| 34 self = [super init]; |
| 35 if (self) { |
| 36 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 37 _scopedWebStateListObserver = base::MakeUnique< |
| 38 ScopedObserver<WebStateList, WebStateListObserverBridge>>( |
| 39 _webStateListObserver.get()); |
| 40 } |
| 41 return self; |
| 42 } |
| 43 |
30 - (void)dealloc { | 44 - (void)dealloc { |
31 [self disconnect]; | 45 [self disconnect]; |
32 } | 46 } |
33 | 47 |
34 #pragma mark - Public | 48 #pragma mark - Public |
35 | 49 |
36 - (void)disconnect { | 50 - (void)disconnect { |
37 if (!self.webStateList) { | 51 if (!self.webStateList) { |
38 return; | 52 return; |
39 } | 53 } |
40 [self disableWebUsage:self.webStateList->GetActiveWebState()]; | 54 [self disableWebUsage:self.webStateList->GetActiveWebState()]; |
41 self.webStateList = nullptr; | 55 self.webStateList = nullptr; |
42 } | 56 } |
43 | 57 |
44 #pragma mark - Properties | 58 #pragma mark - Properties |
45 | 59 |
46 - (void)setWebStateList:(WebStateList*)webStateList { | 60 - (void)setWebStateList:(WebStateList*)webStateList { |
47 if (_webStateList) { | 61 _scopedWebStateListObserver->RemoveAll(); |
48 _webStateList->RemoveObserver(_webStateListObserver.get()); | |
49 _webStateListObserver.reset(); | |
50 } | |
51 _webStateList = webStateList; | 62 _webStateList = webStateList; |
52 if (!_webStateList) { | 63 if (!_webStateList) { |
53 return; | 64 return; |
54 } | 65 } |
55 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); | 66 _scopedWebStateListObserver->Add(_webStateList); |
56 _webStateList->AddObserver(_webStateListObserver.get()); | |
57 if (_webStateList->GetActiveWebState()) { | 67 if (_webStateList->GetActiveWebState()) { |
58 [self updateConsumerWithWebState:_webStateList->GetActiveWebState()]; | 68 [self updateConsumerWithWebState:_webStateList->GetActiveWebState()]; |
59 } | 69 } |
60 } | 70 } |
61 | 71 |
62 - (void)setConsumer:(id<WebContentsConsumer>)consumer { | 72 - (void)setConsumer:(id<WebContentsConsumer>)consumer { |
63 _consumer = consumer; | 73 _consumer = consumer; |
64 if (self.webStateList && self.webStateList->GetActiveWebState()) { | 74 if (self.webStateList && self.webStateList->GetActiveWebState()) { |
65 [self updateConsumerWithWebState:self.webStateList->GetActiveWebState()]; | 75 [self updateConsumerWithWebState:self.webStateList->GetActiveWebState()]; |
66 } | 76 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // PLACEHOLDER: This navigates an empty webstate to the NTP. | 113 // PLACEHOLDER: This navigates an empty webstate to the NTP. |
104 - (void)navigateToDefaultPage:(web::WebState*)webState { | 114 - (void)navigateToDefaultPage:(web::WebState*)webState { |
105 if (!webState->GetNavigationManager()->GetItemCount()) { | 115 if (!webState->GetNavigationManager()->GetItemCount()) { |
106 web::NavigationManager::WebLoadParams params((GURL(kChromeUINewTabURL))); | 116 web::NavigationManager::WebLoadParams params((GURL(kChromeUINewTabURL))); |
107 params.transition_type = ui::PAGE_TRANSITION_TYPED; | 117 params.transition_type = ui::PAGE_TRANSITION_TYPED; |
108 webState->GetNavigationManager()->LoadURLWithParams(params); | 118 webState->GetNavigationManager()->LoadURLWithParams(params); |
109 } | 119 } |
110 } | 120 } |
111 | 121 |
112 @end | 122 @end |
OLD | NEW |