| OLD | NEW |
| 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/options/browser_options_handler.h" | 5 #include "chrome/browser/dom_ui/options/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/singleton.h" | 10 #include "base/singleton.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 dom_ui_->RegisterMessageCallback( | 103 dom_ui_->RegisterMessageCallback( |
| 104 "setDefaultSearchEngine", | 104 "setDefaultSearchEngine", |
| 105 NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine)); | 105 NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine)); |
| 106 dom_ui_->RegisterMessageCallback( | 106 dom_ui_->RegisterMessageCallback( |
| 107 "removeStartupPages", | 107 "removeStartupPages", |
| 108 NewCallback(this, &BrowserOptionsHandler::RemoveStartupPages)); | 108 NewCallback(this, &BrowserOptionsHandler::RemoveStartupPages)); |
| 109 dom_ui_->RegisterMessageCallback( | 109 dom_ui_->RegisterMessageCallback( |
| 110 "addStartupPage", | 110 "addStartupPage", |
| 111 NewCallback(this, &BrowserOptionsHandler::AddStartupPage)); | 111 NewCallback(this, &BrowserOptionsHandler::AddStartupPage)); |
| 112 dom_ui_->RegisterMessageCallback( | 112 dom_ui_->RegisterMessageCallback( |
| 113 "editStartupPage", |
| 114 NewCallback(this, &BrowserOptionsHandler::EditStartupPage)); |
| 115 dom_ui_->RegisterMessageCallback( |
| 113 "setStartupPagesToCurrentPages", | 116 "setStartupPagesToCurrentPages", |
| 114 NewCallback(this, &BrowserOptionsHandler::SetStartupPagesToCurrentPages)); | 117 NewCallback(this, &BrowserOptionsHandler::SetStartupPagesToCurrentPages)); |
| 115 } | 118 } |
| 116 | 119 |
| 117 void BrowserOptionsHandler::Initialize() { | 120 void BrowserOptionsHandler::Initialize() { |
| 118 // Create our favicon data source. | 121 // Create our favicon data source. |
| 119 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
| 120 BrowserThread::IO, FROM_HERE, | 123 BrowserThread::IO, FROM_HERE, |
| 121 NewRunnableMethod( | 124 NewRunnableMethod( |
| 122 ChromeURLDataManager::GetInstance(), | 125 ChromeURLDataManager::GetInstance(), |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 void BrowserOptionsHandler::OnModelChanged() { | 281 void BrowserOptionsHandler::OnModelChanged() { |
| 279 ListValue startup_pages; | 282 ListValue startup_pages; |
| 280 int page_count = startup_custom_pages_table_model_->RowCount(); | 283 int page_count = startup_custom_pages_table_model_->RowCount(); |
| 281 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); | 284 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); |
| 282 for (int i = 0; i < page_count; ++i) { | 285 for (int i = 0; i < page_count; ++i) { |
| 283 DictionaryValue* entry = new DictionaryValue(); | 286 DictionaryValue* entry = new DictionaryValue(); |
| 284 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); | 287 entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); |
| 285 entry->SetString("url", urls[i].spec()); | 288 entry->SetString("url", urls[i].spec()); |
| 286 entry->SetString("tooltip", | 289 entry->SetString("tooltip", |
| 287 startup_custom_pages_table_model_->GetTooltip(i)); | 290 startup_custom_pages_table_model_->GetTooltip(i)); |
| 291 entry->SetString("modelIndex", base::IntToString(i)); |
| 288 startup_pages.Append(entry); | 292 startup_pages.Append(entry); |
| 289 } | 293 } |
| 290 | 294 |
| 291 dom_ui_->CallJavascriptFunction(L"BrowserOptions.updateStartupPages", | 295 dom_ui_->CallJavascriptFunction(L"BrowserOptions.updateStartupPages", |
| 292 startup_pages); | 296 startup_pages); |
| 293 } | 297 } |
| 294 | 298 |
| 295 void BrowserOptionsHandler::OnItemsChanged(int start, int length) { | 299 void BrowserOptionsHandler::OnItemsChanged(int start, int length) { |
| 296 OnModelChanged(); | 300 OnModelChanged(); |
| 297 } | 301 } |
| 298 | 302 |
| 299 void BrowserOptionsHandler::OnItemsAdded(int start, int length) { | 303 void BrowserOptionsHandler::OnItemsAdded(int start, int length) { |
| 300 OnModelChanged(); | 304 OnModelChanged(); |
| 301 } | 305 } |
| 302 | 306 |
| 303 void BrowserOptionsHandler::OnItemsRemoved(int start, int length) { | 307 void BrowserOptionsHandler::OnItemsRemoved(int start, int length) { |
| 304 OnModelChanged(); | 308 OnModelChanged(); |
| 305 } | 309 } |
| 306 | 310 |
| 307 void BrowserOptionsHandler::SetStartupPagesToCurrentPages( | 311 void BrowserOptionsHandler::SetStartupPagesToCurrentPages( |
| 308 const ListValue* args) { | 312 const ListValue* args) { |
| 309 startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); | 313 startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); |
| 310 SaveStartupPagesPref(); | 314 SaveStartupPagesPref(); |
| 311 } | 315 } |
| 312 | 316 |
| 313 void BrowserOptionsHandler::RemoveStartupPages(const ListValue* args) { | 317 void BrowserOptionsHandler::RemoveStartupPages(const ListValue* args) { |
| 314 for (int i = args->GetSize() - 1; i >= 0; --i) { | 318 for (int i = args->GetSize() - 1; i >= 0; --i) { |
| 315 std::string string_value; | 319 std::string string_value; |
| 316 if (!args->GetString(i, &string_value)) { | 320 if (!args->GetString(i, &string_value)) { |
| 317 NOTREACHED(); | 321 CHECK(false); |
| 318 return; | |
| 319 } | 322 } |
| 320 int selected_index; | 323 int selected_index; |
| 321 base::StringToInt(string_value, &selected_index); | 324 base::StringToInt(string_value, &selected_index); |
| 322 if (selected_index < 0 || | 325 if (selected_index < 0 || |
| 323 selected_index >= startup_custom_pages_table_model_->RowCount()) { | 326 selected_index >= startup_custom_pages_table_model_->RowCount()) { |
| 324 NOTREACHED(); | 327 NOTREACHED(); |
| 325 return; | 328 return; |
| 326 } | 329 } |
| 327 startup_custom_pages_table_model_->Remove(selected_index); | 330 startup_custom_pages_table_model_->Remove(selected_index); |
| 328 } | 331 } |
| 329 | 332 |
| 330 SaveStartupPagesPref(); | 333 SaveStartupPagesPref(); |
| 331 } | 334 } |
| 332 | 335 |
| 333 void BrowserOptionsHandler::AddStartupPage(const ListValue* args) { | 336 void BrowserOptionsHandler::AddStartupPage(const ListValue* args) { |
| 334 std::string url_string; | 337 std::string url_string; |
| 335 std::string index_string; | 338 std::string index_string; |
| 336 int index; | 339 int index; |
| 337 if (args->GetSize() != 2 || | 340 if (args->GetSize() != 2 || |
| 338 !args->GetString(0, &url_string) || | 341 !args->GetString(0, &url_string) || |
| 339 !args->GetString(1, &index_string) || | 342 !args->GetString(1, &index_string) || |
| 340 !base::StringToInt(index_string, &index)) { | 343 !base::StringToInt(index_string, &index)) { |
| 341 NOTREACHED(); | 344 CHECK(false); |
| 342 return; | |
| 343 }; | 345 }; |
| 344 | 346 |
| 345 if (index == -1) | 347 if (index == -1) |
| 346 index = startup_custom_pages_table_model_->RowCount(); | 348 index = startup_custom_pages_table_model_->RowCount(); |
| 347 else | 349 else |
| 348 ++index; | 350 ++index; |
| 349 | 351 |
| 350 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); | 352 GURL url = URLFixerUpper::FixupURL(url_string, std::string()); |
| 351 | 353 |
| 352 startup_custom_pages_table_model_->Add(index, url); | 354 startup_custom_pages_table_model_->Add(index, url); |
| 353 SaveStartupPagesPref(); | 355 SaveStartupPagesPref(); |
| 354 } | 356 } |
| 355 | 357 |
| 358 void BrowserOptionsHandler::EditStartupPage(const ListValue* args) { |
| 359 std::string url_string; |
| 360 std::string index_string; |
| 361 int index; |
| 362 if (args->GetSize() != 2 || |
| 363 !args->GetString(0, &index_string) || |
| 364 !base::StringToInt(index_string, &index) || |
| 365 !args->GetString(1, &url_string)) { |
| 366 CHECK(false); |
| 367 }; |
| 368 |
| 369 if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { |
| 370 NOTREACHED(); |
| 371 return; |
| 372 } |
| 373 |
| 374 std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); |
| 375 urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); |
| 376 startup_custom_pages_table_model_->SetURLs(urls); |
| 377 } |
| 378 |
| 356 void BrowserOptionsHandler::SaveStartupPagesPref() { | 379 void BrowserOptionsHandler::SaveStartupPagesPref() { |
| 357 PrefService* prefs = dom_ui_->GetProfile()->GetPrefs(); | 380 PrefService* prefs = dom_ui_->GetProfile()->GetPrefs(); |
| 358 | 381 |
| 359 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); | 382 SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); |
| 360 pref.urls = startup_custom_pages_table_model_->GetURLs(); | 383 pref.urls = startup_custom_pages_table_model_->GetURLs(); |
| 361 | 384 |
| 362 SessionStartupPref::SetStartupPref(prefs, pref); | 385 SessionStartupPref::SetStartupPref(prefs, pref); |
| 363 } | 386 } |
| OLD | NEW |