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