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 |
| 53 source->SetJsonPath("strings.js"); |
| 54 source->AddResourcePath("slow.js", IDR_SLOW_JS); |
| 55 source->SetDefaultResource(IDR_SLOW_HTML); |
| 56 return source; |
| 57 } |
| 58 |
| 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(); |
| 64 |
| 65 // WebUIMessageHandler implementation. |
| 66 virtual void RegisterMessages() OVERRIDE; |
| 67 |
| 68 private: |
| 69 void UpdatePage(); |
| 70 |
| 71 // Handlers for JS WebUI messages. |
| 72 void HandleDisable(const ListValue* args); |
| 73 void HandleEnable(const ListValue* args); |
| 74 void LoadComplete(const ListValue* args); |
| 75 |
| 76 Profile* profile_; |
| 77 scoped_ptr<PrefChangeRegistrar> user_pref_registrar_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(SlowHandler); |
| 80 }; |
| 81 |
| 82 // SlowHandler ------------------------------------------------------------ |
| 83 |
| 84 SlowHandler::SlowHandler(Profile* profile) : profile_(profile) { |
| 85 } |
| 86 |
| 87 SlowHandler::~SlowHandler() { |
| 88 } |
| 89 |
| 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))); |
| 97 |
| 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))); |
| 103 } |
| 104 |
| 105 void SlowHandler::HandleDisable(const ListValue* args) { |
| 106 PrefService* pref_service = profile_->GetPrefs(); |
| 107 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false); |
| 108 } |
| 109 |
| 110 void SlowHandler::HandleEnable(const ListValue* args) { |
| 111 PrefService* pref_service = profile_->GetPrefs(); |
| 112 pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true); |
| 113 } |
| 114 |
| 115 void SlowHandler::LoadComplete(const ListValue* args) { |
| 116 UpdatePage(); |
| 117 } |
| 118 |
| 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); |
| 124 } |
| 125 |
| 126 // SlowUI ----------------------------------------------------------------- |
| 127 |
| 128 SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 129 Profile* profile = Profile::FromWebUI(web_ui); |
| 130 |
| 131 SlowHandler* handler = new SlowHandler(profile); |
| 132 web_ui->AddMessageHandler(handler); |
| 133 |
| 134 // Set up the chrome://slow/ source. |
| 135 content::WebUIDataSource::Add(profile, CreateSlowUIHTMLSource()); |
| 136 } |
| 137 |
| 138 } // namespace chromeos |
| 139 |
OLD | NEW |