File: scalable_iph_debug_ui.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (122 lines) | stat: -rw-r--r-- 4,444 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 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/scalable_iph/scalable_iph_debug_ui.h"

#include <sstream>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "base/containers/fixed_flat_set.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/strcat.h"
#include "chromeos/ash/components/scalable_iph/logger.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph_factory.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"

namespace ash {

namespace {

constexpr char kLoggingPath[] = "log.txt";
constexpr char kRecordFiveMinTickEventPath[] = "record-five-min-tick-event";
constexpr char kRecordedFiveMinTickEvent[] = "Recorded ScalableIphFiveMinTick.";

constexpr char kPreTagBegin[] = "<pre>";
constexpr char kPreTagEnd[] = "</pre>";
constexpr char kNewline[] = "\n";

constexpr auto kSupportedPaths = base::MakeFixedFlatSet<std::string_view>(
    {kLoggingPath, kRecordFiveMinTickEventPath});

std::string WrapWithPreTags(const std::string& content) {
  return base::StrCat({kPreTagBegin, kNewline, content, kNewline, kPreTagEnd});
}

bool ShouldHandleRequest(const std::string& path) {
  return kSupportedPaths.contains(path);
}

std::string CollectServiceStartUpDebugLog(
    content::BrowserContext* browser_context) {
  std::ostringstream log;
  log << "Failed to get a Scalable Iph service. Collect debug logs for the "
         "service initialization.\n";

  ScalableIphFactory* scalable_iph_factory = ScalableIphFactory::GetInstance();
  if (!scalable_iph_factory) {
    log << "Failed to obtain ScalableIphFactory instance.\n";
    return log.str();
  }

  log << "Call ScalableIphFactory::GetBrowserContextToUse for debugging.\n";
  scalable_iph::Logger logger;
  content::BrowserContext* result =
      scalable_iph_factory->GetBrowserContextToUseForDebug(browser_context,
                                                           &logger);
  log << "Return value of ScalableIphFactory::GetBrowserContextToUseForDebug: "
         "result == nullptr: "
      << (result == nullptr) << "\n";
  log << "Log from ScalableIphFactory::GetBrowserContextToUseForDebug:\n";
  log << logger.GenerateLog() << "\n";

  return log.str();
}

}  // namespace

bool ScalableIphDebugUIConfig::IsWebUIEnabled(
    content::BrowserContext* browser_context) {
  return ash::features::IsScalableIphDebugEnabled();
}

ScalableIphDebugUI::ScalableIphDebugUI(content::WebUI* web_ui)
    : ui::UntrustedWebUIController(web_ui) {
  content::WebUIDataSource* web_ui_data_source =
      content::WebUIDataSource::CreateAndAdd(
          web_ui->GetWebContents()->GetBrowserContext(),
          scalable_iph::kScalableIphDebugURL);
  web_ui_data_source->SetRequestFilter(
      base::BindRepeating(&ShouldHandleRequest),
      base::BindRepeating(&ScalableIphDebugUI::HandleRequest,
                          weak_ptr_factory_.GetWeakPtr()));
}

ScalableIphDebugUI::~ScalableIphDebugUI() = default;

void ScalableIphDebugUI::HandleRequest(
    const std::string& path,
    content::WebUIDataSource::GotDataCallback callback) {
  CHECK(kSupportedPaths.contains(path));

  content::BrowserContext* browser_context =
      web_ui()->GetWebContents()->GetBrowserContext();
  scalable_iph::ScalableIph* scalable_iph =
      ScalableIphFactory::GetForBrowserContext(browser_context);

  if (!scalable_iph) {
    // `ScalableIph` might not be available even if the feature flag is on, e.g.
    // pre-conditions don't get satisfied, querying a service before its
    // initialization, etc.
    std::move(callback).Run(base::MakeRefCounted<base::RefCountedString>(
        WrapWithPreTags(CollectServiceStartUpDebugLog(browser_context))));
    return;
  }

  if (path == kRecordFiveMinTickEventPath) {
    scalable_iph->RecordEvent(scalable_iph::ScalableIph::Event::kFiveMinTick);
    std::move(callback).Run(base::MakeRefCounted<base::RefCountedString>(
        WrapWithPreTags(kRecordedFiveMinTickEvent)));
    return;
  } else if (path == kLoggingPath) {
    std::move(callback).Run(base::MakeRefCounted<base::RefCountedString>(
        WrapWithPreTags(scalable_iph->GetLogger()->GenerateLog())));
    return;
  }
}

}  // namespace ash