Chromium Code Reviews| Index: chrome/browser/ui/webui/chromeos/slow_ui.cc |
| diff --git a/chrome/browser/ui/webui/chromeos/slow_ui.cc b/chrome/browser/ui/webui/chromeos/slow_ui.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..32a7a43d5363b5e50284aa16f7ba26fe590cfce2 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/chromeos/slow_ui.cc |
| @@ -0,0 +1,181 @@ |
| +// Copyright (c) 2013 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/chromeos/slow_ui.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/prefs/pref_change_registrar.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/url_data_source.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "content/public/browser/web_ui_message_handler.h" |
| +#include "grit/browser_resources.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/webui/jstemplate_builder.h" |
| +#include "ui/webui/web_ui_util.h" |
| + |
| +using content::WebUIMessageHandler; |
| + |
| +namespace { |
| + |
| +// JS API callbacks names. |
| +const char kJsApiDisableTracing[] = "disableTracing"; |
| +const char kJsApiEnableTracing[] = "enableTracing"; |
| +const char kJsApiLoadComplete[] = "loadComplete"; |
| + |
| +// Page JS API function names. |
| +const char kJsApiTracingPrefChanged[] = "options.Slow.tracingPrefChanged"; |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +class SlowUIHTMLSource : public content::URLDataSource { |
| + public: |
| + SlowUIHTMLSource(); |
| + |
| + // content::URLDataSource implementation. |
| + virtual std::string GetSource() const OVERRIDE; |
| + virtual void StartDataRequest( |
| + const std::string& path, |
| + int render_process_id, |
| + int render_view_id, |
| + const content::URLDataSource::GotDataCallback& callback) OVERRIDE; |
| + virtual std::string GetMimeType(const std::string&) const OVERRIDE { |
| + return "text/html"; |
| + } |
| + virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE { |
| + return false; |
| + } |
| + |
| + private: |
| + virtual ~SlowUIHTMLSource() {} |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SlowUIHTMLSource); |
| +}; |
| + |
| +// The handler for Javascript messages related to the "sim-unlock" view. |
|
Dmitry Polukhin
2013/08/08 21:13:16
It looks like it is not sim-unlock anymore :)
Zachary Kuznia
2013/08/08 21:28:57
Done.
|
| +class SlowHandler : public WebUIMessageHandler { |
| + public: |
| + explicit SlowHandler(Profile* profile); |
| + virtual ~SlowHandler(); |
| + |
| + // WebUIMessageHandler implementation. |
| + virtual void RegisterMessages() OVERRIDE; |
| + |
| + private: |
| + void UpdatePage(); |
| + |
| + // Handlers for JS WebUI messages. |
| + void HandleDisable(const ListValue* args); |
| + void HandleEnable(const ListValue* args); |
| + void LoadComplete(const ListValue* args); |
| + |
| + Profile* profile_; |
| + scoped_ptr<PrefChangeRegistrar> user_pref_registrar_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SlowHandler); |
| +}; |
| + |
| +// SlowUIHTMLSource ------------------------------------------------------- |
| + |
| +SlowUIHTMLSource::SlowUIHTMLSource() { |
| +} |
| + |
| +std::string SlowUIHTMLSource::GetSource() const { |
| + return chrome::kChromeUISlowHost; |
| +} |
| + |
| +void SlowUIHTMLSource::StartDataRequest( |
| + const std::string& path, |
| + int render_process_id, |
| + int render_view_id, |
| + const content::URLDataSource::GotDataCallback& callback) { |
| + DictionaryValue strings; |
| + strings.SetString("title", l10n_util::GetStringUTF16(IDS_SLOW_TITLE)); |
| + strings.SetString("slowDisable", l10n_util::GetStringUTF16(IDS_SLOW_DISABLE)); |
| + strings.SetString("slowEnable", l10n_util::GetStringUTF16(IDS_SLOW_ENABLE)); |
| + strings.SetString("slowDescription", |
| + l10n_util::GetStringUTF16(IDS_SLOW_DESCRIPTION)); |
| + strings.SetString("slowWarning", |
| + l10n_util::GetStringUTF16(IDS_SLOW_WARNING)); |
| + |
| + webui::SetFontAndTextDirection(&strings); |
| + |
| + static const base::StringPiece html( |
| + ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_SLOW_HTML)); |
| + |
| + std::string full_html = webui::GetI18nTemplateHtml(html, &strings); |
| + |
| + callback.Run(base::RefCountedString::TakeString(&full_html)); |
| +} |
| + |
| +// SlowHandler ------------------------------------------------------------ |
| + |
| +SlowHandler::SlowHandler(Profile* profile) : profile_(profile) { |
| +} |
| + |
| +SlowHandler::~SlowHandler() { |
| +} |
| + |
| +void SlowHandler::RegisterMessages() { |
| + web_ui()->RegisterMessageCallback(kJsApiDisableTracing, |
| + base::Bind(&SlowHandler::HandleDisable, base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback(kJsApiEnableTracing, |
| + base::Bind(&SlowHandler::HandleEnable, base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback(kJsApiLoadComplete, |
| + base::Bind(&SlowHandler::LoadComplete, base::Unretained(this))); |
| + |
| + user_pref_registrar_.reset(new PrefChangeRegistrar); |
| + user_pref_registrar_->Init(profile_->GetPrefs()); |
| + user_pref_registrar_->Add(prefs::kPerformanceTracingEnabled, |
| + base::Bind(&SlowHandler::UpdatePage, |
| + base::Unretained(this))); |
| +} |
| + |
| +void SlowHandler::HandleDisable(const ListValue* args) { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false); |
| +} |
| + |
| +void SlowHandler::HandleEnable(const ListValue* args) { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true); |
| +} |
| + |
| +void SlowHandler::LoadComplete(const ListValue* args) { |
| + UpdatePage(); |
| +} |
| + |
| +void SlowHandler::UpdatePage() { |
| + PrefService* pref_service = profile_->GetPrefs(); |
| + bool enabled = pref_service->GetBoolean(prefs::kPerformanceTracingEnabled); |
| + base::FundamentalValue pref_value(enabled); |
| + web_ui()->CallJavascriptFunction(kJsApiTracingPrefChanged, pref_value); |
| +} |
| + |
| +// SlowUI ----------------------------------------------------------------- |
| + |
| +SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| + Profile* profile = Profile::FromWebUI(web_ui); |
| + |
| + SlowHandler* handler = new SlowHandler(profile); |
| + web_ui->AddMessageHandler(handler); |
| + SlowUIHTMLSource* html_source = new SlowUIHTMLSource(); |
| + |
| + // Set up the chrome://slow/ source. |
| + content::URLDataSource::Add(profile, html_source); |
| +} |
| + |
| +} // namespace chromeos |
| + |