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

Side by Side Diff: ios/clean/chrome/browser/ui/tools/tools_mediator.mm

Issue 2906313003: [ios clean] ToolsMenu Mediator observes Webstate. (Closed)
Patch Set: Improved comments and test nits Created 3 years, 6 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/tools/tools_mediator.h" 5 #import "ios/clean/chrome/browser/ui/tools/tools_mediator.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
8 #import "ios/clean/chrome/browser/ui/tools/tools_consumer.h" 9 #import "ios/clean/chrome/browser/ui/tools/tools_consumer.h"
9 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h" 10 #import "ios/clean/chrome/browser/ui/tools/tools_menu_item.h"
10 #import "ios/clean/chrome/browser/ui/tools/tools_menu_model.h" 11 #import "ios/clean/chrome/browser/ui/tools/tools_menu_model.h"
11 #import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h" 12 #import "ios/shared/chrome/browser/ui/tools_menu/tools_menu_configuration.h"
13 #include "ios/web/public/web_state/web_state.h"
14 #import "ios/web/public/web_state/web_state_observer_bridge.h"
12 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
13 16
14 #if !defined(__has_feature) || !__has_feature(objc_arc) 17 #if !defined(__has_feature) || !__has_feature(objc_arc)
15 #error "This file requires ARC support." 18 #error "This file requires ARC support."
16 #endif 19 #endif
17 20
18 @interface ToolsMediator () 21 @interface ToolsMediator ()<CRWWebStateObserver>
19 @property(nonatomic, strong) id<ToolsConsumer> consumer; 22 @property(nonatomic, strong) id<ToolsConsumer> consumer;
20 @property(nonatomic, strong) ToolsMenuConfiguration* toolsMenuConfiguration; 23 @property(nonatomic, strong) ToolsMenuConfiguration* toolsMenuConfiguration;
21 @property(nonatomic, strong) NSMutableArray* menuItems; 24 @property(nonatomic, strong) NSMutableArray* menuItems;
22 // Populates the menuItems array by filtering the model data source based on the 25 // Populates the menuItems array by filtering the model data source based on the
23 // current ToolsMenuConfiguration. 26 // current ToolsMenuConfiguration.
24 - (void)populateMenuItems; 27 - (void)populateMenuItems;
25 // Checks if a specific Menu Item should be visible for the current 28 // Checks if a specific Menu Item should be visible for the current
26 // configuration. 29 // configuration.
27 - (BOOL)itemIsVisibleForCurrentConfiguration:(const MenuModelItem)modelItem; 30 - (BOOL)itemIsVisibleForCurrentConfiguration:(const MenuModelItem)modelItem;
28
29 @end 31 @end
30 32
31 @implementation ToolsMediator 33 @implementation ToolsMediator {
34 std::unique_ptr<web::WebStateObserverBridge> _webStateObserver;
35 }
32 36
33 @synthesize consumer = _consumer; 37 @synthesize consumer = _consumer;
34 @synthesize toolsMenuConfiguration = _toolsMenuConfiguration; 38 @synthesize toolsMenuConfiguration = _toolsMenuConfiguration;
35 @synthesize menuItems = _menuItems; 39 @synthesize menuItems = _menuItems;
40 @synthesize webState = _webState;
36 41
37 - (instancetype)initWithConsumer:(id<ToolsConsumer>)consumer 42 - (instancetype)initWithConsumer:(id<ToolsConsumer>)consumer
38 andConfiguration:(ToolsMenuConfiguration*)menuConfiguration { 43 configuration:(ToolsMenuConfiguration*)menuConfiguration {
39 self = [super init]; 44 self = [super init];
40 if (self) { 45 if (self) {
41 self.toolsMenuConfiguration = menuConfiguration; 46 self.toolsMenuConfiguration = menuConfiguration;
42 self.consumer = consumer; 47 self.consumer = consumer;
43 } 48 }
44 return self; 49 return self;
45 } 50 }
46 51
52 #pragma mark - Setters
53
47 - (void)setConsumer:(id<ToolsConsumer>)consumer { 54 - (void)setConsumer:(id<ToolsConsumer>)consumer {
48 _consumer = consumer; 55 _consumer = consumer;
49
50 [self populateMenuItems]; 56 [self populateMenuItems];
51
52 [_consumer setToolsMenuItems:self.menuItems]; 57 [_consumer setToolsMenuItems:self.menuItems];
53 [_consumer 58 [_consumer
54 setDisplayOverflowControls:!self.toolsMenuConfiguration.isInTabSwitcher]; 59 setDisplayOverflowControls:!self.toolsMenuConfiguration.isInTabSwitcher];
55 } 60 }
56 61
62 - (void)setWebState:(web::WebState*)webState {
63 _webState = webState;
64 // In the tab switcher, there's no overflow controls to update, so the
65 // WebState isn't observed.
66 if (!self.toolsMenuConfiguration.isInTabSwitcher) {
67 _webStateObserver =
68 base::MakeUnique<web::WebStateObserverBridge>(_webState, self);
69 [self.consumer setIsLoading:self.webState->IsLoading()];
70 }
71 }
72
73 #pragma mark - CRWWebStateObserver
74
75 - (void)webStateDidStartLoading:(web::WebState*)webState {
76 [self.consumer setIsLoading:self.webState->IsLoading()];
77 }
78
79 - (void)webStateDidStopLoading:(web::WebState*)webState {
80 [self.consumer setIsLoading:self.webState->IsLoading()];
81 }
82
57 #pragma mark - Private Methods 83 #pragma mark - Private Methods
58 84
59 - (void)populateMenuItems { 85 - (void)populateMenuItems {
60 self.menuItems = [NSMutableArray array]; 86 self.menuItems = [NSMutableArray array];
61 87
62 for (size_t i = 0; i < arraysize(itemsModelList); ++i) { 88 for (size_t i = 0; i < arraysize(itemsModelList); ++i) {
63 const MenuModelItem& modelItem = itemsModelList[i]; 89 const MenuModelItem& modelItem = itemsModelList[i];
64 90
65 if ([self itemIsVisibleForCurrentConfiguration:modelItem]) { 91 if ([self itemIsVisibleForCurrentConfiguration:modelItem]) {
66 ToolsMenuItem* menuItem = [[ToolsMenuItem alloc] init]; 92 ToolsMenuItem* menuItem = [[ToolsMenuItem alloc] init];
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 @end 135 @end
110 136
111 // Private implementation for testing. 137 // Private implementation for testing.
112 @implementation ToolsMediator (Testing) 138 @implementation ToolsMediator (Testing)
113 139
114 - (NSArray*)menuItemsArray { 140 - (NSArray*)menuItemsArray {
115 return self.menuItems; 141 return self.menuItems;
116 } 142 }
117 143
118 @end 144 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698