Skip to content

Commit

Permalink
[ios clean] Creates ToolbarWebState Mediator
Browse files Browse the repository at this point in the history
BUG=683793

Review-Url: https://codereview.chromium.org/2708013002
Cr-Commit-Position: refs/heads/master@{#452059}
  • Loading branch information
sczs authored and Commit bot committed Feb 22, 2017
1 parent 4df731b commit fe033cc
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 28 deletions.
13 changes: 3 additions & 10 deletions ios/clean/chrome/browser/ui/tab/tab_coordinator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.h"
#import "ios/clean/chrome/browser/ui/web_contents/web_coordinator.h"
#import "ios/shared/chrome/browser/coordinator_context/coordinator_context.h"
#import "ios/web/public/web_state/web_state_observer_bridge.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
Expand All @@ -35,10 +34,7 @@ @interface TabCoordinator ()<UIViewControllerTransitioningDelegate>
@property(nonatomic, strong) TabContainerViewController* viewController;
@end

@implementation TabCoordinator {
std::unique_ptr<web::WebStateObserverBridge> _webStateObserver;
}

@implementation TabCoordinator
@synthesize presentationKey = _presentationKey;
@synthesize viewController = _viewController;
@synthesize webState = _webState;
Expand All @@ -57,11 +53,9 @@ - (void)start {
[webCoordinator start];

ToolbarCoordinator* toolbarCoordinator = [[ToolbarCoordinator alloc] init];
toolbarCoordinator.webState = self.webState;
[self addChildCoordinator:toolbarCoordinator];
// PLACEHOLDER : Pass the WebState into the toolbar coordinator and let it
// create a mediator (or whatever) that observes the webState.
_webStateObserver = base::MakeUnique<web::WebStateObserverBridge>(
self.webState, toolbarCoordinator);

// Unset the base view controller, so |toolbarCoordinator| doesn't present
// its view controller.
toolbarCoordinator.context.baseViewController = nil;
Expand Down Expand Up @@ -93,7 +87,6 @@ - (void)stop {
[self.viewController.presentingViewController
dismissViewControllerAnimated:self.context.animated
completion:nil];
_webStateObserver.reset();
}

- (BOOL)canAddOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator {
Expand Down
3 changes: 3 additions & 0 deletions ios/clean/chrome/browser/ui/toolbar/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ source_set("toolbar") {
sources = [
"toolbar_coordinator.h",
"toolbar_coordinator.mm",
"toolbar_mediator.h",
"toolbar_mediator.mm",
]

configs += [ "//build/config/compiler:enable_arc" ]
Expand All @@ -23,6 +25,7 @@ source_set("toolbar") {

source_set("toolbar_ui") {
sources = [
"toolbar_consumer.h",
"toolbar_view_controller.h",
"toolbar_view_controller.mm",
]
Expand Down
17 changes: 17 additions & 0 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONSUMER_H_
#define IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONSUMER_H_

#import <UIKit/UIKit.h>

// ToolbarConsumer sets the current appearance of the Toolbar based on
// WebState or other data sources provided by this protocol.
@protocol ToolbarConsumer
// Sets the text for a label appearing in the center of the toolbar.
- (void)setCurrentPageText:(NSString*)text;
@end

#endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONSUMER_H_
9 changes: 7 additions & 2 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#define IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_COORDINATOR_H_

#import "ios/clean/chrome/browser/browser_coordinator.h"
#import "ios/web/public/web_state/web_state_observer_bridge.h"

namespace web {
class WebState;
}

// Coordinator to run a toolbar -- a UI element housing controls.
@interface ToolbarCoordinator : BrowserCoordinator<CRWWebStateObserver>
@interface ToolbarCoordinator : BrowserCoordinator
// The web state this ToolbarCoordinator is handling.
@property(nonatomic, assign) web::WebState* webState;
@end

#endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_COORDINATOR_H_
27 changes: 17 additions & 10 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#import "ios/clean/chrome/browser/ui/toolbar/toolbar_coordinator.h"

#include "base/strings/sys_string_conversions.h"
#import "ios/clean/chrome/browser/browser_coordinator+internal.h"
#import "ios/clean/chrome/browser/ui/commands/toolbar_commands.h"
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h"
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_view_controller.h"
#import "ios/clean/chrome/browser/ui/tools/tools_coordinator.h"
#import "ios/shared/chrome/browser/coordinator_context/coordinator_context.h"
#include "ios/web/public/web_state/web_state.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
Expand All @@ -23,29 +22,37 @@
@interface ToolbarCoordinator ()<ToolbarCommands>
@property(nonatomic, weak) ToolsCoordinator* toolsMenuCoordinator;
@property(nonatomic, strong) ToolbarViewController* viewController;
@property(nonatomic, strong) ToolbarMediator* mediator;
@end

@implementation ToolbarCoordinator
@synthesize toolsMenuCoordinator = _toolsMenuCoordinator;
@synthesize viewController = _viewController;
@synthesize webState = _webState;
@synthesize mediator = _mediator;

- (instancetype)init {
if ((self = [super init])) {
_mediator = [[ToolbarMediator alloc] init];
}
return self;
}

- (void)setWebState:(web::WebState*)webState {
_webState = webState;
self.mediator.webState = self.webState;
}

- (void)start {
self.viewController = [[ToolbarViewController alloc] init];
self.viewController.toolbarCommandHandler = self;
self.mediator.consumer = self.viewController;

[self.context.baseViewController presentViewController:self.viewController
animated:self.context.animated
completion:nil];
}

#pragma mark - CRWWebStateObserver

- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
const GURL& pageURL = webState->GetVisibleURL();
[self.viewController
setCurrentPageText:base::SysUTF8ToNSString(pageURL.spec())];
}

#pragma mark - ToolbarCommands

- (void)showToolsMenu {
Expand Down
30 changes: 30 additions & 0 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_MEDIATOR_H_
#define IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_MEDIATOR_H_

#import <Foundation/Foundation.h>

@protocol ToolbarConsumer;

namespace web {
class WebState;
}

// A mediator object that provides the relevant properties of a web state
// to a consumer.
@interface ToolbarMediator : NSObject

// The WebState whose properties this object mediates. This can change during
// the lifetime of this object and may be null.
@property(nonatomic, assign) web::WebState* webState;

// The consumer for this object. This can change during the lifetime of this
// object and may be nil.
@property(nonatomic, strong) id<ToolbarConsumer> consumer;

@end

#endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_MEDIATOR_H_
41 changes: 41 additions & 0 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/clean/chrome/browser/ui/toolbar/toolbar_mediator.h"

#include "base/memory/ptr_util.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h"
#include "ios/web/public/web_state/web_state.h"
#import "ios/web/public/web_state/web_state_observer_bridge.h"

@interface ToolbarMediator ()<CRWWebStateObserver>
@end

@implementation ToolbarMediator {
std::unique_ptr<web::WebStateObserverBridge> _webStateObserver;
}

@synthesize consumer = _consumer;
@synthesize webState = _webState;

- (void)dealloc {
_webStateObserver.reset();
_webState = nullptr;
}

- (void)setWebState:(web::WebState*)webState {
_webState = webState;
_webStateObserver =
base::MakeUnique<web::WebStateObserverBridge>(self.webState, self);
}

#pragma mark - CRWWebStateObserver

- (void)webState:(web::WebState*)webState didLoadPageWithSuccess:(BOOL)success {
const GURL& pageURL = webState->GetVisibleURL();
[self.consumer setCurrentPageText:base::SysUTF8ToNSString(pageURL.spec())];
}

@end
8 changes: 3 additions & 5 deletions ios/clean/chrome/browser/ui/toolbar/toolbar_view_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <UIKit/UIKit.h>

#import "ios/clean/chrome/browser/ui/animators/zoom_transition_delegate.h"
#import "ios/clean/chrome/browser/ui/toolbar/toolbar_consumer.h"

@protocol ToolbarCommands;

Expand All @@ -20,14 +21,11 @@
// This view controller will fill its container; it is up to the containing
// view controller or presentation controller to configure an appropriate
// height for it.
@interface ToolbarViewController : UIViewController<ZoomTransitionDelegate>
@interface ToolbarViewController
: UIViewController<ZoomTransitionDelegate, ToolbarConsumer>

// The action delegate for this view controller.
@property(nonatomic, weak) id<ToolbarCommands> toolbarCommandHandler;

// Sets the text for a label appearing in the center of the toolbar.
- (void)setCurrentPageText:(NSString*)text;

@end

#endif // IOS_CLEAN_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_VIEW_CONTROLLER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {
}
}

#pragma mark - Public API
#pragma mark - ToolbarWebStateConsumer

- (void)setCurrentPageText:(NSString*)text {
self.omnibox.text = text;
Expand Down

0 comments on commit fe033cc

Please sign in to comment.