| 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); | |
| 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 | |
| 131 if (!prefs->GetBoolean(prefs::kRestoreOnStartupMigrated)) { | |
| 132 // Read existing values. | |
| 133 const base::Value* homepage_is_new_tab_page_value = | |
| 134 prefs->GetUserPrefValue(prefs::kHomePageIsNewTabPage); | |
| 135 bool homepage_is_new_tab_page = true; | |
| 136 if (homepage_is_new_tab_page_value) { | |
| 137 if (!homepage_is_new_tab_page_value->GetAsBoolean( | |
| 138 &homepage_is_new_tab_page)) | |
| 139 NOTREACHED(); | |
| 140 } | |
| 141 | |
| 142 const base::Value* restore_on_startup_value = | |
| 143 prefs->GetUserPrefValue(prefs::kRestoreOnStartup); | |
| 144 int restore_on_startup = -1; | |
| 145 if (restore_on_startup_value) { | |
| 146 if (!restore_on_startup_value->GetAsInteger(&restore_on_startup)) | |
| 147 NOTREACHED(); | |
| 148 } | |
| 149 | |
| 150 // If restore_on_startup has the deprecated value kPrefValueHomePage, | |
| 151 // migrate it to open the homepage on startup. If 'homepage is NTP' is set, | |
| 152 // that means just opening the NTP. If not, it means opening a one-item URL | |
| 153 // list containing the homepage. | |
| 154 if (restore_on_startup == kPrefValueHomePage) { | |
| 155 if (homepage_is_new_tab_page) { | |
| 156 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueNewTab); | |
| 157 } else { | |
| 158 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
| 159 SetNewURLList(prefs); | |
| 160 } | |
| 161 } else if (!restore_on_startup_value && !homepage_is_new_tab_page && | |
| 162 GetDefaultStartupType() == DEFAULT) { | |
| 163 // kRestoreOnStartup was never set by the user, but the homepage was set. | |
| 164 // Migrate to the list of URLs. (If restore_on_startup was never set, | |
| 165 // and homepage_is_new_tab_page is true, no action is needed. The new | |
| 166 // default value is "open the new tab page" which is what we want.) | |
| 167 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
| 168 SetNewURLList(prefs); | |
| 169 } | |
| 170 | |
| 171 prefs->SetBoolean(prefs::kRestoreOnStartupMigrated, true); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 // static | |
| 176 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { | 115 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { |
| 177 DCHECK(prefs); | 116 DCHECK(prefs); |
| 178 const PrefService::Preference* pref_restore = | 117 const PrefService::Preference* pref_restore = |
| 179 prefs->FindPreference(prefs::kRestoreOnStartup); | 118 prefs->FindPreference(prefs::kRestoreOnStartup); |
| 180 DCHECK(pref_restore); | 119 DCHECK(pref_restore); |
| 181 return pref_restore->IsManaged(); | 120 return pref_restore->IsManaged(); |
| 182 } | 121 } |
| 183 | 122 |
| 184 // static | 123 // static |
| 185 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 124 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 197 prefs->FindPreference(prefs::kRestoreOnStartup); | 136 prefs->FindPreference(prefs::kRestoreOnStartup); |
| 198 DCHECK(pref_restore); | 137 DCHECK(pref_restore); |
| 199 return pref_restore->IsDefaultValue(); | 138 return pref_restore->IsDefaultValue(); |
| 200 } | 139 } |
| 201 | 140 |
| 202 // static | 141 // static |
| 203 SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) { | 142 SessionStartupPref::Type SessionStartupPref::PrefValueToType(int pref_value) { |
| 204 switch (pref_value) { | 143 switch (pref_value) { |
| 205 case kPrefValueLast: return SessionStartupPref::LAST; | 144 case kPrefValueLast: return SessionStartupPref::LAST; |
| 206 case kPrefValueURLs: return SessionStartupPref::URLS; | 145 case kPrefValueURLs: return SessionStartupPref::URLS; |
| 207 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; | |
| 208 default: return SessionStartupPref::DEFAULT; | 146 default: return SessionStartupPref::DEFAULT; |
| 209 } | 147 } |
| 210 } | 148 } |
| 211 | 149 |
| 212 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 150 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
| 213 | 151 |
| 214 SessionStartupPref::~SessionStartupPref() {} | 152 SessionStartupPref::~SessionStartupPref() {} |
| OLD | NEW |