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 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_consumer.h" | 8 #import "ios/clean/chrome/browser/ui/web_contents/web_contents_consumer.h" |
| 9 #import "ios/shared/chrome/browser/tabs/web_state_list.h" |
| 10 #import "ios/shared/chrome/browser/tabs/web_state_list_observer_bridge.h" |
8 #import "ios/web/public/navigation_manager.h" | 11 #import "ios/web/public/navigation_manager.h" |
9 #include "ios/web/public/web_state/web_state.h" | 12 #include "ios/web/public/web_state/web_state.h" |
10 #include "ui/base/page_transition_types.h" | 13 #include "ui/base/page_transition_types.h" |
11 #include "url/gurl.h" | 14 #include "url/gurl.h" |
12 | 15 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
14 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
15 #endif | 18 #endif |
16 | 19 |
17 @implementation WebContentsMediator | 20 @interface WebContentsMediator ()<WebStateListObserving> |
18 @synthesize webState = _webState; | 21 @end |
| 22 |
| 23 @implementation WebContentsMediator { |
| 24 std::unique_ptr<WebStateListObserverBridge> _webStateListObserver; |
| 25 } |
| 26 @synthesize webStateList = _webStateList; |
19 @synthesize consumer = _consumer; | 27 @synthesize consumer = _consumer; |
20 | 28 |
21 - (void)setWebState:(web::WebState*)webState { | 29 - (void)dealloc { |
22 if (_webState) | 30 [self disconnect]; |
23 _webState->SetWebUsageEnabled(false); | 31 } |
24 | 32 |
25 _webState = webState; | 33 #pragma mark - Public |
26 if (!self.webState) | 34 |
| 35 - (void)disconnect { |
| 36 if (!self.webStateList) { |
27 return; | 37 return; |
| 38 } |
| 39 [self disableWebUsage:self.webStateList->GetActiveWebState()]; |
| 40 self.webStateList = nullptr; |
| 41 } |
28 | 42 |
29 self.webState->SetWebUsageEnabled(true); | 43 #pragma mark - Properties |
30 if (!self.webState->GetNavigationManager()->GetItemCount()) { | 44 |
31 web::NavigationManager::WebLoadParams params( | 45 - (void)setWebStateList:(WebStateList*)webStateList { |
32 GURL("https://dev.chromium.org/")); | 46 if (_webStateList) { |
33 params.transition_type = ui::PAGE_TRANSITION_TYPED; | 47 _webStateList->RemoveObserver(_webStateListObserver.get()); |
34 self.webState->GetNavigationManager()->LoadURLWithParams(params); | 48 _webStateListObserver.reset(); |
35 } | 49 } |
36 [self.consumer contentViewDidChange:self.webState->GetView()]; | 50 _webStateList = webStateList; |
| 51 if (!_webStateList) { |
| 52 return; |
| 53 } |
| 54 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self); |
| 55 _webStateList->AddObserver(_webStateListObserver.get()); |
| 56 if (_webStateList->GetActiveWebState()) { |
| 57 [self updateConsumerWithWebState:_webStateList->GetActiveWebState()]; |
| 58 } |
37 } | 59 } |
38 | 60 |
39 - (void)setConsumer:(id<WebContentsConsumer>)consumer { | 61 - (void)setConsumer:(id<WebContentsConsumer>)consumer { |
40 _consumer = consumer; | 62 _consumer = consumer; |
41 if (self.webState) | 63 if (self.webStateList && self.webStateList->GetActiveWebState()) { |
42 [self.consumer contentViewDidChange:self.webState->GetView()]; | 64 [self updateConsumerWithWebState:self.webStateList->GetActiveWebState()]; |
| 65 } |
| 66 } |
| 67 |
| 68 #pragma mark - WebStateListObserving |
| 69 |
| 70 - (void)webStateList:(WebStateList*)webStateList |
| 71 didChangeActiveWebState:(web::WebState*)newWebState |
| 72 oldWebState:(web::WebState*)oldWebState |
| 73 atIndex:(int)atIndex |
| 74 userAction:(BOOL)userAction { |
| 75 [self disableWebUsage:oldWebState]; |
| 76 [self updateConsumerWithWebState:newWebState]; |
| 77 } |
| 78 |
| 79 #pragma mark - Private |
| 80 |
| 81 - (void)disableWebUsage:(web::WebState*)webState { |
| 82 if (webState) { |
| 83 webState->SetWebUsageEnabled(false); |
| 84 } |
| 85 } |
| 86 |
| 87 // Sets |webState| webUsageEnabled and updates the consumer's contentView. |
| 88 - (void)updateConsumerWithWebState:(web::WebState*)webState { |
| 89 UIView* updatedView = nil; |
| 90 if (webState) { |
| 91 webState->SetWebUsageEnabled(true); |
| 92 updatedView = webState->GetView(); |
| 93 // PLACEHOLDER: This navigates the page since the omnibox is not yet |
| 94 // hooked up. |
| 95 [self navigateToDefaultPage:webState]; |
| 96 } |
| 97 if (self.consumer) { |
| 98 [self.consumer contentViewDidChange:updatedView]; |
| 99 } |
| 100 } |
| 101 |
| 102 // PLACEHOLDER: This navigates the page since the omnibox is not yet hooked up. |
| 103 - (void)navigateToDefaultPage:(web::WebState*)webState { |
| 104 if (!webState->GetNavigationManager()->GetItemCount()) { |
| 105 web::NavigationManager::WebLoadParams params( |
| 106 GURL("https://dev.chromium.org/")); |
| 107 params.transition_type = ui::PAGE_TRANSITION_TYPED; |
| 108 webState->GetNavigationManager()->LoadURLWithParams(params); |
| 109 } |
43 } | 110 } |
44 | 111 |
45 @end | 112 @end |
OLD | NEW |