| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/prefs/session_startup_pref.h" | 5 #include "chrome/browser/prefs/session_startup_pref.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 11 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "base/time/time.h" | |
| 13 #include "base/values.h" | 11 #include "base/values.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 16 #include "components/pref_registry/pref_registry_syncable.h" | 14 #include "components/pref_registry/pref_registry_syncable.h" |
| 17 #include "components/url_formatter/url_fixer.h" | 15 #include "components/url_formatter/url_fixer.h" |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 enum StartupURLsMigrationMetrics { | |
| 22 STARTUP_URLS_MIGRATION_METRICS_PERFORMED, | |
| 23 STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT, | |
| 24 STARTUP_URLS_MIGRATION_METRICS_RESET, | |
| 25 STARTUP_URLS_MIGRATION_METRICS_MAX, | |
| 26 }; | |
| 27 | |
| 28 // Converts a SessionStartupPref::Type to an integer written to prefs. | 19 // Converts a SessionStartupPref::Type to an integer written to prefs. |
| 29 int TypeToPrefValue(SessionStartupPref::Type type) { | 20 int TypeToPrefValue(SessionStartupPref::Type type) { |
| 30 switch (type) { | 21 switch (type) { |
| 31 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; | 22 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; |
| 32 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; | 23 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; |
| 33 default: return SessionStartupPref::kPrefValueNewTab; | 24 default: return SessionStartupPref::kPrefValueNewTab; |
| 34 } | 25 } |
| 35 } | 26 } |
| 36 | 27 |
| 37 void SetNewURLList(PrefService* prefs) { | 28 void SetNewURLList(PrefService* prefs) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 59 | 50 |
| 60 // static | 51 // static |
| 61 void SessionStartupPref::RegisterProfilePrefs( | 52 void SessionStartupPref::RegisterProfilePrefs( |
| 62 user_prefs::PrefRegistrySyncable* registry) { | 53 user_prefs::PrefRegistrySyncable* registry) { |
| 63 registry->RegisterIntegerPref( | 54 registry->RegisterIntegerPref( |
| 64 prefs::kRestoreOnStartup, | 55 prefs::kRestoreOnStartup, |
| 65 TypeToPrefValue(GetDefaultStartupType()), | 56 TypeToPrefValue(GetDefaultStartupType()), |
| 66 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 57 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 67 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 58 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
| 68 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 59 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 69 registry->RegisterListPref(prefs::kURLsToRestoreOnStartupOld); | |
| 70 registry->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, false); | 60 registry->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, false); |
| 71 registry->RegisterInt64Pref(prefs::kRestoreStartupURLsMigrationTime, false); | |
| 72 } | 61 } |
| 73 | 62 |
| 74 // static | 63 // static |
| 75 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { | 64 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { |
| 76 #if defined(OS_CHROMEOS) | 65 #if defined(OS_CHROMEOS) |
| 77 return SessionStartupPref::LAST; | 66 return SessionStartupPref::LAST; |
| 78 #else | 67 #else |
| 79 return SessionStartupPref::DEFAULT; | 68 return SessionStartupPref::DEFAULT; |
| 80 #endif | 69 #endif |
| 81 } | 70 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const base::ListValue* url_list = | 120 const base::ListValue* url_list = |
| 132 prefs->GetList(prefs::kURLsToRestoreOnStartup); | 121 prefs->GetList(prefs::kURLsToRestoreOnStartup); |
| 133 URLListToPref(url_list, &pref); | 122 URLListToPref(url_list, &pref); |
| 134 | 123 |
| 135 return pref; | 124 return pref; |
| 136 } | 125 } |
| 137 | 126 |
| 138 // static | 127 // static |
| 139 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { | 128 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { |
| 140 DCHECK(prefs); | 129 DCHECK(prefs); |
| 141 | |
| 142 // Check if we need to migrate the old version of the startup URLs preference | |
| 143 // to the new name, and also send metrics about the migration. | |
| 144 StartupURLsMigrationMetrics metrics_result = | |
| 145 STARTUP_URLS_MIGRATION_METRICS_MAX; | |
| 146 const base::ListValue* old_startup_urls = | |
| 147 prefs->GetList(prefs::kURLsToRestoreOnStartupOld); | |
| 148 if (!prefs->GetUserPrefValue(prefs::kRestoreStartupURLsMigrationTime)) { | |
| 149 // Record the absence of the migration timestamp, this will get overwritten | |
| 150 // below if migration occurs now. | |
| 151 metrics_result = STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT; | |
| 152 | |
| 153 // Seems like we never migrated, do it if necessary. | |
| 154 if (!prefs->GetUserPrefValue(prefs::kURLsToRestoreOnStartup)) { | |
| 155 if (old_startup_urls && !old_startup_urls->empty()) { | |
| 156 prefs->Set(prefs::kURLsToRestoreOnStartup, *old_startup_urls); | |
| 157 prefs->ClearPref(prefs::kURLsToRestoreOnStartupOld); | |
| 158 } | |
| 159 metrics_result = STARTUP_URLS_MIGRATION_METRICS_PERFORMED; | |
| 160 } | |
| 161 | |
| 162 prefs->SetInt64(prefs::kRestoreStartupURLsMigrationTime, | |
| 163 base::Time::Now().ToInternalValue()); | |
| 164 } else if (old_startup_urls && !old_startup_urls->empty()) { | |
| 165 // Migration needs to be reset. | |
| 166 prefs->ClearPref(prefs::kURLsToRestoreOnStartupOld); | |
| 167 base::Time last_migration_time = base::Time::FromInternalValue( | |
| 168 prefs->GetInt64(prefs::kRestoreStartupURLsMigrationTime)); | |
| 169 base::Time now = base::Time::Now(); | |
| 170 prefs->SetInt64(prefs::kRestoreStartupURLsMigrationTime, | |
| 171 now.ToInternalValue()); | |
| 172 if (now < last_migration_time) | |
| 173 last_migration_time = now; | |
| 174 UMA_HISTOGRAM_CUSTOM_TIMES("Settings.StartupURLsResetTime", | |
| 175 now - last_migration_time, | |
| 176 base::TimeDelta::FromDays(0), | |
| 177 base::TimeDelta::FromDays(7), | |
| 178 50); | |
| 179 metrics_result = STARTUP_URLS_MIGRATION_METRICS_RESET; | |
| 180 } | |
| 181 | |
| 182 // Record a metric migration event if something interesting happened. | |
| 183 if (metrics_result != STARTUP_URLS_MIGRATION_METRICS_MAX) { | |
| 184 UMA_HISTOGRAM_ENUMERATION( | |
| 185 "Settings.StartupURLsMigration", | |
| 186 metrics_result, | |
| 187 STARTUP_URLS_MIGRATION_METRICS_MAX); | |
| 188 } | |
| 189 | |
| 190 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { | 130 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { |
| 191 // Read existing values. | 131 // Read existing values. |
| 192 const base::Value* homepage_is_new_tab_page_value = | 132 const base::Value* homepage_is_new_tab_page_value = |
| 193 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); | 133 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); |
| 194 bool homepage_is_new_tab_page = true; | 134 bool homepage_is_new_tab_page = true; |
| 195 if (homepage_is_new_tab_page_value) { | 135 if (homepage_is_new_tab_page_value) { |
| 196 if (!homepage_is_new_tab_page_value->GetAsBoolean( | 136 if (!homepage_is_new_tab_page_value->GetAsBoolean( |
| 197 &homepage_is_new_tab_page)) | 137 &homepage_is_new_tab_page)) |
| 198 NOTREACHED(); | 138 NOTREACHED(); |
| 199 } | 139 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 case kPrefValueLast: return SessionStartupPref::LAST; | 204 case kPrefValueLast: return SessionStartupPref::LAST; |
| 265 case kPrefValueURLs: return SessionStartupPref::URLS; | 205 case kPrefValueURLs: return SessionStartupPref::URLS; |
| 266 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; | 206 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; |
| 267 default: return SessionStartupPref::DEFAULT; | 207 default: return SessionStartupPref::DEFAULT; |
| 268 } | 208 } |
| 269 } | 209 } |
| 270 | 210 |
| 271 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 211 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
| 272 | 212 |
| 273 SessionStartupPref::~SessionStartupPref() {} | 213 SessionStartupPref::~SessionStartupPref() {} |
| OLD | NEW |