diff options
author | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 21:54:31 +0000 |
---|---|---|
committer | stuartmorgan@chromium.org <stuartmorgan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 21:54:31 +0000 |
commit | 42dc0c200a52e7644f22156c4839ae414ae941d6 (patch) | |
tree | 65fd8abda15df97be83c9c72ea8b40f7009ad534 | |
parent | 5de3c6834b77d8b3eeec7b5d7175a2ab246ea234 (diff) | |
download | chromium_src-42dc0c200a52e7644f22156c4839ae414ae941d6.zip chromium_src-42dc0c200a52e7644f22156c4839ae414ae941d6.tar.gz chromium_src-42dc0c200a52e7644f22156c4839ae414ae941d6.tar.bz2 |
Re-land 53849 (Revert 53857) - Implement most of the startup page controls in DOMUI prefs
Hooks up display of startup pages, removal of pages, and setting the startup pages to the currently open tabs.
Uses a select for now, but will eventually be replaced with a List so the favicons can be displayed.
BUG=48713
TEST=Startup pages can be viewed, removed, and set to current in DOMUI prefs.
Review URL: http://codereview.chromium.org/3046025
TBR=stuartmorgan@chromium.org
Review URL: http://codereview.chromium.org/3058018
TBR=stuartmorgan@chromium.org
Review URL: http://codereview.chromium.org/3032035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54031 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/custom_home_pages_table_model.cc | 5 | ||||
-rw-r--r-- | chrome/browser/dom_ui/browser_options_handler.cc | 105 | ||||
-rw-r--r-- | chrome/browser/dom_ui/browser_options_handler.h | 32 | ||||
-rw-r--r-- | chrome/browser/resources/options/browser_options.html | 4 | ||||
-rw-r--r-- | chrome/browser/resources/options/browser_options.js | 71 |
5 files changed, 198 insertions, 19 deletions
diff --git a/chrome/browser/custom_home_pages_table_model.cc b/chrome/browser/custom_home_pages_table_model.cc index 6095afe..90dd523 100644 --- a/chrome/browser/custom_home_pages_table_model.cc +++ b/chrome/browser/custom_home_pages_table_model.cc @@ -14,6 +14,7 @@ #include "chrome/browser/profile.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/common/pref_names.h" +#include "chrome/common/url_constants.h" #include "gfx/codec/png_codec.h" #include "grit/app_resources.h" #include "grit/generated_resources.h" @@ -83,7 +84,9 @@ void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) { const GURL url = browser->GetTabContentsAt(tab_index)->GetURL(); - if (!url.is_empty()) + if (!url.is_empty() && + !(url.SchemeIs(chrome::kChromeUIScheme) && + url.host() == chrome::kChromeUIOptionsHost)) Add(add_index++, url); } } diff --git a/chrome/browser/dom_ui/browser_options_handler.cc b/chrome/browser/dom_ui/browser_options_handler.cc index 58d002d..fda6802 100644 --- a/chrome/browser/dom_ui/browser_options_handler.cc +++ b/chrome/browser/dom_ui/browser_options_handler.cc @@ -10,13 +10,15 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "chrome/browser/custom_home_pages_table_model.h" #include "chrome/browser/profile.h" +#include "chrome/browser/session_startup_pref.h" #include "chrome/installer/util/browser_distribution.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" BrowserOptionsHandler::BrowserOptionsHandler() - : template_url_model_(NULL) { + : template_url_model_(NULL), startup_custom_pages_table_model_(NULL) { #if !defined(OS_MACOSX) default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this); #endif @@ -75,17 +77,18 @@ void BrowserOptionsHandler::RegisterMessages() { dom_ui_->RegisterMessageCallback( "setDefaultSearchEngine", NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine)); + dom_ui_->RegisterMessageCallback( + "removeStartupPages", + NewCallback(this, &BrowserOptionsHandler::RemoveStartupPages)); + dom_ui_->RegisterMessageCallback( + "setStartupPagesToCurrentPages", + NewCallback(this, &BrowserOptionsHandler::SetStartupPagesToCurrentPages)); } void BrowserOptionsHandler::Initialize() { UpdateDefaultBrowserState(); - - template_url_model_ = dom_ui_->GetProfile()->GetTemplateURLModel(); - if (template_url_model_) { - template_url_model_->Load(); - template_url_model_->AddObserver(this); - OnTemplateURLModelChanged(); - } + UpdateStartupPages(); + UpdateSearchEngines(); } void BrowserOptionsHandler::UpdateDefaultBrowserState() { @@ -211,3 +214,89 @@ void BrowserOptionsHandler::SetDefaultSearchEngine(const Value* value) { template_url_model_->SetDefaultSearchProvider(model_urls[selected_index]); } +void BrowserOptionsHandler::UpdateSearchEngines() { + template_url_model_ = dom_ui_->GetProfile()->GetTemplateURLModel(); + if (template_url_model_) { + template_url_model_->Load(); + template_url_model_->AddObserver(this); + OnTemplateURLModelChanged(); + } +} + +void BrowserOptionsHandler::UpdateStartupPages() { + Profile* profile = dom_ui_->GetProfile(); + startup_custom_pages_table_model_.reset( + new CustomHomePagesTableModel(profile)); + startup_custom_pages_table_model_->SetObserver(this); + + const SessionStartupPref startup_pref = + SessionStartupPref::GetStartupPref(profile->GetPrefs()); + startup_custom_pages_table_model_->SetURLs(startup_pref.urls); +} + +void BrowserOptionsHandler::OnModelChanged() { + // TODO(stuartmorgan): Add support for showing favicons. + ListValue startup_pages; + int page_count = startup_custom_pages_table_model_->RowCount(); + for (int i = 0; i < page_count; ++i) { + DictionaryValue* entry = new DictionaryValue(); + entry->SetString(L"title", + startup_custom_pages_table_model_->GetText(i, 0)); + entry->SetString(L"tooltip", + startup_custom_pages_table_model_->GetTooltip(i)); + startup_pages.Append(entry); + } + + dom_ui_->CallJavascriptFunction(L"BrowserOptions.updateStartupPages", + startup_pages); +} + +void BrowserOptionsHandler::OnItemsChanged(int start, int length) { + OnModelChanged(); +} + +void BrowserOptionsHandler::OnItemsAdded(int start, int length) { + OnModelChanged(); +} + +void BrowserOptionsHandler::OnItemsRemoved(int start, int length) { + OnModelChanged(); +} + +void BrowserOptionsHandler::SetStartupPagesToCurrentPages(const Value* value) { + startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); + SaveStartupPagesPref(); +} + +void BrowserOptionsHandler::RemoveStartupPages(const Value* value) { + if (!value || !value->IsType(Value::TYPE_LIST)) { + NOTREACHED(); + return; + } + const ListValue* param_values = static_cast<const ListValue*>(value); + for (int i = param_values->GetSize() - 1; i >= 0; --i) { + std::string string_value; + if (!param_values->GetString(i, &string_value)) { + NOTREACHED(); + return; + } + int selected_index = StringToInt(string_value); + if (selected_index < 0 || + selected_index >= startup_custom_pages_table_model_->RowCount()) { + NOTREACHED(); + return; + } + startup_custom_pages_table_model_->Remove(selected_index); + } + + SaveStartupPagesPref(); +} + +void BrowserOptionsHandler::SaveStartupPagesPref() { + PrefService* prefs = dom_ui_->GetProfile()->GetPrefs(); + + SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); + pref.urls = startup_custom_pages_table_model_->GetURLs(); + + SessionStartupPref::SetStartupPref(prefs, pref); +} diff --git a/chrome/browser/dom_ui/browser_options_handler.h b/chrome/browser/dom_ui/browser_options_handler.h index 6c37bd5..794b525 100644 --- a/chrome/browser/dom_ui/browser_options_handler.h +++ b/chrome/browser/dom_ui/browser_options_handler.h @@ -6,14 +6,18 @@ #define CHROME_BROWSER_DOM_UI_BROWSER_OPTIONS_HANDLER_H_ #pragma once +#include "app/table_model_observer.h" #include "chrome/browser/dom_ui/options_ui.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/shell_integration.h" +class CustomHomePagesTableModel; + // Chrome browser options page UI handler. class BrowserOptionsHandler : public OptionsPageUIHandler, public ShellIntegration::DefaultBrowserObserver, - public TemplateURLModelObserver { + public TemplateURLModelObserver, + public TableModelObserver { public: BrowserOptionsHandler(); virtual ~BrowserOptionsHandler(); @@ -31,6 +35,12 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, // TemplateURLModelObserver implementation. virtual void OnTemplateURLModelChanged(); + // TableModelObserver implementation. + virtual void OnModelChanged(); + virtual void OnItemsChanged(int start, int length); + virtual void OnItemsAdded(int start, int length); + virtual void OnItemsRemoved(int start, int length); + private: // Makes this the default browser. Called from DOMUI. void BecomeDefaultBrowser(const Value* value); @@ -38,6 +48,12 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, // Sets the search engine at the given index to be default. Called from DOMUI. void SetDefaultSearchEngine(const Value* value); + // Removes the startup page at the given indexes. Called from DOMUI. + void RemoveStartupPages(const Value* value); + + // Sets the startup page set to the current pages. Called from DOMUI. + void SetStartupPagesToCurrentPages(const Value* value); + // Returns the string ID for the given default browser state. int StatusStringIdForState(ShellIntegration::DefaultBrowserState state); @@ -48,10 +64,24 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, // Updates the UI with the given state for the default browser. void SetDefaultBrowserUIString(int status_string_id); + // Loads the current set of custom startup pages and reports it to the DOMUI. + void UpdateStartupPages(); + + // Loads the possible default search engine list and reports it to the DOMUI. + void UpdateSearchEngines(); + + // Writes the current set of startup pages to prefs. + void SaveStartupPagesPref(); + scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_; TemplateURLModel* template_url_model_; // Weak. + // TODO(stuartmorgan): Once there are no other clients of + // CustomHomePagesTableModel, consider changing it to something more like + // TemplateURLModel. + scoped_ptr<CustomHomePagesTableModel> startup_custom_pages_table_model_; + DISALLOW_COPY_AND_ASSIGN(BrowserOptionsHandler); }; diff --git a/chrome/browser/resources/options/browser_options.html b/chrome/browser/resources/options/browser_options.html index abeb46e..7a8ad39 100644 --- a/chrome/browser/resources/options/browser_options.html +++ b/chrome/browser/resources/options/browser_options.html @@ -13,13 +13,13 @@ pref="session.restore_on_startup" value="4"><span i18n-content="startupShowPages"></span></label> <div class="suboption"> - <select id="startupPages" size="3"> + <select id="startupPages" size="3" multiple> </select><br> <button id="startupAddButton" i18n-content="startupAddButton"></button> <button id="startupRemoveButton" disabled i18n-content="startupRemoveButton"></button> - <button id="startupUseCurrentButton" disabled + <button id="startupUseCurrentButton" i18n-content="startupUseCurrent"></button> </div> </div> diff --git a/chrome/browser/resources/options/browser_options.js b/chrome/browser/resources/options/browser_options.js index 834c993..53e0656 100644 --- a/chrome/browser/resources/options/browser_options.js +++ b/chrome/browser/resources/options/browser_options.js @@ -23,15 +23,19 @@ BrowserOptions.prototype = { // Call base class implementation to start preference initialization. OptionsPage.prototype.initializePage.call(this); - // Wire up buttons. + // Wire up controls. + var self = this; + $('startupPages').onchange = function(event) { + self.updateRemoveButtonState_(); + }; $('startupAddButton').onclick = function(event) { OptionsPage.showOverlay('addStartupPageOverlay'); }; $('startupRemoveButton').onclick = function(event) { - // TODO(stuartmorgan): Remove selected element(s). + self.removeSelectedStartupPages_(); }; $('startupUseCurrentButton').onclick = function(event) { - // TODO(stuartmorgan): Add all open tabs (except this one). + chrome.send('setStartupPagesToCurrentPages'); }; $('defaultSearchManageEnginesButton').onclick = function(event) { OptionsPage.showPageByName('searchEngines'); @@ -81,10 +85,10 @@ BrowserOptions.prototype = { updateSearchEngines_: function(engines, defaultValue) { this.clearSearchEngines_(); engineSelect = $('defaultSearchEngine'); - engineCount = engines.length + engineCount = engines.length; var defaultIndex = -1; for (var i = 0; i < engineCount; i++) { - var engine = engines[i] + var engine = engines[i]; var option = new Option(engine['name'], engine['index']); if (defaultValue == option.value) defaultIndex = i; @@ -95,6 +99,55 @@ BrowserOptions.prototype = { }, /** + * Clears the startup page list. + * @private + */ + clearStartupPages_: function() { + $('startupPages').textContent = ''; + }, + + /** + * Updates the startup pages list with the given entries. + * @param {Array} pages List of startup pages. + */ + updateStartupPages_: function(pages) { + // TODO(stuartmorgan): Replace <select> with a DOMUI List. + this.clearStartupPages_(); + pageList = $('startupPages'); + pageCount = pages.length; + for (var i = 0; i < pageCount; i++) { + var page = pages[i]; + var option = new Option(page['title']); + option.title = page['tooltip']; + pageList.appendChild(option); + } + + this.updateRemoveButtonState_(); + }, + + /** + * Sets the enabled state of the startup page Remove button based on + * the current selection in the startup pages list. + */ + updateRemoveButtonState_: function() { + $('startupRemoveButton').disabled = ($('startupPages').selectedIndex == -1); + }, + + /** + * Removes the selected startup pages. + */ + removeSelectedStartupPages_: function() { + var pageSelect = $('startupPages'); + var optionCount = pageSelect.options.length; + var selections = []; + for (var i = 0; i < optionCount; i++) { + if (pageSelect.options[i].selected) + selections.push(String(i)); + } + chrome.send('removeStartupPages', selections); + }, + + /** * Set the default search engine based on the popup selection. */ setDefaultBrowser: function() { @@ -110,8 +163,12 @@ BrowserOptions.prototype = { BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault) { BrowserOptions.getInstance().updateDefaultBrowserState_(statusString, isDefault); -} +}; BrowserOptions.updateSearchEngines = function(engines, defaultValue) { BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue); -} +}; + +BrowserOptions.updateStartupPages = function(pages) { + BrowserOptions.getInstance().updateStartupPages_(pages); +}; |