File: slow_ui.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 4,768 bytes parent folder | download | duplicates (6)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2013 The Chromium Authors
// 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/ash/slow/slow_ui.h"

#include <memory>
#include <string>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.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/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.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 "ui/base/webui/jstemplate_builder.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/webui/webui_util.h"

using content::WebUIMessageHandler;

namespace {

// JS API callbacks names.
const char kJsApiDisableTracing[] = "disableTracing";
const char kJsApiEnableTracing[] = "enableTracing";
const char kJsApiLoadComplete[] = "loadComplete";

}  // namespace

namespace ash {

void CreateAndAddSlowUIHTMLSource(Profile* profile) {
  content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
      profile, chrome::kChromeUISlowHost);

  static constexpr webui::LocalizedString kStrings[] = {
      {"slowDisable", IDS_SLOW_DISABLE},
      {"slowEnable", IDS_SLOW_ENABLE},
      {"slowDescription", IDS_SLOW_DESCRIPTION},
      {"slowWarning", IDS_SLOW_WARNING},
  };
  source->AddLocalizedStrings(kStrings);

  source->AddResourcePath("slow.js", IDR_SLOW_JS);
  source->AddResourcePath("slow.css", IDR_SLOW_CSS);
  source->SetDefaultResource(IDR_SLOW_HTML);
}

// The handler for Javascript messages related to the "slow" view.
class SlowHandler : public WebUIMessageHandler {
 public:
  explicit SlowHandler(Profile* profile);

  SlowHandler(const SlowHandler&) = delete;
  SlowHandler& operator=(const SlowHandler&) = delete;

  ~SlowHandler() override;

  // WebUIMessageHandler implementation.
  void RegisterMessages() override;
  void OnJavascriptAllowed() override;
  void OnJavascriptDisallowed() override;

 private:
  void UpdatePage();

  // Handlers for JS WebUI messages.
  void HandleDisable(const base::Value::List& args);
  void HandleEnable(const base::Value::List& args);
  void LoadComplete(const base::Value::List& args);

  raw_ptr<Profile> profile_;
  std::unique_ptr<PrefChangeRegistrar> user_pref_registrar_;
};

// SlowHandler ------------------------------------------------------------

SlowHandler::SlowHandler(Profile* profile) : profile_(profile) {
  user_pref_registrar_ = std::make_unique<PrefChangeRegistrar>();
  user_pref_registrar_->Init(profile_->GetPrefs());
}

SlowHandler::~SlowHandler() = default;

void SlowHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      kJsApiDisableTracing,
      base::BindRepeating(&SlowHandler::HandleDisable, base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      kJsApiEnableTracing,
      base::BindRepeating(&SlowHandler::HandleEnable, base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      kJsApiLoadComplete,
      base::BindRepeating(&SlowHandler::LoadComplete, base::Unretained(this)));
}

void SlowHandler::OnJavascriptAllowed() {
  user_pref_registrar_->Add(
      prefs::kPerformanceTracingEnabled,
      base::BindRepeating(&SlowHandler::UpdatePage, base::Unretained(this)));
}

void SlowHandler::OnJavascriptDisallowed() {
  user_pref_registrar_->RemoveAll();
}

void SlowHandler::HandleDisable(const base::Value::List& args) {
  PrefService* pref_service = profile_->GetPrefs();
  pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, false);
}

void SlowHandler::HandleEnable(const base::Value::List& args) {
  PrefService* pref_service = profile_->GetPrefs();
  pref_service->SetBoolean(prefs::kPerformanceTracingEnabled, true);
}

void SlowHandler::LoadComplete(const base::Value::List& args) {
  AllowJavascript();
  UpdatePage();
}

void SlowHandler::UpdatePage() {
  PrefService* pref_service = profile_->GetPrefs();
  bool enabled = pref_service->GetBoolean(prefs::kPerformanceTracingEnabled);
  base::Value pref_value(enabled);
  FireWebUIListener("tracing-pref-changed", pref_value);
}

// SlowUI -----------------------------------------------------------------

SlowUI::SlowUI(content::WebUI* web_ui) : WebUIController(web_ui) {
  Profile* profile = Profile::FromWebUI(web_ui);

  web_ui->AddMessageHandler(std::make_unique<SlowHandler>(profile));

  // Set up the chrome://slow/ source.
  CreateAndAddSlowUIHTMLSource(profile);
}

}  // namespace ash