session_startup_pref.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_pref_update.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/common/pref_names.h" 17 18namespace { 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. 22const int kPrefValueDefault = 0; 23const int kPrefValueLast = 1; 24const int kPrefValueURLs = 4; 25 26// Converts a SessionStartupPref::Type to an integer written to prefs. 27int 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. 36SessionStartupPref::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 47void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { 48 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, 49 TypeToPrefValue(browser_defaults::kDefaultSessionStartupType)); 50 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup); 51} 52 53// static 54void SessionStartupPref::SetStartupPref( 55 Profile* profile, 56 const SessionStartupPref& pref) { 57 DCHECK(profile); 58 SetStartupPref(profile->GetPrefs(), pref); 59} 60 61// static 62void 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 ScopedPrefUpdate update(prefs, prefs::kURLsToRestoreOnStartup); 74 ListValue* url_pref_list = 75 prefs->GetMutableList(prefs::kURLsToRestoreOnStartup); 76 DCHECK(url_pref_list); 77 url_pref_list->Clear(); 78 for (size_t i = 0; i < pref.urls.size(); ++i) { 79 url_pref_list->Set(static_cast<int>(i), 80 new StringValue(pref.urls[i].spec())); 81 } 82 } 83} 84 85// static 86SessionStartupPref SessionStartupPref::GetStartupPref(Profile* profile) { 87 DCHECK(profile); 88 return GetStartupPref(profile->GetPrefs()); 89} 90 91// static 92SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { 93 DCHECK(prefs); 94 SessionStartupPref pref( 95 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); 96 97 // Always load the urls, even if the pref type isn't URLS. This way the 98 // preferences panels can show the user their last choice. 99 const ListValue* url_pref_list = prefs->GetList( 100 prefs::kURLsToRestoreOnStartup); 101 if (url_pref_list) { 102 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { 103 Value* value = NULL; 104 if (url_pref_list->Get(i, &value)) { 105 std::string url_text; 106 if (value->GetAsString(&url_text)) { 107 GURL fixed_url = URLFixerUpper::FixupURL(url_text, ""); 108 pref.urls.push_back(fixed_url); 109 } 110 } 111 } 112 } 113 return pref; 114} 115 116// static 117bool SessionStartupPref::TypeIsManaged(PrefService* prefs) { 118 DCHECK(prefs); 119 const PrefService::Preference* pref_restore = 120 prefs->FindPreference(prefs::kRestoreOnStartup); 121 DCHECK(pref_restore); 122 return pref_restore->IsManaged(); 123} 124 125// static 126bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { 127 DCHECK(prefs); 128 const PrefService::Preference* pref_urls = 129 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); 130 DCHECK(pref_urls); 131 return pref_urls->IsManaged(); 132} 133 134SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} 135 136SessionStartupPref::SessionStartupPref(Type type) : type(type) {} 137 138SessionStartupPref::~SessionStartupPref() {} 139