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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
Index: ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm
diff --git a/ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm b/ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm
index 2a5529bc5f8dd067bc843f649c472665d58b8d77..5c97ac58f49affa31bd2853485fd6a0c299a9432 100644
--- a/ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm
+++ b/ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.mm
@@ -4,7 +4,10 @@
#import "ios/clean/chrome/browser/ui/web_contents/web_contents_mediator.h"
+#include "base/memory/ptr_util.h"
#import "ios/clean/chrome/browser/ui/web_contents/web_contents_consumer.h"
+#import "ios/shared/chrome/browser/tabs/web_state_list.h"
+#import "ios/shared/chrome/browser/tabs/web_state_list_observer_bridge.h"
#import "ios/web/public/navigation_manager.h"
#include "ios/web/public/web_state/web_state.h"
#include "ui/base/page_transition_types.h"
@@ -14,32 +17,84 @@
#error "This file requires ARC support."
#endif
-@implementation WebContentsMediator
-@synthesize webState = _webState;
+@interface WebContentsMediator ()<WebStateListObserving>
+@end
+
+@implementation WebContentsMediator {
+ std::unique_ptr<WebStateListObserverBridge> _webStateListObserver;
+}
+@synthesize webStateList = _webStateList;
@synthesize consumer = _consumer;
-- (void)setWebState:(web::WebState*)webState {
- if (_webState)
- _webState->SetWebUsageEnabled(false);
+- (void)dealloc {
+ if (_webStateList) {
+ _webStateList->RemoveObserver(_webStateListObserver.get());
+ _webStateListObserver.reset();
+ }
+}
- _webState = webState;
- if (!self.webState)
- return;
+#pragma mark - Properties
- self.webState->SetWebUsageEnabled(true);
- if (!self.webState->GetNavigationManager()->GetItemCount()) {
- web::NavigationManager::WebLoadParams params(
- GURL("https://dev.chromium.org/"));
- params.transition_type = ui::PAGE_TRANSITION_TYPED;
- self.webState->GetNavigationManager()->LoadURLWithParams(params);
+- (void)setWebStateList:(WebStateList*)webStateList {
+ if (_webStateList) {
+ _webStateList->RemoveObserver(_webStateListObserver.get());
+ _webStateListObserver.reset();
+ }
+ _webStateList = webStateList;
+ if (!_webStateList) {
+ return;
+ }
+ _webStateListObserver = base::MakeUnique<WebStateListObserverBridge>(self);
+ _webStateList->AddObserver(_webStateListObserver.get());
+ if (_webStateList->GetActiveWebState()) {
+ [self updateActiveWebState:_webStateList->GetActiveWebState()];
}
- [self.consumer contentViewDidChange:self.webState->GetView()];
}
- (void)setConsumer:(id<WebContentsConsumer>)consumer {
_consumer = consumer;
- if (self.webState)
- [self.consumer contentViewDidChange:self.webState->GetView()];
+ if (self.webStateList && self.webStateList->GetActiveWebState()) {
+ [self updateActiveWebState:self.webStateList->GetActiveWebState()];
+ }
+}
+
+#pragma mark - WebStateListObserving
+
+- (void)webStateList:(WebStateList*)webStateList
+ didChangeActiveWebState:(web::WebState*)newWebState
+ oldWebState:(web::WebState*)oldWebState
+ atIndex:(int)atIndex
+ userAction:(BOOL)userAction {
+ if (oldWebState) {
+ 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.
+ }
+ [self updateActiveWebState:newWebState];
+}
+
+#pragma mark - Private
+
+- (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.
+ UIView* updatedView = nil;
+ if (webState) {
+ webState->SetWebUsageEnabled(true);
+ updatedView = webState->GetView();
+ // PLACEHOLDER: This navigates the page since the omnibox is not yet
+ // hooked up.
+ [self navigateToDefaultPage:webState];
+ }
+ if (self.consumer) {
+ [self.consumer contentViewDidChange:updatedView];
+ }
+}
+
+// PLACEHOLDER: This navigates the page since the omnibox is not yet hooked up.
+- (void)navigateToDefaultPage:(web::WebState*)webState {
+ if (!webState->GetNavigationManager()->GetItemCount()) {
+ web::NavigationManager::WebLoadParams params(
+ GURL("https://dev.chromium.org/"));
+ params.transition_type = ui::PAGE_TRANSITION_TYPED;
+ webState->GetNavigationManager()->LoadURLWithParams(params);
+ }
}
@end

Powered by Google App Engine
This is Rietveld 408576698