Changing device ID when device type changes.
Because Device id is stored in user preferences, it can cross devices
through backup/restore. To mitigate this problem, the device type is
also stored in the preferences so that the id is reset when the type
changes.
[email protected]
BUG=None
TEST=Start with a first device, startup sync, enable session syncing. Backup the device, and restore it to another device that is not of the exact same type (iPhone4 to iPhone5 for example). Check that you can see the session from one device on the other one.
Review URL: https://chromiumcodereview.appspot.com/11640044
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174167 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/ios/device_util.mm b/base/ios/device_util.mm
index b538ea8..8c9e8a7 100644
--- a/base/ios/device_util.mm
+++ b/base/ios/device_util.mm
@@ -26,6 +26,9 @@
// Client ID key in the user preferences.
NSString* const kLegacyClientIdPreferenceKey = @"ChromiumClientID";
NSString* const kClientIdPreferenceKey = @"ChromeClientID";
+// Current hardware type. This is used to detect that a device has been backed
+// up and restored to another device, and allows regenerating a new device id.
+NSString* const kHardwareTypePreferenceKey = @"ClientIDGenerationHardwareType";
// Default salt for device ids.
const char kDefaultSalt[] = "Salt";
// Zero UUID returned on buggy iOS devices.
@@ -125,11 +128,22 @@
std::string GetDeviceIdentifier(const char* salt) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
+
+ NSString* last_seen_hardware =
+ [defaults stringForKey:kHardwareTypePreferenceKey];
+ NSString* current_hardware = base::SysUTF8ToNSString(GetPlatform());
+ if (!last_seen_hardware) {
+ last_seen_hardware = current_hardware;
+ [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
+ [defaults synchronize];
+ }
+
NSString* client_id = [defaults stringForKey:kClientIdPreferenceKey];
- if (!client_id) {
+ if (!client_id || ![last_seen_hardware isEqualToString:current_hardware]) {
client_id = GenerateClientId();
[defaults setObject:client_id forKey:kClientIdPreferenceKey];
+ [defaults setObject:current_hardware forKey:kHardwareTypePreferenceKey];
[defaults synchronize];
}