Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: chrome/browser/prefs/session_startup_pref.cc

Issue 24930003: Migrate startup URLs pref. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update histograms strings. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
9 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
11 #include "base/time/time.h"
10 #include "base/values.h" 12 #include "base/values.h"
11 #include "base/version.h" 13 #include "base/version.h"
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" 14 #include "chrome/browser/prefs/scoped_user_pref_update.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/net/url_fixer_upper.h" 16 #include "chrome/common/net/url_fixer_upper.h"
15 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
16 #include "components/user_prefs/pref_registry_syncable.h" 18 #include "components/user_prefs/pref_registry_syncable.h"
17 19
18 #if defined(OS_MACOSX) 20 #if defined(OS_MACOSX)
19 #include "chrome/browser/ui/cocoa/window_restore_utils.h" 21 #include "chrome/browser/ui/cocoa/window_restore_utils.h"
20 #endif 22 #endif
21 23
22 namespace { 24 namespace {
23 25
26 enum StartupURLsMigrationMetrics {
27 STARTUP_URLS_MIGRATION_METRICS_PERFORMED,
28 STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT,
29 STARTUP_URLS_MIGRATION_METRICS_RESET,
30 STARTUP_URLS_MIGRATION_METRICS_MAX,
31 };
32
24 // Converts a SessionStartupPref::Type to an integer written to prefs. 33 // Converts a SessionStartupPref::Type to an integer written to prefs.
25 int TypeToPrefValue(SessionStartupPref::Type type) { 34 int TypeToPrefValue(SessionStartupPref::Type type) {
26 switch (type) { 35 switch (type) {
27 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; 36 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast;
28 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; 37 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs;
29 default: return SessionStartupPref::kPrefValueNewTab; 38 default: return SessionStartupPref::kPrefValueNewTab;
30 } 39 }
31 } 40 }
32 41
33 void SetNewURLList(PrefService* prefs) { 42 void SetNewURLList(PrefService* prefs) {
(...skipping 21 matching lines...) Expand all
55 64
56 // static 65 // static
57 void SessionStartupPref::RegisterProfilePrefs( 66 void SessionStartupPref::RegisterProfilePrefs(
58 user_prefs::PrefRegistrySyncable* registry) { 67 user_prefs::PrefRegistrySyncable* registry) {
59 registry->RegisterIntegerPref( 68 registry->RegisterIntegerPref(
60 prefs::kRestoreOnStartup, 69 prefs::kRestoreOnStartup,
61 TypeToPrefValue(GetDefaultStartupType()), 70 TypeToPrefValue(GetDefaultStartupType()),
62 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 71 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
63 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup, 72 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup,
64 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 73 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
74 registry->RegisterListPref(prefs::kURLsToRestoreOnStartupOld,
75 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
65 registry->RegisterBooleanPref( 76 registry->RegisterBooleanPref(
66 prefs::kRestoreOnStartupMigrated, 77 prefs::kRestoreOnStartupMigrated,
67 false, 78 false,
68 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 79 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
80 registry->RegisterInt64Pref(
81 prefs::kRestoreStartupURLsMigrated,
82 false,
83 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
69 } 84 }
70 85
71 // static 86 // static
72 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { 87 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() {
73 #if defined(OS_CHROMEOS) 88 #if defined(OS_CHROMEOS)
74 return SessionStartupPref::LAST; 89 return SessionStartupPref::LAST;
75 #else 90 #else
76 return SessionStartupPref::DEFAULT; 91 return SessionStartupPref::DEFAULT;
77 #endif 92 #endif
78 } 93 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); 144 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup);
130 URLListToPref(url_list, &pref); 145 URLListToPref(url_list, &pref);
131 146
132 return pref; 147 return pref;
133 } 148 }
134 149
135 // static 150 // static
136 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { 151 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) {
137 DCHECK(prefs); 152 DCHECK(prefs);
138 153
154 // Check if we need to migrate the old version of the startup URLs preference
155 // to the new name, and also send metrics about the migration.
156 const base::ListValue* old_startup_urls =
157 prefs->GetList(prefs::kURLsToRestoreOnStartupOld);
158 if (!prefs->GetUserPrefValue(prefs::kRestoreStartupURLsMigrated)) {
159 // Record the absence of the migration timestamp, this will get overwritten
160 // below if migration occurs now.
161 StartupURLsMigrationMetrics metrics_result =
162 STARTUP_URLS_MIGRATION_METRICS_NOT_PRESENT;
163
164 // Seems like we never migrated, do it if necessary.
165 if (!prefs->GetUserPrefValue(prefs::kURLsToRestoreOnStartup)) {
166 if (old_startup_urls && !old_startup_urls->empty()) {
Bernhard Bauer 2013/10/15 22:03:52 |old_startup_urls| is guaranteed not to be NULL.
robertshield 2013/10/16 01:23:27 That does not line up with my reading of PrefServi
Bernhard Bauer 2013/10/16 15:13:33 Well, it's guaranteed not to be NULL in the absenc
robertshield 2013/10/16 15:50:08 On line 308 (https://code.google.com/p/chromium/co
167 prefs->Set(prefs::kURLsToRestoreOnStartup, *old_startup_urls);
168 prefs->Set(prefs::kURLsToRestoreOnStartupOld, base::ListValue());
Bernhard Bauer 2013/10/15 22:03:52 You can use prefs->ClearPref().
robertshield 2013/10/16 01:23:27 Done.
169 }
170 metrics_result = STARTUP_URLS_MIGRATION_METRICS_PERFORMED;
171 }
172
173 UMA_HISTOGRAM_ENUMERATION(
174 "Settings.StartupURLsMigration",
175 metrics_result,
176 STARTUP_URLS_MIGRATION_METRICS_MAX);
177
178 prefs->SetInt64(prefs::kRestoreStartupURLsMigrated,
179 base::Time::Now().ToInternalValue());
180 } else if (old_startup_urls && !old_startup_urls->empty()) {
181 // Migration needs to be reset.
182 prefs->Set(prefs::kURLsToRestoreOnStartupOld, base::ListValue());
Bernhard Bauer 2013/10/15 22:03:52 It seems like you are doing a lot of similar thing
robertshield 2013/10/16 01:23:27 Played around with the logic a bit. I extracted th
183 base::Time last_migration_time = base::Time::FromInternalValue(
184 prefs->GetInt64(prefs::kRestoreStartupURLsMigrated));
185 base::Time now = base::Time::Now();
186 prefs->SetInt64(prefs::kRestoreStartupURLsMigrated, now.ToInternalValue());
187 DCHECK(now > last_migration_time);
Bernhard Bauer 2013/10/15 22:03:52 Nit: DCHECK_GT(now, last_migration_time) gives nic
robertshield 2013/10/16 01:23:27 Sadly, DCHECK_GT doesn't work with base::Time inst
188 if (now < last_migration_time)
Bernhard Bauer 2013/10/15 22:03:52 This looks like you're handling a DCHECK failure?
robertshield 2013/10/16 01:23:27 Removing the DCHECK, there's no reason for it to b
189 last_migration_time = now;
190 HISTOGRAM_CUSTOM_TIMES("Settings.StartupURLsResetTime",
191 now - last_migration_time,
192 base::TimeDelta::FromDays(0),
193 base::TimeDelta::FromDays(7),
194 50);
195 UMA_HISTOGRAM_ENUMERATION(
196 "Settings.StartupURLsMigration",
197 STARTUP_URLS_MIGRATION_METRICS_RESET,
198 STARTUP_URLS_MIGRATION_METRICS_MAX);
199 }
200
139 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { 201 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) {
140 // Read existing values 202 // Read existing values.
141 const base::Value* homepage_is_new_tab_page_value = 203 const base::Value* homepage_is_new_tab_page_value =
142 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); 204 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage);
143 bool homepage_is_new_tab_page = true; 205 bool homepage_is_new_tab_page = true;
144 if (homepage_is_new_tab_page_value) { 206 if (homepage_is_new_tab_page_value) {
145 if (!homepage_is_new_tab_page_value->GetAsBoolean( 207 if (!homepage_is_new_tab_page_value->GetAsBoolean(
146 &homepage_is_new_tab_page)) 208 &homepage_is_new_tab_page))
147 NOTREACHED(); 209 NOTREACHED();
148 } 210 }
149 211
150 const base::Value* restore_on_startup_value = 212 const base::Value* restore_on_startup_value =
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 case kPrefValueLast: return SessionStartupPref::LAST; 294 case kPrefValueLast: return SessionStartupPref::LAST;
233 case kPrefValueURLs: return SessionStartupPref::URLS; 295 case kPrefValueURLs: return SessionStartupPref::URLS;
234 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; 296 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE;
235 default: return SessionStartupPref::DEFAULT; 297 default: return SessionStartupPref::DEFAULT;
236 } 298 }
237 } 299 }
238 300
239 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} 301 SessionStartupPref::SessionStartupPref(Type type) : type(type) {}
240 302
241 SessionStartupPref::~SessionStartupPref() {} 303 SessionStartupPref::~SessionStartupPref() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698