OLD | NEW |
1 // Copyright (c) 2012 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/values.h" | |
11 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/defaults.h" | 11 #include "chrome/browser/defaults.h" |
13 #include "chrome/browser/net/url_fixer_upper.h" | 12 #include "chrome/browser/net/url_fixer_upper.h" |
14 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
18 | 17 |
19 #ifdef OS_MACOSX | 18 #ifdef OS_MACOSX |
20 #include "chrome/browser/first_run/first_run.h" | 19 #include "chrome/browser/first_run/first_run.h" |
21 #include "chrome/browser/ui/cocoa/window_restore_utils.h" | 20 #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
22 #endif | 21 #endif |
23 | 22 |
24 namespace { | 23 namespace { |
25 | 24 |
26 // For historical reasons the enum and value registered in the prefs don't line | 25 // For historical reasons the enum and value registered in the prefs don't line |
27 // up. These are the values registered in prefs. | 26 // up. These are the values registered in prefs. |
28 const int kPrefValueHomePage = 0; // Deprecated | 27 const int kPrefValueDefault = 0; |
29 const int kPrefValueLast = 1; | 28 const int kPrefValueLast = 1; |
30 const int kPrefValueURLs = 4; | 29 const int kPrefValueURLs = 4; |
31 const int kPrefValueNewTab = 5; | |
32 | 30 |
33 // Converts a SessionStartupPref::Type to an integer written to prefs. | 31 // Converts a SessionStartupPref::Type to an integer written to prefs. |
34 int TypeToPrefValue(SessionStartupPref::Type type) { | 32 int TypeToPrefValue(SessionStartupPref::Type type) { |
35 switch (type) { | 33 switch (type) { |
36 case SessionStartupPref::LAST: return kPrefValueLast; | 34 case SessionStartupPref::LAST: return kPrefValueLast; |
37 case SessionStartupPref::URLS: return kPrefValueURLs; | 35 case SessionStartupPref::URLS: return kPrefValueURLs; |
38 default: return kPrefValueNewTab; | 36 default: return kPrefValueDefault; |
39 } | 37 } |
40 } | 38 } |
41 | 39 |
42 // Converts an integer pref value to a SessionStartupPref::Type. | 40 // Converts an integer pref value to a SessionStartupPref::Type. |
43 SessionStartupPref::Type PrefValueToType(int pref_value) { | 41 SessionStartupPref::Type PrefValueToType(int pref_value) { |
44 switch (pref_value) { | 42 switch (pref_value) { |
45 case kPrefValueLast: return SessionStartupPref::LAST; | 43 case kPrefValueLast: return SessionStartupPref::LAST; |
46 case kPrefValueURLs: return SessionStartupPref::URLS; | 44 case kPrefValueURLs: return SessionStartupPref::URLS; |
47 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; | 45 default: return SessionStartupPref::DEFAULT; |
48 default: return SessionStartupPref::DEFAULT; | |
49 } | 46 } |
50 } | 47 } |
51 | 48 |
52 // Sets the list of URLs to display at startup to a list consisting of only the | |
53 // user's home page. | |
54 void SetNewUrlList(PrefService* prefs) { | |
55 ListValue new_url_pref_list; | |
56 StringValue* home_page = new StringValue(prefs->GetString(prefs::kHomePage)); | |
57 new_url_pref_list.Append(home_page); | |
58 prefs->Set(prefs::kURLsToRestoreOnStartup, new_url_pref_list); | |
59 } | |
60 | |
61 } // namespace | 49 } // namespace |
62 | 50 |
63 // static | 51 // static |
64 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 52 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
65 SessionStartupPref::Type type = browser_defaults::kDefaultSessionStartupType; | 53 SessionStartupPref::Type type = browser_defaults::kDefaultSessionStartupType; |
66 | 54 |
67 #ifdef OS_MACOSX | 55 #ifdef OS_MACOSX |
68 // During first run the calling code relies on |DEFAULT| session preference | 56 // During first run the calling code relies on |DEFAULT| session preference |
69 // value to avoid session restore. That is respected here. | 57 // value to avoid session restore. That is respected here. |
70 if (!first_run::IsChromeFirstRun()) { | 58 if (!first_run::IsChromeFirstRun()) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 DCHECK(profile); | 108 DCHECK(profile); |
121 return GetStartupPref(profile->GetPrefs()); | 109 return GetStartupPref(profile->GetPrefs()); |
122 } | 110 } |
123 | 111 |
124 // static | 112 // static |
125 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 113 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
126 DCHECK(prefs); | 114 DCHECK(prefs); |
127 SessionStartupPref pref( | 115 SessionStartupPref pref( |
128 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 116 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
129 | 117 |
130 // Migrate from "Open the home page" to "Open the following URLs". If the user | |
131 // had the "Open the homepage" option selected, then we need switch them to | |
132 // "Open the following URLs" and set the list of URLs to a list containing | |
133 // just the user's homepage. | |
134 if (pref.type == SessionStartupPref::HOMEPAGE) { | |
135 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
136 pref.type = SessionStartupPref::URLS; | |
137 SetNewUrlList(prefs); | |
138 } | |
139 | |
140 // Always load the urls, even if the pref type isn't URLS. This way the | 118 // Always load the urls, even if the pref type isn't URLS. This way the |
141 // preferences panels can show the user their last choice. | 119 // preferences panels can show the user their last choice. |
142 const ListValue* url_pref_list = prefs->GetList( | 120 const ListValue* url_pref_list = prefs->GetList( |
143 prefs::kURLsToRestoreOnStartup); | 121 prefs::kURLsToRestoreOnStartup); |
144 if (url_pref_list) { | 122 if (url_pref_list) { |
145 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { | 123 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { |
146 Value* value = NULL; | 124 Value* value = NULL; |
147 if (url_pref_list->Get(i, &value)) { | 125 if (url_pref_list->Get(i, &value)) { |
148 std::string url_text; | 126 std::string url_text; |
149 if (value->GetAsString(&url_text)) { | 127 if (value->GetAsString(&url_text)) { |
(...skipping 17 matching lines...) Expand all Loading... |
167 | 145 |
168 // static | 146 // static |
169 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 147 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
170 DCHECK(prefs); | 148 DCHECK(prefs); |
171 const PrefService::Preference* pref_urls = | 149 const PrefService::Preference* pref_urls = |
172 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); | 150 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); |
173 DCHECK(pref_urls); | 151 DCHECK(pref_urls); |
174 return pref_urls->IsManaged(); | 152 return pref_urls->IsManaged(); |
175 } | 153 } |
176 | 154 |
| 155 SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} |
| 156 |
177 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 157 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
178 | 158 |
179 SessionStartupPref::~SessionStartupPref() {} | 159 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |