Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: chrome/browser/dom_ui/browser_options_handler.cc

Issue 3046025: Implement most of the startup page controls in DOMUI prefs (Closed)
Patch Set: Addresses review comments Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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/dom_ui/browser_options_handler.h" 5 #include "chrome/browser/dom_ui/browser_options_handler.h"
6 6
7 #include "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/custom_home_pages_table_model.h"
13 #include "chrome/browser/profile.h" 14 #include "chrome/browser/profile.h"
15 #include "chrome/browser/session_startup_pref.h"
14 #include "chrome/installer/util/browser_distribution.h" 16 #include "chrome/installer/util/browser_distribution.h"
15 #include "grit/chromium_strings.h" 17 #include "grit/chromium_strings.h"
16 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
17 19
18 BrowserOptionsHandler::BrowserOptionsHandler() 20 BrowserOptionsHandler::BrowserOptionsHandler()
19 : template_url_model_(NULL) { 21 : template_url_model_(NULL), startup_custom_pages_table_model_(NULL) {
20 #if !defined(OS_MACOSX) 22 #if !defined(OS_MACOSX)
21 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); 23 default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this);
22 #endif 24 #endif
23 } 25 }
24 26
25 BrowserOptionsHandler::~BrowserOptionsHandler() { 27 BrowserOptionsHandler::~BrowserOptionsHandler() {
26 if (default_browser_worker_.get()) 28 if (default_browser_worker_.get())
27 default_browser_worker_->ObserverDestroyed(); 29 default_browser_worker_->ObserverDestroyed();
28 if (template_url_model_) 30 if (template_url_model_)
29 template_url_model_->RemoveObserver(this); 31 template_url_model_->RemoveObserver(this);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 l10n_util::GetString(IDS_PRODUCT_NAME))); 70 l10n_util::GetString(IDS_PRODUCT_NAME)));
69 } 71 }
70 72
71 void BrowserOptionsHandler::RegisterMessages() { 73 void BrowserOptionsHandler::RegisterMessages() {
72 dom_ui_->RegisterMessageCallback( 74 dom_ui_->RegisterMessageCallback(
73 "becomeDefaultBrowser", 75 "becomeDefaultBrowser",
74 NewCallback(this, &BrowserOptionsHandler::BecomeDefaultBrowser)); 76 NewCallback(this, &BrowserOptionsHandler::BecomeDefaultBrowser));
75 dom_ui_->RegisterMessageCallback( 77 dom_ui_->RegisterMessageCallback(
76 "setDefaultSearchEngine", 78 "setDefaultSearchEngine",
77 NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine)); 79 NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine));
80 dom_ui_->RegisterMessageCallback(
81 "removeStartupPages",
82 NewCallback(this, &BrowserOptionsHandler::RemoveStartupPages));
83 dom_ui_->RegisterMessageCallback(
84 "setStartupPagesToCurrentPages",
85 NewCallback(this, &BrowserOptionsHandler::SetStartupPagesToCurrentPages));
78 } 86 }
79 87
80 void BrowserOptionsHandler::Initialize() { 88 void BrowserOptionsHandler::Initialize() {
81 UpdateDefaultBrowserState(); 89 UpdateDefaultBrowserState();
82 90 UpdateStartupPages();
83 template_url_model_ = dom_ui_->GetProfile()->GetTemplateURLModel(); 91 UpdateSearchEngines();
84 if (template_url_model_) {
85 template_url_model_->Load();
86 template_url_model_->AddObserver(this);
87 OnTemplateURLModelChanged();
88 }
89 } 92 }
90 93
91 void BrowserOptionsHandler::UpdateDefaultBrowserState() { 94 void BrowserOptionsHandler::UpdateDefaultBrowserState() {
92 #if defined(OS_WIN) 95 #if defined(OS_WIN)
93 // Check for side-by-side first. 96 // Check for side-by-side first.
94 if (!BrowserDistribution::GetDistribution()->CanSetAsDefault()) { 97 if (!BrowserDistribution::GetDistribution()->CanSetAsDefault()) {
95 SetDefaultBrowserUIString(IDS_OPTIONS_DEFAULTBROWSER_SXS); 98 SetDefaultBrowserUIString(IDS_OPTIONS_DEFAULTBROWSER_SXS);
96 return; 99 return;
97 } 100 }
98 #endif 101 #endif
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 207 }
205 int selected_index = StringToInt(string_value); 208 int selected_index = StringToInt(string_value);
206 209
207 std::vector<const TemplateURL*> model_urls = 210 std::vector<const TemplateURL*> model_urls =
208 template_url_model_->GetTemplateURLs(); 211 template_url_model_->GetTemplateURLs();
209 if (selected_index >= 0 && 212 if (selected_index >= 0 &&
210 selected_index < static_cast<int>(model_urls.size())) 213 selected_index < static_cast<int>(model_urls.size()))
211 template_url_model_->SetDefaultSearchProvider(model_urls[selected_index]); 214 template_url_model_->SetDefaultSearchProvider(model_urls[selected_index]);
212 } 215 }
213 216
217 void BrowserOptionsHandler::UpdateSearchEngines() {
218 template_url_model_ = dom_ui_->GetProfile()->GetTemplateURLModel();
219 if (template_url_model_) {
220 template_url_model_->Load();
221 template_url_model_->AddObserver(this);
222 OnTemplateURLModelChanged();
223 }
224 }
225
226 void BrowserOptionsHandler::UpdateStartupPages() {
227 Profile* profile = dom_ui_->GetProfile();
228 startup_custom_pages_table_model_.reset(
229 new CustomHomePagesTableModel(profile));
230 startup_custom_pages_table_model_->SetObserver(this);
zel 2010/07/27 18:05:05 Where do you unregister this observer? Should this
stuartmorgan 2010/07/27 18:18:26 BrowserOptionsHandler owns the table model, so the
zel 2010/07/27 18:20:44 Fair enough... as long as CustomHomePagesTableMode
231
232 const SessionStartupPref startup_pref =
233 SessionStartupPref::GetStartupPref(profile->GetPrefs());
234 startup_custom_pages_table_model_->SetURLs(startup_pref.urls);
235 }
236
237 void BrowserOptionsHandler::OnModelChanged() {
238 // TODO(stuartmorgan): Add support for showing favicons.
239 ListValue startup_pages;
240 int page_count = startup_custom_pages_table_model_->RowCount();
241 for (int i = 0; i < page_count; ++i) {
242 DictionaryValue* entry = new DictionaryValue();
243 entry->SetString(L"title",
244 startup_custom_pages_table_model_->GetText(i, 0));
245 entry->SetString(L"tooltip",
246 startup_custom_pages_table_model_->GetTooltip(i));
247 startup_pages.Append(entry);
248 }
249
250 dom_ui_->CallJavascriptFunction(L"BrowserOptions.updateStartupPages",
251 startup_pages);
252 }
253
254 void BrowserOptionsHandler::OnItemsChanged(int start, int length) {
255 OnModelChanged();
256 }
257
258 void BrowserOptionsHandler::OnItemsAdded(int start, int length) {
259 OnModelChanged();
260 }
261
262 void BrowserOptionsHandler::OnItemsRemoved(int start, int length) {
263 OnModelChanged();
264 }
265
266 void BrowserOptionsHandler::SetStartupPagesToCurrentPages(const Value* value) {
267 startup_custom_pages_table_model_->SetToCurrentlyOpenPages();
268 SaveStartupPagesPref();
269 }
270
271 void BrowserOptionsHandler::RemoveStartupPages(const Value* value) {
272 if (!value || !value->IsType(Value::TYPE_LIST)) {
273 NOTREACHED();
274 return;
275 }
276 const ListValue* param_values = static_cast<const ListValue*>(value);
277 for (int i = param_values->GetSize() - 1; i >= 0; --i) {
278 std::string string_value;
279 if (!param_values->GetString(i, &string_value)) {
280 NOTREACHED();
281 return;
282 }
283 int selected_index = StringToInt(string_value);
284 if (selected_index < 0 ||
285 selected_index >= startup_custom_pages_table_model_->RowCount()) {
286 NOTREACHED();
287 return;
288 }
289 startup_custom_pages_table_model_->Remove(selected_index);
290 }
291
292 SaveStartupPagesPref();
293 }
294
295 void BrowserOptionsHandler::SaveStartupPagesPref() {
296 PrefService* prefs = dom_ui_->GetProfile()->GetPrefs();
297
298 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs);
299 pref.urls = startup_custom_pages_table_model_->GetURLs();
300
301 SessionStartupPref::SetStartupPref(prefs, pref);
302 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698