diff options
author | zelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-17 17:09:05 +0000 |
---|---|---|
committer | zelidrag@chromium.org <zelidrag@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-17 17:09:05 +0000 |
commit | 478739e35c9a1992276c0f38f862e378a8de1de7 (patch) | |
tree | 11c1448ea945cb7aec25a2bdea8b115e235b9d0f /chrome/browser/dom_ui | |
parent | fc232b598c601e066f6a17b5c647b1e30667f38e (diff) | |
download | chromium_src-478739e35c9a1992276c0f38f862e378a8de1de7.zip chromium_src-478739e35c9a1992276c0f38f862e378a8de1de7.tar.gz chromium_src-478739e35c9a1992276c0f38f862e378a8de1de7.tar.bz2 |
Basic code structures for rewritten options dialogs as DOM UI. Also includes the initial implementation of System page in ChromeOS options.
BUG=chromium-os:
TEST=none yet, work in progress
Review URL: http://codereview.chromium.org/2781005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50117 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r-- | chrome/browser/dom_ui/core_options_handler.cc | 213 | ||||
-rw-r--r-- | chrome/browser/dom_ui/core_options_handler.h | 53 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_factory.cc | 11 | ||||
-rw-r--r-- | chrome/browser/dom_ui/options_ui.cc | 124 | ||||
-rw-r--r-- | chrome/browser/dom_ui/options_ui.h | 83 |
5 files changed, 481 insertions, 3 deletions
diff --git a/chrome/browser/dom_ui/core_options_handler.cc b/chrome/browser/dom_ui/core_options_handler.cc new file mode 100644 index 0000000..3aa1587 --- /dev/null +++ b/chrome/browser/dom_ui/core_options_handler.cc @@ -0,0 +1,213 @@ +// Copyright (c) 2010 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/dom_ui/core_options_handler.h" + +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "grit/browser_resources.h" +#include "grit/chromium_strings.h" +#include "grit/generated_resources.h" +#include "grit/locale_settings.h" +#include "grit/theme_resources.h" + +CoreOptionsHandler::CoreOptionsHandler() { +} + +void CoreOptionsHandler::GetLocalizedValues( + DictionaryValue* localized_strings) { + DCHECK(localized_strings); + // Main + localized_strings->SetString(L"title", + l10n_util::GetStringF(IDS_OPTIONS_DIALOG_TITLE, + l10n_util::GetString(IDS_PRODUCT_NAME))); + +#if defined(OS_CHROMEOS) + localized_strings->SetString(L"systemPage", + l10n_util::GetString(IDS_OPTIONS_SYSTEM_TAB_LABEL)); + localized_strings->SetString(L"internetPage", + l10n_util::GetString(IDS_OPTIONS_INTERNET_TAB_LABEL)); +#endif + + localized_strings->SetString(L"basicsPage", + l10n_util::GetString(IDS_OPTIONS_GENERAL_TAB_LABEL)); + localized_strings->SetString(L"personalStuffPage", + l10n_util::GetString(IDS_OPTIONS_CONTENT_TAB_LABEL)); + localized_strings->SetString(L"underHoodPage", + l10n_util::GetString(IDS_OPTIONS_ADVANCED_TAB_LABEL)); +} + +void CoreOptionsHandler::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::PREF_CHANGED) + NotifyPrefChanged(Details<std::wstring>(details).ptr()); +} + +void CoreOptionsHandler::RegisterMessages() { + dom_ui_->RegisterMessageCallback("fetchPrefs", + NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); + dom_ui_->RegisterMessageCallback("observePrefs", + NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); + dom_ui_->RegisterMessageCallback("setBooleanPref", + NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); + dom_ui_->RegisterMessageCallback("setIntegerPref", + NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); + dom_ui_->RegisterMessageCallback("setStringPref", + NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); +} + + +void CoreOptionsHandler::HandleFetchPrefs(const Value* value) { + if (!value || !value->IsType(Value::TYPE_LIST)) + return; + + const ListValue* param_values = static_cast<const ListValue*>(value); + + // First param is name of callback function, the second one is the value of + // context that is just passed through - so, there needs to be at least one + // more for the actual preference identifier. + const size_t kMinFetchPrefsParamCount = 3; + if (param_values->GetSize() < kMinFetchPrefsParamCount) + return; + + size_t idx = param_values->GetSize(); + LOG(INFO) << "param_values->GetSize() = " << idx; + // Get callback JS function name. + Value* callback; + if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) + return; + + std::wstring callback_function; + if (!callback->GetAsString(&callback_function)) + return; + + // Get context param (just passthrough value) + Value* context; + if (!param_values->Get(1, &context) || !context) + return; + + // Get the list of name for prefs to build the response dictionary. + DictionaryValue result_value; + Value* list_member; + DCHECK(dom_ui_); + PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); + + for (size_t i = 2; i < param_values->GetSize(); i++) { + if (!param_values->Get(i, &list_member)) + break; + + if (!list_member->IsType(Value::TYPE_STRING)) + continue; + + std::wstring pref_name; + if (!list_member->GetAsString(&pref_name)) + continue; + + const PrefService::Preference* pref = + pref_service->FindPreference(pref_name.c_str()); + result_value.Set(pref_name.c_str(), + pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue()); + } + dom_ui_->CallJavascriptFunction(callback_function.c_str(), *context, + result_value); +} + +void CoreOptionsHandler::HandleObservePefs(const Value* value) { + if (!value || !value->IsType(Value::TYPE_LIST)) + return; + + DCHECK(dom_ui_); + PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); + DictionaryValue result_value; + const ListValue* list_value = static_cast<const ListValue*>(value); + Value* list_member; + for (size_t i = 0; i < list_value->GetSize(); i++) { + if (!list_value->Get(i, &list_member)) + break; + + if (!list_member->IsType(Value::TYPE_STRING)) + continue; + + std::wstring pref_name; + if (!list_member->GetAsString(&pref_name)) + continue; + + pref_service->AddPrefObserver(pref_name.c_str(), this); + } +} + +void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { + HandleSetPref(value, Value::TYPE_BOOLEAN); +} + +void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { + HandleSetPref(value, Value::TYPE_INTEGER); +} + +void CoreOptionsHandler::HandleSetStringPref(const Value* value) { + HandleSetPref(value, Value::TYPE_STRING); +} + +void CoreOptionsHandler::HandleSetPref(const Value* value, + Value::ValueType type) { + if (!value || !value->IsType(Value::TYPE_LIST)) + return; + const ListValue* param_values = static_cast<const ListValue*>(value); + size_t size = param_values->GetSize(); + LOG(INFO) << "Array size = " << size; + if (param_values->GetSize() != 2) + return; + + DCHECK(dom_ui_); + PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); + + Value* name_element; + std::wstring pref_name; + if (!param_values->Get(0, &name_element) || + !name_element->IsType(Value::TYPE_STRING) || + !name_element->GetAsString(&pref_name)) + return; + + Value* value_element; + std::string value_string; + if (!param_values->Get(1, &value_element) || + !value_element->IsType(Value::TYPE_STRING) || + !value_element->GetAsString(&value_string)) + return; + + switch (type) { + case Value::TYPE_BOOLEAN: + pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); + break; + case Value::TYPE_INTEGER: + int int_value; + if (StringToInt(value_string, &int_value)) + pref_service->SetInteger(pref_name.c_str(), int_value); + break; + case Value::TYPE_STRING: + pref_service->SetString(pref_name.c_str(), UTF8ToWide(value_string)); + break; + default: + NOTREACHED(); + } +} + +void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) { + DCHECK(dom_ui_); + PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); + const PrefService::Preference* pref = + pref_service->FindPreference(pref_name->c_str()); + if (pref) { + DictionaryValue result_value; + result_value.Set(pref_name->c_str(), pref->GetValue()->DeepCopy()); + dom_ui_->CallJavascriptFunction(L"prefsChanged", result_value); + } +} diff --git a/chrome/browser/dom_ui/core_options_handler.h b/chrome/browser/dom_ui/core_options_handler.h new file mode 100644 index 0000000..a71489f --- /dev/null +++ b/chrome/browser/dom_ui/core_options_handler.h @@ -0,0 +1,53 @@ +// Copyright (c) 2010 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_DOM_UI_CORE_OPTIONS_HANDLER_H_ +#define CHROME_BROWSER_DOM_UI_CORE_OPTIONS_HANDLER_H_ + +#include "base/values.h" +#include "chrome/browser/dom_ui/options_ui.h" + +// Core options UI handler. +// Handles resource and JS calls common to all options sub-pages. +class CoreOptionsHandler : public OptionsPageUIHandler { + public: + CoreOptionsHandler(); + + // OptionsUIHandler implementation. + virtual void GetLocalizedValues(DictionaryValue* localized_strings); + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // DOMMessageHandler implementation. + virtual void RegisterMessages(); + + private: + // Callback for the "fetchPrefs" message. This message accepts the list of + // preference names passed as |value| parameter (ListValue). It passes results + // dictionary of preference values by calling prefsFetched() JS method on the + // page. + void HandleFetchPrefs(const Value* value); + + // Callback for the "observePrefs" message. This message initiates + // notification observing for given array of preference names. + void HandleObservePefs(const Value* value); + + // Callbacks for the "set<type>Pref" message. This message saves the new + // preference value. The input value is an array of strings representing + // name-value preference pair. + void HandleSetBooleanPref(const Value* value); + void HandleSetIntegerPref(const Value* value); + void HandleSetStringPref(const Value* value); + + void HandleSetPref(const Value* value, Value::ValueType type); + + void NotifyPrefChanged(const std::wstring* pref_name); + + DISALLOW_COPY_AND_ASSIGN(CoreOptionsHandler); +}; + +#endif // CHROME_BROWSER_DOM_UI_CORE_OPTIONS_HANDLER_H_ diff --git a/chrome/browser/dom_ui/dom_ui_factory.cc b/chrome/browser/dom_ui/dom_ui_factory.cc index 0ef066a..4ddfac6 100644 --- a/chrome/browser/dom_ui/dom_ui_factory.cc +++ b/chrome/browser/dom_ui/dom_ui_factory.cc @@ -8,10 +8,8 @@ #include "chrome/browser/dom_ui/bookmarks_ui.h" #include "chrome/browser/dom_ui/downloads_ui.h" #include "chrome/browser/dom_ui/devtools_ui.h" -#include "chrome/browser/dom_ui/filebrowse_ui.h" #include "chrome/browser/dom_ui/history_ui.h" #include "chrome/browser/dom_ui/html_dialog_ui.h" -#include "chrome/browser/dom_ui/mediaplayer_ui.h" #include "chrome/browser/dom_ui/net_internals_ui.h" #include "chrome/browser/dom_ui/new_tab_ui.h" #include "chrome/browser/dom_ui/plugins_ui.h" @@ -24,6 +22,12 @@ #include "chrome/common/url_constants.h" #include "googleurl/src/gurl.h" +#if defined(OS_CHROMEOS) +#include "chrome/browser/dom_ui/filebrowse_ui.h" +#include "chrome/browser/dom_ui/mediaplayer_ui.h" +#include "chrome/browser/dom_ui/options_ui.h" +#endif + const DOMUITypeID DOMUIFactory::kNoDOMUI = NULL; // A function for creating a new DOMUI. The caller owns the return value, which @@ -106,9 +110,10 @@ static DOMUIFactoryFunction GetDOMUIFactoryFunction(const GURL& url) { #if defined(OS_CHROMEOS) if (url.host() == chrome::kChromeUIFileBrowseHost) return &NewDOMUI<FileBrowseUI>; - if (url.host() == chrome::kChromeUIMediaplayerHost) return &NewDOMUI<MediaplayerUI>; + if (url.host() == chrome::kChromeUIOptionsHost) + return &NewDOMUI<OptionsUI>; #endif return NULL; diff --git a/chrome/browser/dom_ui/options_ui.cc b/chrome/browser/dom_ui/options_ui.cc new file mode 100644 index 0000000..652a168 --- /dev/null +++ b/chrome/browser/dom_ui/options_ui.cc @@ -0,0 +1,124 @@ +// Copyright (c) 2010 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/dom_ui/options_ui.h" + +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "base/callback.h" +#include "base/i18n/time_formatting.h" +#include "base/message_loop.h" +#include "base/singleton.h" +#include "base/string_piece.h" +#include "base/string_util.h" +#include "base/thread.h" +#include "base/time.h" +#include "base/values.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/dom_ui/core_options_handler.h" +#include "chrome/browser/metrics/user_metrics.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/browser/tab_contents/tab_contents_delegate.h" +#include "chrome/common/jstemplate_builder.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/time_format.h" +#include "chrome/common/url_constants.h" +#include "net/base/escape.h" + +#include "grit/browser_resources.h" +#include "grit/chromium_strings.h" +#include "grit/generated_resources.h" +#include "grit/locale_settings.h" +#include "grit/theme_resources.h" + +#if defined(OS_CHROMEOS) +#include "chrome/browser/chromeos/dom_ui/system_options_handler.h" +#endif + +//////////////////////////////////////////////////////////////////////////////// +// +// OptionsUIHTMLSource +// +//////////////////////////////////////////////////////////////////////////////// + +OptionsUIHTMLSource::OptionsUIHTMLSource(DictionaryValue* localized_strings) + : DataSource(chrome::kChromeUIOptionsHost, MessageLoop::current()) { + DCHECK(localized_strings); + localized_strings_.reset(localized_strings); +} + +void OptionsUIHTMLSource::StartDataRequest(const std::string& path, + bool is_off_the_record, + int request_id) { + SetFontAndTextDirection(localized_strings_.get()); + + static const base::StringPiece options_html( + ResourceBundle::GetSharedInstance().GetRawDataResource( + IDR_OPTIONS_HTML)); + const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( + options_html, localized_strings_.get()); + + scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); + html_bytes->data.resize(full_html.size()); + std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); + + SendResponse(request_id, html_bytes); +} + +//////////////////////////////////////////////////////////////////////////////// +// +// OptionsUIHandler +// +//////////////////////////////////////////////////////////////////////////////// + +OptionsPageUIHandler::OptionsPageUIHandler() { +} + +OptionsPageUIHandler::~OptionsPageUIHandler() { +} + +void OptionsPageUIHandler::UserMetricsRecordAction( + const UserMetricsAction& action, PrefService* prefs) { + UserMetrics::RecordAction(action, dom_ui_->GetProfile()); + if (prefs) + prefs->ScheduleSavePersistentPrefs(); +} + +//////////////////////////////////////////////////////////////////////////////// +// +// OptionsUIContents +// +//////////////////////////////////////////////////////////////////////////////// + +OptionsUI::OptionsUI(TabContents* contents) : DOMUI(contents) { + DictionaryValue* localized_strings = new DictionaryValue(); + + // TODO(zelidrag): Add all other page handlers here as we implement them. + AddOptionsPageUIHandler(localized_strings, new CoreOptionsHandler()); +#if defined(OS_CHROMEOS) + AddOptionsPageUIHandler(localized_strings, new SystemOptionsHandler()); +#endif + + // |localized_strings| ownership is taken over by this constructor. + OptionsUIHTMLSource* html_source = + new OptionsUIHTMLSource(localized_strings); + + // Set up the chrome://options/ source. + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, + NewRunnableMethod( + Singleton<ChromeURLDataManager>::get(), + &ChromeURLDataManager::AddDataSource, + make_scoped_refptr(html_source))); +} + +void OptionsUI::AddOptionsPageUIHandler(DictionaryValue* localized_strings, + OptionsPageUIHandler* handler) { + DCHECK(handler); + handler->GetLocalizedValues(localized_strings); + AddMessageHandler(handler->Attach(this)); +} diff --git a/chrome/browser/dom_ui/options_ui.h b/chrome/browser/dom_ui/options_ui.h new file mode 100644 index 0000000..a79b3bb --- /dev/null +++ b/chrome/browser/dom_ui/options_ui.h @@ -0,0 +1,83 @@ +// Copyright (c) 2010 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_DOM_UI_OPTIONS_UI_H_ +#define CHROME_BROWSER_DOM_UI_OPTIONS_UI_H_ + +#include <string> +#include <vector> + +#include "base/scoped_ptr.h" +#include "chrome/browser/dom_ui/chrome_url_data_manager.h" +#include "chrome/browser/dom_ui/dom_ui.h" +#include "chrome/common/notification_registrar.h" +#include "chrome/common/notification_type.h" + +class GURL; +class PrefService; +struct UserMetricsAction; + +class OptionsUIHTMLSource : public ChromeURLDataManager::DataSource { + public: + // The constructor takes over ownership of |localized_strings|. + explicit OptionsUIHTMLSource(DictionaryValue* localized_strings); + virtual ~OptionsUIHTMLSource() {} + + // Called when the network layer has requested a resource underneath + // the path we registered. + virtual void StartDataRequest(const std::string& path, + bool is_off_the_record, + int request_id); + virtual std::string GetMimeType(const std::string&) const { + return "text/html"; + } + + private: + // Localized strings collection. + scoped_ptr<DictionaryValue> localized_strings_; + + DISALLOW_COPY_AND_ASSIGN(OptionsUIHTMLSource); +}; + +// The base class handler of Javascript messages of options pages. +class OptionsPageUIHandler : public DOMMessageHandler, + public NotificationObserver { + public: + OptionsPageUIHandler(); + virtual ~OptionsPageUIHandler(); + + // Collects localized strings for options page. + virtual void GetLocalizedValues(DictionaryValue* localized_strings) = 0; + + // DOMMessageHandler implementation. + virtual void RegisterMessages() {} + + // NotificationObserver implementation. + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) {} + + void UserMetricsRecordAction(const UserMetricsAction& action, + PrefService* prefs); + + protected: + NotificationRegistrar registrar_; + + private: + DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler); +}; + +class OptionsUI : public DOMUI { + public: + explicit OptionsUI(TabContents* contents); + virtual ~OptionsUI() {} + + private: + void AddOptionsPageUIHandler(DictionaryValue* localized_strings, + OptionsPageUIHandler* handler); + + DISALLOW_COPY_AND_ASSIGN(OptionsUI); +}; + +#endif // CHROME_BROWSER_DOM_UI_OPTIONS_UI_H_ |