1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/prefs/session_startup_pref.h" 6 7 #include <string> 8 9 #include "base/string_piece.h" 10 #include "base/utf_string_conversions.h" 11 #include "chrome/browser/defaults.h" 12 #include "chrome/browser/net/url_fixer_upper.h" 13 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" 15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/common/pref_names.h" 17 18 namespace { 19 20 // For historical reasons the enum and value registered in the prefs don't line 21 // up. These are the values registered in prefs. 22 const int kPrefValueDefault = 0; 23 const int kPrefValueLast = 1; 24 const int kPrefValueURLs = 4; 25 26 // Converts a SessionStartupPref::Type to an integer written to prefs. 27 int TypeToPrefValue(SessionStartupPref::Type type) { 28 switch (type) { 29 case SessionStartupPref::LAST: return kPrefValueLast; 30 case SessionStartupPref::URLS: return kPrefValueURLs; 31 default: return kPrefValueDefault; 32 } 33 } 34 35 // Converts an integer pref value to a SessionStartupPref::Type. 36 SessionStartupPref::Type PrefValueToType(int pref_value) { 37 switch (pref_value) { 38 case kPrefValueLast: return SessionStartupPref::LAST; 39 case kPrefValueURLs: return SessionStartupPref::URLS; 40 default: return SessionStartupPref::DEFAULT; 41 } 42 } 43 44 } // namespace 45 46 // static 47 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { 48 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, 49 TypeToPrefValue(browser_defaults::kDefaultSessionStartupType)); 50 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup); 51 } 52 53 // static 54 void SessionStartupPref::SetStartupPref( 55 Profile* profile, 56 const SessionStartupPref& pref) { 57 DCHECK(profile); 58 SetStartupPref(profile->GetPrefs(), pref); 59 } 60 61 // static 62 void SessionStartupPref::SetStartupPref(PrefService* prefs, 63 const SessionStartupPref& pref) { 64 DCHECK(prefs); 65 66 if (!SessionStartupPref::TypeIsManaged(prefs)) 67 prefs->SetInteger(prefs::kRestoreOnStartup, TypeToPrefValue(pref.type)); 68 69 if (!SessionStartupPref::URLsAreManaged(prefs)) { 70 // Always save the URLs, that way the UI can remain consistent even if the 71 // user changes the startup type pref. 72 // Ownership of the ListValue retains with the pref service. 73 ListPrefUpdate update(prefs, prefs::kURLsToRestoreOnStartup); 74 ListValue* url_pref_list = update.Get(); 75 DCHECK(url_pref_list); 76 url_pref_list->Clear(); 77 for (size_t i = 0; i < pref.urls.size(); ++i) { 78 url_pref_list->Set(static_cast<int>(i), 79 new StringValue(pref.urls[i].spec())); 80 } 81 } 82 } 83 84 // static 85 SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { 86 DCHECK(profile); 87 return GetStartupPref(profile->GetPrefs()); 88 } 89 90 // static 91 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { 92 DCHECK(prefs); 93 SessionStartupPref pref( 94 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); 95 96 // Always load the urls, even if the pref type isn't URLS. This way the 97 // preferences panels can show the user their last choice. 98 const ListValue* url_pref_list = prefs->GetList( 99 prefs::kURLsToRestoreOnStartup); 100 if (url_pref_list) { 101 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { 102 Value* value = NULL; 103 if (url_pref_list->Get(i, &value)) { 104 std::string url_text; 105 if (value->GetAsString(&url_text)) { 106 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); 107 pref.urls.push_back(fixed_url); 108 } 109 } 110 } 111 } 112 return pref; 113 } 114 115 // static 116 bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { 117 DCHECK(prefs); 118 const PrefService::Preference* pref_restore = 119 prefs->FindPreference(prefs::kRestoreOnStartup); 120 DCHECK(pref_restore); 121 return pref_restore->IsManaged(); 122 } 123 124 // static 125 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { 126 DCHECK(prefs); 127 const PrefService::Preference* pref_urls = 128 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); 129 DCHECK(pref_urls); 130 return pref_urls->IsManaged(); 131 } 132 133 SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} 134 135 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} 136 137 SessionStartupPref::~SessionStartupPref() {} 138