Commit 7063e0ac authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

Clear suggestions when language changes

When the language of the device is changed, the suggestions should be
displayed in the new language.
This CL clears the cached suggestions when the language is changed.

Bug: 765164
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I12894c18531c8d1771d6c7e0d2b6c558ba9ce110
Reviewed-on: https://chromium-review.googlesource.com/737631
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#520109}
parent 35407eb6
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "components/feature_engagement/public/tracker.h" #include "components/feature_engagement/public/tracker.h"
#include "components/metrics/metrics_pref_names.h" #include "components/metrics/metrics_pref_names.h"
#include "components/metrics/metrics_service.h" #include "components/metrics/metrics_service.h"
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/payments/core/features.h" #include "components/payments/core/features.h"
#include "components/prefs/pref_change_registrar.h" #include "components/prefs/pref_change_registrar.h"
#include "components/signin/core/browser/signin_manager.h" #include "components/signin/core/browser/signin_manager.h"
...@@ -83,6 +84,7 @@ ...@@ -83,6 +84,7 @@
#include "ios/chrome/browser/metrics/first_user_action_recorder.h" #include "ios/chrome/browser/metrics/first_user_action_recorder.h"
#import "ios/chrome/browser/metrics/previous_session_info.h" #import "ios/chrome/browser/metrics/previous_session_info.h"
#import "ios/chrome/browser/net/cookie_util.h" #import "ios/chrome/browser/net/cookie_util.h"
#include "ios/chrome/browser/ntp_snippets/ios_chrome_content_suggestions_service_factory.h"
#include "ios/chrome/browser/payments/ios_payment_instrument_launcher.h" #include "ios/chrome/browser/payments/ios_payment_instrument_launcher.h"
#include "ios/chrome/browser/payments/ios_payment_instrument_launcher_factory.h" #include "ios/chrome/browser/payments/ios_payment_instrument_launcher_factory.h"
#import "ios/chrome/browser/payments/payment_request_constants.h" #import "ios/chrome/browser/payments/payment_request_constants.h"
...@@ -701,6 +703,12 @@ const int kExternalFilesCleanupDelaySeconds = 60; ...@@ -701,6 +703,12 @@ const int kExternalFilesCleanupDelaySeconds = 60;
[MDCTypography setFontLoader:[MDFRobotoFontLoader sharedInstance]]; [MDCTypography setFontLoader:[MDFRobotoFontLoader sharedInstance]];
if ([PreviousSessionInfo sharedInstance].isFirstSessionAfterLanguageChange) {
IOSChromeContentSuggestionsServiceFactory::GetForBrowserState(
chromeBrowserState)
->ClearAllCachedSuggestions();
}
[self createInitialUI:(startInIncognito ? ApplicationMode::INCOGNITO [self createInitialUI:(startInIncognito ? ApplicationMode::INCOGNITO
: ApplicationMode::NORMAL)]; : ApplicationMode::NORMAL)];
......
...@@ -26,6 +26,10 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating; ...@@ -26,6 +26,10 @@ extern NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating;
// Whether the app was updated between the previous and the current session. // Whether the app was updated between the previous and the current session.
@property(nonatomic, assign, readonly) BOOL isFirstSessionAfterUpgrade; @property(nonatomic, assign, readonly) BOOL isFirstSessionAfterUpgrade;
// Whether the language has been changed between the previous and the current
// session.
@property(nonatomic, assign, readonly) BOOL isFirstSessionAfterLanguageChange;
// Singleton PreviousSessionInfo. During the lifetime of the app, the returned // Singleton PreviousSessionInfo. During the lifetime of the app, the returned
// object is the same, and describes the previous session, even after a new // object is the same, and describes the previous session, even after a new
// session has started (by calling beginRecordingCurrentSession). // session has started (by calling beginRecordingCurrentSession).
......
...@@ -19,6 +19,10 @@ namespace { ...@@ -19,6 +19,10 @@ namespace {
// last session. // last session.
NSString* const kLastRanVersion = @"LastRanVersion"; NSString* const kLastRanVersion = @"LastRanVersion";
// Key in the NSUserDefaults for a string value that stores the language of the
// last session.
NSString* const kLastRanLanguage = @"LastRanLanguage";
} // namespace } // namespace
namespace previous_session_info_constants { namespace previous_session_info_constants {
...@@ -34,6 +38,7 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = ...@@ -34,6 +38,7 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
// Redefined to be read-write. // Redefined to be read-write.
@property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating; @property(nonatomic, assign) BOOL didSeeMemoryWarningShortlyBeforeTerminating;
@property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade; @property(nonatomic, assign) BOOL isFirstSessionAfterUpgrade;
@property(nonatomic, assign) BOOL isFirstSessionAfterLanguageChange;
@end @end
...@@ -43,6 +48,8 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = ...@@ -43,6 +48,8 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
@synthesize didSeeMemoryWarningShortlyBeforeTerminating = @synthesize didSeeMemoryWarningShortlyBeforeTerminating =
_didSeeMemoryWarningShortlyBeforeTerminating; _didSeeMemoryWarningShortlyBeforeTerminating;
@synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade; @synthesize isFirstSessionAfterUpgrade = _isFirstSessionAfterUpgrade;
@synthesize isFirstSessionAfterLanguageChange =
_isFirstSessionAfterLanguageChange;
// Singleton PreviousSessionInfo. // Singleton PreviousSessionInfo.
static PreviousSessionInfo* gSharedInstance = nil; static PreviousSessionInfo* gSharedInstance = nil;
...@@ -61,6 +68,11 @@ static PreviousSessionInfo* gSharedInstance = nil; ...@@ -61,6 +68,11 @@ static PreviousSessionInfo* gSharedInstance = nil;
base::SysUTF8ToNSString(version_info::GetVersionNumber()); base::SysUTF8ToNSString(version_info::GetVersionNumber());
gSharedInstance.isFirstSessionAfterUpgrade = gSharedInstance.isFirstSessionAfterUpgrade =
![lastRanVersion isEqualToString:currentVersion]; ![lastRanVersion isEqualToString:currentVersion];
NSString* lastRanLanguage = [defaults stringForKey:kLastRanLanguage];
NSString* currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
gSharedInstance.isFirstSessionAfterLanguageChange =
![lastRanLanguage isEqualToString:currentLanguage];
} }
return gSharedInstance; return gSharedInstance;
} }
...@@ -81,6 +93,10 @@ static PreviousSessionInfo* gSharedInstance = nil; ...@@ -81,6 +93,10 @@ static PreviousSessionInfo* gSharedInstance = nil;
base::SysUTF8ToNSString(version_info::GetVersionNumber()); base::SysUTF8ToNSString(version_info::GetVersionNumber());
[defaults setObject:currentVersion forKey:kLastRanVersion]; [defaults setObject:currentVersion forKey:kLastRanVersion];
// Set the new language.
NSString* currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
[defaults setObject:currentLanguage forKey:kLastRanLanguage];
// Clear the memory warning flag. // Clear the memory warning flag.
[defaults [defaults
removeObjectForKey:previous_session_info_constants:: removeObjectForKey:previous_session_info_constants::
......
...@@ -25,6 +25,9 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating = ...@@ -25,6 +25,9 @@ NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
// Key in the NSUserDefaults for a string value that stores the version of the // Key in the NSUserDefaults for a string value that stores the version of the
// last session. // last session.
NSString* const kLastRanVersion = @"LastRanVersion"; NSString* const kLastRanVersion = @"LastRanVersion";
// Key in the NSUserDefaults for a string value that stores the language of the
// last session.
NSString* const kLastRanLanguage = @"LastRanLanguage";
using PreviousSessionInfoTest = PlatformTest; using PreviousSessionInfoTest = PlatformTest;
...@@ -33,6 +36,7 @@ TEST_F(PreviousSessionInfoTest, InitializationWithEmptyDefaults) { ...@@ -33,6 +36,7 @@ TEST_F(PreviousSessionInfoTest, InitializationWithEmptyDefaults) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion]; [defaults removeObjectForKey:kLastRanVersion];
[defaults removeObjectForKey:kLastRanLanguage];
// Instantiate the PreviousSessionInfo sharedInstance. // Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance]; PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
...@@ -40,6 +44,39 @@ TEST_F(PreviousSessionInfoTest, InitializationWithEmptyDefaults) { ...@@ -40,6 +44,39 @@ TEST_F(PreviousSessionInfoTest, InitializationWithEmptyDefaults) {
// Checks the default values. // Checks the default values.
EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]); EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]); EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithSameLanguage) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kLastRanLanguage];
// Set the current language as the last ran language.
NSString* currentVersion = [[NSLocale preferredLanguages] objectAtIndex:0];
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithDifferentLanguage) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kLastRanLanguage];
// Set the current language as the last ran language.
NSString* currentVersion = @"Fake Language";
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
} }
TEST_F(PreviousSessionInfoTest, InitializationWithSameVersionNoMemoryWarning) { TEST_F(PreviousSessionInfoTest, InitializationWithSameVersionNoMemoryWarning) {
......
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