Skip to content

Commit

Permalink
[ios][BVC] Inject webUsageEnabled in BVC
Browse files Browse the repository at this point in the history
Move updates to the WebUsageEnablerBrowserAgent out of the BVC, by
injecting webUsageEnabled in it.

Fixed: 1272516
Change-Id: I869dc46ceec2423bc5d1aba76bd829e4beb00cc7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4303311
Reviewed-by: Mark Cogan <marq@chromium.org>
Commit-Queue: Federica Germinario <fedegermi@google.com>
Cr-Commit-Position: refs/heads/main@{#1115082}
  • Loading branch information
Federica Germinario authored and Chromium LUCI CQ committed Mar 9, 2023
1 parent 5c00ba5 commit c319bd8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
30 changes: 19 additions & 11 deletions ios/chrome/browser/ui/browser_view/browser_coordinator.mm
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ @interface BrowserCoordinator () <BrowserCoordinatorCommands,
// Whether the coordinator is started.
@property(nonatomic, assign, getter=isStarted) BOOL started;

// Whether web usage is enabled for the WebStates in `self.browser`.
@property(nonatomic, assign, getter=isWebUsageEnabled) BOOL webUsageEnabled;

// Handles command dispatching, provided by the Browser instance.
@property(nonatomic, weak) CommandDispatcher* dispatcher;

Expand Down Expand Up @@ -479,8 +482,9 @@ - (void)start {
- (void)stop {
if (!self.started)
return;
[super stop];

self.started = NO;
[super stop];
self.active = NO;
[self uninstallDelegatesForBrowserState];
[self uninstallDelegatesForBrowser];
Expand All @@ -490,7 +494,6 @@ - (void)stop {
[self stopChildCoordinators];
[self destroyViewController];
[self destroyViewControllerDependencies];
self.started = NO;
}

#pragma mark - Public
Expand Down Expand Up @@ -521,9 +524,7 @@ - (void)setActive:(BOOL)active {
->GetForBrowserState(browserState)
->SetEnabled(active);
}

// TODO(crbug.com/1272516): Update the WebUsageEnablerBrowserAgent as part of
// setting active/inactive.
self.webUsageEnabled = active;
self.viewController.active = active;

// Stop the NTP on web usage toggle. This happens when clearing browser
Expand Down Expand Up @@ -587,6 +588,16 @@ - (void)displayPopupMenuWithBadgeItems:(NSArray<id<BadgeItem>>*)badgeItems {

#pragma mark - Private

- (void)setWebUsageEnabled:(BOOL)webUsageEnabled {
if (!self.browser->GetBrowserState() || !self.started) {
return;
}
WebUsageEnablerBrowserAgent::FromBrowser(self.browser)
->SetWebUsageEnabled(webUsageEnabled);
_webUsageEnabled = webUsageEnabled;
self.viewController.webUsageEnabled = webUsageEnabled;
}

// Displays activity overlay.
- (void)showActivityOverlay {
self.activityOverlayCoordinator = [[ActivityOverlayCoordinator alloc]
Expand Down Expand Up @@ -638,8 +649,8 @@ - (void)createViewController {

// Shuts down the BrowserViewController.
- (void)destroyViewController {
// TODO(crbug.com/1272516): Set the WebUsageEnablerBrowserAgent to disabled.
self.viewController.active = NO;
self.viewController.webUsageEnabled = NO;

// TODO(crbug.com/1415244): Remove when BVC will no longer handle commands.
[self.dispatcher stopDispatchingToTarget:self.viewController];
Expand Down Expand Up @@ -2372,13 +2383,10 @@ - (UIEdgeInsets)snapshotGenerator:(SnapshotGenerator*)snapshotGenerator
WebStateList* webStateList = self.browser->GetWebStateList();
DCHECK_NE(webStateList->GetIndexOfWebState(webState),
WebStateList::kInvalidIndex);
BOOL isWebUsageEnabled =
self.browser->GetBrowserState() && self.started &&
WebUsageEnablerBrowserAgent::FromBrowser(self.browser)
->IsWebUsageEnabled();

if (!isWebUsageEnabled || webState != webStateList->GetActiveWebState())
if (!self.webUsageEnabled || webState != webStateList->GetActiveWebState()) {
return @[];
}

NSMutableArray<UIView*>* overlays = [NSMutableArray array];

Expand Down
3 changes: 3 additions & 0 deletions ios/chrome/browser/ui/browser_view/browser_view_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ typedef struct {
// text to speech is playing.
@property(nonatomic, assign, readonly, getter=isPlayingTTS) BOOL playingTTS;

// Whether web usage is enabled for the WebStates in `self.browser`.
@property(nonatomic) BOOL webUsageEnabled;

// The container used for infobar banner overlays.
@property(nonatomic, strong)
UIViewController* infobarBannerOverlayContainerViewController;
Expand Down
25 changes: 4 additions & 21 deletions ios/chrome/browser/ui/browser_view/browser_view_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ @interface BrowserViewController () <FindBarPresentationDelegate,
@property(nonatomic, strong) IncognitoReauthView* blockingView;
// Whether the controller is currently dismissing a presented view controller.
@property(nonatomic, assign, getter=isDismissingModal) BOOL dismissingModal;
// Whether web usage is enabled for the WebStates in `self.browser`.
@property(nonatomic, assign, getter=isWebUsageEnabled) BOOL webUsageEnabled;
// Whether a new tab animation is occurring.
@property(nonatomic, assign, getter=isInNewTabAnimation) BOOL inNewTabAnimation;
// Whether BVC prefers to hide the status bar. This value is used to determine
Expand Down Expand Up @@ -634,22 +632,6 @@ - (void)setBroadcasting:(BOOL)broadcasting {
}
}

// TODO(crbug.com/1272516): Change webUsageEnabled to be a regular BOOL ivar.
- (BOOL)isWebUsageEnabled {
return self.browserState && !_isShutdown &&
WebUsageEnablerBrowserAgent::FromBrowser(self.browser)
->IsWebUsageEnabled();
}

// TODO(crbug.com/1272516): Change webUsageEnabled to be a regular BOOL ivar.
// BrowserCoordinator should update the WebUsageEnablerBrowserAgent.
- (void)setWebUsageEnabled:(BOOL)webUsageEnabled {
if (!self.browserState || _isShutdown)
return;
WebUsageEnablerBrowserAgent::FromBrowser(self.browser)
->SetWebUsageEnabled(webUsageEnabled);
}

- (void)setInNewTabAnimation:(BOOL)inNewTabAnimation {
if (_inNewTabAnimation == inNewTabAnimation)
return;
Expand Down Expand Up @@ -859,7 +841,6 @@ - (void)setActive:(BOOL)active {
}
_active = active;

self.webUsageEnabled = active;
[self updateBroadcastState];

if (active) {
Expand Down Expand Up @@ -1151,8 +1132,9 @@ - (void)viewWillAppear:(BOOL)animated {

// If the controller is suspended, or has been paged out due to low memory,
// updating the view will be handled when it's displayed again.
if (!self.webUsageEnabled || !self.contentArea)
if (!self.webUsageEnabled || !self.contentArea) {
return;
}
// Update the displayed WebState (if any; the switcher may not have created
// one yet) in case it changed while showing the switcher.
if (self.currentWebState)
Expand Down Expand Up @@ -3077,8 +3059,9 @@ - (void)initiateNewTabForegroundAnimationForWebState:(web::WebState*)webState {
}
// Do nothing if browsing is currently suspended. The BVC will set everything
// up correctly when browsing resumes.
if (!self.visible || !self.webUsageEnabled)
if (!self.visible || !self.webUsageEnabled) {
return;
}

self.inNewTabAnimation = YES;
__weak __typeof(self) weakSelf = self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ void SetUp() override {
browserContainerViewController:container_
keyCommandsProvider:key_commands_provider_
dependencies:dependencies];
bvc_.webUsageEnabled = YES;

id NTPCoordinator_ =
[[NewTabPageCoordinator alloc] initWithBrowser:browser_.get()];
Expand Down

0 comments on commit c319bd8

Please sign in to comment.