Commit 7332e079 authored by Ewann's avatar Ewann Committed by Commit Bot

[iOS][Chrome-Guard] Adds Cookies mediator to NTP

This CL links the cookies mediator to the NTP. The switch also updates
his state according to the current cookies setting.

Bug: 1063824
Change-Id: I072f14668d236bde6d4327156696619cb6090dc2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2247029
Commit-Queue: Ewann Pellé <ewannpv@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779732}
parent b679ffdc
......@@ -22,9 +22,12 @@ source_set("coordinator") {
":ntp",
":ntp_internal",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/content_settings",
"//ios/chrome/browser/main:public",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/content_suggestions",
"//ios/chrome/browser/ui/coordinators:chrome_coordinators",
"//ios/chrome/browser/ui/settings/privacy",
"//ios/chrome/browser/url_loading",
"//ios/chrome/browser/web_state_list",
"//ios/public/provider/chrome/browser/voice",
......@@ -108,6 +111,7 @@ source_set("ntp_internal") {
"//ios/chrome/browser/ui/favicon",
"//ios/chrome/browser/ui/overscroll_actions",
"//ios/chrome/browser/ui/settings/cells",
"//ios/chrome/browser/ui/settings/privacy:privacy_ui",
"//ios/chrome/browser/ui/toolbar/buttons",
"//ios/chrome/browser/ui/toolbar/public",
"//ios/chrome/browser/ui/util",
......
......@@ -15,7 +15,7 @@
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
// Switch to manage cookies blocking.
@property(nonatomic, strong, readonly) UISwitch* switchView;
@property(nonatomic, strong, readonly) UISwitch* cookiesBlockedSwitch;
@end
......
......@@ -67,14 +67,10 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[self addSubview:descriptionLabel];
// Cookies switch.
_switchView = [[UISwitch alloc] init];
[_switchView setOn:NO];
[_switchView addTarget:self
action:@selector(onCookieSwitchToggled:)
forControlEvents:UIControlEventValueChanged];
_switchView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_switchView];
[_switchView
_cookiesBlockedSwitch = [[UISwitch alloc] init];
_cookiesBlockedSwitch.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_cookiesBlockedSwitch];
[_cookiesBlockedSwitch
setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1
forAxis:
UILayoutConstraintAxisHorizontal];
......@@ -84,7 +80,7 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[titleLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor
constant:kHorizontalSpacing],
[titleLabel.trailingAnchor
constraintEqualToAnchor:_switchView.leadingAnchor
constraintEqualToAnchor:_cookiesBlockedSwitch.leadingAnchor
constant:-kHorizontalSpacing],
[titleLabel.trailingAnchor
constraintEqualToAnchor:descriptionLabel.trailingAnchor],
......@@ -101,19 +97,16 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[descriptionLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
constant:-kVerticalSpacing],
// switchView constraints.
[_switchView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor
constant:-kHorizontalSpacing],
[_switchView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
// cookiesBlockedSwitch constraints.
[_cookiesBlockedSwitch.trailingAnchor
constraintEqualToAnchor:self.trailingAnchor
constant:-kHorizontalSpacing],
[_cookiesBlockedSwitch.centerYAnchor
constraintEqualToAnchor:self.centerYAnchor],
];
[NSLayoutConstraint activateConstraints:constraints];
}
return self;
}
#pragma mark - Private
- (void)onCookieSwitchToggled:(UISwitch*)paramSender {
// TODO(crbug.com/1063824): Implement this.
}
@end
......@@ -16,6 +16,9 @@ class UrlLoadingBrowserAgent;
- (instancetype)initWithFrame:(CGRect)frame
URLLoader:(UrlLoadingBrowserAgent*)URLLoader;
// Switch to manage cookies blocking.
@property(nonatomic, readonly) UISwitch* cookiesBlockedSwitch;
@end
#endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_H_
......@@ -120,6 +120,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
} // namespace
@interface IncognitoView ()
@property(nonatomic, strong) IncognitoCookiesView* cookiesView;
@end
@implementation IncognitoView {
UIView* _containerView;
UIStackView* _stackView;
......@@ -278,6 +284,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
return self;
}
#pragma mark - Properties
- (UISwitch*)cookiesBlockedSwitch {
return self.cookiesView.cookiesBlockedSwitch;
}
#pragma mark - UIView overrides
- (void)didMoveToSuperview {
......@@ -462,8 +474,8 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
}
- (void)addCookiesViewController {
IncognitoCookiesView* cookiesView = [[IncognitoCookiesView alloc] init];
[_stackView addArrangedSubview:cookiesView];
self.cookiesView = [[IncognitoCookiesView alloc] init];
[_stackView addArrangedSubview:self.cookiesView];
}
@end
......@@ -7,16 +7,21 @@
#import <UIKit/UIKit.h>
class UrlLoadingBrowserAgent;
#import "ios/chrome/browser/ui/settings/privacy/cookies_consumer.h"
@protocol NewTabPageControllerDelegate;
@protocol PrivacyCookiesCommands;
class UrlLoadingBrowserAgent;
@interface IncognitoViewController : UIViewController
@interface IncognitoViewController : UIViewController <PrivacyCookiesConsumer>
// Init with the given loader object. |loader| may be nil, but isn't
// retained so it must outlive this controller.
- (instancetype)initWithUrlLoader:(UrlLoadingBrowserAgent*)URLLoader;
// Handler used to update Cookies settings.
@property(nonatomic, weak) id<PrivacyCookiesCommands> handler;
@end
#endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_CONTROLLER_H_
......@@ -6,9 +6,12 @@
#include <string>
#include "components/content_settings/core/common/features.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/ui/commands/browser_commands.h"
#import "ios/chrome/browser/ui/ntp/incognito_view.h"
#import "ios/chrome/browser/ui/settings/privacy/cookies_commands.h"
#include "ios/chrome/browser/ui/ui_feature_flags.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#import "ios/chrome/browser/url_loading/url_loading_browser_agent.h"
#import "ios/chrome/common/ui/colors/dynamic_color_util.h"
......@@ -19,8 +22,12 @@
#endif
@interface IncognitoViewController ()
// The scrollview containing the actual views.
@property(nonatomic, strong) IncognitoView* incognitoView;
@property(nonatomic, assign) CookiesSettingType cookiesSettingSelected;
@end
@implementation IncognitoViewController {
......@@ -45,17 +52,65 @@
URLLoader:_URLLoader];
[self.incognitoView setAutoresizingMask:UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth];
UIColor* backgroundColor =
color::DarkModeDynamicColor([UIColor colorNamed:kBackgroundColor], true,
[UIColor colorNamed:kBackgroundDarkColor]);
self.incognitoView.backgroundColor = backgroundColor;
[self.view addSubview:self.incognitoView];
if (self.incognitoView.cookiesBlockedSwitch) {
[self.incognitoView.cookiesBlockedSwitch
addTarget:self
action:@selector(onCookieSwitchToggled:)
forControlEvents:UIControlEventValueChanged];
// The mediator may have updated the cookies switch before before it's
// returned.
[self cookiesSettingsOptionSelected:self.cookiesSettingSelected];
} else {
// cookiesBlockedSwitch must exists if kImprovedCookieControls
// feature is enabled.
DCHECK(!base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls));
}
}
- (void)dealloc {
[_incognitoView setDelegate:nil];
}
#pragma mark - Private
// Called when the cookies switch is toogled.
- (void)onCookieSwitchToggled:(UISwitch*)cookiesBlockedSwitch {
[self.handler selectedCookiesSettingType:
self.incognitoView.cookiesBlockedSwitch.isOn
? SettingTypeBlockThirdPartyCookiesIncognito
: SettingTypeAllowCookies];
}
#pragma mark - PrivacyCookiesConsumer
- (void)cookiesSettingsOptionSelected:(CookiesSettingType)settingType {
self.cookiesSettingSelected = settingType;
switch (settingType) {
case SettingTypeBlockThirdPartyCookiesIncognito:
self.incognitoView.cookiesBlockedSwitch.enabled = YES;
[self.incognitoView.cookiesBlockedSwitch setOn:YES animated:YES];
break;
case SettingTypeBlockThirdPartyCookies:
self.incognitoView.cookiesBlockedSwitch.enabled = NO;
[self.incognitoView.cookiesBlockedSwitch setOn:YES animated:YES];
break;
case SettingTypeBlockAllCookies:
self.incognitoView.cookiesBlockedSwitch.enabled = NO;
[self.incognitoView.cookiesBlockedSwitch setOn:YES animated:YES];
break;
case SettingTypeAllowCookies:
self.incognitoView.cookiesBlockedSwitch.enabled = YES;
[self.incognitoView.cookiesBlockedSwitch setOn:NO animated:YES];
break;
}
}
@end
......@@ -6,11 +6,15 @@
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.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"
#import "ios/chrome/browser/main/browser.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_coordinator.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_header_view_controller.h"
#import "ios/chrome/browser/ui/ntp/incognito_view_controller.h"
#import "ios/chrome/browser/ui/settings/privacy/cookies_mediator.h"
#include "ios/chrome/browser/ui/ui_feature_flags.h"
#import "ios/chrome/browser/url_loading/url_loading_browser_agent.h"
#import "ios/web/public/navigation/navigation_context.h"
#import "ios/web/public/navigation/navigation_item.h"
......@@ -29,6 +33,7 @@
// View controller for incognito.
@property(nonatomic, strong) IncognitoViewController* incognitoViewController;
@property(nonatomic, strong) PrivacyCookiesMediator* mediator;
@end
......@@ -54,6 +59,17 @@
UrlLoadingBrowserAgent::FromBrowser(self.browser);
self.incognitoViewController =
[[IncognitoViewController alloc] initWithUrlLoader:URLLoader];
if (base::FeatureList::IsEnabled(
content_settings::kImprovedCookieControls)) {
ChromeBrowserState* originalBrowser =
self.browser->GetBrowserState()->GetOriginalChromeBrowserState();
self.mediator = [[PrivacyCookiesMediator alloc]
initWithPrefService:originalBrowser->GetPrefs()
settingsMap:ios::HostContentSettingsMapFactory::
GetForBrowserState(originalBrowser)];
self.mediator.consumer = self.incognitoViewController;
self.incognitoViewController.handler = self.mediator;
}
} else {
DCHECK(!self.contentSuggestionsCoordinator);
self.contentSuggestionsCoordinator = [[ContentSuggestionsCoordinator alloc]
......@@ -73,6 +89,7 @@
[self.contentSuggestionsCoordinator stop];
self.contentSuggestionsCoordinator = nil;
self.incognitoViewController = nil;
self.mediator = nil;
self.started = NO;
}
......
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