Commit f55c6b19 authored by gambard's avatar gambard Committed by Commit Bot

Have a settings to opt out of ContentSuggestions

The user needs to be able to opt out of Content Suggestions if needed.
Android is coupling the Omnibox suggestions and the Content suggestions
setting but it is probably clearer to have it separated.
However the names are misleading and need to be changed.

TEST= Toggle the switch Settings->Privacy->"Show Content Suggestions".
- When enabled the suggestions are shown as expected.
- When disabled, the remote suggestions("Articles for You") currently
  displayed are removed.
  When opening a new NTP the Articles for You section is empty.
The Reading List section is unaffected.

Bug: 751623
Change-Id: I076f2f25697741e6f351a90da500a6ef33b21bf1
Reviewed-on: https://chromium-review.googlesource.com/598069
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarEric Noyau <noyau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491384}
parent 69f889fb
......@@ -809,6 +809,9 @@ To adjust your search term, long press to select. To refine your search, slide t
<message name="IDS_IOS_OPTIONS_ADVANCED_TAB_LABEL" desc="The title of the Advanced section of the Settings page. [Length: 20em] [iOS only]">
Advanced
</message>
<message name="IDS_IOS_OPTIONS_CONTENT_SUGGESTIONS" desc="Title for the option to turn on/off content suggestions. [Length: 20em] [iOS only]">
Show Content Suggestions
</message>
<message name="IDS_IOS_OPTIONS_CONTINUITY_LABEL" desc="Title for group of items in the Privacy settings table, including Handoff. [Length: 10em] [iOS only]">
Other Devices
</message>
......
......@@ -43,6 +43,7 @@
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/favicon/ios_chrome_large_icon_service_factory.h"
#include "ios/chrome/browser/history/history_service_factory.h"
#include "ios/chrome/browser/pref_names.h"
#include "ios/chrome/browser/reading_list/reading_list_model_factory.h"
#include "ios/chrome/browser/signin/oauth2_token_service_factory.h"
#include "ios/chrome/browser/signin/signin_manager_factory.h"
......@@ -185,6 +186,7 @@ void RegisterRemoteSuggestionsProvider(ContentSuggestionsService* service,
base::Bind(&ParseJson), GetFetchEndpoint(GetChannel()), api_key,
service->user_classifier());
std::string pref_name = prefs::kContentSuggestionsRemoteEnabled;
auto provider = base::MakeUnique<RemoteSuggestionsProviderImpl>(
service, prefs, GetApplicationContext()->GetApplicationLocale(),
service->category_ranker(), service->remote_suggestions_scheduler(),
......@@ -193,7 +195,7 @@ void RegisterRemoteSuggestionsProvider(ContentSuggestionsService* service,
request_context.get()),
base::MakeUnique<RemoteSuggestionsDatabase>(database_dir, task_runner),
base::MakeUnique<RemoteSuggestionsStatusService>(signin_manager, prefs,
std::string()),
pref_name),
/*prefetched_pages_tracker=*/nullptr);
service->remote_suggestions_scheduler()->SetProvider(provider.get());
......
......@@ -100,6 +100,10 @@ const char kSavingBrowserHistoryDisabled[] = "history.saving_disabled";
// Boolean that is true when Suggest support is enabled.
const char kSearchSuggestEnabled[] = "search.suggest_enabled";
// Boolean that is true when Suggest support is enabled.
const char kContentSuggestionsRemoteEnabled[] =
"content_suggestions.remote_enabled";
// A boolean pref set to true if prediction of network actions is allowed.
// Actions include prerendering of web pages.
// NOTE: The "dns_prefetching.enabled" value is used so that historical user
......
......@@ -30,6 +30,7 @@ extern const char kMetricsReportingWifiOnly[];
extern const char kNtpShownPage[];
extern const char kSavingBrowserHistoryDisabled[];
extern const char kSearchSuggestEnabled[];
extern const char kContentSuggestionsRemoteEnabled[];
// TODO(crbug.com/538573): Consider migrating from these two bools to an integer
// since only three cases are supported.
......
......@@ -156,6 +156,9 @@ void RegisterBrowserStatePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kSearchSuggestEnabled, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kContentSuggestionsRemoteEnabled, true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(prefs::kSavingBrowserHistoryDisabled, false);
registry->RegisterIntegerPref(prefs::kNtpShownPage, 1 << 10);
......
......@@ -246,8 +246,13 @@ const CGFloat kNumberOfMostVisitedLines = 2;
- (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo {
SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
NSInteger section = [self.collectionViewController.collectionViewModel
sectionIdentifierForSection:sectionIdentifier];
CSCollectionViewModel* model =
self.collectionViewController.collectionViewModel;
if (![model hasSectionForSectionIdentifier:sectionIdentifier])
return;
NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
[self.collectionViewController dismissSection:section];
}
......
......@@ -67,6 +67,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeWebServicesHeader,
ItemTypeWebServicesFooter,
ItemTypeWebServicesShowSuggestions,
ItemTypeWebServicesShowContentSuggestions,
ItemTypeWebServicesSendUsageData,
ItemTypeWebServicesDoNotTrack,
ItemTypeWebServicesPhysicalWeb,
......@@ -79,8 +80,11 @@ typedef NS_ENUM(NSInteger, ItemType) {
PrefObserverDelegate> {
ios::ChromeBrowserState* _browserState; // weak
PrefBackedBoolean* _suggestionsEnabled;
PrefBackedBoolean* _contentSuggestionsEnabled;
// The item related to the switch for the show suggestions setting.
CollectionViewSwitchItem* _showSuggestionsItem;
// The item related to the switch for the show content suggestions setting.
CollectionViewSwitchItem* _showContentSuggestionsItem;
// Pref observer to track changes to prefs.
std::unique_ptr<PrefObserverBridge> _prefObserverBridge;
......@@ -96,6 +100,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
// Initialization methods for various model items.
- (CollectionViewItem*)handoffDetailItem;
- (CollectionViewSwitchItem*)showSuggestionsSwitchItem;
- (CollectionViewSwitchItem*)showContentSuggestionsSwitchItem;
- (CollectionViewItem*)showSuggestionsFooterItem;
- (CollectionViewItem*)clearBrowsingDetailItem;
- (CollectionViewItem*)sendUsageDetailItem;
......@@ -122,6 +127,10 @@ typedef NS_ENUM(NSInteger, ItemType) {
initWithPrefService:_browserState->GetPrefs()
prefName:prefs::kSearchSuggestEnabled];
[_suggestionsEnabled setObserver:self];
_contentSuggestionsEnabled = [[PrefBackedBoolean alloc]
initWithPrefService:_browserState->GetPrefs()
prefName:prefs::kContentSuggestionsRemoteEnabled];
[_contentSuggestionsEnabled setObserver:self];
PrefService* prefService = _browserState->GetPrefs();
......@@ -147,6 +156,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
- (void)dealloc {
[_suggestionsEnabled setObserver:nil];
[_contentSuggestionsEnabled setObserver:nil];
}
#pragma mark - SettingsRootCollectionViewController
......@@ -181,6 +191,12 @@ typedef NS_ENUM(NSInteger, ItemType) {
[model addItem:_showSuggestionsItem
toSectionWithIdentifier:SectionIdentifierWebServices];
if (experimental_flags::IsSuggestionsUIEnabled()) {
_showContentSuggestionsItem = [self showContentSuggestionsSwitchItem];
[model addItem:_showContentSuggestionsItem
toSectionWithIdentifier:SectionIdentifierWebServices];
}
[model addItem:[self sendUsageDetailItem]
toSectionWithIdentifier:SectionIdentifierWebServices];
......@@ -231,6 +247,17 @@ typedef NS_ENUM(NSInteger, ItemType) {
return showSuggestionsSwitchItem;
}
- (CollectionViewSwitchItem*)showContentSuggestionsSwitchItem {
CollectionViewSwitchItem* showContentSuggestionsSwitchItem =
[[CollectionViewSwitchItem alloc]
initWithType:ItemTypeWebServicesShowContentSuggestions];
showContentSuggestionsSwitchItem.text =
l10n_util::GetNSString(IDS_IOS_OPTIONS_CONTENT_SUGGESTIONS);
showContentSuggestionsSwitchItem.on = [_contentSuggestionsEnabled value];
return showContentSuggestionsSwitchItem;
}
- (CollectionViewItem*)showSuggestionsFooterItem {
CollectionViewFooterItem* showSuggestionsFooterItem =
[[CollectionViewFooterItem alloc] initWithType:ItemTypeWebServicesFooter];
......@@ -314,6 +341,12 @@ typedef NS_ENUM(NSInteger, ItemType) {
[switchCell.switchView addTarget:self
action:@selector(showSuggestionsToggled:)
forControlEvents:UIControlEventValueChanged];
} else if (itemType == ItemTypeWebServicesShowContentSuggestions) {
CollectionViewSwitchCell* switchCell =
base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell);
[switchCell.switchView addTarget:self
action:@selector(showContentSuggestionsToggled:)
forControlEvents:UIControlEventValueChanged];
}
return cell;
......@@ -416,13 +449,21 @@ typedef NS_ENUM(NSInteger, ItemType) {
#pragma mark - BooleanObserver
- (void)booleanDidChange:(id<ObservableBoolean>)observableBoolean {
DCHECK_EQ(observableBoolean, _suggestionsEnabled);
// Update the item.
_showSuggestionsItem.on = [_suggestionsEnabled value];
// Update the cell.
[self reconfigureCellsForItems:@[ _showSuggestionsItem ]];
if (observableBoolean == _suggestionsEnabled) {
// Update the item.
_showSuggestionsItem.on = [_suggestionsEnabled value];
// Update the cell.
[self reconfigureCellsForItems:@[ _showSuggestionsItem ]];
} else if (observableBoolean == _contentSuggestionsEnabled) {
// Update the item.
_showContentSuggestionsItem.on = [_contentSuggestionsEnabled value];
// Update the cell.
[self reconfigureCellsForItems:@[ _showContentSuggestionsItem ]];
} else {
NOTREACHED();
}
}
#pragma mark - Actions
......@@ -445,6 +486,24 @@ typedef NS_ENUM(NSInteger, ItemType) {
[_suggestionsEnabled setValue:isOn];
}
- (void)showContentSuggestionsToggled:(UISwitch*)sender {
NSIndexPath* switchPath = [self.collectionViewModel
indexPathForItemType:ItemTypeWebServicesShowContentSuggestions
sectionIdentifier:SectionIdentifierWebServices];
CollectionViewSwitchItem* switchItem =
base::mac::ObjCCastStrict<CollectionViewSwitchItem>(
[self.collectionViewModel itemAtIndexPath:switchPath]);
CollectionViewSwitchCell* switchCell =
base::mac::ObjCCastStrict<CollectionViewSwitchCell>(
[self.collectionView cellForItemAtIndexPath:switchPath]);
DCHECK_EQ(switchCell.switchView, sender);
BOOL isOn = switchCell.switchView.isOn;
switchItem.on = isOn;
[_contentSuggestionsEnabled setValue:isOn];
}
#pragma mark - PrefObserverDelegate
- (void)onPreferenceChanged:(const std::string&)preferenceName {
......
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