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") { ...@@ -22,9 +22,12 @@ source_set("coordinator") {
":ntp", ":ntp",
":ntp_internal", ":ntp_internal",
"//ios/chrome/browser/browser_state", "//ios/chrome/browser/browser_state",
"//ios/chrome/browser/content_settings",
"//ios/chrome/browser/main:public", "//ios/chrome/browser/main:public",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/content_suggestions", "//ios/chrome/browser/ui/content_suggestions",
"//ios/chrome/browser/ui/coordinators:chrome_coordinators", "//ios/chrome/browser/ui/coordinators:chrome_coordinators",
"//ios/chrome/browser/ui/settings/privacy",
"//ios/chrome/browser/url_loading", "//ios/chrome/browser/url_loading",
"//ios/chrome/browser/web_state_list", "//ios/chrome/browser/web_state_list",
"//ios/public/provider/chrome/browser/voice", "//ios/public/provider/chrome/browser/voice",
...@@ -108,6 +111,7 @@ source_set("ntp_internal") { ...@@ -108,6 +111,7 @@ source_set("ntp_internal") {
"//ios/chrome/browser/ui/favicon", "//ios/chrome/browser/ui/favicon",
"//ios/chrome/browser/ui/overscroll_actions", "//ios/chrome/browser/ui/overscroll_actions",
"//ios/chrome/browser/ui/settings/cells", "//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/buttons",
"//ios/chrome/browser/ui/toolbar/public", "//ios/chrome/browser/ui/toolbar/public",
"//ios/chrome/browser/ui/util", "//ios/chrome/browser/ui/util",
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE; - (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
// Switch to manage cookies blocking. // Switch to manage cookies blocking.
@property(nonatomic, strong, readonly) UISwitch* switchView; @property(nonatomic, strong, readonly) UISwitch* cookiesBlockedSwitch;
@end @end
......
...@@ -67,14 +67,10 @@ const CGFloat kVerticalLabelMargin = 6.0f; ...@@ -67,14 +67,10 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[self addSubview:descriptionLabel]; [self addSubview:descriptionLabel];
// Cookies switch. // Cookies switch.
_switchView = [[UISwitch alloc] init]; _cookiesBlockedSwitch = [[UISwitch alloc] init];
[_switchView setOn:NO]; _cookiesBlockedSwitch.translatesAutoresizingMaskIntoConstraints = NO;
[_switchView addTarget:self [self addSubview:_cookiesBlockedSwitch];
action:@selector(onCookieSwitchToggled:) [_cookiesBlockedSwitch
forControlEvents:UIControlEventValueChanged];
_switchView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_switchView];
[_switchView
setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1 setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1
forAxis: forAxis:
UILayoutConstraintAxisHorizontal]; UILayoutConstraintAxisHorizontal];
...@@ -84,7 +80,7 @@ const CGFloat kVerticalLabelMargin = 6.0f; ...@@ -84,7 +80,7 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[titleLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor [titleLabel.leadingAnchor constraintEqualToAnchor:self.leadingAnchor
constant:kHorizontalSpacing], constant:kHorizontalSpacing],
[titleLabel.trailingAnchor [titleLabel.trailingAnchor
constraintEqualToAnchor:_switchView.leadingAnchor constraintEqualToAnchor:_cookiesBlockedSwitch.leadingAnchor
constant:-kHorizontalSpacing], constant:-kHorizontalSpacing],
[titleLabel.trailingAnchor [titleLabel.trailingAnchor
constraintEqualToAnchor:descriptionLabel.trailingAnchor], constraintEqualToAnchor:descriptionLabel.trailingAnchor],
...@@ -101,19 +97,16 @@ const CGFloat kVerticalLabelMargin = 6.0f; ...@@ -101,19 +97,16 @@ const CGFloat kVerticalLabelMargin = 6.0f;
[descriptionLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor [descriptionLabel.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
constant:-kVerticalSpacing], constant:-kVerticalSpacing],
// switchView constraints. // cookiesBlockedSwitch constraints.
[_switchView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor [_cookiesBlockedSwitch.trailingAnchor
constant:-kHorizontalSpacing], constraintEqualToAnchor:self.trailingAnchor
[_switchView.centerYAnchor constraintEqualToAnchor:self.centerYAnchor], constant:-kHorizontalSpacing],
[_cookiesBlockedSwitch.centerYAnchor
constraintEqualToAnchor:self.centerYAnchor],
]; ];
[NSLayoutConstraint activateConstraints:constraints]; [NSLayoutConstraint activateConstraints:constraints];
} }
return self; return self;
} }
#pragma mark - Private
- (void)onCookieSwitchToggled:(UISwitch*)paramSender {
// TODO(crbug.com/1063824): Implement this.
}
@end @end
...@@ -16,6 +16,9 @@ class UrlLoadingBrowserAgent; ...@@ -16,6 +16,9 @@ class UrlLoadingBrowserAgent;
- (instancetype)initWithFrame:(CGRect)frame - (instancetype)initWithFrame:(CGRect)frame
URLLoader:(UrlLoadingBrowserAgent*)URLLoader; URLLoader:(UrlLoadingBrowserAgent*)URLLoader;
// Switch to manage cookies blocking.
@property(nonatomic, readonly) UISwitch* cookiesBlockedSwitch;
@end @end
#endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_H_ #endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_H_
...@@ -120,6 +120,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) { ...@@ -120,6 +120,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
} // namespace } // namespace
@interface IncognitoView ()
@property(nonatomic, strong) IncognitoCookiesView* cookiesView;
@end
@implementation IncognitoView { @implementation IncognitoView {
UIView* _containerView; UIView* _containerView;
UIStackView* _stackView; UIStackView* _stackView;
...@@ -278,6 +284,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) { ...@@ -278,6 +284,12 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
return self; return self;
} }
#pragma mark - Properties
- (UISwitch*)cookiesBlockedSwitch {
return self.cookiesView.cookiesBlockedSwitch;
}
#pragma mark - UIView overrides #pragma mark - UIView overrides
- (void)didMoveToSuperview { - (void)didMoveToSuperview {
...@@ -462,8 +474,8 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) { ...@@ -462,8 +474,8 @@ NSAttributedString* FormatHTMLListForUILabel(NSString* listString) {
} }
- (void)addCookiesViewController { - (void)addCookiesViewController {
IncognitoCookiesView* cookiesView = [[IncognitoCookiesView alloc] init]; self.cookiesView = [[IncognitoCookiesView alloc] init];
[_stackView addArrangedSubview:cookiesView]; [_stackView addArrangedSubview:self.cookiesView];
} }
@end @end
...@@ -7,16 +7,21 @@ ...@@ -7,16 +7,21 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
class UrlLoadingBrowserAgent; #import "ios/chrome/browser/ui/settings/privacy/cookies_consumer.h"
@protocol NewTabPageControllerDelegate; @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 // Init with the given loader object. |loader| may be nil, but isn't
// retained so it must outlive this controller. // retained so it must outlive this controller.
- (instancetype)initWithUrlLoader:(UrlLoadingBrowserAgent*)URLLoader; - (instancetype)initWithUrlLoader:(UrlLoadingBrowserAgent*)URLLoader;
// Handler used to update Cookies settings.
@property(nonatomic, weak) id<PrivacyCookiesCommands> handler;
@end @end
#endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_CONTROLLER_H_ #endif // IOS_CHROME_BROWSER_UI_NTP_INCOGNITO_VIEW_CONTROLLER_H_
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
#include <string> #include <string>
#include "components/content_settings/core/common/features.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.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/commands/browser_commands.h"
#import "ios/chrome/browser/ui/ntp/incognito_view.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/ui/util/uikit_ui_util.h"
#import "ios/chrome/browser/url_loading/url_loading_browser_agent.h" #import "ios/chrome/browser/url_loading/url_loading_browser_agent.h"
#import "ios/chrome/common/ui/colors/dynamic_color_util.h" #import "ios/chrome/common/ui/colors/dynamic_color_util.h"
...@@ -19,8 +22,12 @@ ...@@ -19,8 +22,12 @@
#endif #endif
@interface IncognitoViewController () @interface IncognitoViewController ()
// The scrollview containing the actual views. // The scrollview containing the actual views.
@property(nonatomic, strong) IncognitoView* incognitoView; @property(nonatomic, strong) IncognitoView* incognitoView;
@property(nonatomic, assign) CookiesSettingType cookiesSettingSelected;
@end @end
@implementation IncognitoViewController { @implementation IncognitoViewController {
...@@ -45,17 +52,65 @@ ...@@ -45,17 +52,65 @@
URLLoader:_URLLoader]; URLLoader:_URLLoader];
[self.incognitoView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | [self.incognitoView setAutoresizingMask:UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth]; UIViewAutoresizingFlexibleWidth];
UIColor* backgroundColor = UIColor* backgroundColor =
color::DarkModeDynamicColor([UIColor colorNamed:kBackgroundColor], true, color::DarkModeDynamicColor([UIColor colorNamed:kBackgroundColor], true,
[UIColor colorNamed:kBackgroundDarkColor]); [UIColor colorNamed:kBackgroundDarkColor]);
self.incognitoView.backgroundColor = backgroundColor; self.incognitoView.backgroundColor = backgroundColor;
[self.view addSubview:self.incognitoView]; [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 { - (void)dealloc {
[_incognitoView setDelegate:nil]; [_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 @end
...@@ -6,11 +6,15 @@ ...@@ -6,11 +6,15 @@
#include "base/metrics/user_metrics.h" #include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.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/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/main/browser.h"
#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_coordinator.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/content_suggestions/content_suggestions_header_view_controller.h"
#import "ios/chrome/browser/ui/ntp/incognito_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/chrome/browser/url_loading/url_loading_browser_agent.h"
#import "ios/web/public/navigation/navigation_context.h" #import "ios/web/public/navigation/navigation_context.h"
#import "ios/web/public/navigation/navigation_item.h" #import "ios/web/public/navigation/navigation_item.h"
...@@ -29,6 +33,7 @@ ...@@ -29,6 +33,7 @@
// View controller for incognito. // View controller for incognito.
@property(nonatomic, strong) IncognitoViewController* incognitoViewController; @property(nonatomic, strong) IncognitoViewController* incognitoViewController;
@property(nonatomic, strong) PrivacyCookiesMediator* mediator;
@end @end
...@@ -54,6 +59,17 @@ ...@@ -54,6 +59,17 @@
UrlLoadingBrowserAgent::FromBrowser(self.browser); UrlLoadingBrowserAgent::FromBrowser(self.browser);
self.incognitoViewController = self.incognitoViewController =
[[IncognitoViewController alloc] initWithUrlLoader:URLLoader]; [[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 { } else {
DCHECK(!self.contentSuggestionsCoordinator); DCHECK(!self.contentSuggestionsCoordinator);
self.contentSuggestionsCoordinator = [[ContentSuggestionsCoordinator alloc] self.contentSuggestionsCoordinator = [[ContentSuggestionsCoordinator alloc]
...@@ -73,6 +89,7 @@ ...@@ -73,6 +89,7 @@
[self.contentSuggestionsCoordinator stop]; [self.contentSuggestionsCoordinator stop];
self.contentSuggestionsCoordinator = nil; self.contentSuggestionsCoordinator = nil;
self.incognitoViewController = nil; self.incognitoViewController = nil;
self.mediator = nil;
self.started = NO; 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