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 24 matching lines...) Expand all Loading... | |
35 // Converts a SessionStartupPref::Type to an integer written to prefs. | 35 // Converts a SessionStartupPref::Type to an integer written to prefs. |
36 int TypeToPrefValue(SessionStartupPref::Type type) { | 36 int TypeToPrefValue(SessionStartupPref::Type type) { |
37 switch (type) { | 37 switch (type) { |
38 case SessionStartupPref::LAST: return kPrefValueLast; | 38 case SessionStartupPref::LAST: return kPrefValueLast; |
39 case SessionStartupPref::URLS: return kPrefValueURLs; | 39 case SessionStartupPref::URLS: return kPrefValueURLs; |
40 default: return kPrefValueNewTab; | 40 default: return kPrefValueNewTab; |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 void SetNewURLList(PrefService* prefs) { | 44 void SetNewURLList(PrefService* prefs) { |
45 ListValue new_url_pref_list; | 45 if (prefs->IsUserModifiablePreference(prefs::kURLsToRestoreOnStartup)) { |
46 StringValue* home_page = new StringValue(prefs->GetString(prefs::kHomePage)); | 46 ListValue new_url_pref_list; |
47 new_url_pref_list.Append(home_page); | 47 StringValue* home_page = |
48 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | 48 new StringValue(prefs->GetString(prefs::kHomePage)); |
49 new_url_pref_list.Append(home_page); | |
50 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | |
51 } | |
49 } | 52 } |
50 | 53 |
51 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { | 54 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { |
52 pref->urls.clear(); | 55 pref->urls.clear(); |
53 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 56 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
54 Value* value = NULL; | 57 Value* value = NULL; |
55 CHECK(url_list->Get(i, &value)); | 58 CHECK(url_list->Get(i, &value)); |
56 std::string url_text; | 59 std::string url_text; |
57 if (value->GetAsString(&url_text)) { | 60 if (value->GetAsString(&url_text)) { |
58 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); | 61 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); |
59 pref->urls.push_back(fixed_url); | 62 pref->urls.push_back(fixed_url); |
60 } | 63 } |
61 } | 64 } |
62 } | 65 } |
63 | 66 |
64 } // namespace | 67 } // namespace |
65 | 68 |
66 // static | 69 // static |
67 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 70 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
68 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, | 71 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, |
69 TypeToPrefValue(GetDefaultStartupType()), | 72 TypeToPrefValue(GetDefaultStartupType()), |
70 PrefService::SYNCABLE_PREF); | 73 PrefService::SYNCABLE_PREF); |
71 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 74 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
72 PrefService::SYNCABLE_PREF); | 75 PrefService::SYNCABLE_PREF); |
76 prefs->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, | |
77 false, | |
78 PrefService::UNSYNCABLE_PREF); | |
73 } | 79 } |
74 | 80 |
75 // static | 81 // static |
76 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { | 82 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { |
77 #if defined(OS_CHROMEOS) | 83 #if defined(OS_CHROMEOS) |
78 SessionStartupPref::Type type = SessionStartupPref::LAST; | 84 SessionStartupPref::Type type = SessionStartupPref::LAST; |
79 #else | 85 #else |
80 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; | 86 SessionStartupPref::Type type = SessionStartupPref::DEFAULT; |
81 #endif | 87 #endif |
82 | 88 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 | 128 |
123 // static | 129 // static |
124 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { | 130 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { |
125 DCHECK(profile); | 131 DCHECK(profile); |
126 return GetStartupPref(profile->GetPrefs()); | 132 return GetStartupPref(profile->GetPrefs()); |
127 } | 133 } |
128 | 134 |
129 // static | 135 // static |
130 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 136 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
131 DCHECK(prefs); | 137 DCHECK(prefs); |
138 | |
139 MigrateIfNecessary(prefs); | |
140 | |
132 SessionStartupPref pref( | 141 SessionStartupPref pref( |
133 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 142 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
134 | 143 |
135 // Migrate from "Open the home page" to "Open the following URLs". If the user | 144 if (pref.type == HOMEPAGE) { |
136 // had the "Open the homepage" option selected, then we need to switch them to | 145 // We've already migrated, so the only way this can happen is if |
137 // "Open the following URLs" and set the list of URLs to a list containing | 146 // kRestoreOnStartup is controlled by a policy. In that case, act as though |
138 // just the user's homepage. | 147 // URLS was selected. |
Mattias Nissler (ping if slow)
2012/04/18 11:40:54
We should change RestoreOnStartupPolicyHandler in
Tyler Breisacher (Chromium)
2012/04/18 18:26:03
http://crbug.com/124027
The patch I just uploaded
Mattias Nissler (ping if slow)
2012/04/19 13:23:17
Thanks for fixing the policy!
| |
139 if (pref.type == SessionStartupPref::HOMEPAGE) { | 148 pref.type = URLS; |
140 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
141 pref.type = SessionStartupPref::URLS; | |
142 SetNewURLList(prefs); | |
143 } | 149 } |
144 | 150 |
145 // Always load the urls, even if the pref type isn't URLS. This way the | 151 // Always load the urls, even if the pref type isn't URLS. This way the |
146 // preferences panels can show the user their last choice. | 152 // preferences panels can show the user their last choice. |
147 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); | 153 const ListValue* url_list = prefs->GetList(prefs::kURLsToRestoreOnStartup); |
148 URLListToPref(url_list, &pref); | 154 URLListToPref(url_list, &pref); |
149 | 155 |
150 return pref; | 156 return pref; |
151 } | 157 } |
152 | 158 |
153 // static | 159 // static |
160 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { | |
161 DCHECK(prefs); | |
162 | |
163 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { | |
164 | |
165 if (prefs->GetInteger(prefs::kRestoreOnStartup) == kPrefValueHomePage || | |
166 (!prefs->HasPrefPath(prefs::kRestoreOnStartup) && | |
167 !prefs->GetBoolean(prefs::kHomePageIsNewTabPage))) { | |
168 | |
169 if (prefs->GetBoolean(prefs::kHomePageIsNewTabPage)) { | |
Mattias Nissler (ping if slow)
2012/04/18 11:40:54
This still breaks if prefs::kHomePageIsNewTabPage
Tyler Breisacher (Chromium)
2012/04/18 18:26:03
I don't think we change the value of kHomePageIsNe
Mattias Nissler (ping if slow)
2012/04/19 13:23:17
What I meant was that the pref value you get in li
| |
170 if (prefs->IsUserModifiablePreference(prefs::kRestoreOnStartup)) | |
171 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueNewTab); | |
172 } else { | |
173 if (prefs->IsUserModifiablePreference(prefs::kRestoreOnStartup)) | |
174 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
175 SetNewURLList(prefs); | |
176 } | |
177 } | |
178 | |
179 prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true); | |
180 } | |
181 } | |
182 | |
183 // static | |
154 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { | 184 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { |
155 DCHECK(prefs); | 185 DCHECK(prefs); |
156 const PrefService::Preference* pref_restore = | 186 const PrefService::Preference* pref_restore = |
157 prefs->FindPreference(prefs::kRestoreOnStartup); | 187 prefs->FindPreference(prefs::kRestoreOnStartup); |
158 DCHECK(pref_restore); | 188 DCHECK(pref_restore); |
159 return pref_restore->IsManaged(); | 189 return pref_restore->IsManaged(); |
160 } | 190 } |
161 | 191 |
162 // static | 192 // static |
163 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 193 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 CHECK(prefs_watcher->GetBackupForPref( | 239 CHECK(prefs_watcher->GetBackupForPref( |
210 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); | 240 prefs::kURLsToRestoreOnStartup)->GetAsList(&url_list)); |
211 URLListToPref(url_list, &backup_pref); | 241 URLListToPref(url_list, &backup_pref); |
212 | 242 |
213 return backup_pref; | 243 return backup_pref; |
214 } | 244 } |
215 | 245 |
216 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 246 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
217 | 247 |
218 SessionStartupPref::~SessionStartupPref() {} | 248 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |