Commit 749bbfaf authored by Ewann's avatar Ewann Committed by Commit Bot

[iOS][Page-Info] Adds Cookies mediator

This CL adds a mediator that handle Cookies settings for the page info.

Bug: 103891
Change-Id: I03934822e4bf9cd3d8f7b57e1c777ed9c0fb934c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2204122Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Ewann Pellé <ewannpv@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775036}
parent 5bc0971b
......@@ -7,6 +7,10 @@ source_set("page_info") {
sources = [
"legacy_page_info_view_controller.h",
"legacy_page_info_view_controller.mm",
"page_info_cookies_consumer.h",
"page_info_cookies_delegate.h",
"page_info_cookies_description.h",
"page_info_cookies_description.mm",
"page_info_site_security_description.h",
"page_info_site_security_description.mm",
"page_info_view_controller.h",
......@@ -69,6 +73,8 @@ source_set("features") {
source_set("coordinator") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"page_info_cookies_mediator.h",
"page_info_cookies_mediator.mm",
"page_info_coordinator.h",
"page_info_coordinator.mm",
"page_info_legacy_coordinator.h",
......@@ -84,19 +90,23 @@ source_set("coordinator") {
"resources:page_info_info",
"resources:page_info_offline",
"//base",
"//components/content_settings/core/browser",
"//components/security_state/core",
"//components/ssl_errors",
"//components/strings",
"//ios/chrome/app/strings",
"//ios/chrome/browser",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/content_settings",
"//ios/chrome/browser/main",
"//ios/chrome/browser/reading_list",
"//ios/chrome/browser/tabs",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/commands",
"//ios/chrome/browser/ui/coordinators:chrome_coordinators",
"//ios/chrome/browser/ui/fullscreen:coordinators",
"//ios/chrome/browser/ui/page_info/requirements",
"//ios/chrome/browser/ui/settings/utils",
"//ios/chrome/browser/ui/table_view",
"//ios/chrome/browser/web_state_list",
"//ios/components/webui:url_constants",
......
// 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_PAGE_INFO_PAGE_INFO_COOKIES_CONSUMER_H_
#define IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_CONSUMER_H_
// Consumer for the page info Cookies.
@protocol PageInfoCookiesConsumer
// Called when the Block Third-Party Cookies switch value has changed.
- (void)cookiesSwitchChanged:(BOOL)value;
@end
#endif // IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_CONSUMER_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.
#ifndef IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DELEGATE_H_
#define IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DELEGATE_H_
// Delegate related to Cookies state.
@protocol PageInfoCookiesDelegate
// Blocks or allows Third-Party Cookies.
- (void)blockThirdPartyCookies:(BOOL)blocked;
@end
#endif // IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DELEGATE_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.
#ifndef IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DESCRIPTION_H_
#define IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DESCRIPTION_H_
#import <Foundation/Foundation.h>
// Config for the information displayed by the page info Cookies section.
@interface PageInfoCookiesDescription : NSObject
// Whether the third party cookies are blocked or not.
@property(nonatomic, assign) BOOL blockThirdPartyCookies;
// Number of blocked Cookies on the current site.
@property(nonatomic, assign) NSInteger blockedCookies;
// Number of Cookies on the current site.
@property(nonatomic, assign) NSInteger cookiesInUse;
@end
#endif // IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_DESCRIPTION_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/page_info/page_info_cookies_description.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation PageInfoCookiesDescription
@end
// 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_PAGE_INFO_PAGE_INFO_COOKIES_MEDIATOR_H_
#define IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_MEDIATOR_H_
#import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/page_info/page_info_cookies_delegate.h"
#import "ios/web/public/web_state_observer_bridge.h"
class HostContentSettingsMap;
@protocol PageInfoCookiesConsumer;
@class PageInfoCookiesDescription;
class PrefService;
// The mediator is pushing the data for the page info Cookies section to the
// consumer.
@interface PageInfoCookiesMediator : NSObject <PageInfoCookiesDelegate>
- (instancetype)init NS_UNAVAILABLE;
// Designated initializer.
- (instancetype)initWithWebState:(web::WebState*)webState
prefService:(PrefService*)prefService
settingsMap:(HostContentSettingsMap*)settingsMap
NS_DESIGNATED_INITIALIZER;
// The consumer for this mediator.
@property(nonatomic, weak) id<PageInfoCookiesConsumer> consumer;
// Returns a configuration for the page info Cookies section to the coordinator.
- (PageInfoCookiesDescription*)cookiesDescription;
@end
#endif // IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_COOKIES_MEDIATOR_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/page_info/page_info_cookies_mediator.h"
#import "base/logging.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/prefs/pref_member.h"
#include "components/prefs/pref_service.h"
#include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_consumer.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_description.h"
#import "ios/chrome/browser/ui/settings/utils/content_setting_backed_boolean.h"
#import "ios/chrome/browser/ui/settings/utils/pref_backed_boolean.h"
#include "ios/web/public/web_state_observer.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
typedef NS_ENUM(NSInteger, CookiesSettingType) {
SettingTypeAllowCookies,
SettingTypeBlockThirdPartyCookiesIncognito,
SettingTypeBlockThirdPartyCookies,
SettingTypeBlockAllCookies,
};
} // namespace
@interface PageInfoCookiesMediator () <BooleanObserver,
PageInfoCookiesDelegate> {
// The preference that decides when the cookie controls UI is enabled.
IntegerPrefMember _prefsCookieControlsMode;
}
// The observable boolean that binds to the "Enable cookie controls" setting
// state.
@property(nonatomic, strong)
ContentSettingBackedBoolean* prefsContentSettingCookieControl;
@end
@implementation PageInfoCookiesMediator
// TODO(crbug.com/1038919): Use webState to retrieve Cookies information.
- (instancetype)initWithWebState:(web::WebState*)webState
prefService:(PrefService*)prefService
settingsMap:(HostContentSettingsMap*)settingsMap {
self = [super init];
if (self) {
__weak PageInfoCookiesMediator* weakSelf = self;
_prefsCookieControlsMode.Init(prefs::kCookieControlsMode, prefService,
base::BindRepeating(^() {
[weakSelf updateConsumer];
}));
_prefsContentSettingCookieControl = [[ContentSettingBackedBoolean alloc]
initWithHostContentSettingsMap:settingsMap
settingID:ContentSettingsType::COOKIES
inverted:NO];
[_prefsContentSettingCookieControl setObserver:self];
}
return self;
}
#pragma mark - Public
- (PageInfoCookiesDescription*)cookiesDescription {
PageInfoCookiesDescription* dataHolder =
[[PageInfoCookiesDescription alloc] init];
dataHolder.blockThirdPartyCookies =
[self cookiesSettingType] == SettingTypeBlockThirdPartyCookies;
// TODO(crbug.com/1038919): Implement this,the value is a placeholder.
dataHolder.blockedCookies = 7;
// TODO(crbug.com/1038919): Implement this,the value is a placeholder.
dataHolder.cookiesInUse = 44;
return dataHolder;
}
#pragma mark - Private
// Updates consumer.
- (void)updateConsumer {
[self.consumer cookiesSwitchChanged:[self cookiesSettingType] ==
SettingTypeBlockThirdPartyCookies];
}
// Returns the cookiesSettingType according to preferences.
- (CookiesSettingType)cookiesSettingType {
if (!self.prefsContentSettingCookieControl.value)
return SettingTypeBlockAllCookies;
const content_settings::CookieControlsMode mode =
static_cast<content_settings::CookieControlsMode>(
_prefsCookieControlsMode.GetValue());
switch (mode) {
case content_settings::CookieControlsMode::kBlockThirdParty:
return SettingTypeBlockThirdPartyCookies;
case content_settings::CookieControlsMode::kIncognitoOnly:
return SettingTypeBlockThirdPartyCookiesIncognito;
default:
return SettingTypeAllowCookies;
}
}
#pragma mark - BooleanObserver
- (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean {
[self updateConsumer];
}
#pragma mark - PageInfoCookiesDelegate
- (void)blockThirdPartyCookies:(BOOL)blocked {
if (blocked) {
[self.prefsContentSettingCookieControl setValue:YES];
_prefsCookieControlsMode.SetValue(static_cast<int>(
content_settings::CookieControlsMode::kBlockThirdParty));
} else {
[self.prefsContentSettingCookieControl setValue:YES];
_prefsCookieControlsMode.SetValue(
static_cast<int>(content_settings::CookieControlsMode::kOff));
}
[self updateConsumer];
}
@end
......@@ -4,14 +4,20 @@
#import "ios/chrome/browser/ui/page_info/page_info_coordinator.h"
#include "components/content_settings/core/common/features.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/reading_list/offline_page_tab_helper.h"
#include "ios/chrome/browser/ui/commands/browser_commands.h"
#import "ios/chrome/browser/ui/commands/command_dispatcher.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_delegate.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_mediator.h"
#import "ios/chrome/browser/ui/page_info/page_info_site_security_description.h"
#import "ios/chrome/browser/ui/page_info/page_info_site_security_mediator.h"
#import "ios/chrome/browser/ui/page_info/page_info_view_controller.h"
#import "ios/chrome/browser/ui/table_view/table_view_navigation_controller.h"
#include "ios/chrome/browser/ui/ui_feature_flags.h"
#import "ios/chrome/browser/web_state_list/web_state_list.h"
#include "ios/web/public/navigation/navigation_item.h"
#include "ios/web/public/navigation/navigation_manager.h"
......@@ -27,6 +33,7 @@
TableViewNavigationController* navigationController;
@property(nonatomic, strong) CommandDispatcher* dispatcher;
@property(nonatomic, strong) PageInfoViewController* viewController;
@property(nonatomic, strong) PageInfoCookiesMediator* cookiesMediator;
@end
......@@ -47,13 +54,26 @@
bool offlinePage =
OfflinePageTabHelper::FromWebState(webState)->presenting_offline_page();
PageInfoSiteSecurityDescription* description =
PageInfoSiteSecurityDescription* siteSecurityDescription =
[PageInfoSiteSecurityMediator configurationForURL:navItem->GetURL()
SSLStatus:navItem->GetSSL()
offlinePage:offlinePage];
if (!siteSecurityDescription.isEmpty &&
base::FeatureList::IsEnabled(content_settings::kImprovedCookieControls)) {
self.cookiesMediator = [[PageInfoCookiesMediator alloc]
initWithWebState:webState
prefService:self.browser->GetBrowserState()->GetPrefs()
settingsMap:ios::HostContentSettingsMapFactory::GetForBrowserState(
self.browser->GetBrowserState())];
}
self.viewController = [[PageInfoViewController alloc]
initWithSiteSecurityDescription:description];
initWithSiteSecurityDescription:siteSecurityDescription
cookiesDescription:[self.cookiesMediator
cookiesDescription]];
self.cookiesMediator.consumer = self.viewController;
self.viewController.delegate = self.cookiesMediator;
self.navigationController =
[[TableViewNavigationController alloc] initWithTable:self.viewController];
......@@ -72,6 +92,7 @@
completion:nil];
self.navigationController = nil;
self.viewController = nil;
self.cookiesMediator = nil;
}
@end
......@@ -7,7 +7,7 @@
#import <UIKit/UIKit.h>
// Types of the different actions the page info site security button can have.
// Types of the different actions the page info Site Security button can have.
typedef NS_ENUM(NSUInteger, PageInfoSiteSecurityButtonAction) {
// No action.
PageInfoSiteSecurityButtonActionNone,
......@@ -17,7 +17,7 @@ typedef NS_ENUM(NSUInteger, PageInfoSiteSecurityButtonAction) {
PageInfoSiteSecurityButtonActionReload,
};
// Config for the information displayed by the page info site security.
// Config for the information displayed by the page info Site Security section.
@interface PageInfoSiteSecurityDescription : NSObject
@property(nonatomic, copy) NSString* siteURL;
......
......@@ -7,23 +7,32 @@
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/page_info/page_info_cookies_consumer.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_description.h"
#import "ios/chrome/browser/ui/page_info/page_info_site_security_description.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_controller.h"
@protocol BrowserCommands;
@protocol PageInfoCookiesDelegate;
// View Controller for displaying the page info.
@interface PageInfoViewController : ChromeTableViewController
@interface PageInfoViewController
: ChromeTableViewController <PageInfoCookiesConsumer>
// Designated initializer.
- (instancetype)initWithSiteSecurityDescription:
(PageInfoSiteSecurityDescription*)description NS_DESIGNATED_INITIALIZER;
(PageInfoSiteSecurityDescription*)siteSecurityDescription
cookiesDescription:
(PageInfoCookiesDescription*)cookiesDescription
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithStyle:(UITableViewStyle)style NS_UNAVAILABLE;
// Handler used to navigate outside the page info.
@property(nonatomic, weak) id<BrowserCommands> handler;
// Delegate used to update Cookies settings.
@property(nonatomic, weak) id<PageInfoCookiesDelegate> delegate;
@end
#endif // IOS_CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_VIEW_CONTROLLER_H_
......@@ -9,6 +9,8 @@
#include "ios/chrome/browser/chrome_url_constants.h"
#include "ios/chrome/browser/ui/commands/browser_commands.h"
#import "ios/chrome/browser/ui/page_info/features.h"
#import "ios/chrome/browser/ui/page_info/page_info_cookies_delegate.h"
#import "ios/chrome/browser/ui/settings/cells/settings_switch_cell.h"
#import "ios/chrome/browser/ui/settings/cells/settings_switch_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_detail_icon_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_multi_detail_text_item.h"
......@@ -49,6 +51,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
@property(nonatomic, strong)
PageInfoSiteSecurityDescription* pageInfoSecurityDescription;
@property(nonatomic, strong)
PageInfoCookiesDescription* pageInfoCookiesDescription;
@end
......@@ -57,10 +61,13 @@ typedef NS_ENUM(NSInteger, ItemType) {
#pragma mark - UIViewController
- (instancetype)initWithSiteSecurityDescription:
(PageInfoSiteSecurityDescription*)description {
(PageInfoSiteSecurityDescription*)siteSecurityDescription
cookiesDescription:(PageInfoCookiesDescription*)
cookiesDescription {
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
_pageInfoSecurityDescription = description;
_pageInfoSecurityDescription = siteSecurityDescription;
_pageInfoCookiesDescription = cookiesDescription;
}
return self;
}
......@@ -130,32 +137,30 @@ typedef NS_ENUM(NSInteger, ItemType) {
[self.tableViewModel setHeader:cookiesHeader
forSectionWithIdentifier:SectionIdentifierCookiesContent];
// TODO(crbug.com/1038919): Implement this.
SettingsSwitchItem* cookiesSwitchItem =
[[SettingsSwitchItem alloc] initWithType:ItemTypeCookiesSwitch];
cookiesSwitchItem.text =
l10n_util::GetNSString(IDS_IOS_PAGE_INFO_COOKIES_SWITCH_LABEL);
cookiesSwitchItem.on = self.pageInfoCookiesDescription.blockThirdPartyCookies;
[self.tableViewModel addItem:cookiesSwitchItem
toSectionWithIdentifier:SectionIdentifierCookiesContent];
// TODO(crbug.com/1038919): Implement this.
TableViewMultiDetailTextItem* cookiesBlocked =
[[TableViewMultiDetailTextItem alloc]
initWithType:ItemTypeCookiesBlocked];
cookiesBlocked.text =
l10n_util::GetNSString(IDS_IOS_PAGE_INFO_COOKIES_BLOCKED_LABEL);
// TODO(crbug.com/1038919): Remove this.
cookiesBlocked.trailingDetailText = @"2";
cookiesBlocked.trailingDetailText = [NSString
stringWithFormat:@"%zd", self.pageInfoCookiesDescription.blockedCookies];
[self.tableViewModel addItem:cookiesBlocked
toSectionWithIdentifier:SectionIdentifierCookiesContent];
// TODO(crbug.com/1038919): Implement this.
TableViewMultiDetailTextItem* cookiesInUse =
[[TableViewMultiDetailTextItem alloc] initWithType:ItemTypeCookiesInUse];
cookiesInUse.text =
l10n_util::GetNSString(IDS_IOS_PAGE_INFO_COOKIES_IN_USE_LABEL);
// TODO(crbug.com/1038919): Remove this.
cookiesInUse.trailingDetailText = @"2";
cookiesInUse.trailingDetailText = [NSString
stringWithFormat:@"%zd", self.pageInfoCookiesDescription.cookiesInUse];
[self.tableViewModel addItem:cookiesInUse
toSectionWithIdentifier:SectionIdentifierCookiesContent];
......@@ -189,6 +194,13 @@ typedef NS_ENUM(NSInteger, ItemType) {
tableViewTextLinkCell.delegate = self;
}
if (item.type == ItemTypeCookiesSwitch) {
SettingsSwitchCell* switchCell =
base::mac::ObjCCastStrict<SettingsSwitchCell>(cellToReturn);
[switchCell.switchView addTarget:self
action:@selector(cookiesSwitchToogled:)
forControlEvents:UIControlEventValueChanged];
}
return cellToReturn;
}
......@@ -222,4 +234,23 @@ typedef NS_ENUM(NSInteger, ItemType) {
return nil;
}
#pragma mark - Private
// Updates Cookies preference according to the switch state.
- (void)cookiesSwitchToogled:(UISwitch*)switchView {
[self.delegate blockThirdPartyCookies:switchView.isOn];
}
#pragma mark - PageInfoCookiesConsumer
- (void)cookiesSwitchChanged:(BOOL)value {
NSIndexPath* switchPath = [self.tableViewModel
indexPathForItemType:ItemTypeCookiesSwitch
sectionIdentifier:SectionIdentifierCookiesContent];
SettingsSwitchItem* switchItem =
base::mac::ObjCCastStrict<SettingsSwitchItem>(
[self.tableViewModel itemAtIndexPath:switchPath]);
switchItem.on = value;
}
@end
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