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

Side by Side Diff: ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm

Issue 2780423003: [ios] Use web_state_list in web_contents_mediator. (Closed)
Patch Set: Update unittests. Created 3 years, 8 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
OLDNEW
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 if (_webStateList) {
23 _webState->SetWebUsageEnabled(false); 31 _webStateList->RemoveObserver(_webStateListObserver.get());
32 _webStateListObserver.reset();
33 }
34 }
24 35
25 _webState = webState; 36 #pragma mark - Properties
26 if (!self.webState) 37
38 - (void)setWebStateList:(WebStateList*)webStateList {
39 if (_webStateList) {
40 _webStateList->RemoveObserver(_webStateListObserver.get());
41 _webStateListObserver.reset();
42 }
43 _webStateList = webStateList;
44 if (!_webStateList) {
27 return; 45 return;
28
29 self.webState->SetWebUsageEnabled(true);
30 if (!self.webState->GetNavigationManager()->GetItemCount()) {
31 web::NavigationManager::WebLoadParams params(
32 GURL("https://dev.chromium.org/"));
33 params.transition_type = ui::PAGE_TRANSITION_TYPED;
34 self.webState->GetNavigationManager()->LoadURLWithParams(params);
35 } 46 }
36 [self.consumer contentViewDidChange:self.webState->GetView()]; 47 _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self);
48 _webStateList->AddObserver(_webStateListObserver.get());
49 if (_webStateList->GetActiveWebState()) {
50 [self updateActiveWebState:_webStateList->GetActiveWebState()];
51 }
37 } 52 }
38 53
39 - (void)setConsumer:(id<WebContentsConsumer>)consumer { 54 - (void)setConsumer:(id<WebContentsConsumer>)consumer {
40 _consumer = consumer; 55 _consumer = consumer;
41 if (self.webState) 56 if (self.webStateList && self.webStateList->GetActiveWebState()) {
42 [self.consumer contentViewDidChange:self.webState->GetView()]; 57 [self updateActiveWebState:self.webStateList->GetActiveWebState()];
58 }
59 }
60
61 #pragma mark - WebStateListObserving
62
63 - (void)webStateList:(WebStateList*)webStateList
64 didChangeActiveWebState:(web::WebState*)newWebState
65 oldWebState:(web::WebState*)oldWebState
66 atIndex:(int)atIndex
67 userAction:(BOOL)userAction {
68 if (oldWebState) {
69 oldWebState->SetWebUsageEnabled(false);
rohitrao (ping after 24h) 2017/04/03 23:48:51 Did you copy this line from somewhere in the old a
edchin 2017/04/04 19:50:12 Line 23 of this file, prior to this change.
70 }
71 [self updateActiveWebState:newWebState];
72 }
73
74 #pragma mark - Private
75
76 - (void)updateActiveWebState:(web::WebState*)webState {
lpromero 2017/04/03 09:48:36 Add a comment. The name is a bit misleading, it's
edchin 2017/04/04 19:50:11 Done.
77 UIView* updatedView = nil;
78 if (webState) {
79 webState->SetWebUsageEnabled(true);
80 updatedView = webState->GetView();
81 // PLACEHOLDER: This navigates the page since the omnibox is not yet
82 // hooked up.
83 [self navigateToDefaultPage:webState];
84 }
85 if (self.consumer) {
86 [self.consumer contentViewDidChange:updatedView];
87 }
88 }
89
90 // PLACEHOLDER: This navigates the page since the omnibox is not yet hooked up.
91 - (void)navigateToDefaultPage:(web::WebState*)webState {
92 if (!webState->GetNavigationManager()->GetItemCount()) {
93 web::NavigationManager::WebLoadParams params(
94 GURL("https://dev.chromium.org/"));
95 params.transition_type = ui::PAGE_TRANSITION_TYPED;
96 webState->GetNavigationManager()->LoadURLWithParams(params);
97 }
43 } 98 }
44 99
45 @end 100 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698