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/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "components/pref_registry/pref_registry_syncable.h" | 14 #include "components/pref_registry/pref_registry_syncable.h" |
15 #include "components/url_formatter/url_fixer.h" | 15 #include "components/url_formatter/url_fixer.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Converts a SessionStartupPref::Type to an integer written to prefs. | 19 // Converts a SessionStartupPref::Type to an integer written to prefs. |
20 int TypeToPrefValue(SessionStartupPref::Type type) { | 20 int TypeToPrefValue(SessionStartupPref::Type type) { |
21 switch (type) { | 21 switch (type) { |
22 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; | 22 case SessionStartupPref::LAST: return SessionStartupPref::kPrefValueLast; |
23 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; | 23 case SessionStartupPref::URLS: return SessionStartupPref::kPrefValueURLs; |
24 default: return SessionStartupPref::kPrefValueNewTab; | 24 default: return SessionStartupPref::kPrefValueNewTab; |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 void SetNewURLList(PrefService* prefs) { | |
29 if (prefs->IsUserModifiablePreference(prefs::kURLsToRestoreOnStartup)) { | |
30 base::ListValue new_url_pref_list; | |
31 base::StringValue* home_page = | |
32 new base::StringValue(prefs->GetString(prefs::kHomePage)); | |
33 new_url_pref_list.Append(home_page); | |
34 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | |
35 } | |
36 } | |
37 | |
38 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { | 28 void URLListToPref(const base::ListValue* url_list, SessionStartupPref* pref) { |
39 pref->urls.clear(); | 29 pref->urls.clear(); |
40 for (size_t i = 0; i < url_list->GetSize(); ++i) { | 30 for (size_t i = 0; i < url_list->GetSize(); ++i) { |
41 std::string url_text; | 31 std::string url_text; |
42 if (url_list->GetString(i, &url_text)) { | 32 if (url_list->GetString(i, &url_text)) { |
43 GURL fixed_url = url_formatter::FixupURL(url_text, std::string()); | 33 GURL fixed_url = url_formatter::FixupURL(url_text, std::string()); |
44 pref->urls.push_back(fixed_url); | 34 pref->urls.push_back(fixed_url); |
45 } | 35 } |
46 } | 36 } |
47 } | 37 } |
48 | 38 |
49 } // namespace | 39 } // namespace |
50 | 40 |
51 // static | 41 // static |
52 void SessionStartupPref::RegisterProfilePrefs( | 42 void SessionStartupPref::RegisterProfilePrefs( |
53 user_prefs::PrefRegistrySyncable* registry) { | 43 user_prefs::PrefRegistrySyncable* registry) { |
54 registry->RegisterIntegerPref( | 44 registry->RegisterIntegerPref( |
55 prefs::kRestoreOnStartup, | 45 prefs::kRestoreOnStartup, |
56 TypeToPrefValue(GetDefaultStartupType()), | 46 TypeToPrefValue(GetDefaultStartupType()), |
57 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 47 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
58 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 48 registry->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
59 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 49 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
60 registry->RegisterBooleanPref(prefs::kRestoreOnStartupMigrated, false); | |
gab
2015/09/08 21:23:50
Actually just thought of something more, we will n
Alexei Svitkine (slow)
2015/10/09 19:11:31
+1
sdefresne
2015/12/02 14:35:18
Done.
| |
61 } | 50 } |
62 | 51 |
63 // static | 52 // static |
64 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { | 53 SessionStartupPref::Type SessionStartupPref::GetDefaultStartupType() { |
65 #if defined(OS_CHROMEOS) | 54 #if defined(OS_CHROMEOS) |
66 return SessionStartupPref::LAST; | 55 return SessionStartupPref::LAST; |
67 #else | 56 #else |
68 return SessionStartupPref::DEFAULT; | 57 return SessionStartupPref::DEFAULT; |
69 #endif | 58 #endif |
70 } | 59 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 // static | 92 // static |
104 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { | 93 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { |
105 DCHECK(profile); | 94 DCHECK(profile); |
106 return GetStartupPref(profile->GetPrefs()); | 95 return GetStartupPref(profile->GetPrefs()); |
107 } | 96 } |
108 | 97 |
109 // static | 98 // static |
110 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 99 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
111 DCHECK(prefs); | 100 DCHECK(prefs); |
112 | 101 |
113 MigrateIfNecessary(prefs); | |
114 | |
115 SessionStartupPref pref( | 102 SessionStartupPref pref( |
116 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 103 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
117 | 104 |
118 // Always load the urls, even if the pref type isn't URLS. This way the | 105 // Always load the urls, even if the pref type isn't URLS. This way the |
119 // preferences panels can show the user their last choice. | 106 // preferences panels can show the user their last choice. |
120 const base::ListValue* url_list = | 107 const base::ListValue* url_list = |
121 prefs->GetList(prefs::kURLsToRestoreOnStartup); | 108 prefs->GetList(prefs::kURLsToRestoreOnStartup); |
122 URLListToPref(url_list, &pref); | 109 URLListToPref(url_list, &pref); |
123 | 110 |
124 return pref; | 111 return pref; |
125 } | 112 } |
126 | 113 |
127 // static | 114 // static |
128 void SessionStartupPref::MigrateIfNecessary(PrefService* prefs) { | |
129 DCHECK(prefs); | |
130 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { | |
131 // Read existing values. | |
132 const base::Value* homepage_is_new_tab_page_value = | |
133 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); | |
134 bool homepage_is_new_tab_page = true; | |
135 if (homepage_is_new_tab_page_value) { | |
136 if (!homepage_is_new_tab_page_value->GetAsBoolean( | |
137 &homepage_is_new_tab_page)) | |
138 NOTREACHED(); | |
139 } | |
140 | |
141 const base::Value* restore_on_startup_value = | |
142 prefs->GetUserPrefValue(prefs::kRestoreOnStartup); | |
143 int restore_on_startup = -1; | |
144 if (restore_on_startup_value) { | |
145 if (!restore_on_startup_value->GetAsInteger(&restore_on_startup)) | |
146 NOTREACHED(); | |
147 } | |
148 | |
149 // If restore_on_startup has the deprecated value kPrefValueHomePage, | |
150 // migrate it to open the homepage on startup. If 'homepage is NTP' is set, | |
151 // that means just opening the NTP. If not, it means opening a one-item URL | |
152 // list containing the homepage. | |
153 if (restore_on_startup == kPrefValueHomePage) { | |
154 if (homepage_is_new_tab_page) { | |
155 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueNewTab); | |
156 } else { | |
157 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
158 SetNewURLList(prefs); | |
159 } | |
160 } else if (!restore_on_startup_value && !homepage_is_new_tab_page && | |
161 GetDefaultStartupType() == DEFAULT) { | |
162 // kRestoreOnStartup was never set by the user, but the homepage was set. | |
163 // Migrate to the list of URLs. (If restore_on_startup was never set, | |
164 // and homepage_is_new_tab_page is true, no action is needed. The new | |
165 // default value is "open the new tab page" which is what we want.) | |
166 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
167 SetNewURLList(prefs); | |
168 } | |
169 | |
170 prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true); | |
171 } | |
172 } | |
173 | |
174 // static | |
175 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { | 115 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { |
176 DCHECK(prefs); | 116 DCHECK(prefs); |
177 const PrefService::Preference* pref_restore = | 117 const PrefService::Preference* pref_restore = |
178 prefs->FindPreference(prefs::kRestoreOnStartup); | 118 prefs->FindPreference(prefs::kRestoreOnStartup); |
179 DCHECK(pref_restore); | 119 DCHECK(pref_restore); |
180 return pref_restore->IsManaged(); | 120 return pref_restore->IsManaged(); |
181 } | 121 } |
182 | 122 |
183 // static | 123 // static |
184 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 124 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
(...skipping 11 matching lines...) Expand all Loading... | |
196 prefs->FindPreference(prefs::kRestoreOnStartup); | 136 prefs->FindPreference(prefs::kRestoreOnStartup); |
197 DCHECK(pref_restore); | 137 DCHECK(pref_restore); |
198 return pref_restore->IsDefaultValue(); | 138 return pref_restore->IsDefaultValue(); |
199 } | 139 } |
200 | 140 |
201 // static | 141 // static |
202 SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) { | 142 SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) { |
203 switch (pref_value) { | 143 switch (pref_value) { |
204 case kPrefValueLast: return SessionStartupPref::LAST; | 144 case kPrefValueLast: return SessionStartupPref::LAST; |
205 case kPrefValueURLs: return SessionStartupPref::URLS; | 145 case kPrefValueURLs: return SessionStartupPref::URLS; |
206 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; | |
207 default: return SessionStartupPref::DEFAULT; | 146 default: return SessionStartupPref::DEFAULT; |
208 } | 147 } |
209 } | 148 } |
210 | 149 |
211 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 150 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
212 | 151 |
213 SessionStartupPref::~SessionStartupPref() {} | 152 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |