diff options
author | tbreisacher@chromium.org <tbreisacher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-10 02:33:22 +0000 |
---|---|---|
committer | tbreisacher@chromium.org <tbreisacher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-10 02:33:22 +0000 |
commit | ea15a97eb5b397aaaf338f97f33087454b3566bb (patch) | |
tree | def9ce975e4d0a1b5916b37bedf9dd6b842aed1c | |
parent | 52208ebddf2fbdc726798af27da774f89f08e6eb (diff) | |
download | chromium_src-ea15a97eb5b397aaaf338f97f33087454b3566bb.zip chromium_src-ea15a97eb5b397aaaf338f97f33087454b3566bb.tar.gz chromium_src-ea15a97eb5b397aaaf338f97f33087454b3566bb.tar.bz2 |
[uber page] Move startup pages list to a dialog
BUG=107474
TEST=none
Review URL: http://codereview.chromium.org/9124020
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116988 0039d316-1c4b-4281-b951-d872f2087c98
18 files changed, 599 insertions, 294 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index d09e9cd..8252886 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -8042,9 +8042,18 @@ Would you like to start <ph name="CONTROL_PANEL_APPLET_NAME">$1<ex>Add/Remove Pr <message name="IDS_OPTIONS_STARTUP_SHOW_LAST_SESSION" desc="The label of the 'Restore previously open pages' startup option radio button"> Reopen the pages that were open last </message> - <message name="IDS_OPTIONS_STARTUP_SHOW_PAGES" desc="The label of the 'Show these pages:' startup option radio button"> + <message name="IDS_OPTIONS_STARTUP_SHOW_PAGES" desc="The label of the 'Show these pages:' startup option radio button on the old settings page"> Open the following pages: </message> + <message name="IDS_OPTIONS2_STARTUP_SHOW_PAGES" desc="The label of the 'Show these pages:' startup option radio button"> + Open a specific page or set of pages. + </message> + <message name="IDS_OPTIONS2_STARTUP_SET_PAGES" desc="The link that is clicked to choose the set of pages that will be shown at startup"> + Set pages + </message> + <message name="IDS_OPTIONS2_STARTUP_PAGES_DIALOG_TITLE" desc="The title of the dialog where the user chooses the set of pages that will be shown at startup"> + Set startup pages + </message> <message name="IDS_OPTIONS_STARTUP_PAGE_TOOLTIP" desc="A tooltip to display for a page in the list of pages to open on startup"> <ph name="PAGE_TITLE">$1<ex>Google</ex></ph> - <ph name="PAGE_URL">$2<ex>http://www.google.com/</ex></ph> </message> diff --git a/chrome/browser/custom_home_pages_table_model.cc b/chrome/browser/custom_home_pages_table_model.cc index d57c3e7..90616d0 100644 --- a/chrome/browser/custom_home_pages_table_model.cc +++ b/chrome/browser/custom_home_pages_table_model.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -172,10 +172,14 @@ void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { for (int tab_index = 0; tab_index < browser->tab_count(); ++tab_index) { const GURL url = browser->GetWebContentsAt(tab_index)->GetURL(); + // TODO(tbreisacher) remove kChromeUISettingsHost once options is deleted + // and replaced by options2 if (!url.is_empty() && !(url.SchemeIs(chrome::kChromeUIScheme) && - url.host() == chrome::kChromeUISettingsHost)) + (url.host() == chrome::kChromeUISettingsHost || + url.host() == chrome::kChromeUIUberHost))) { Add(add_index++, url); + } } } } diff --git a/chrome/browser/resources/options2/browser_options.html b/chrome/browser/resources/options2/browser_options.html index 6f8731b..0e47002 100644 --- a/chrome/browser/resources/options2/browser_options.html +++ b/chrome/browser/resources/options2/browser_options.html @@ -47,20 +47,14 @@ <input id="startupShowPagesButton" type="radio" name="startup" pref="session.restore_on_startup" value="4" metric="Options_Startup_Custom"> - <span id="startupShowPagesLabel" - i18n-content="startupShowPages"></span> + <span> + <span id="startupShowPagesLabel" + i18n-content="startupShowPages"></span> + <button id="startupSetPages" class="link-button" + i18n-content="startupSetPages"></button> + </span> </label> </div> - <div class="suboption"> - <div id="startupPageManagement" class="settings-list"> - <list id="startupPagesList"></list> - <div> - <button id="startupUseCurrentButton" - i18n-content="startupUseCurrent"></button> - </div> - </div> - <div id="startupPagesListDropmarker"></div> - </div> </div> </section> <section> diff --git a/chrome/browser/resources/options2/browser_options.js b/chrome/browser/resources/options2/browser_options.js index 2491566..dab7645 100644 --- a/chrome/browser/resources/options2/browser_options.js +++ b/chrome/browser/resources/options2/browser_options.js @@ -26,13 +26,9 @@ cr.define('options', function() { syncEnabled: false, syncSetupCompleted: false, - startup_pages_pref_: { - 'name': 'session.urls_to_restore_on_startup', - 'disabled': false - }, - /** - * At autocomplete list that can be attached to a text field during editing. + * An autocomplete list that can be attached to the homepage URL text field + * during editing. * @type {HTMLElement} * @private */ @@ -63,8 +59,8 @@ cr.define('options', function() { }; // Wire up controls. - $('startupUseCurrentButton').onclick = function(event) { - chrome.send('setStartupPagesToCurrentPages'); + $('startupSetPages').onclick = function() { + OptionsPage.navigateToPage('startup'); }; $('defaultSearchManageEnginesButton').onclick = function(event) { OptionsPage.navigateToPage('searchEngines'); @@ -121,23 +117,10 @@ cr.define('options', function() { }; } - var startupPagesList = $('startupPagesList'); - options.browser_options.StartupPageList.decorate(startupPagesList); - startupPagesList.autoExpands = true; - // Check if we are in the guest mode. if (cr.commandLine && cr.commandLine.options['--bwsi']) { // Hide the startup section. $('startupSection').hidden = true; - } else { - // Initialize control enabled states. - Preferences.getInstance().addEventListener('session.restore_on_startup', - this.updateCustomStartupPageControlStates_.bind(this)); - Preferences.getInstance().addEventListener( - this.startup_pages_pref_.name, - this.handleStartupPageListChange_.bind(this)); - - this.updateCustomStartupPageControlStates_(); } var suggestionList = new options.AutocompleteList(); @@ -146,7 +129,6 @@ cr.define('options', function() { this.requestAutocompleteSuggestions_.bind(this); $('main-content').appendChild(suggestionList); this.autocompleteList_ = suggestionList; - startupPagesList.autocompleteList = suggestionList; }, setSyncEnabled_: function(enabled) { @@ -327,20 +309,6 @@ cr.define('options', function() { }, /** - * Updates the startup pages list with the given entries. - * @param {Array} pages List of startup pages. - * @private - */ - updateStartupPages_: function(pages) { - var model = new ArrayDataModel(pages); - // Add a "new page" row. - model.push({ - 'modelIndex': '-1' - }); - $('startupPagesList').dataModel = model; - }, - - /** * Sets the enabled state of the custom startup page list controls * based on the current startup radio button selection. * @private @@ -358,17 +326,6 @@ cr.define('options', function() { }, /** - * Handle change events of the preference - * 'session.urls_to_restore_on_startup'. - * @param {event} preference changed event. - * @private - */ - handleStartupPageListChange_: function(event) { - this.startup_pages_pref_.disabled = event.value['disabled']; - this.updateCustomStartupPageControlStates_(); - }, - - /** * Set the default search engine based on the popup selection. */ setDefaultSearchEngine_: function() { @@ -396,6 +353,10 @@ cr.define('options', function() { * @param {Array} pages List of autocomplete suggestions. * @private */ + // This function is duplicated between here and startup_overlay.js. There is + // also some autocomplete-related duplication in the C++ handler code, + // browser_options_handler2.cc and startup_pages_handler2.cc. + // TODO(tbreisacher): remove the duplication by refactoring updateAutocompleteSuggestions_: function(suggestions) { var list = this.autocompleteList_; // If the trigger for this update was a value being selected from the diff --git a/chrome/browser/resources/options2/browser_options_page.css b/chrome/browser/resources/options2/browser_options_page.css index 21001ce..342854c 100644 --- a/chrome/browser/resources/options2/browser_options_page.css +++ b/chrome/browser/resources/options2/browser_options_page.css @@ -1,41 +1,3 @@ -#startupPageManagement.settings-list > :last-child { - border-top: 1px solid #d9d9d9; - padding: 5px 10px; -} - -#startupPagesList { - min-height: 64px; -} - -#startupPagesList .title { - width: 40%; -} - -#startupPagesList .url { - -webkit-box-flex: 1; - color: #666; -} - -#startupPagesList > * { - max-width: 700px; -} - -#startupPagesListDropmarker { - background-clip: padding-box; - background-color: hsl(214, 91%, 65%); - border-bottom-color: transparent; - border-radius: 0; - border-top-color: transparent; - border: 2px solid hsl(214, 91%, 65%); - box-sizing: border-box; - display: none; - height: 6px; - overflow: hidden; - pointer-events: none; - position: fixed; - z-index: 10; -} - #defaultSearchEngineGroup { display: -webkit-box; -webkit-box-orient: vertical; diff --git a/chrome/browser/resources/options2/options.html b/chrome/browser/resources/options2/options.html index 77c1f07..ea4ec8c 100644 --- a/chrome/browser/resources/options2/options.html +++ b/chrome/browser/resources/options2/options.html @@ -38,6 +38,7 @@ <link rel="stylesheet" href="search_engine_manager.css"> <link rel="stylesheet" href="search_page.css"> <link rel="stylesheet" href="subpages_tab_controls.css"> +<link rel="stylesheet" href="startup_overlay.css"> <link rel="stylesheet" href="../sync_setup_overlay.css"> <if expr="pp_ifdef('chromeos')"> <link rel="stylesheet" href="about_page.css"> @@ -87,6 +88,7 @@ <include src="instant_confirm_overlay.html"> <include src="language_add_language_overlay.html"> <include src="manage_profile_overlay.html"> + <include src="startup_overlay.html"> <include src="../sync_setup_overlay.html"> <if expr="pp_ifdef('chromeos')"> <include src="chromeos/bluetooth_add_device_overlay.html"> diff --git a/chrome/browser/resources/options2/options.js b/chrome/browser/resources/options2/options.js index 8467aa2..a96eb9f 100644 --- a/chrome/browser/resources/options2/options.js +++ b/chrome/browser/resources/options2/options.js @@ -28,6 +28,7 @@ var ManageProfileOverlay = options.ManageProfileOverlay; var ProxyOptions = options.ProxyOptions; var SearchEngineManager = options.SearchEngineManager; var SearchPage = options.SearchPage; +var StartupOverlay = options.StartupOverlay; var SyncSetupOverlay = options.SyncSetupOverlay; var VirtualKeyboardManager = options.VirtualKeyboardManager; @@ -172,6 +173,8 @@ function load() { BrowserOptions.getInstance()); OptionsPage.registerOverlay(ManageProfileOverlay.getInstance(), PersonalOptions.getInstance()); + OptionsPage.registerOverlay(StartupOverlay.getInstance(), + BrowserOptions.getInstance()); if (cr.isChromeOS) { OptionsPage.register(AccountsOptions.getInstance()); diff --git a/chrome/browser/resources/options2/options_bundle.js b/chrome/browser/resources/options2/options_bundle.js index fc600e7..6f1d13e 100644 --- a/chrome/browser/resources/options2/options_bundle.js +++ b/chrome/browser/resources/options2/options_bundle.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // @@ -91,5 +91,6 @@ <include src="search_engine_manager.js"></include> <include src="search_engine_manager_engine_list.js"></include> <include src="search_page.js"></include> +<include src="startup_overlay.js"></include> <include src="../sync_setup_overlay.js"></include> <include src="options.js"></include> diff --git a/chrome/browser/resources/options2/options_page.css b/chrome/browser/resources/options2/options_page.css index dc706b2..6e306f4 100644 --- a/chrome/browser/resources/options2/options_page.css +++ b/chrome/browser/resources/options2/options_page.css @@ -593,7 +593,7 @@ html[os=mac] list[hasElementFocus] { color: grey; } -#main-content list.autocomplete-suggestions { +list.autocomplete-suggestions { background-color: white; border: 1px solid #aaa; border-radius: 2px; diff --git a/chrome/browser/resources/options2/startup_overlay.css b/chrome/browser/resources/options2/startup_overlay.css new file mode 100644 index 0000000..0efb922 --- /dev/null +++ b/chrome/browser/resources/options2/startup_overlay.css @@ -0,0 +1,42 @@ +#startup-overlay { + min-width: 600px; +} + +#startupPageManagement.settings-list > :last-child { + border-top: 1px solid #d9d9d9; + padding: 5px 10px; +} + +#startupPagesList { + min-height: 64px; + min-width: 600px; +} + +#startupPagesList .title { + width: 40%; +} + +#startupPagesList .url { + -webkit-box-flex: 1; + color: #666; +} + +#startupPagesList > * { + max-width: 700px; +} + +#startupPagesListDropmarker { + background-clip: padding-box; + background-color: hsl(214, 91%, 65%); + border-bottom-color: transparent; + border-radius: 0; + border-top-color: transparent; + border: 2px solid hsl(214, 91%, 65%); + box-sizing: border-box; + display: none; + height: 6px; + overflow: hidden; + pointer-events: none; + position: fixed; + z-index: 10; +} diff --git a/chrome/browser/resources/options2/startup_overlay.html b/chrome/browser/resources/options2/startup_overlay.html new file mode 100644 index 0000000..e87efa5 --- /dev/null +++ b/chrome/browser/resources/options2/startup_overlay.html @@ -0,0 +1,12 @@ +<div id="startup-overlay" class="page" hidden> + <div id="startupPageManagement" class="settings-list"> + <h1 i18n-content="startupPagesDialogTitle"></h1> + <list id="startupPagesList"></list> + <div> + <button id="startupUseCurrentButton" + i18n-content="startupUseCurrent"></button> + <button id="startup-overlay-ok" i18n-content="ok"></button> + </div> + </div> + <div id="startupPagesListDropmarker"></div> +</div> diff --git a/chrome/browser/resources/options2/startup_overlay.js b/chrome/browser/resources/options2/startup_overlay.js new file mode 100644 index 0000000..78d7b76 --- /dev/null +++ b/chrome/browser/resources/options2/startup_overlay.js @@ -0,0 +1,143 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +cr.define('options', function() { + var OptionsPage = options.OptionsPage; + var ArrayDataModel = cr.ui.ArrayDataModel; + + const localStrings = new LocalStrings(); + + /** + * StartupOverlay class + * Encapsulated handling of the 'Set Startup pages' overlay page. + * @constructor + * @class + */ + function StartupOverlay() { + OptionsPage.call(this, 'startup', + templateData.startupOverlayTabTitle, + 'startup-overlay'); + }; + + cr.addSingletonGetter(StartupOverlay); + + StartupOverlay.prototype = { + // Inherit from OptionsPage. + __proto__: OptionsPage.prototype, + + startup_pages_pref_: { + 'name': 'session.urls_to_restore_on_startup', + 'disabled': false + }, + + /** + * An autocomplete list that can be attached to a text field during editing. + * @type {HTMLElement} + * @private + */ + autocompleteList_ : null, + + /** + * Initialize the page. + */ + initializePage: function() { + // Call base class implementation to start preference initialization. + OptionsPage.prototype.initializePage.call(this); + + var self = this; + + var startupPagesList = $('startupPagesList'); + options.browser_options.StartupPageList.decorate(startupPagesList); + startupPagesList.autoExpands = true; + + $('startupUseCurrentButton').onclick = function(event) { + chrome.send('setStartupPagesToCurrentPages'); + }; + + $('startup-overlay-ok').onclick = function() { + OptionsPage.closeOverlay(); + } + + // Initialize control enabled states. + Preferences.getInstance().addEventListener( + this.startup_pages_pref_.name, + this.handleStartupPageListChange_.bind(this)); + + var suggestionList = new options.AutocompleteList(); + suggestionList.autoExpands = true; + suggestionList.suggestionUpdateRequestCallback = + this.requestAutocompleteSuggestions_.bind(this); + $('startup-overlay').appendChild(suggestionList); + this.autocompleteList_ = suggestionList; + startupPagesList.autocompleteList = suggestionList; + }, + + /** + * Updates the startup pages list with the given entries. + * @param {Array} pages List of startup pages. + * @private + */ + updateStartupPages_: function(pages) { + var model = new ArrayDataModel(pages); + // Add a "new page" row. + model.push({ + 'modelIndex': '-1' + }); + $('startupPagesList').dataModel = model; + }, + + /** + * Sends an asynchronous request for new autocompletion suggestions for the + * the given query. When new suggestions are available, the C++ handler will + * call updateAutocompleteSuggestions_. + * @param {string} query List of autocomplete suggestions. + * @private + */ + requestAutocompleteSuggestions_: function(query) { + chrome.send('requestAutocompleteSuggestionsForStartupPages', [query]); + }, + + /** + * Updates the autocomplete suggestion list with the given entries. + * @param {Array} pages List of autocomplete suggestions. + * @private + */ + updateAutocompleteSuggestions_: function(suggestions) { + var list = this.autocompleteList_; + // If the trigger for this update was a value being selected from the + // current list, do nothing. + if (list.targetInput && list.selectedItem && + list.selectedItem['url'] == list.targetInput.value) { + console.log('value selected from current list') + return; + } + list.suggestions = suggestions; + }, + + /** + * Handle change events of the preference + * 'session.urls_to_restore_on_startup'. + * @param {event} preference changed event. + * @private + */ + handleStartupPageListChange_: function(event) { + this.startup_pages_pref_.disabled = event.value['disabled']; + }, + }; + + // Forward public APIs to private implementations. + [ + 'updateStartupPages', + 'updateAutocompleteSuggestions', + ].forEach(function(name) { + StartupOverlay[name] = function(value) { + StartupOverlay.getInstance()[name + '_'](value); + }; + }); + + // Export + return { + StartupOverlay: StartupOverlay + }; +}); diff --git a/chrome/browser/ui/webui/options2/browser_options_handler2.cc b/chrome/browser/ui/webui/options2/browser_options_handler2.cc index 814faac..5883f83 100644 --- a/chrome/browser/ui/webui/options2/browser_options_handler2.cc +++ b/chrome/browser/ui/webui/options2/browser_options_handler2.cc @@ -62,7 +62,6 @@ namespace options2 { BrowserOptionsHandler::BrowserOptionsHandler() : template_url_service_(NULL), - startup_custom_pages_table_model_(NULL), ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_file_(this)), ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_for_ui_(this)) { multiprofile_ = ProfileManager::IsMultipleProfilesEnabled(); @@ -100,9 +99,8 @@ void BrowserOptionsHandler::GetLocalizedValues( { "startupShowDefaultAndNewTab", IDS_OPTIONS_STARTUP_SHOW_DEFAULT_AND_NEWTAB}, { "startupShowLastSession", IDS_OPTIONS_STARTUP_SHOW_LAST_SESSION }, - { "startupShowPages", IDS_OPTIONS_STARTUP_SHOW_PAGES }, - { "startupAddLabel", IDS_OPTIONS_STARTUP_ADD_LABEL }, - { "startupUseCurrent", IDS_OPTIONS_STARTUP_USE_CURRENT }, + { "startupShowPages", IDS_OPTIONS2_STARTUP_SHOW_PAGES }, + { "startupSetPages", IDS_OPTIONS2_STARTUP_SET_PAGES }, { "toolbarGroupName", IDS_OPTIONS2_TOOLBAR_GROUP_NAME }, { "toolbarShowHomeButton", IDS_OPTIONS_TOOLBAR_SHOW_HOME_BUTTON }, { "toolbarShowBookmarksBar", IDS_OPTIONS_TOOLBAR_SHOW_BOOKMARKS_BAR }, @@ -158,21 +156,6 @@ void BrowserOptionsHandler::RegisterMessages() { web_ui()->RegisterMessageCallback("setDefaultSearchEngine", base::Bind(&BrowserOptionsHandler::SetDefaultSearchEngine, base::Unretained(this))); - web_ui()->RegisterMessageCallback("removeStartupPages", - base::Bind(&BrowserOptionsHandler::RemoveStartupPages, - base::Unretained(this))); - web_ui()->RegisterMessageCallback("addStartupPage", - base::Bind(&BrowserOptionsHandler::AddStartupPage, - base::Unretained(this))); - web_ui()->RegisterMessageCallback("editStartupPage", - base::Bind(&BrowserOptionsHandler::EditStartupPage, - base::Unretained(this))); - web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages", - base::Bind(&BrowserOptionsHandler::SetStartupPagesToCurrentPages, - base::Unretained(this))); - web_ui()->RegisterMessageCallback("dragDropStartupPage", - base::Bind(&BrowserOptionsHandler::DragDropStartupPage, - base::Unretained(this))); web_ui()->RegisterMessageCallback("requestAutocompleteSuggestions", base::Bind(&BrowserOptionsHandler::RequestAutocompleteSuggestions, base::Unretained(this))); @@ -303,13 +286,7 @@ void BrowserOptionsHandler::Initialize() { this); UpdateDefaultBrowserState(); - startup_custom_pages_table_model_.reset( - new CustomHomePagesTableModel(profile)); - startup_custom_pages_table_model_->SetObserver(this); - UpdateStartupPages(); - pref_change_registrar_.Init(profile->GetPrefs()); - pref_change_registrar_.Add(prefs::kURLsToRestoreOnStartup, this); pref_change_registrar_.Add(prefs::kHomePage, this); UpdateSearchEngines(); @@ -504,13 +481,6 @@ void BrowserOptionsHandler::UpdateSearchEngines() { } } -void BrowserOptionsHandler::UpdateStartupPages() { - Profile* profile = Profile::FromWebUI(web_ui()); - const SessionStartupPref startup_pref = - SessionStartupPref::GetStartupPref(profile->GetPrefs()); - startup_custom_pages_table_model_->SetURLs(startup_pref.urls); -} - void BrowserOptionsHandler::UpdateHomePageLabel() const { Profile* profile = Profile::FromWebUI(web_ui()); PrefService* prefs = profile->GetPrefs(); @@ -522,36 +492,6 @@ void BrowserOptionsHandler::UpdateHomePageLabel() const { *label); } -void BrowserOptionsHandler::OnModelChanged() { - ListValue startup_pages; - int page_count = startup_custom_pages_table_model_->RowCount(); - std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); - for (int i = 0; i < page_count; ++i) { - DictionaryValue* entry = new DictionaryValue(); - entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); - entry->SetString("url", urls[i].spec()); - entry->SetString("tooltip", - startup_custom_pages_table_model_->GetTooltip(i)); - entry->SetString("modelIndex", base::IntToString(i)); - startup_pages.Append(entry); - } - - web_ui()->CallJavascriptFunction("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::Observe( int type, const content::NotificationSource& source, @@ -560,8 +500,6 @@ void BrowserOptionsHandler::Observe( std::string* pref = content::Details<std::string>(details).ptr(); if (*pref == prefs::kDefaultBrowserSettingEnabled) { UpdateDefaultBrowserState(); - } else if (*pref == prefs::kURLsToRestoreOnStartup) { - UpdateStartupPages(); } else if (*pref == prefs::kHomePage) { UpdateHomePageLabel(); } else { @@ -575,96 +513,6 @@ void BrowserOptionsHandler::Observe( } } -void BrowserOptionsHandler::SetStartupPagesToCurrentPages( - const ListValue* args) { - startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); - SaveStartupPagesPref(); -} - -void BrowserOptionsHandler::RemoveStartupPages(const ListValue* args) { - for (int i = args->GetSize() - 1; i >= 0; --i) { - std::string string_value; - CHECK(args->GetString(i, &string_value)); - - int selected_index; - base::StringToInt(string_value, &selected_index); - 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::AddStartupPage(const ListValue* args) { - std::string url_string; - CHECK_EQ(args->GetSize(), 1U); - CHECK(args->GetString(0, &url_string)); - - GURL url = URLFixerUpper::FixupURL(url_string, std::string()); - if (!url.is_valid()) - return; - int index = startup_custom_pages_table_model_->RowCount(); - startup_custom_pages_table_model_->Add(index, url); - SaveStartupPagesPref(); -} - -void BrowserOptionsHandler::EditStartupPage(const ListValue* args) { - std::string url_string; - std::string index_string; - int index; - CHECK_EQ(args->GetSize(), 2U); - CHECK(args->GetString(0, &index_string)); - CHECK(base::StringToInt(index_string, &index)); - CHECK(args->GetString(1, &url_string)); - - if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { - NOTREACHED(); - return; - } - - std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); - urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); - startup_custom_pages_table_model_->SetURLs(urls); - SaveStartupPagesPref(); -} - -void BrowserOptionsHandler::DragDropStartupPage(const ListValue* args) { - CHECK_EQ(args->GetSize(), 2U); - - std::string value; - int to_index; - - CHECK(args->GetString(0, &value)); - base::StringToInt(value, &to_index); - - ListValue* selected; - CHECK(args->GetList(1, &selected)); - - std::vector<int> index_list; - for (size_t i = 0; i < selected->GetSize(); ++i) { - int index; - CHECK(selected->GetString(i, &value)); - base::StringToInt(value, &index); - index_list.push_back(index); - } - - startup_custom_pages_table_model_->MoveURLs(to_index, index_list); - SaveStartupPagesPref(); -} - -void BrowserOptionsHandler::SaveStartupPagesPref() { - PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); - - SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); - pref.urls = startup_custom_pages_table_model_->GetURLs(); - - SessionStartupPref::SetStartupPref(prefs, pref); -} - void BrowserOptionsHandler::RequestAutocompleteSuggestions( const ListValue* args) { string16 input; diff --git a/chrome/browser/ui/webui/options2/browser_options_handler2.h b/chrome/browser/ui/webui/options2/browser_options_handler2.h index c3c0f47..e0ed996 100644 --- a/chrome/browser/ui/webui/options2/browser_options_handler2.h +++ b/chrome/browser/ui/webui/options2/browser_options_handler2.h @@ -29,8 +29,7 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, public ProfileSyncServiceObserver, public AutocompleteControllerDelegate, public ShellIntegration::DefaultWebClientObserver, - public TemplateURLServiceObserver, - public ui::TableModelObserver { + public TemplateURLServiceObserver { public: BrowserOptionsHandler(); virtual ~BrowserOptionsHandler(); @@ -54,12 +53,6 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, // TemplateURLServiceObserver implementation. virtual void OnTemplateURLServiceChanged() OVERRIDE; - // ui::TableModelObserver implementation. - virtual void OnModelChanged() OVERRIDE; - virtual void OnItemsChanged(int start, int length) OVERRIDE; - virtual void OnItemsAdded(int start, int length) OVERRIDE; - virtual void OnItemsRemoved(int start, int length) OVERRIDE; - private: // content::NotificationObserver implementation. virtual void Observe(int type, @@ -72,24 +65,7 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, // Sets the search engine at the given index to be default. Called from WebUI. void SetDefaultSearchEngine(const ListValue* args); - // Removes the startup page at the given indexes. Called from WebUI. - void RemoveStartupPages(const ListValue* args); - - // Adds a startup page with the given URL after the given index. - // Called from WebUI. - void AddStartupPage(const ListValue* args); - - // Changes the startup page at the given index to the given URL. - // Called from WebUI. - void EditStartupPage(const ListValue* args); - - // Sets the startup page set to the current pages. Called from WebUI. - void SetStartupPagesToCurrentPages(const ListValue* args); - - // Writes the current set of startup pages to prefs. Called from WebUI. - void DragDropStartupPage(const ListValue* args); - - // Gets autocomplete suggestions asychronously for the given string. + // Gets autocomplete suggestions asynchronously for the given string. // Called from WebUI. void RequestAutocompleteSuggestions(const ListValue* args); @@ -126,18 +102,12 @@ 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 WebUI. - void UpdateStartupPages(); - // Updates the label of the 'Show Home page'. void UpdateHomePageLabel() const; // Loads the possible default search engine list and reports it to the WebUI. void UpdateSearchEngines(); - // Writes the current set of startup pages to prefs. - void SaveStartupPagesPref(); - // Sends an array of Profile objects to javascript. // Each object is of the form: // profileInfo = { @@ -164,11 +134,6 @@ class BrowserOptionsHandler : public OptionsPageUIHandler, TemplateURLService* template_url_service_; // Weak. - // TODO(stuartmorgan): Once there are no other clients of - // CustomHomePagesTableModel, consider changing it to something more like - // TemplateURLService. - scoped_ptr<CustomHomePagesTableModel> startup_custom_pages_table_model_; - scoped_ptr<AutocompleteController> autocomplete_controller_; // Used to get |weak_ptr_| to self for use on the File thread. diff --git a/chrome/browser/ui/webui/options2/options_ui2.cc b/chrome/browser/ui/webui/options2/options_ui2.cc index 793064b..93afc6c 100644 --- a/chrome/browser/ui/webui/options2/options_ui2.cc +++ b/chrome/browser/ui/webui/options2/options_ui2.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -35,6 +35,7 @@ #include "chrome/browser/ui/webui/options2/password_manager_handler2.h" #include "chrome/browser/ui/webui/options2/personal_options_handler2.h" #include "chrome/browser/ui/webui/options2/search_engine_manager_handler2.h" +#include "chrome/browser/ui/webui/options2/startup_pages_handler2.h" #include "chrome/browser/ui/webui/options2/stop_syncing_handler2.h" #include "chrome/browser/ui/webui/options2/web_intents_settings_handler2.h" #include "chrome/browser/ui/webui/theme_source.h" @@ -229,6 +230,7 @@ OptionsUI::OptionsUI(WebContents* contents) AddOptionsPageUIHandler(localized_strings, new PersonalOptionsHandler()); AddOptionsPageUIHandler(localized_strings, new SearchEngineManagerHandler()); AddOptionsPageUIHandler(localized_strings, new ImportDataHandler()); + AddOptionsPageUIHandler(localized_strings, new StartupPagesHandler()); AddOptionsPageUIHandler(localized_strings, new StopSyncingHandler()); AddOptionsPageUIHandler(localized_strings, new OptionsSyncSetupHandler( g_browser_process->profile_manager())); diff --git a/chrome/browser/ui/webui/options2/startup_pages_handler2.cc b/chrome/browser/ui/webui/options2/startup_pages_handler2.cc new file mode 100644 index 0000000..0bb779d --- /dev/null +++ b/chrome/browser/ui/webui/options2/startup_pages_handler2.cc @@ -0,0 +1,259 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/webui/options2/startup_pages_handler2.h" + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/string_number_conversions.h" +#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" +#include "chrome/browser/autocomplete/autocomplete_match.h" +#include "chrome/browser/autocomplete/autocomplete.h" +#include "chrome/browser/custom_home_pages_table_model.h" +#include "chrome/browser/net/url_fixer_upper.h" +#include "chrome/browser/prefs/session_startup_pref.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/common/chrome_notification_types.h" +#include "chrome/common/pref_names.h" +#include "content/public/browser/notification_details.h" +#include "grit/generated_resources.h" + +namespace options2 { + +StartupPagesHandler::StartupPagesHandler() + : autocomplete_controller_(NULL), + startup_custom_pages_table_model_(NULL) { +} + +StartupPagesHandler::~StartupPagesHandler() { + +} + +void StartupPagesHandler::GetLocalizedValues( + DictionaryValue* localized_strings) { + DCHECK(localized_strings); + + static OptionsStringResource resources[] = { + { "startupAddLabel", IDS_OPTIONS_STARTUP_ADD_LABEL }, + { "startupPagesDialogTitle", IDS_OPTIONS2_STARTUP_PAGES_DIALOG_TITLE }, + { "startupUseCurrent", IDS_OPTIONS_STARTUP_USE_CURRENT }, + }; + + RegisterStrings(localized_strings, resources, arraysize(resources)); + +} + +void StartupPagesHandler::RegisterMessages() { + web_ui()->RegisterMessageCallback("removeStartupPages", + base::Bind(&StartupPagesHandler::RemoveStartupPages, + base::Unretained(this))); + web_ui()->RegisterMessageCallback("addStartupPage", + base::Bind(&StartupPagesHandler::AddStartupPage, + base::Unretained(this))); + web_ui()->RegisterMessageCallback("editStartupPage", + base::Bind(&StartupPagesHandler::EditStartupPage, + base::Unretained(this))); + web_ui()->RegisterMessageCallback("setStartupPagesToCurrentPages", + base::Bind(&StartupPagesHandler::SetStartupPagesToCurrentPages, + base::Unretained(this))); + web_ui()->RegisterMessageCallback("dragDropStartupPage", + base::Bind(&StartupPagesHandler::DragDropStartupPage, + base::Unretained(this))); + web_ui()->RegisterMessageCallback( + "requestAutocompleteSuggestionsForStartupPages", + base::Bind(&StartupPagesHandler::RequestAutocompleteSuggestions, + base::Unretained(this))); +} + +void StartupPagesHandler::UpdateStartupPages() { + Profile* profile = Profile::FromWebUI(web_ui()); + const SessionStartupPref startup_pref = + SessionStartupPref::GetStartupPref(profile->GetPrefs()); + startup_custom_pages_table_model_->SetURLs(startup_pref.urls); +} + +void StartupPagesHandler::Initialize() { + Profile* profile = Profile::FromWebUI(web_ui()); + + startup_custom_pages_table_model_.reset( + new CustomHomePagesTableModel(profile)); + startup_custom_pages_table_model_->SetObserver(this); + UpdateStartupPages(); + + pref_change_registrar_.Init(profile->GetPrefs()); + pref_change_registrar_.Add(prefs::kURLsToRestoreOnStartup, this); + + autocomplete_controller_.reset(new AutocompleteController(profile, this)); +} + +void StartupPagesHandler::OnModelChanged() { + ListValue startup_pages; + int page_count = startup_custom_pages_table_model_->RowCount(); + std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); + for (int i = 0; i < page_count; ++i) { + DictionaryValue* entry = new DictionaryValue(); + entry->SetString("title", startup_custom_pages_table_model_->GetText(i, 0)); + entry->SetString("url", urls[i].spec()); + entry->SetString("tooltip", + startup_custom_pages_table_model_->GetTooltip(i)); + entry->SetString("modelIndex", base::IntToString(i)); + startup_pages.Append(entry); + } + + web_ui()->CallJavascriptFunction("StartupOverlay.updateStartupPages", + startup_pages); +} + +void StartupPagesHandler::OnItemsChanged(int start, int length) { + OnModelChanged(); +} + +void StartupPagesHandler::OnItemsAdded(int start, int length) { + OnModelChanged(); +} + +void StartupPagesHandler::OnItemsRemoved(int start, int length) { + OnModelChanged(); +} + +void StartupPagesHandler::Observe( + int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + if (type == chrome::NOTIFICATION_PREF_CHANGED) { + std::string* pref = content::Details<std::string>(details).ptr(); + if (*pref == prefs::kURLsToRestoreOnStartup) { + UpdateStartupPages(); + } else { + NOTREACHED(); + } + } else { + NOTREACHED(); + } +} + +void StartupPagesHandler::SetStartupPagesToCurrentPages( + const ListValue* args) { + startup_custom_pages_table_model_->SetToCurrentlyOpenPages(); + SaveStartupPagesPref(); +} + +void StartupPagesHandler::RemoveStartupPages(const ListValue* args) { + for (int i = args->GetSize() - 1; i >= 0; --i) { + std::string string_value; + CHECK(args->GetString(i, &string_value)); + + int selected_index; + base::StringToInt(string_value, &selected_index); + if (selected_index < 0 || + selected_index >= startup_custom_pages_table_model_->RowCount()) { + NOTREACHED(); + return; + } + startup_custom_pages_table_model_->Remove(selected_index); + } + + SaveStartupPagesPref(); +} + +void StartupPagesHandler::AddStartupPage(const ListValue* args) { + std::string url_string; + CHECK_EQ(args->GetSize(), 1U); + CHECK(args->GetString(0, &url_string)); + + GURL url = URLFixerUpper::FixupURL(url_string, std::string()); + if (!url.is_valid()) + return; + int index = startup_custom_pages_table_model_->RowCount(); + startup_custom_pages_table_model_->Add(index, url); + SaveStartupPagesPref(); +} + +void StartupPagesHandler::EditStartupPage(const ListValue* args) { + std::string url_string; + std::string index_string; + int index; + CHECK_EQ(args->GetSize(), 2U); + CHECK(args->GetString(0, &index_string)); + CHECK(base::StringToInt(index_string, &index)); + CHECK(args->GetString(1, &url_string)); + + if (index < 0 || index > startup_custom_pages_table_model_->RowCount()) { + NOTREACHED(); + return; + } + + std::vector<GURL> urls = startup_custom_pages_table_model_->GetURLs(); + urls[index] = URLFixerUpper::FixupURL(url_string, std::string()); + startup_custom_pages_table_model_->SetURLs(urls); + SaveStartupPagesPref(); +} + +void StartupPagesHandler::DragDropStartupPage(const ListValue* args) { + CHECK_EQ(args->GetSize(), 2U); + + std::string value; + int to_index; + + CHECK(args->GetString(0, &value)); + base::StringToInt(value, &to_index); + + ListValue* selected; + CHECK(args->GetList(1, &selected)); + + std::vector<int> index_list; + for (size_t i = 0; i < selected->GetSize(); ++i) { + int index; + CHECK(selected->GetString(i, &value)); + base::StringToInt(value, &index); + index_list.push_back(index); + } + + startup_custom_pages_table_model_->MoveURLs(to_index, index_list); + SaveStartupPagesPref(); +} + +void StartupPagesHandler::SaveStartupPagesPref() { + PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); + + SessionStartupPref pref = SessionStartupPref::GetStartupPref(prefs); + pref.urls = startup_custom_pages_table_model_->GetURLs(); + + SessionStartupPref::SetStartupPref(prefs, pref); +} + +void StartupPagesHandler::RequestAutocompleteSuggestions( + const ListValue* args) { + string16 input; + CHECK_EQ(args->GetSize(), 1U); + CHECK(args->GetString(0, &input)); + + autocomplete_controller_->Start(input, string16(), true, false, false, + AutocompleteInput::ALL_MATCHES); +} + +void StartupPagesHandler::OnResultChanged(bool default_match_changed) { + const AutocompleteResult& result = autocomplete_controller_->result(); + ListValue suggestions; + for (size_t i = 0; i < result.size(); ++i) { + const AutocompleteMatch& match = result.match_at(i); + AutocompleteMatch::Type type = match.type; + if (type != AutocompleteMatch::HISTORY_URL && + type != AutocompleteMatch::HISTORY_TITLE && + type != AutocompleteMatch::HISTORY_BODY && + type != AutocompleteMatch::HISTORY_KEYWORD && + type != AutocompleteMatch::NAVSUGGEST) + continue; + DictionaryValue* entry = new DictionaryValue(); + entry->SetString("title", match.description); + entry->SetString("displayURL", match.contents); + entry->SetString("url", match.destination_url.spec()); + suggestions.Append(entry); + } + + web_ui()->CallJavascriptFunction( + "StartupOverlay.updateAutocompleteSuggestions", suggestions); +} + +} // namespace options2 diff --git a/chrome/browser/ui/webui/options2/startup_pages_handler2.h b/chrome/browser/ui/webui/options2/startup_pages_handler2.h new file mode 100644 index 0000000..481bfc7 --- /dev/null +++ b/chrome/browser/ui/webui/options2/startup_pages_handler2.h @@ -0,0 +1,96 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS2_STARTUP_PAGES_HANDLER2_H_ +#define CHROME_BROWSER_UI_WEBUI_OPTIONS2_STARTUP_PAGES_HANDLER2_H_ +#pragma once + +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" +#include "chrome/browser/prefs/pref_change_registrar.h" +#include "chrome/browser/prefs/pref_member.h" +#include "chrome/browser/ui/webui/options2/options_ui2.h" +#include "ui/base/models/table_model_observer.h" + +class AutocompleteController; +class CustomHomePagesTableModel; +class TemplateURLService; + +namespace options2 { + +// Chrome browser options page UI handler. +class StartupPagesHandler : public OptionsPageUIHandler, + public AutocompleteControllerDelegate, + public ui::TableModelObserver { + public: + StartupPagesHandler(); + virtual ~StartupPagesHandler(); + + virtual void Initialize() OVERRIDE; + + // OptionsPageUIHandler implementation. + virtual void GetLocalizedValues(DictionaryValue* localized_strings) OVERRIDE; + virtual void RegisterMessages() OVERRIDE; + + // AutocompleteControllerDelegate implementation. + virtual void OnResultChanged(bool default_match_changed) OVERRIDE; + + // ui::TableModelObserver implementation. + virtual void OnModelChanged() OVERRIDE; + virtual void OnItemsChanged(int start, int length) OVERRIDE; + virtual void OnItemsAdded(int start, int length) OVERRIDE; + virtual void OnItemsRemoved(int start, int length) OVERRIDE; + + private: + // content::NotificationObserver implementation. + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; + + // Removes the startup page at the given indexes. Called from WebUI. + void RemoveStartupPages(const ListValue* args); + + // Adds a startup page with the given URL after the given index. + // Called from WebUI. + void AddStartupPage(const ListValue* args); + + // Changes the startup page at the given index to the given URL. + // Called from WebUI. + void EditStartupPage(const ListValue* args); + + // Sets the startup page set to the current pages. Called from WebUI. + void SetStartupPagesToCurrentPages(const ListValue* args); + + // Writes the current set of startup pages to prefs. Called from WebUI. + void DragDropStartupPage(const ListValue* args); + + // Gets autocomplete suggestions asychronously for the given string. + // Called from WebUI. + void RequestAutocompleteSuggestions(const ListValue* args); + + // Loads the current set of custom startup pages and reports it to the WebUI. + void UpdateStartupPages(); + + // Writes the current set of startup pages to prefs. + void SaveStartupPagesPref(); + + scoped_ptr<AutocompleteController> autocomplete_controller_; + + // Used to observe updates to the preference of the list of URLs to load + // on startup, which can be updated via sync. + PrefChangeRegistrar pref_change_registrar_; + + // TODO(stuartmorgan): Once there are no other clients of + // CustomHomePagesTableModel, consider changing it to something more like + // TemplateURLService. + scoped_ptr<CustomHomePagesTableModel> startup_custom_pages_table_model_; + + DISALLOW_COPY_AND_ASSIGN(StartupPagesHandler); +}; + +} // namespace options2 + +#endif // CHROME_BROWSER_UI_WEBUI_OPTIONS2_STARTUP_PAGES_HANDLER2_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 6f50665..c39c7a3 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -4053,6 +4053,8 @@ 'browser/ui/webui/options2/personal_options_handler2.h', 'browser/ui/webui/options2/search_engine_manager_handler2.cc', 'browser/ui/webui/options2/search_engine_manager_handler2.h', + 'browser/ui/webui/options2/startup_pages_handler2.cc', + 'browser/ui/webui/options2/startup_pages_handler2.h', 'browser/ui/webui/options2/stop_syncing_handler2.cc', 'browser/ui/webui/options2/stop_syncing_handler2.h', 'browser/ui/webui/options2/web_intents_settings_handler2.cc', |