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/url_data_source.h" | |
17 #include "content/public/browser/web_contents.h" | |
18 #include "content/public/browser/web_ui.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 class SlowUIHTMLSource : public content::URLDataSource { | |
44 public: | |
45 SlowUIHTMLSource(); | |
46 | |
47 // content::URLDataSource implementation. | |
48 virtual std::string GetSource() const OVERRIDE; | |
49 virtual void StartDataRequest( | |
50 const std::string& path, | |
51 int render_process_id, | |
52 int render_view_id, | |
53 const content::URLDataSource::GotDataCallback& callback) OVERRIDE; | |
54 virtual std::string GetMimeType(const std::string&) const OVERRIDE { | |
55 return "text/html"; | |
56 } | |
57 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE { | |
58 return false; | |
59 } | |
60 | |
61 private: | |
62 virtual ~SlowUIHTMLSource() {} | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(SlowUIHTMLSource); | |
65 }; | |
66 | |
67 // 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.
| |
68 class SlowHandler : public WebUIMessageHandler { | |
69 public: | |
70 explicit SlowHandler(Profile* profile); | |
71 virtual ~SlowHandler(); | |
72 | |
73 // WebUIMessageHandler implementation. | |
74 virtual void RegisterMessages() OVERRIDE; | |
75 | |
76 private: | |
77 void UpdatePage(); | |
78 | |
79 // Handlers for JS WebUI messages. | |
80 void HandleDisable(const ListValue* args); | |
81 void HandleEnable(const ListValue* args); | |
82 void LoadComplete(const ListValue* args); | |
83 | |
84 Profile* profile_; | |
85 scoped_ptr<PrefChangeRegistrar> user_pref_registrar_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(SlowHandler); | |
88 }; | |
89 | |
90 // SlowUIHTMLSource ------------------------------------------------------- | |
91 | |
92 SlowUIHTMLSource::SlowUIHTMLSource() { | |
93 } | |
94 | |
95 std::string SlowUIHTMLSource::GetSource() const { | |
96 return chrome::kChromeUISlowHost; | |
97 } | |
98 | |
99 void SlowUIHTMLSource::StartDataRequest( | |
100 const std::string& path, | |
101 int render_process_id, | |
102 int render_view_id, | |
103 const content::URLDataSource::GotDataCallback& callback) { | |
104 DictionaryValue strings; | |
105 strings.SetString("title", l10n_util::GetStringUTF16(IDS_SLOW_TITLE)); | |
106 strings.SetString("slowDisable", l10n_util::GetStringUTF16(IDS_SLOW_DISABLE)); | |
107 strings.SetString("slowEnable", l10n_util::GetStringUTF16(IDS_SLOW_ENABLE)); | |
108 strings.SetString("slowDescription", | |
109 l10n_util::GetStringUTF16(IDS_SLOW_DESCRIPTION)); | |
110 strings.SetString("slowWarning", | |
111 l10n_util::GetStringUTF16(IDS_SLOW_WARNING)); | |
112 | |
113 webui::SetFontAndTextDirection(&strings); | |
114 | |
115 static const base::StringPiece html( | |
116 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_SLOW_HTML)); | |
117 | |
118 std::string full_html = webui::GetI18nTemplateHtml(html, &strings); | |
119 | |
120 callback.Run(base::RefCountedString::TakeString(&full_html)); | |
121 } | |
122 | |
123 // SlowHandler ------------------------------------------------------------ | |
124 | |
125 SlowHandler::SlowHandler(Profile* profile) : profile_(profile) { | |
126 } | |
127 | |
128 SlowHandler::~SlowHandler() { | |
129 } | |
130 | |
131 void SlowHandler::RegisterMessages() { | |
132 web_ui()->RegisterMessageCallback(kJsApiDisableTracing, | |
133 base::Bind(&SlowHandler::HandleDisable, base::Unretained(this))); | |
134 web_ui()->RegisterMessageCallback(kJsApiEnableTracing, | |
135 base::Bind(&SlowHandler::HandleEnable, base::Unretained(this))); | |
136 web_ui()->RegisterMessageCallback(kJsApiLoadComplete, | |
137 base::Bind(&SlowHandler::LoadComplete, base::Unretained(this))); | |
138 | |
139 user_pref_registrar_.reset(new PrefChangeRegistrar); | |
140 user_pref_registrar_->Init(profile_->GetPrefs()); | |
141 user_pref_registrar_->Add(prefs::kPerformanceTracingEnabled, | |
142 base::Bind(&SlowHandler::UpdatePage, | |
143 base::Unretained(this))); | |
144 } | |
145 | |
146 void SlowHandler::HandleDisable(const ListValue* args) { | |
147 PrefService* pref_service = profile_->GetPrefs(); | |
148 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false); | |
149 } | |
150 | |
151 void SlowHandler::HandleEnable(const ListValue* args) { | |
152 PrefService* pref_service = profile_->GetPrefs(); | |
153 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true); | |
154 } | |
155 | |
156 void SlowHandler::LoadComplete(const ListValue* args) { | |
157 UpdatePage(); | |
158 } | |
159 | |
160 void SlowHandler::UpdatePage() { | |
161 PrefService* pref_service = profile_->GetPrefs(); | |
162 bool enabled = pref_service->GetBoolean(prefs::kPerformanceTracingEnabled); | |
163 base::FundamentalValue pref_value(enabled); | |
164 web_ui()->CallJavascriptFunction(kJsApiTracingPrefChanged, pref_value); | |
165 } | |
166 | |
167 // SlowUI ----------------------------------------------------------------- | |
168 | |
169 SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
170 Profile* profile = Profile::FromWebUI(web_ui); | |
171 | |
172 SlowHandler* handler = new SlowHandler(profile); | |
173 web_ui->AddMessageHandler(handler); | |
174 SlowUIHTMLSource* html_source = new SlowUIHTMLSource(); | |
175 | |
176 // Set up the chrome://slow/ source. | |
177 content::URLDataSource::Add(profile, html_source); | |
178 } | |
179 | |
180 } // namespace chromeos | |
181 | |
OLD | NEW |