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