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 if (prefs->IsUserModifiablePreference(prefs::kURLsToRestoreOnStartup)) { | 38 ListValue new_url_pref_list; |
39 base::ListValue new_url_pref_list; | 39 StringValue* home_page = new StringValue(prefs->GetString(prefs::kHomePage)); |
40 base::StringValue* home_page = | 40 new_url_pref_list.Append(home_page); |
41 new base::StringValue(prefs->GetString(prefs::kHomePage)); | 41 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); |
42 new_url_pref_list.Append(home_page); | |
43 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | |
44 } | |
45 } | 42 } |
46 | 43 |
47 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { | 44 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { |
48 pref->urls.clear(); | 45 pref->urls.clear(); |
49 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 46 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
50 std::string url_text; | 47 std::string url_text; |
51 if (url_list->GetString(i, &url_text)) { | 48 if (url_list->GetString(i, &url_text)) { |
52 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); | 49 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); |
53 pref->urls.push_back(fixed_url); | 50 pref->urls.push_back(fixed_url); |
54 } | 51 } |
55 } | 52 } |
56 } | 53 } |
57 | 54 |
58 } // namespace | 55 } // namespace |
59 | 56 |
60 // static | 57 // static |
61 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 58 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
62 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, | 59 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, |
63 TypeToPrefValue(GetDefaultStartupType()), | 60 TypeToPrefValue(GetDefaultStartupType()), |
64 PrefService::SYNCABLE_PREF); | 61 PrefService::SYNCABLE_PREF); |
65 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 62 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
66 PrefService::SYNCABLE_PREF); | 63 PrefService::SYNCABLE_PREF); |
67 prefs->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, | |
68 false, | |
69 PrefService::UNSYNCABLE_PREF); | |
70 } | 64 } |
71 | 65 |
72 // static | 66 // static |
73 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { | 67 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { |
74 #if defined(OS_CHROMEOS) | 68 #if defined(OS_CHROMEOS) |
75 SessionStartupPref::Type type = SessionStartupPref::LAST; | 69 SessionStartupPref::Type type = SessionStartupPref::LAST; |
76 #else | 70 #else |
77 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; | 71 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; |
78 #endif | 72 #endif |
79 | 73 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 113 |
120 // static | 114 // static |
121 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { | 115 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { |
122 DCHECK(profile); | 116 DCHECK(profile); |
123 return GetStartupPref(profile->GetPrefs()); | 117 return GetStartupPref(profile->GetPrefs()); |
124 } | 118 } |
125 | 119 |
126 // static | 120 // static |
127 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 121 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
128 DCHECK(prefs); | 122 DCHECK(prefs); |
129 | |
130 MigrateIfNecessary(prefs); | |
131 | |
132 SessionStartupPref pref( | 123 SessionStartupPref pref( |
133 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 124 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
134 | 125 |
| 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 |
135 // Always load the urls, even if the pref type isn't URLS. This way the | 136 // Always load the urls, even if the pref type isn't URLS. This way the |
136 // preferences panels can show the user their last choice. | 137 // preferences panels can show the user their last choice. |
137 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | 138 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); |
138 URLListToPref(url_list, &pref); | 139 URLListToPref(url_list, &pref); |
139 | 140 |
140 return pref; | 141 return pref; |
141 } | 142 } |
142 | 143 |
143 // static | 144 // 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 // kRestoreOnStartup was never set by the user, but the homepage was set. | |
174 // Migrate to the list of URLs. (If restore_on_startup was never set, | |
175 // and homepage_is_new_tab_page is true, no action is needed. The new | |
176 // default value is "open the new tab page" which is what we want.) | |
177 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
178 SetNewURLList(prefs); | |
179 } | |
180 | |
181 prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true); | |
182 } | |
183 } | |
184 | |
185 // static | |
186 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { | 145 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { |
187 DCHECK(prefs); | 146 DCHECK(prefs); |
188 const PrefService::Preference* pref_restore = | 147 const PrefService::Preference* pref_restore = |
189 prefs->FindPreference(prefs::kRestoreOnStartup); | 148 prefs->FindPreference(prefs::kRestoreOnStartup); |
190 DCHECK(pref_restore); | 149 DCHECK(pref_restore); |
191 return pref_restore->IsManaged(); | 150 return pref_restore->IsManaged(); |
192 } | 151 } |
193 | 152 |
194 // static | 153 // static |
195 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 154 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 CHECK(prefs_watcher->GetBackupForPref( | 200 CHECK(prefs_watcher->GetBackupForPref( |
242 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); | 201 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); |
243 URLListToPref(url_list, &backup_pref); | 202 URLListToPref(url_list, &backup_pref); |
244 | 203 |
245 return backup_pref; | 204 return backup_pref; |
246 } | 205 } |
247 | 206 |
248 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 207 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
249 | 208 |
250 SessionStartupPref::~SessionStartupPref() {} | 209 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |