| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/defaults.h" | 11 #include "chrome/browser/defaults.h" |
| 12 #include "chrome/browser/net/url_fixer_upper.h" | 12 #include "chrome/browser/net/url_fixer_upper.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 | 17 |
| 18 #ifdef OS_MACOSX |
| 19 #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
| 20 #endif |
| 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 // For historical reasons the enum and value registered in the prefs don't line | 24 // For historical reasons the enum and value registered in the prefs don't line |
| 21 // up. These are the values registered in prefs. | 25 // up. These are the values registered in prefs. |
| 22 const int kPrefValueDefault = 0; | 26 const int kPrefValueDefault = 0; |
| 23 const int kPrefValueLast = 1; | 27 const int kPrefValueLast = 1; |
| 24 const int kPrefValueURLs = 4; | 28 const int kPrefValueURLs = 4; |
| 25 | 29 |
| 26 // Converts a SessionStartupPref::Type to an integer written to prefs. | 30 // Converts a SessionStartupPref::Type to an integer written to prefs. |
| 27 int TypeToPrefValue(SessionStartupPref::Type type) { | 31 int TypeToPrefValue(SessionStartupPref::Type type) { |
| 28 switch (type) { | 32 switch (type) { |
| 29 case SessionStartupPref::LAST: return kPrefValueLast; | 33 case SessionStartupPref::LAST: return kPrefValueLast; |
| 30 case SessionStartupPref::URLS: return kPrefValueURLs; | 34 case SessionStartupPref::URLS: return kPrefValueURLs; |
| 31 default: return kPrefValueDefault; | 35 default: return kPrefValueDefault; |
| 32 } | 36 } |
| 33 } | 37 } |
| 34 | 38 |
| 35 // Converts an integer pref value to a SessionStartupPref::Type. | 39 // Converts an integer pref value to a SessionStartupPref::Type. |
| 36 SessionStartupPref::Type PrefValueToType(int pref_value) { | 40 SessionStartupPref::Type PrefValueToType(int pref_value) { |
| 37 switch (pref_value) { | 41 switch (pref_value) { |
| 38 case kPrefValueLast: return SessionStartupPref::LAST; | 42 case kPrefValueLast: return SessionStartupPref::LAST; |
| 39 case kPrefValueURLs: return SessionStartupPref::URLS; | 43 case kPrefValueURLs: return SessionStartupPref::URLS; |
| 40 default: return SessionStartupPref::DEFAULT; | 44 default: return SessionStartupPref::DEFAULT; |
| 41 } | 45 } |
| 42 } | 46 } |
| 43 | 47 |
| 48 bool TypeIsDefaultValue(PrefService* prefs) { |
| 49 const PrefService::Preference* pref_restore = |
| 50 prefs->FindPreference(prefs::kRestoreOnStartup); |
| 51 return pref_restore->IsDefaultValue(); |
| 52 } |
| 53 |
| 44 } // namespace | 54 } // namespace |
| 45 | 55 |
| 46 // static | 56 // static |
| 47 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 57 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
| 48 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, | 58 prefs->RegisterIntegerPref(prefs::kRestoreOnStartup, |
| 49 TypeToPrefValue( | 59 TypeToPrefValue( |
| 50 browser_defaults::kDefaultSessionStartupType), | 60 browser_defaults::kDefaultSessionStartupType), |
| 51 PrefService::SYNCABLE_PREF); | 61 PrefService::SYNCABLE_PREF); |
| 52 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, | 62 prefs->RegisterListPref(prefs::kURLsToRestoreOnStartup, |
| 53 PrefService::SYNCABLE_PREF); | 63 PrefService::SYNCABLE_PREF); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 DCHECK(profile); | 99 DCHECK(profile); |
| 90 return GetStartupPref(profile->GetPrefs()); | 100 return GetStartupPref(profile->GetPrefs()); |
| 91 } | 101 } |
| 92 | 102 |
| 93 // static | 103 // static |
| 94 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 104 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
| 95 DCHECK(prefs); | 105 DCHECK(prefs); |
| 96 SessionStartupPref pref( | 106 SessionStartupPref pref( |
| 97 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 107 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
| 98 | 108 |
| 109 #ifdef OS_MACOSX |
| 110 if (TypeIsDefaultValue(prefs)) { |
| 111 // |DEFAULT| really means "Don't restore". The actual default value could |
| 112 // change, so explicitly set both. |
| 113 if (restore_utils::IsWindowRestoreEnabled()) |
| 114 pref.type = SessionStartupPref::LAST; |
| 115 else |
| 116 pref.type = SessionStartupPref::DEFAULT; |
| 117 } |
| 118 #endif |
| 119 |
| 99 // Always load the urls, even if the pref type isn't URLS. This way the | 120 // Always load the urls, even if the pref type isn't URLS. This way the |
| 100 // preferences panels can show the user their last choice. | 121 // preferences panels can show the user their last choice. |
| 101 const ListValue* url_pref_list = prefs->GetList( | 122 const ListValue* url_pref_list = prefs->GetList( |
| 102 prefs::kURLsToRestoreOnStartup); | 123 prefs::kURLsToRestoreOnStartup); |
| 103 if (url_pref_list) { | 124 if (url_pref_list) { |
| 104 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { | 125 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { |
| 105 Value* value = NULL; | 126 Value* value = NULL; |
| 106 if (url_pref_list->Get(i, &value)) { | 127 if (url_pref_list->Get(i, &value)) { |
| 107 std::string url_text; | 128 std::string url_text; |
| 108 if (value->GetAsString(&url_text)) { | 129 if (value->GetAsString(&url_text)) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); | 152 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); |
| 132 DCHECK(pref_urls); | 153 DCHECK(pref_urls); |
| 133 return pref_urls->IsManaged(); | 154 return pref_urls->IsManaged(); |
| 134 } | 155 } |
| 135 | 156 |
| 136 SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} | 157 SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} |
| 137 | 158 |
| 138 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 159 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
| 139 | 160 |
| 140 SessionStartupPref::~SessionStartupPref() {} | 161 SessionStartupPref::~SessionStartupPref() {} |
| OLD | NEW |