diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-20 16:41:33 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-20 16:41:33 +0000 |
commit | af6a8c970c2666ca12af04d26f813ac58d0fcd1a (patch) | |
tree | 8a538456156613b8f7a3cc954c857c5e4329cd07 /base/ios | |
parent | 1ef59610d05d8ce26508d715af83e569d92180ff (diff) | |
download | chromium_src-af6a8c970c2666ca12af04d26f813ac58d0fcd1a.zip chromium_src-af6a8c970c2666ca12af04d26f813ac58d0fcd1a.tar.gz chromium_src-af6a8c970c2666ca12af04d26f813ac58d0fcd1a.tar.bz2 |
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.
R=stuartmorgan@chromium.org
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
Diffstat (limited to 'base/ios')
-rw-r--r-- | base/ios/device_util.mm | 16 | ||||
-rw-r--r-- | base/ios/device_util_unittest.mm | 17 |
2 files changed, 32 insertions, 1 deletions
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 @@ namespace { // 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 GetRandomId() { 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]; } diff --git a/base/ios/device_util_unittest.mm b/base/ios/device_util_unittest.mm index 12bfe09..8d69db2 100644 --- a/base/ios/device_util_unittest.mm +++ b/base/ios/device_util_unittest.mm @@ -22,6 +22,7 @@ void CleanNSUserDefaultsForDeviceId() { NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; [defaults removeObjectForKey:@"ChromeClientID"]; [defaults removeObjectForKey:@"ChromiumClientID"]; + [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"]; [defaults synchronize]; } @@ -102,4 +103,20 @@ TEST_F(DeviceUtilTest, CheckMigrationFromZero) { CleanNSUserDefaultsForDeviceId(); } +TEST_F(DeviceUtilTest, CheckDeviceMigration) { + CleanNSUserDefaultsForDeviceId(); + + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + [defaults setObject:@"10000000-0000-0000-0000-000000000000" + forKey:@"ChromeClientID"]; + [defaults synchronize]; + std::string base_id = ios::device_util::GetDeviceIdentifier(NULL); + [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"]; + [defaults synchronize]; + std::string new_id = ios::device_util::GetDeviceIdentifier(NULL); + EXPECT_NE(new_id, base_id); + + CleanNSUserDefaultsForDeviceId(); +} + } // namespace |