File: screen_info_metrics_provider.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 (107 lines) | stat: -rw-r--r-- 3,261 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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/ui/screen_info_metrics_provider.h"

#include <algorithm>

#include "build/build_config.h"
#include "third_party/metrics_proto/system_profile.pb.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif

namespace metrics {

#if BUILDFLAG(IS_WIN)

namespace {

struct ScreenDPIInformation {
  double max_dpi_x;
  double max_dpi_y;
};

// Called once for each connected monitor.
BOOL CALLBACK GetMonitorDPICallback(HMONITOR, HDC hdc, LPRECT, LPARAM dwData) {
  const double kMillimetersPerInch = 25.4;
  ScreenDPIInformation* screen_info =
      reinterpret_cast<ScreenDPIInformation*>(dwData);
  // Size of screen, in mm.
  DWORD size_x = GetDeviceCaps(hdc, HORZSIZE);
  DWORD size_y = GetDeviceCaps(hdc, VERTSIZE);
  double dpi_x = (size_x > 0) ?
      GetDeviceCaps(hdc, HORZRES) / (size_x / kMillimetersPerInch) : 0;
  double dpi_y = (size_y > 0) ?
      GetDeviceCaps(hdc, VERTRES) / (size_y / kMillimetersPerInch) : 0;
  screen_info->max_dpi_x = std::max(dpi_x, screen_info->max_dpi_x);
  screen_info->max_dpi_y = std::max(dpi_y, screen_info->max_dpi_y);
  return TRUE;
}

void WriteScreenDPIInformationProto(SystemProfileProto::Hardware* hardware) {
  HDC desktop_dc = GetDC(nullptr);
  if (desktop_dc) {
    ScreenDPIInformation si = {0, 0};
    if (EnumDisplayMonitors(desktop_dc, nullptr, GetMonitorDPICallback,
                            reinterpret_cast<LPARAM>(&si))) {
      hardware->set_max_dpi_x(si.max_dpi_x);
      hardware->set_max_dpi_y(si.max_dpi_y);
    }
    ReleaseDC(GetDesktopWindow(), desktop_dc);
  }
}

}  // namespace

#endif  // BUILDFLAG(IS_WIN)

ScreenInfoMetricsProvider::ScreenInfoMetricsProvider() {
}

ScreenInfoMetricsProvider::~ScreenInfoMetricsProvider() {
}

void ScreenInfoMetricsProvider::ProvideSystemProfileMetrics(
    SystemProfileProto* system_profile_proto) {
  // This may be called before the screen info has been initialized, such as
  // when the persistent system profile gets filled in initially.
  const std::optional<gfx::Size> display_size = GetScreenSize();
  if (!display_size.has_value())
    return;

  SystemProfileProto::Hardware* hardware =
      system_profile_proto->mutable_hardware();

  hardware->set_primary_screen_width(display_size->width());
  hardware->set_primary_screen_height(display_size->height());
  hardware->set_primary_screen_scale_factor(GetScreenDeviceScaleFactor());
  hardware->set_screen_count(GetScreenCount());

#if BUILDFLAG(IS_WIN)
  WriteScreenDPIInformationProto(hardware);
#endif
}

std::optional<gfx::Size> ScreenInfoMetricsProvider::GetScreenSize() const {
  auto* screen = display::Screen::GetScreen();
  if (!screen)
    return std::nullopt;
  return std::make_optional(screen->GetPrimaryDisplay().GetSizeInPixel());
}

float ScreenInfoMetricsProvider::GetScreenDeviceScaleFactor() const {
  return display::Screen::GetScreen()
      ->GetPrimaryDisplay()
      .device_scale_factor();
}

int ScreenInfoMetricsProvider::GetScreenCount() const {
  return display::Screen::GetScreen()->GetNumDisplays();
}

}  // namespace metrics