Commit 39f5b43b authored by Stepan Khapugin's avatar Stepan Khapugin Committed by Chromium LUCI CQ

[iOS] Introduce a common superclass for scene agents.

Creates a new superclass for scene agents.
Existing scene agents are all scene state observers, and all have a
sceneState property.
It also seems useful to have a way to retrieve a scene agent for a
given scene.

These use features are factored out in a lightweight ObservingSceneAgent
class. Existing agents are made subclasses of that.

Bug: none
Change-Id: Ia1908e339178e52396294fa4f308d759ee32a6ed
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2568271
Commit-Queue: Stepan Khapugin <stkhapugin@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833337}
parent de26f290
......@@ -480,14 +480,9 @@
// Starts mediators owned by this coordinator.
- (void)startMediators {
if (self.browser->GetBrowserState()->IsOffTheRecord()) {
IncognitoReauthSceneAgent* reauthAgent = nil;
for (id agent in SceneStateBrowserAgent::FromBrowser(self.browser)
->GetSceneState()
.connectedAgents) {
if ([agent isKindOfClass:[IncognitoReauthSceneAgent class]]) {
reauthAgent = agent;
}
}
IncognitoReauthSceneAgent* reauthAgent = [IncognitoReauthSceneAgent
agentFromScene:SceneStateBrowserAgent::FromBrowser(self.browser)
->GetSceneState()];
self.incognitoAuthMediator =
[[IncognitoReauthMediator alloc] initWithConsumer:self.viewController
......
......@@ -42,7 +42,7 @@ source_set("incognito_reauth_scene_agent") {
"//ios/chrome/browser:pref_names",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/main:browser_interface_provider",
"//ios/chrome/browser/ui/main:scene_state_header",
"//ios/chrome/browser/ui/main:observing_scene_agent",
"//ios/chrome/browser/ui/util:multiwindow_util",
"//ios/chrome/browser/web_state_list",
"//ios/chrome/common/ui/reauthentication",
......
......@@ -5,7 +5,7 @@
#ifndef IOS_CHROME_BROWSER_UI_INCOGNITO_REAUTH_INCOGNITO_REAUTH_SCENE_AGENT_H_
#define IOS_CHROME_BROWSER_UI_INCOGNITO_REAUTH_INCOGNITO_REAUTH_SCENE_AGENT_H_
#import "ios/chrome/browser/ui/main/scene_state.h"
#import "ios/chrome/browser/ui/main/observing_scene_state_agent.h"
#import "ios/chrome/browser/ui/incognito_reauth/incognito_reauth_commands.h"
......@@ -27,7 +27,7 @@ class PrefService;
// A scene agent that tracks the incognito authentication status for the current
// scene.
@interface IncognitoReauthSceneAgent
: NSObject <IncognitoReauthCommands, SceneAgent>
: ObservingSceneAgent <IncognitoReauthCommands>
// Designated initializer.
// The |reauthModule| is used for authentication.
......
......@@ -28,10 +28,7 @@
#pragma mark - IncognitoReauthSceneAgent
@interface IncognitoReauthSceneAgent () <SceneStateObserver>
// Scene state this agent serves.
@property(nonatomic, weak) SceneState* sceneState;
@interface IncognitoReauthSceneAgent ()
// Set when the scene goes foreground. Checks if any incognito tabs were open.
@property(nonatomic, assign) BOOL windowHadIncognitoContentOnForeground;
......@@ -144,14 +141,6 @@
}
}
#pragma mark - SceneAgent
- (void)setSceneState:(SceneState*)sceneState {
DCHECK(!_sceneState);
_sceneState = sceneState;
[sceneState addObserver:self];
}
#pragma mark - private
- (PrefService*)localState {
......
......@@ -17,6 +17,22 @@ source_set("scene_state_observer") {
frameworks = [ "UIKit.framework" ]
}
source_set("observing_scene_agent") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"observing_scene_state_agent.h",
"observing_scene_state_agent.mm",
]
frameworks = [ "UIKit.framework" ]
public_deps = [
":scene_state_header",
":scene_state_observer",
]
}
source_set("scene_state_header") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
......@@ -63,6 +79,7 @@ source_set("scene") {
deps = [
":incognito_blocker_scene_agent",
":main",
":observing_scene_agent",
":scene_testing",
"//base",
"//components/infobars/core",
......
......@@ -433,12 +433,8 @@
}
- (void)dispatchToEndpointsForBrowser:(Browser*)browser {
IncognitoReauthSceneAgent* reauthAgent = nil;
for (id agent in _sceneState.connectedAgents) {
if ([agent isKindOfClass:[IncognitoReauthSceneAgent class]]) {
reauthAgent = agent;
}
}
IncognitoReauthSceneAgent* reauthAgent =
[IncognitoReauthSceneAgent agentFromScene:_sceneState];
CommandDispatcher* dispatcher = browser->GetCommandDispatcher();
[dispatcher startDispatchingToTarget:reauthAgent
......
......@@ -5,13 +5,13 @@
#ifndef IOS_CHROME_BROWSER_UI_MAIN_DEFAULT_BROWSER_SCENE_AGENT_H_
#define IOS_CHROME_BROWSER_UI_MAIN_DEFAULT_BROWSER_SCENE_AGENT_H_
#import "ios/chrome/browser/ui/main/scene_state.h"
#import "ios/chrome/browser/ui/main/observing_scene_state_agent.h"
@class CommandDispatcher;
// A scene agent that shows the default browser fullscreen promo UI based on the
// SceneActivationLevel changes.
@interface DefaultBrowserSceneAgent : NSObject <SceneAgent>
@interface DefaultBrowserSceneAgent : ObservingSceneAgent
- (instancetype)initWithCommandDispatcher:(CommandDispatcher*)dispatcher;
......
......@@ -16,11 +16,8 @@
#error "This file requires ARC support."
#endif
@interface DefaultBrowserSceneAgent () <SceneStateObserver>
@interface DefaultBrowserSceneAgent ()
// Scene to which this agent is attached.
// Implements the setter from SceneAgent protocol.
@property(nonatomic, weak) SceneState* sceneState;
// Command Dispatcher.
@property(nonatomic, weak) CommandDispatcher* dispatcher;
......@@ -34,14 +31,6 @@
return self;
}
#pragma mark - SceneAgent
- (void)setSceneState:(SceneState*)sceneState {
DCHECK(!_sceneState);
_sceneState = sceneState;
[sceneState addObserver:self];
}
#pragma mark - SceneStateObserver
- (void)sceneState:(SceneState*)sceneState
......
// Copyright 2020 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_CHROME_BROWSER_UI_MAIN_OBSERVING_SCENE_STATE_AGENT_H_
#define IOS_CHROME_BROWSER_UI_MAIN_OBSERVING_SCENE_STATE_AGENT_H_
#import "ios/chrome/browser/ui/main/scene_state.h"
#import "ios/chrome/browser/ui/main/scene_state_observer.h"
// A scene agent that acts as a scene state observer.
// Since most agents are also scene state observers, this is a convenience base
// class that provides universally useful functionality for scene agents.
@interface ObservingSceneAgent : NSObject <SceneAgent, SceneStateObserver>
// Scene state this agent serves and observes.
// See also allowsMultipleAgentsOfSameTypePerScene.
@property(nonatomic, weak) SceneState* sceneState;
// Returns the agent of this class iff one is already added to |sceneState|.
+ (instancetype)agentFromScene:(SceneState*)sceneState;
// You can override this in your subclass. The default is NO.
// When calling -sceneState, if this is NO, the setter will DCHECK if
// another agent of its class is already added to the scene.
+ (BOOL)allowsMultipleAgentsOfSameTypePerScene;
@end
#endif // IOS_CHROME_BROWSER_UI_MAIN_OBSERVING_SCENE_STATE_AGENT_H_
// Copyright 2020 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/chrome/browser/ui/main/observing_scene_state_agent.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation ObservingSceneAgent
#pragma mark - SceneAgent
+ (instancetype)agentFromScene:(SceneState*)sceneState {
for (id agent in sceneState.connectedAgents) {
if ([agent isKindOfClass:[self class]]) {
return agent;
}
}
return nil;
}
+ (BOOL)allowsMultipleAgentsOfSameTypePerScene {
return NO;
}
#pragma mark - public
- (void)setSceneState:(SceneState*)sceneState {
DCHECK(!_sceneState);
// Sanity check: most of the time only one object of the same type per scene
// is required, and multiple of them is a programming error.
if (![[self class] allowsMultipleAgentsOfSameTypePerScene]) {
// Verify that no other agent of this class is added to the scene state.
// Note that this object is already added, and it's ok.
for (id agent in sceneState.connectedAgents) {
if ([agent isKindOfClass:[self class]]) {
DCHECK(agent == self);
}
}
}
_sceneState = sceneState;
[sceneState addObserver:self];
}
#pragma mark - private
- (void)dealloc {
[_sceneState removeObserver:self];
}
@end
......@@ -5,11 +5,11 @@
#ifndef IOS_CHROME_BROWSER_UI_MAIN_UI_BLOCKER_SCENE_AGENT_H_
#define IOS_CHROME_BROWSER_UI_MAIN_UI_BLOCKER_SCENE_AGENT_H_
#import "ios/chrome/browser/ui/main/scene_state.h"
#import "ios/chrome/browser/ui/main/observing_scene_state_agent.h"
// A scene agent that shows a UI overlay on the scene based on modal overlay
// show/hide events.
@interface UIBlockerSceneAgent : NSObject <SceneAgent>
@interface UIBlockerSceneAgent : ObservingSceneAgent
@end
#endif // IOS_CHROME_BROWSER_UI_MAIN_UI_BLOCKER_SCENE_AGENT_H_
......@@ -15,11 +15,7 @@
#error "This file requires ARC support."
#endif
@interface UIBlockerSceneAgent () <SceneStateObserver>
// Scene to which this agent is attached.
// Implements the setter from SceneAgent protocol.
@property(nonatomic, weak) SceneState* sceneState;
@interface UIBlockerSceneAgent ()
// TODO(crbug.com/1107873): Create a coordinator to own this view controller.
// The view controller that blocks all interactions with the scene.
......@@ -30,14 +26,6 @@
@implementation UIBlockerSceneAgent
#pragma mark - SceneAgent
- (void)setSceneState:(SceneState*)sceneState {
DCHECK(!_sceneState);
_sceneState = sceneState;
[sceneState addObserver:self];
}
#pragma mark - SceneStateObserver
- (void)sceneStateWillShowModalOverlay:(SceneState*)sceneState {
......
......@@ -306,14 +306,9 @@
#pragma mark - ChromeCoordinator
- (void)start {
IncognitoReauthSceneAgent* reauthAgent = nil;
for (id agent in SceneStateBrowserAgent::FromBrowser(_incognitoBrowser)
->GetSceneState()
.connectedAgents) {
if ([agent isKindOfClass:[IncognitoReauthSceneAgent class]]) {
reauthAgent = agent;
}
}
IncognitoReauthSceneAgent* reauthAgent = [IncognitoReauthSceneAgent
agentFromScene:SceneStateBrowserAgent::FromBrowser(_incognitoBrowser)
->GetSceneState()];
[self.dispatcher startDispatchingToTarget:reauthAgent
forProtocol:@protocol(IncognitoReauthCommands)];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment