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/values.h" | |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/defaults.h" | 12 #include "chrome/browser/defaults.h" |
12 #include "chrome/browser/net/url_fixer_upper.h" | 13 #include "chrome/browser/net/url_fixer_upper.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
17 | 18 |
18 #ifdef OS_MACOSX | 19 #ifdef OS_MACOSX |
19 #include "chrome/browser/first_run/first_run.h" | 20 #include "chrome/browser/first_run/first_run.h" |
20 #include "chrome/browser/ui/cocoa/window_restore_utils.h" | 21 #include "chrome/browser/ui/cocoa/window_restore_utils.h" |
21 #endif | 22 #endif |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // For historical reasons the enum and value registered in the prefs don't line | 26 // For historical reasons the enum and value registered in the prefs don't line |
26 // up. These are the values registered in prefs. | 27 // up. These are the values registered in prefs. |
27 const int kPrefValueDefault = 0; | 28 |
29 // kPrefValueHomePage is deprecated, and means the user wants to load the | |
30 // homepage on startup. If this is set, we automatically migrate the to | |
31 // SessionStartupPref::URLS where the list of URLs consists of the user's | |
32 // homepage only. | |
33 const int kPrefValueHomePage = 0; | |
csilv
2012/02/01 20:37:29
Let's save this longer explanation for where the p
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Done.
| |
28 const int kPrefValueLast = 1; | 34 const int kPrefValueLast = 1; |
29 const int kPrefValueURLs = 4; | 35 const int kPrefValueURLs = 4; |
36 const int kPrefValueNewTab = 5; | |
30 | 37 |
31 // Converts a SessionStartupPref::Type to an integer written to prefs. | 38 // Converts a SessionStartupPref::Type to an integer written to prefs. |
32 int TypeToPrefValue(SessionStartupPref::Type type) { | 39 int TypeToPrefValue(SessionStartupPref::Type type) { |
33 switch (type) { | 40 switch (type) { |
34 case SessionStartupPref::LAST: return kPrefValueLast; | 41 case SessionStartupPref::HOMEPAGE: return kPrefValueHomePage; |
csilv
2012/02/01 20:37:29
This line is unneeded since we won't ever write th
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Done.
| |
35 case SessionStartupPref::URLS: return kPrefValueURLs; | 42 case SessionStartupPref::LAST: return kPrefValueLast; |
36 default: return kPrefValueDefault; | 43 case SessionStartupPref::URLS: return kPrefValueURLs; |
44 default: return kPrefValueNewTab; | |
37 } | 45 } |
38 } | 46 } |
39 | 47 |
40 // Converts an integer pref value to a SessionStartupPref::Type. | 48 // Converts an integer pref value to a SessionStartupPref::Type. |
41 SessionStartupPref::Type PrefValueToType(int pref_value) { | 49 SessionStartupPref::Type PrefValueToType(int pref_value) { |
42 switch (pref_value) { | 50 switch (pref_value) { |
43 case kPrefValueLast: return SessionStartupPref::LAST; | 51 case kPrefValueLast: return SessionStartupPref::LAST; |
44 case kPrefValueURLs: return SessionStartupPref::URLS; | 52 case kPrefValueURLs: return SessionStartupPref::URLS; |
45 default: return SessionStartupPref::DEFAULT; | 53 case kPrefValueHomePage: return SessionStartupPref::HOMEPAGE; |
54 default: return SessionStartupPref::NEWTAB; | |
46 } | 55 } |
47 } | 56 } |
48 | 57 |
58 // Sets the list of URLs to display at startup to a list consisting of only the | |
59 // user's home page. | |
60 void SetNewUrlList(PrefService * prefs) { | |
csilv
2012/02/01 20:37:29
get rid of space before '*'
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Done.
| |
61 ListValue* new_url_pref_list = new ListValue(); | |
csilv
2012/02/01 20:37:29
This leaks. I would recommend instead:
ListValu
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Done.
| |
62 StringValue* homePage = new StringValue(prefs->GetString(prefs::kHomePage)); | |
63 new_url_pref_list->Append(static_cast<Value*>(homePage)); | |
csilv
2012/02/01 20:37:29
I don't think you need the static_cast here
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Thanks, I think I was misreading the compiler erro
| |
64 const ListValue* const_list = const_cast<ListValue*>(new_url_pref_list); | |
65 prefs->Set(prefs::kURLsToRestoreOnStartup, *const_list); | |
csilv
2012/02/01 20:37:29
kill line 64, change 64 to:
prefs->Set(prefs::kU
Tyler Breisacher (Chromium)
2012/02/01 21:49:38
Done.
| |
66 } | |
67 | |
csilv
2012/02/01 20:37:29
After making the above changes, the function may b
| |
49 } // namespace | 68 } // namespace |
50 | 69 |
51 // static | 70 // static |
52 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { | 71 void SessionStartupPref::RegisterUserPrefs(PrefService* prefs) { |
53 SessionStartupPref::Type type = browser_defaults::kDefaultSessionStartupType; | 72 SessionStartupPref::Type type = browser_defaults::kDefaultSessionStartupType; |
54 | 73 |
55 #ifdef OS_MACOSX | 74 #ifdef OS_MACOSX |
56 // During first run the calling code relies on |DEFAULT| session preference | 75 // During first run the calling code relies on |DEFAULT| session preference |
57 // value to avoid session restore. That is respected here. | 76 // value to avoid session restore. That is respected here. |
58 if (!first_run::IsChromeFirstRun()) { | 77 if (!first_run::IsChromeFirstRun()) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 DCHECK(profile); | 127 DCHECK(profile); |
109 return GetStartupPref(profile->GetPrefs()); | 128 return GetStartupPref(profile->GetPrefs()); |
110 } | 129 } |
111 | 130 |
112 // static | 131 // static |
113 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { | 132 SessionStartupPref SessionStartupPref::GetStartupPref(PrefService* prefs) { |
114 DCHECK(prefs); | 133 DCHECK(prefs); |
115 SessionStartupPref pref( | 134 SessionStartupPref pref( |
116 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); | 135 PrefValueToType(prefs->GetInteger(prefs::kRestoreOnStartup))); |
117 | 136 |
137 // Migrate from "Open the home page" to "Open the following URLs" | |
138 if (pref.type == SessionStartupPref::HOMEPAGE) { | |
139 prefs->SetInteger(prefs::kRestoreOnStartup, kPrefValueURLs); | |
140 pref.type = SessionStartupPref::URLS; | |
141 SetNewUrlList(prefs); | |
142 } | |
143 | |
118 // Always load the urls, even if the pref type isn't URLS. This way the | 144 // 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. | 145 // preferences panels can show the user their last choice. |
120 const ListValue* url_pref_list = prefs->GetList( | 146 const ListValue* url_pref_list = prefs->GetList( |
121 prefs::kURLsToRestoreOnStartup); | 147 prefs::kURLsToRestoreOnStartup); |
122 if (url_pref_list) { | 148 if (url_pref_list) { |
123 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { | 149 for (size_t i = 0; i < url_pref_list->GetSize(); ++i) { |
124 Value* value = NULL; | 150 Value* value = NULL; |
125 if (url_pref_list->Get(i, &value)) { | 151 if (url_pref_list->Get(i, &value)) { |
126 std::string url_text; | 152 std::string url_text; |
127 if (value->GetAsString(&url_text)) { | 153 if (value->GetAsString(&url_text)) { |
(...skipping 17 matching lines...) Expand all Loading... | |
145 | 171 |
146 // static | 172 // static |
147 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { | 173 bool SessionStartupPref::URLsAreManaged(PrefService* prefs) { |
148 DCHECK(prefs); | 174 DCHECK(prefs); |
149 const PrefService::Preference* pref_urls = | 175 const PrefService::Preference* pref_urls = |
150 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); | 176 prefs->FindPreference(prefs::kURLsToRestoreOnStartup); |
151 DCHECK(pref_urls); | 177 DCHECK(pref_urls); |
152 return pref_urls->IsManaged(); | 178 return pref_urls->IsManaged(); |
153 } | 179 } |
154 | 180 |
155 SessionStartupPref::SessionStartupPref() : type(DEFAULT) {} | |
156 | |
157 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} | 181 SessionStartupPref::SessionStartupPref(Type type) : type(type) {} |
158 | 182 |
159 SessionStartupPref::~SessionStartupPref() {} | 183 SessionStartupPref::~SessionStartupPref() {} |
OLD | NEW |