Commit cfa44bea authored by sczs's avatar sczs Committed by Commit Bot

[ios] Creates InfobarBadgeTabHelper

- InfobarBadgeTabHelper observes InfoBarManagerObserver for any Infobars
that were presented or removed.
- If the Infobar is a Message we will let the delegate know so it displays
a badge.


Bug: 911864
Change-Id: I3569efe9c982c1d473368ca199ce2a65251ce197
Reviewed-on: https://chromium-review.googlesource.com/c/1487797Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarChris Lu <thegreenfrog@chromium.org>
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#635917}
parent 59514e1f
......@@ -34,6 +34,21 @@ source_set("infobars") {
]
}
source_set("badge") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"infobar_badge_tab_helper.h",
"infobar_badge_tab_helper.mm",
"infobar_badge_tab_helper_delegate.h",
]
deps = [
":infobars",
"//ios/chrome/browser/ui/infobars:feature_flags",
"//ios/chrome/browser/ui/infobars:infobars_ui",
"//ios/web",
]
}
source_set("public") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
......
// Copyright 2019 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_INFOBARS_INFOBAR_BADGE_TAB_HELPER_H_
#define IOS_CHROME_BROWSER_INFOBARS_INFOBAR_BADGE_TAB_HELPER_H_
#include "base/scoped_observer.h"
#import "ios/web/public/web_state/web_state_user_data.h"
#include "components/infobars/core/infobar_manager.h"
namespace web {
class WebState;
}
@protocol InfobarBadgeTabHelperDelegate;
// TabHelper that observes InfoBarManager. It updates an InfobarBadge delegate
// for relevant Infobar changes.
class InfobarBadgeTabHelper
: public infobars::InfoBarManager::Observer,
public web::WebStateUserData<InfobarBadgeTabHelper> {
public:
// Creates the tab helper for |web_state| if it does not exist.
static void CreateForWebState(web::WebState* web_state);
// Sets the InfobarBadgeTabHelperDelegate to |delegate|.
void SetDelegate(id<InfobarBadgeTabHelperDelegate> delegate);
~InfobarBadgeTabHelper() override;
private:
friend class web::WebStateUserData<InfobarBadgeTabHelper>;
// Constructor.
explicit InfobarBadgeTabHelper(web::WebState* web_state);
// InfoBarManagerObserver implementation.
void OnInfoBarAdded(infobars::InfoBar* infobar) override;
void OnInfoBarRemoved(infobars::InfoBar* infobar, bool animate) override;
void OnInfoBarReplaced(infobars::InfoBar* old_infobar,
infobars::InfoBar* new_infobar) override;
void OnManagerShuttingDown(infobars::InfoBarManager* manager) override;
// Updates the badge delegate for |infobar|.
void UpdateBadgeForInfobar(infobars::InfoBar* infobar, bool display);
// Manages this object as an observer of infobars.
ScopedObserver<infobars::InfoBarManager, infobars::InfoBarManager::Observer>
infobar_observer_;
// Delegate which displays the Infobar badge.
__weak id<InfobarBadgeTabHelperDelegate> delegate_ = nil;
WEB_STATE_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(InfobarBadgeTabHelper);
};
#endif // IOS_CHROME_BROWSER_INFOBARS_INFOBAR_BADGE_TAB_HELPER_H_
// Copyright 2019 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.
#include "ios/chrome/browser/infobars/infobar_badge_tab_helper.h"
#include "ios/chrome/browser/infobars/infobar.h"
#include "ios/chrome/browser/infobars/infobar_badge_tab_helper_delegate.h"
#include "ios/chrome/browser/infobars/infobar_manager_impl.h"
#import "ios/chrome/browser/ui/infobars/infobar_feature.h"
#import "ios/chrome/browser/ui/infobars/infobar_ui_delegate.h"
#import "ios/web/public/web_state/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#pragma mark - Public
// static
void InfobarBadgeTabHelper::CreateForWebState(web::WebState* web_state) {
DCHECK(web_state);
if (!FromWebState(web_state)) {
web_state->SetUserData(
UserDataKey(), base::WrapUnique(new InfobarBadgeTabHelper(web_state)));
}
}
void InfobarBadgeTabHelper::SetDelegate(
id<InfobarBadgeTabHelperDelegate> delegate) {
delegate_ = delegate;
}
InfobarBadgeTabHelper::~InfobarBadgeTabHelper() = default;
#pragma mark - Private
InfobarBadgeTabHelper::InfobarBadgeTabHelper(web::WebState* web_state)
: infobar_observer_(this) {
infobars::InfoBarManager* infoBarManager =
InfoBarManagerImpl::FromWebState(web_state);
if (infoBarManager) {
infobar_observer_.Add(infoBarManager);
}
}
#pragma mark InfobarObserver
void InfobarBadgeTabHelper::OnInfoBarAdded(infobars::InfoBar* infobar) {
this->UpdateBadgeForInfobar(infobar, true);
}
void InfobarBadgeTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar,
bool animate) {
this->UpdateBadgeForInfobar(infobar, false);
}
void InfobarBadgeTabHelper::OnInfoBarReplaced(infobars::InfoBar* old_infobar,
infobars::InfoBar* new_infobar) {}
void InfobarBadgeTabHelper::OnManagerShuttingDown(
infobars::InfoBarManager* manager) {
infobar_observer_.Remove(manager);
}
#pragma mark Helpers
void InfobarBadgeTabHelper::UpdateBadgeForInfobar(infobars::InfoBar* infobar,
bool display) {
InfoBarIOS* infobar_ios = static_cast<InfoBarIOS*>(infobar);
id<InfobarUIDelegate> controller_ = infobar_ios->InfobarUIDelegate();
if (IsInfobarUIRebootEnabled() && [controller_ isPresented]) {
[delegate_ displayBadge:display];
}
}
WEB_STATE_USER_DATA_KEY_IMPL(InfobarBadgeTabHelper)
// Copyright 2019 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_INFOBARS_INFOBAR_BADGE_TAB_HELPER_DELEGATE_H_
#define IOS_CHROME_BROWSER_INFOBARS_INFOBAR_BADGE_TAB_HELPER_DELEGATE_H_
// Delegate used by InfobarBadgeTabHelper to manage the Infobar badges.
@protocol InfobarBadgeTabHelperDelegate
// Asks the delegate to display or stop displaying a badge.
- (void)displayBadge:(BOOL)display;
@end
#endif // IOS_CHROME_BROWSER_INFOBARS_INFOBAR_BADGE_TAB_HELPER_DELEGATE_H_
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