summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/dom_ui/browser_options_handler.cc81
-rw-r--r--chrome/browser/dom_ui/browser_options_handler.h24
-rw-r--r--chrome/browser/resources/options.html1
-rw-r--r--chrome/browser/resources/options/browser_options.html8
-rw-r--r--chrome/browser/resources/options/browser_options.js65
-rw-r--r--chrome/browser/resources/options/browser_options_page.css4
6 files changed, 157 insertions, 26 deletions
diff --git a/chrome/browser/dom_ui/browser_options_handler.cc b/chrome/browser/dom_ui/browser_options_handler.cc
index 7ab1bc1..58d002d 100644
--- a/chrome/browser/dom_ui/browser_options_handler.cc
+++ b/chrome/browser/dom_ui/browser_options_handler.cc
@@ -7,12 +7,16 @@
#include "app/l10n_util.h"
#include "base/basictypes.h"
#include "base/scoped_ptr.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
#include "base/values.h"
+#include "chrome/browser/profile.h"
#include "chrome/installer/util/browser_distribution.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
-BrowserOptionsHandler::BrowserOptionsHandler() {
+BrowserOptionsHandler::BrowserOptionsHandler()
+ : template_url_model_(NULL) {
#if !defined(OS_MACOSX)
default_browser_worker_ = new ShellIntegration::DefaultBrowserWorker(this);
#endif
@@ -21,6 +25,8 @@ BrowserOptionsHandler::BrowserOptionsHandler() {
BrowserOptionsHandler::~BrowserOptionsHandler() {
if (default_browser_worker_.get())
default_browser_worker_->ObserverDestroyed();
+ if (template_url_model_)
+ template_url_model_->RemoveObserver(this);
}
void BrowserOptionsHandler::GetLocalizedValues(
@@ -64,14 +70,25 @@ void BrowserOptionsHandler::GetLocalizedValues(
void BrowserOptionsHandler::RegisterMessages() {
dom_ui_->RegisterMessageCallback(
- "updateDefaultBrowserState",
- NewCallback(this, &BrowserOptionsHandler::UpdateDefaultBrowserState));
- dom_ui_->RegisterMessageCallback(
"becomeDefaultBrowser",
NewCallback(this, &BrowserOptionsHandler::BecomeDefaultBrowser));
+ dom_ui_->RegisterMessageCallback(
+ "setDefaultSearchEngine",
+ NewCallback(this, &BrowserOptionsHandler::SetDefaultSearchEngine));
+}
+
+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();
+ }
}
-void BrowserOptionsHandler::UpdateDefaultBrowserState(const Value* value) {
+void BrowserOptionsHandler::UpdateDefaultBrowserState() {
#if defined(OS_WIN)
// Check for side-by-side first.
if (!BrowserDistribution::GetDistribution()->CanSetAsDefault()) {
@@ -100,7 +117,7 @@ void BrowserOptionsHandler::UpdateDefaultBrowserState(const Value* value) {
void BrowserOptionsHandler::BecomeDefaultBrowser(const Value* value) {
#if defined(OS_MACOSX)
if (ShellIntegration::SetAsDefaultBrowser())
- UpdateDefaultBrowserState(NULL);
+ UpdateDefaultBrowserState();
#else
default_browser_worker_->StartSetAsDefaultBrowser();
// Callback takes care of updating UI.
@@ -140,7 +157,57 @@ void BrowserOptionsHandler::SetDefaultBrowserUIString(int status_string_id) {
status_string_id == IDS_OPTIONS_DEFAULTBROWSER_DEFAULT));
dom_ui_->CallJavascriptFunction(
- L"BrowserOptions.updateDefaultBrowserStateCallback",
+ L"BrowserOptions.updateDefaultBrowserState",
*(status_string.get()), *(is_default.get()));
}
+void BrowserOptionsHandler::OnTemplateURLModelChanged() {
+ if (!template_url_model_ || !template_url_model_->loaded())
+ return;
+
+ const TemplateURL* default_url =
+ template_url_model_->GetDefaultSearchProvider();
+
+ int default_index = 0;
+ ListValue search_engines;
+ std::vector<const TemplateURL*> model_urls =
+ template_url_model_->GetTemplateURLs();
+ for (size_t i = 0; i < model_urls.size(); ++i) {
+ if (!model_urls[i]->ShowInDefaultList())
+ continue;
+
+ DictionaryValue* entry = new DictionaryValue();
+ entry->SetString(L"name", model_urls[i]->short_name());
+ entry->SetInteger(L"index", i);
+ search_engines.Append(entry);
+ if (model_urls[i] == default_url)
+ default_index = i;
+ }
+
+ scoped_ptr<Value> default_value(Value::CreateIntegerValue(default_index));
+
+ dom_ui_->CallJavascriptFunction(L"BrowserOptions.updateSearchEngines",
+ search_engines, *(default_value.get()));
+}
+
+void BrowserOptionsHandler::SetDefaultSearchEngine(const Value* value) {
+ if (!value || !value->IsType(Value::TYPE_LIST)) {
+ NOTREACHED();
+ return;
+ }
+ const ListValue* param_values = static_cast<const ListValue*>(value);
+ std::string string_value;
+ if (param_values->GetSize() != 1 ||
+ !param_values->GetString(0, &string_value)) {
+ NOTREACHED();
+ return;
+ }
+ int selected_index = StringToInt(string_value);
+
+ std::vector<const TemplateURL*> model_urls =
+ template_url_model_->GetTemplateURLs();
+ if (selected_index >= 0 &&
+ selected_index < static_cast<int>(model_urls.size()))
+ template_url_model_->SetDefaultSearchProvider(model_urls[selected_index]);
+}
+
diff --git a/chrome/browser/dom_ui/browser_options_handler.h b/chrome/browser/dom_ui/browser_options_handler.h
index 2ba41da..3fb1a8e 100644
--- a/chrome/browser/dom_ui/browser_options_handler.h
+++ b/chrome/browser/dom_ui/browser_options_handler.h
@@ -6,15 +6,19 @@
#define CHROME_BROWSER_DOM_UI_BROWSER_OPTIONS_HANDLER_H_
#include "chrome/browser/dom_ui/options_ui.h"
+#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/shell_integration.h"
// Chrome browser options page UI handler.
class BrowserOptionsHandler : public OptionsPageUIHandler,
- public ShellIntegration::DefaultBrowserObserver {
+ public ShellIntegration::DefaultBrowserObserver,
+ public TemplateURLModelObserver {
public:
BrowserOptionsHandler();
virtual ~BrowserOptionsHandler();
+ virtual void Initialize();
+
// OptionsUIHandler implementation.
virtual void GetLocalizedValues(DictionaryValue* localized_strings);
virtual void RegisterMessages();
@@ -23,22 +27,30 @@ class BrowserOptionsHandler : public OptionsPageUIHandler,
virtual void SetDefaultBrowserUIState(
ShellIntegration::DefaultBrowserUIState state);
- private:
- // Gets the current default browser state, and asynchronously reports it back
- // to the DOMUI page. Called from DOMUI.
- void UpdateDefaultBrowserState(const Value* value);
+ // TemplateURLModelObserver implementation.
+ virtual void OnTemplateURLModelChanged();
+ private:
// Makes this the default browser. Called from DOMUI.
void BecomeDefaultBrowser(const Value* value);
+ // Sets the search engine at the given index to be default. Called from DOMUI.
+ void SetDefaultSearchEngine(const Value* value);
+
// Returns the string ID for the given default browser state.
int StatusStringIdForState(ShellIntegration::DefaultBrowserState state);
- // Updates the UI with the given state.
+ // Gets the current default browser state, and asynchronously reports it to
+ // the DOMUI page.
+ void UpdateDefaultBrowserState();
+
+ // Updates the UI with the given state for the default browser.
void SetDefaultBrowserUIString(int status_string_id);
scoped_refptr<ShellIntegration::DefaultBrowserWorker> default_browser_worker_;
+ TemplateURLModel* template_url_model_; // Weak.
+
DISALLOW_COPY_AND_ASSIGN(BrowserOptionsHandler);
};
diff --git a/chrome/browser/resources/options.html b/chrome/browser/resources/options.html
index 4044526..811a57a 100644
--- a/chrome/browser/resources/options.html
+++ b/chrome/browser/resources/options.html
@@ -123,6 +123,7 @@ window.onpopstate = function(e) {
<include src="options/sync_options.html">
</div>
</div>
+
<script>
// Decorate the existing elements in the document.
cr.ui.decorate('input[pref][type=checkbox]', PrefCheckbox);
diff --git a/chrome/browser/resources/options/browser_options.html b/chrome/browser/resources/options/browser_options.html
index 6fce008..84b2897 100644
--- a/chrome/browser/resources/options/browser_options.html
+++ b/chrome/browser/resources/options/browser_options.html
@@ -13,7 +13,7 @@
value="ShowPages"><span
i18n-content="startupShowPages"></span></label>
<div class="suboption">
- <select size="3" style="width:100%">
+ <select id="startupPages" size="3">
</select><br>
<button id="startupAddButton" disabled
i18n-content="startupAddButton"></button>
@@ -34,7 +34,7 @@
value="UseURL"><span
i18n-content="homepageUseURL"></span></label><br>
<div class="suboption">
- <input type="url" style="width:100%" disabled>
+ <input id="homepageURL" type="url" disabled>
</div>
<div class="option">
<label><input id="homepageShowButton"
@@ -46,8 +46,8 @@
<section>
<h3 i18n-content="defaultSearchGroupName"></h3>
<div>
- <select disabled>
- <option value="TODO" i18n-content="TODO"></option>
+ <select id="defaultSearchEngine"
+ onchange="BrowserOptions.getInstance().setDefaultBrowser()">
</select>
<button id="defaultSearchManageEnginesButton" disabled
i18n-content="defaultSearchManageEnginesLink"></button>
diff --git a/chrome/browser/resources/options/browser_options.js b/chrome/browser/resources/options/browser_options.js
index 9dfaecd..37064c4 100644
--- a/chrome/browser/resources/options/browser_options.js
+++ b/chrome/browser/resources/options/browser_options.js
@@ -16,14 +16,13 @@ BrowserOptions.prototype = {
// Inherit BrowserOptions from OptionsPage.
__proto__: OptionsPage.prototype,
- // Initialize BrowserOptions page.
+ /**
+ * Initialize BrowserOptions page.
+ */
initializePage: function() {
// Call base class implementation to start preference initialization.
OptionsPage.prototype.initializePage.call(this);
- // Fetch initial state of the default browser pref section.
- chrome.send('updateDefaultBrowserState');
-
// Wire up buttons.
$('startupAddButton').onclick = function(event) {
// TODO(stuartmorgan): Spawn add sub-dialog.
@@ -42,7 +41,11 @@ BrowserOptions.prototype = {
};
},
- // Update the Default Browsers section based on the current state.
+ /**
+ * Update the Default Browsers section based on the current state.
+ * @param {String} statusString Description of the current default state.
+ * @param {Boolean} isDefault Whether or not the browser is currently default.
+ */
updateDefaultBrowserState_: function(statusString, isDefault) {
var label = $('defaultBrowserState');
label.textContent = statusString;
@@ -53,11 +56,55 @@ BrowserOptions.prototype = {
}
$('defaultBrowserUseAsDefaultButton').disabled = isDefault;
- }
+ },
+
+ /**
+ * Clears the search engine popup.
+ * @private
+ */
+ clearSearchEngines_: function() {
+ $('defaultSearchEngine').textContent = '';
+ },
+
+ /**
+ * Updates the search engine popup with the given entries.
+ * @param {Array} engines List of available search engines.
+ * @param {Integer} defaultValue The value of the current default engine.
+ */
+ updateSearchEngines_: function(engines, defaultValue) {
+ this.clearSearchEngines_();
+ engineSelect = $('defaultSearchEngine');
+ engineCount = engines.length
+ var defaultIndex = -1;
+ for (var i = 0; i < engineCount; i++) {
+ var engine = engines[i]
+ var option = new Option(engine['name'], engine['index']);
+ if (defaultValue == option.value)
+ defaultIndex = i;
+ engineSelect.appendChild(option);
+ }
+ if (defaultIndex >= 0)
+ engineSelect.selectedIndex = defaultIndex;
+ },
+
+ /**
+ * Set the default search engine based on the popup selection.
+ */
+ setDefaultBrowser: function() {
+ var engineSelect = $('defaultSearchEngine');
+ var selectedIndex = engineSelect.selectedIndex;
+ if (selectedIndex >= 0) {
+ var selection = engineSelect.options[selectedIndex];
+ chrome.send('setDefaultSearchEngine', [String(selection.value)]);
+ }
+ },
};
-BrowserOptions.updateDefaultBrowserStateCallback = function(statusString,
- isDefault) {
+BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault) {
BrowserOptions.getInstance().updateDefaultBrowserState_(statusString,
isDefault);
-};
+}
+
+BrowserOptions.updateSearchEngines = function(engines, defaultValue) {
+ BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue);
+}
diff --git a/chrome/browser/resources/options/browser_options_page.css b/chrome/browser/resources/options/browser_options_page.css
index 17dfc63..b3d5a39 100644
--- a/chrome/browser/resources/options/browser_options_page.css
+++ b/chrome/browser/resources/options/browser_options_page.css
@@ -1,3 +1,7 @@
+#startupPages, #homepageURL {
+ width: 100%;
+}
+
#defaultBrowserState {
color: #880000;
}