NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / slow_ui.cc
blob71161db7e4af00066198813d5f2779eb632ad002
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/chromeos/slow_ui.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/values.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_ui.h"
18 #include "content/public/browser/web_ui_data_source.h"
19 #include "content/public/browser/web_ui_message_handler.h"
20 #include "grit/browser_resources.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/base/webui/jstemplate_builder.h"
25 #include "ui/base/webui/web_ui_util.h"
27 using content::WebUIMessageHandler;
29 namespace {
31 // JS API callbacks names.
32 const char kJsApiDisableTracing[] = "disableTracing";
33 const char kJsApiEnableTracing[] = "enableTracing";
34 const char kJsApiLoadComplete[] = "loadComplete";
36 // Page JS API function names.
37 const char kJsApiTracingPrefChanged[] = "options.Slow.tracingPrefChanged";
39 } // namespace
41 namespace chromeos {
43 content::WebUIDataSource* CreateSlowUIHTMLSource() {
44 content::WebUIDataSource* source =
45 content::WebUIDataSource::Create(chrome::kChromeUISlowHost);
47 source->SetUseJsonJSFormatV2();
48 source->AddLocalizedString("slowDisable", IDS_SLOW_DISABLE);
49 source->AddLocalizedString("slowEnable", IDS_SLOW_ENABLE);
50 source->AddLocalizedString("slowDescription", IDS_SLOW_DESCRIPTION);
51 source->AddLocalizedString("slowWarning", IDS_SLOW_WARNING);
53 source->SetJsonPath("strings.js");
54 source->AddResourcePath("slow.js", IDR_SLOW_JS);
55 source->SetDefaultResource(IDR_SLOW_HTML);
56 return source;
59 // The handler for Javascript messages related to the "slow" view.
60 class SlowHandler : public WebUIMessageHandler {
61 public:
62 explicit SlowHandler(Profile* profile);
63 virtual ~SlowHandler();
65 // WebUIMessageHandler implementation.
66 virtual void RegisterMessages() OVERRIDE;
68 private:
69 void UpdatePage();
71 // Handlers for JS WebUI messages.
72 void HandleDisable(const base::ListValue* args);
73 void HandleEnable(const base::ListValue* args);
74 void LoadComplete(const base::ListValue* args);
76 Profile* profile_;
77 scoped_ptr<PrefChangeRegistrar> user_pref_registrar_;
79 DISALLOW_COPY_AND_ASSIGN(SlowHandler);
82 // SlowHandler ------------------------------------------------------------
84 SlowHandler::SlowHandler(Profile* profile) : profile_(profile) {
87 SlowHandler::~SlowHandler() {
90 void SlowHandler::RegisterMessages() {
91 web_ui()->RegisterMessageCallback(kJsApiDisableTracing,
92 base::Bind(&SlowHandler::HandleDisable, base::Unretained(this)));
93 web_ui()->RegisterMessageCallback(kJsApiEnableTracing,
94 base::Bind(&SlowHandler::HandleEnable, base::Unretained(this)));
95 web_ui()->RegisterMessageCallback(kJsApiLoadComplete,
96 base::Bind(&SlowHandler::LoadComplete, base::Unretained(this)));
98 user_pref_registrar_.reset(new PrefChangeRegistrar);
99 user_pref_registrar_->Init(profile_->GetPrefs());
100 user_pref_registrar_->Add(prefs::kPerformanceTracingEnabled,
101 base::Bind(&SlowHandler::UpdatePage,
102 base::Unretained(this)));
105 void SlowHandler::HandleDisable(const base::ListValue* args) {
106 PrefService* pref_service = profile_->GetPrefs();
107 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false);
110 void SlowHandler::HandleEnable(const base::ListValue* args) {
111 PrefService* pref_service = profile_->GetPrefs();
112 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true);
115 void SlowHandler::LoadComplete(const base::ListValue* args) {
116 UpdatePage();
119 void SlowHandler::UpdatePage() {
120 PrefService* pref_service = profile_->GetPrefs();
121 bool enabled = pref_service->GetBoolean(prefs::kPerformanceTracingEnabled);
122 base::FundamentalValue pref_value(enabled);
123 web_ui()->CallJavascriptFunction(kJsApiTracingPrefChanged, pref_value);
126 // SlowUI -----------------------------------------------------------------
128 SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) {
129 Profile* profile = Profile::FromWebUI(web_ui);
131 SlowHandler* handler = new SlowHandler(profile);
132 web_ui->AddMessageHandler(handler);
134 // Set up the chrome://slow/ source.
135 content::WebUIDataSource::Add(profile, CreateSlowUIHTMLSource());
138 } // namespace chromeos