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/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/net/url_fixer_upper.h" | 10 #include "chrome/browser/net/url_fixer_upper.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 // Converts a SessionStartupPref::Type to an integer written to prefs. | 28 // Converts a SessionStartupPref::Type to an integer written to prefs. |
29 int TypeToPrefValue(SessionStartupPref::Type type) { | 29 int TypeToPrefValue(SessionStartupPref::Type type) { |
30 switch (type) { | 30 switch (type) { |
31 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; | 31 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; |
32 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; | 32 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; |
33 default: return SessionStartupPref::kPrefValueNewTab; | 33 default: return SessionStartupPref::kPrefValueNewTab; |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 void SetNewURLList(PrefService* prefs) { | 37 void SetNewURLList(PrefService* prefs) { |
38 ListValue new_url_pref_list; | 38 if (prefs->IsUserModifiablePreference(prefs::kURLsToRestoreOnStartup)) { |
39 StringValue* home_page = new StringValue(prefs->GetString(prefs::kHomePage)); | 39 base::ListValue new_url_pref_list; |
40 new_url_pref_list.Append(home_page); | 40 base::StringValue* home_page = |
41 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | 41 new base::StringValue(prefs->GetString(prefs::kHomePage)); |
42 new_url_pref_list.Append(home_page); | |
43 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | |
44 } | |
42 } | 45 } |
43 | 46 |
44 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { | 47 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { |
45 pref->urls.clear(); | 48 pref->urls.clear(); |
46 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 49 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
47 std::string url_text; | 50 std::string url_text; |
48 if (url_list->GetString(i, &url_text)) { | 51 if (url_list->GetString(i, &url_text)) { |
49 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); | 52 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); |
50 pref->urls.push_back(fixed_url); | 53 pref->urls.push_back(fixed_url); |
51 } | 54 } |
52 } | 55 } |
53 } | 56 } |
54 | 57 |
55 } // namespace | 58 } // namespace |
56 | 59 |
57 // static | 60 // static |
58 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 61 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
59 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, | 62 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, |
60 TypeToPrefValue(GetDefaultStartupType()), | 63 TypeToPrefValue(GetDefaultStartupType()), |
61 PrefService::SYNCABLE_PREF); | 64 PrefService::SYNCABLE_PREF); |
62 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 65 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
63 PrefService::SYNCABLE_PREF); | 66 PrefService::SYNCABLE_PREF); |
67 prefs->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, | |
68 false, | |
69 PrefService::UNSYNCABLE_PREF); | |
64 } | 70 } |
65 | 71 |
66 // static | 72 // static |
67 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { | 73 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { |
Tyler Breisacher (Chromium)
2012/04/24 21:27:22
Notice that 'DEFAULT' is not always the default. T
| |
68 #if defined(OS_CHROMEOS) | 74 #if defined(OS_CHROMEOS) |
69 SessionStartupPref::Type type = SessionStartupPref::LAST; | 75 SessionStartupPref::Type type = SessionStartupPref::LAST; |
70 #else | 76 #else |
71 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; | 77 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; |
72 #endif | 78 #endif |
73 | 79 |
74 #if defined(OS_MACOSX) | 80 #if defined(OS_MACOSX) |
75 // Use Lion's system preference, if it is set. | 81 // Use Lion's system preference, if it is set. |
76 if (restore_utils::IsWindowRestoreEnabled()) | 82 if (restore_utils::IsWindowRestoreEnabled()) |
77 type = SessionStartupPref::LAST; | 83 type = SessionStartupPref::LAST; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 | 119 |
114 // static | 120 // static |
115 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { | 121 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { |
116 DCHECK(profile); | 122 DCHECK(profile); |
117 return GetStartupPref(profile->GetPrefs()); | 123 return GetStartupPref(profile->GetPrefs()); |
118 } | 124 } |
119 | 125 |
120 // static | 126 // static |
121 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 127 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
122 DCHECK(prefs); | 128 DCHECK(prefs); |
129 | |
130 MigrateIfNecessary(prefs); | |
131 | |
123 SessionStartupPref pref( | 132 SessionStartupPref pref( |
124 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 133 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
125 | 134 |
126 // Migrate from "Open the home page" to "Open the following URLs". If the user | |
127 // had the "Open the homepage" option selected, then we need to switch them to | |
128 // "Open the following URLs" and set the list of URLs to a list containing | |
129 // just the user's homepage. | |
130 if (pref.type == SessionStartupPref::HOMEPAGE) { | |
131 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
132 pref.type = SessionStartupPref::URLS; | |
133 SetNewURLList(prefs); | |
134 } | |
135 | |
136 // Always load the urls, even if the pref type isn't URLS. This way the | 135 // Always load the urls, even if the pref type isn't URLS. This way the |
137 // preferences panels can show the user their last choice. | 136 // preferences panels can show the user their last choice. |
138 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | 137 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); |
139 URLListToPref(url_list, &pref); | 138 URLListToPref(url_list, &pref); |
140 | 139 |
141 return pref; | 140 return pref; |
142 } | 141 } |
143 | 142 |
144 // static | 143 // static |
144 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { | |
145 DCHECK(prefs); | |
146 | |
147 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { | |
148 // Read existing values | |
149 const base::Value* homepage_is_new_tab_page_value = | |
150 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); | |
151 bool homepage_is_new_tab_page = true; | |
152 if (homepage_is_new_tab_page_value) | |
153 homepage_is_new_tab_page_value->GetAsBoolean(&homepage_is_new_tab_page); | |
154 | |
155 const base::Value* restore_on_startup_value = | |
156 prefs->GetUserPrefValue(prefs::kRestoreOnStartup); | |
157 int restore_on_startup = -1; | |
158 if (restore_on_startup_value) | |
159 restore_on_startup_value->GetAsInteger(&restore_on_startup); | |
160 | |
161 // If restore_on_startup has the deprecated value kPrefValueHomePage, | |
162 // migrate it to open the homepage on startup. If 'homepage is NTP' is set, | |
163 // that means just opening the NTP. If not, it means opening a one-item URL | |
164 // list containing the homepage. | |
165 if (restore_on_startup == kPrefValueHomePage) { | |
166 if (homepage_is_new_tab_page) { | |
167 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueNewTab); | |
168 } else { | |
169 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
170 SetNewURLList(prefs); | |
171 } | |
172 } else if (!restore_on_startup_value && !homepage_is_new_tab_page && | |
173 GetDefaultStartupType() == DEFAULT) { | |
174 // kRestoreOnStartup was never set by the user, but the homepage was set. | |
175 // Migrate to the list of URLs. (If restore_on_startup was never set, | |
176 // and homepage_is_new_tab_page is true, no action is needed. The new | |
177 // default value is "open the new tab page" which is what we want.) | |
178 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
179 SetNewURLList(prefs); | |
180 } | |
181 | |
182 prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true); | |
183 } | |
184 } | |
185 | |
186 // static | |
145 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { | 187 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { |
146 DCHECK(prefs); | 188 DCHECK(prefs); |
147 const PrefService::Preference* pref_restore = | 189 const PrefService::Preference* pref_restore = |
148 prefs->FindPreference(prefs::kRestoreOnStartup); | 190 prefs->FindPreference(prefs::kRestoreOnStartup); |
149 DCHECK(pref_restore); | 191 DCHECK(pref_restore); |
150 return pref_restore->IsManaged(); | 192 return pref_restore->IsManaged(); |
151 } | 193 } |
152 | 194 |
153 // static | 195 // static |
154 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 196 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 CHECK(prefs_watcher->GetBackupForPref( | 242 CHECK(prefs_watcher->GetBackupForPref( |
201 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); | 243 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); |
202 URLListToPref(url_list, &backup_pref); | 244 URLListToPref(url_list, &backup_pref); |
203 | 245 |
204 return backup_pref; | 246 return backup_pref; |
205 } | 247 } |
206 | 248 |
207 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 249 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
208 | 250 |
209 SessionStartupPref::~SessionStartupPref() {} | 251 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |