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.
4
5#include "chrome/browser/ui/webui/chromeos/slow_ui.h"
6
7#include <string>
8
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 "chrome/grit/generated_resources.h"
17#include "content/public/browser/web_contents.h"
18#include "content/public/browser/web_ui.h"
19#include "content/public/browser/web_ui_data_source.h"
20#include "content/public/browser/web_ui_message_handler.h"
21#include "grit/browser_resources.h"
22#include "ui/base/webui/jstemplate_builder.h"
23#include "ui/base/webui/web_ui_util.h"
24
25using content::WebUIMessageHandler;
26
27namespace {
28
29// JS API callbacks names.
30const char kJsApiDisableTracing[] = "disableTracing";
31const char kJsApiEnableTracing[] = "enableTracing";
32const char kJsApiLoadComplete[] = "loadComplete";
33
34// Page JS API function names.
35const char kJsApiTracingPrefChanged[] = "options.Slow.tracingPrefChanged";
36
37}  // namespace
38
39namespace chromeos {
40
41content::WebUIDataSource* CreateSlowUIHTMLSource() {
42  content::WebUIDataSource* source =
43      content::WebUIDataSource::Create(chrome::kChromeUISlowHost);
44
45  source->SetUseJsonJSFormatV2();
46  source->AddLocalizedString("slowDisable", IDS_SLOW_DISABLE);
47  source->AddLocalizedString("slowEnable", IDS_SLOW_ENABLE);
48  source->AddLocalizedString("slowDescription", IDS_SLOW_DESCRIPTION);
49  source->AddLocalizedString("slowWarning", IDS_SLOW_WARNING);
50
51  source->SetJsonPath("strings.js");
52  source->AddResourcePath("slow.js", IDR_SLOW_JS);
53  source->SetDefaultResource(IDR_SLOW_HTML);
54  return source;
55}
56
57// The handler for Javascript messages related to the "slow" view.
58class SlowHandler : public WebUIMessageHandler {
59 public:
60  explicit SlowHandler(Profile* profile);
61  virtual ~SlowHandler();
62
63  // WebUIMessageHandler implementation.
64  virtual void RegisterMessages() OVERRIDE;
65
66 private:
67  void UpdatePage();
68
69  // Handlers for JS WebUI messages.
70  void HandleDisable(const base::ListValue* args);
71  void HandleEnable(const base::ListValue* args);
72  void LoadComplete(const base::ListValue* args);
73
74  Profile* profile_;
75  scoped_ptr<PrefChangeRegistrar> user_pref_registrar_;
76
77  DISALLOW_COPY_AND_ASSIGN(SlowHandler);
78};
79
80// SlowHandler ------------------------------------------------------------
81
82SlowHandler::SlowHandler(Profile* profile) : profile_(profile) {
83}
84
85SlowHandler::~SlowHandler() {
86}
87
88void SlowHandler::RegisterMessages() {
89  web_ui()->RegisterMessageCallback(kJsApiDisableTracing,
90      base::Bind(&SlowHandler::HandleDisable, base::Unretained(this)));
91  web_ui()->RegisterMessageCallback(kJsApiEnableTracing,
92      base::Bind(&SlowHandler::HandleEnable, base::Unretained(this)));
93  web_ui()->RegisterMessageCallback(kJsApiLoadComplete,
94      base::Bind(&SlowHandler::LoadComplete, base::Unretained(this)));
95
96  user_pref_registrar_.reset(new PrefChangeRegistrar);
97  user_pref_registrar_->Init(profile_->GetPrefs());
98  user_pref_registrar_->Add(prefs::kPerformanceTracingEnabled,
99                            base::Bind(&SlowHandler::UpdatePage,
100                                       base::Unretained(this)));
101}
102
103void SlowHandler::HandleDisable(const base::ListValue* args) {
104  PrefService* pref_service = profile_->GetPrefs();
105  pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false);
106}
107
108void SlowHandler::HandleEnable(const base::ListValue* args) {
109  PrefService* pref_service = profile_->GetPrefs();
110  pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true);
111}
112
113void SlowHandler::LoadComplete(const base::ListValue* args) {
114  UpdatePage();
115}
116
117void SlowHandler::UpdatePage() {
118  PrefService* pref_service = profile_->GetPrefs();
119  bool enabled = pref_service->GetBoolean(prefs::kPerformanceTracingEnabled);
120  base::FundamentalValue pref_value(enabled);
121  web_ui()->CallJavascriptFunction(kJsApiTracingPrefChanged, pref_value);
122}
123
124// SlowUI -----------------------------------------------------------------
125
126SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) {
127  Profile* profile = Profile::FromWebUI(web_ui);
128
129  SlowHandler* handler = new SlowHandler(profile);
130  web_ui->AddMessageHandler(handler);
131
132  // Set up the chrome://slow/ source.
133  content::WebUIDataSource::Add(profile, CreateSlowUIHTMLSource());
134}
135
136}  // namespace chromeos
137
138