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"
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"
25 using content::WebUIMessageHandler
;
29 // JS API callbacks names.
30 const char kJsApiDisableTracing
[] = "disableTracing";
31 const char kJsApiEnableTracing
[] = "enableTracing";
32 const char kJsApiLoadComplete
[] = "loadComplete";
34 // Page JS API function names.
35 const char kJsApiTracingPrefChanged
[] = "options.Slow.tracingPrefChanged";
41 content::WebUIDataSource
* CreateSlowUIHTMLSource() {
42 content::WebUIDataSource
* source
=
43 content::WebUIDataSource::Create(chrome::kChromeUISlowHost
);
45 source
->AddLocalizedString("slowDisable", IDS_SLOW_DISABLE
);
46 source
->AddLocalizedString("slowEnable", IDS_SLOW_ENABLE
);
47 source
->AddLocalizedString("slowDescription", IDS_SLOW_DESCRIPTION
);
48 source
->AddLocalizedString("slowWarning", IDS_SLOW_WARNING
);
50 source
->SetJsonPath("strings.js");
51 source
->AddResourcePath("slow.js", IDR_SLOW_JS
);
52 source
->SetDefaultResource(IDR_SLOW_HTML
);
56 // The handler for Javascript messages related to the "slow" view.
57 class SlowHandler
: public WebUIMessageHandler
{
59 explicit SlowHandler(Profile
* profile
);
60 ~SlowHandler() override
;
62 // WebUIMessageHandler implementation.
63 void RegisterMessages() override
;
68 // Handlers for JS WebUI messages.
69 void HandleDisable(const base::ListValue
* args
);
70 void HandleEnable(const base::ListValue
* args
);
71 void LoadComplete(const base::ListValue
* args
);
74 scoped_ptr
<PrefChangeRegistrar
> user_pref_registrar_
;
76 DISALLOW_COPY_AND_ASSIGN(SlowHandler
);
79 // SlowHandler ------------------------------------------------------------
81 SlowHandler::SlowHandler(Profile
* profile
) : profile_(profile
) {
84 SlowHandler::~SlowHandler() {
87 void SlowHandler::RegisterMessages() {
88 web_ui()->RegisterMessageCallback(kJsApiDisableTracing
,
89 base::Bind(&SlowHandler::HandleDisable
, base::Unretained(this)));
90 web_ui()->RegisterMessageCallback(kJsApiEnableTracing
,
91 base::Bind(&SlowHandler::HandleEnable
, base::Unretained(this)));
92 web_ui()->RegisterMessageCallback(kJsApiLoadComplete
,
93 base::Bind(&SlowHandler::LoadComplete
, base::Unretained(this)));
95 user_pref_registrar_
.reset(new PrefChangeRegistrar
);
96 user_pref_registrar_
->Init(profile_
->GetPrefs());
97 user_pref_registrar_
->Add(prefs::kPerformanceTracingEnabled
,
98 base::Bind(&SlowHandler::UpdatePage
,
99 base::Unretained(this)));
102 void SlowHandler::HandleDisable(const base::ListValue
* args
) {
103 PrefService
* pref_service
= profile_
->GetPrefs();
104 pref_service
->SetBoolean(prefs::kPerformanceTracingEnabled
, false);
107 void SlowHandler::HandleEnable(const base::ListValue
* args
) {
108 PrefService
* pref_service
= profile_
->GetPrefs();
109 pref_service
->SetBoolean(prefs::kPerformanceTracingEnabled
, true);
112 void SlowHandler::LoadComplete(const base::ListValue
* args
) {
116 void SlowHandler::UpdatePage() {
117 PrefService
* pref_service
= profile_
->GetPrefs();
118 bool enabled
= pref_service
->GetBoolean(prefs::kPerformanceTracingEnabled
);
119 base::FundamentalValue
pref_value(enabled
);
120 web_ui()->CallJavascriptFunction(kJsApiTracingPrefChanged
, pref_value
);
123 // SlowUI -----------------------------------------------------------------
125 SlowUI::SlowUI(content::WebUI
* web_ui
) : WebUIController(web_ui
) {
126 Profile
* profile
= Profile::FromWebUI(web_ui
);
128 SlowHandler
* handler
= new SlowHandler(profile
);
129 web_ui
->AddMessageHandler(handler
);
131 // Set up the chrome://slow/ source.
132 content::WebUIDataSource::Add(profile
, CreateSlowUIHTMLSource());
135 } // namespace chromeos