File: always_on_top_window_killer_win.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 (166 lines) | stat: -rw-r--r-- 6,191 bytes parent folder | download | duplicates (7)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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 "chrome/test/base/always_on_top_window_killer_win.h"

#include <Windows.h>

#include <ios>
#include <string>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/win/window_enumerator.h"
#include "chrome/test/base/process_lineage_win.h"
#include "chrome/test/base/save_desktop_snapshot.h"
#include "ui/display/win/screen_win.h"

namespace {

constexpr char kDialogFoundBeforeTest[] =
    "There is an always on top dialog on the desktop. This was most likely "
    "caused by a previous test and may cause this test to fail. Trying to "
    "close it;";

constexpr char kDialogFoundPostTest[] =
    "There is an always on top dialog on the desktop after this test timed "
    "out. This was most likely caused by this test and may cause future tests "
    "to fail, trying to close it;";

constexpr char kWindowFoundBeforeTest[] =
    "There is an always on top window on the desktop. This may have been "
    "caused by a previous test and may cause this test to fail;";

constexpr char kWindowFoundPostTest[] =
    "There is an always on top window on the desktop after this test timed "
    "out. This may have been caused by this test or a previous test and may "
    "cause flakes;";

}  // namespace

void KillAlwaysOnTopWindows(RunType run_type,
                            const base::CommandLine* child_command_line) {
  const base::FilePath output_dir =
      base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
          kSnapshotOutputDir);
  bool saved_snapshot = false;
  if (run_type == RunType::AFTER_TEST_TIMEOUT && !output_dir.empty()) {
    base::FilePath snapshot_file = SaveDesktopSnapshot(output_dir);
    if (!snapshot_file.empty()) {
      saved_snapshot = true;

      std::wostringstream sstream;
      sstream << "Screen snapshot saved to file: \"" << snapshot_file.value()
              << "\" after timeout of test";
      if (child_command_line) {
        sstream << " process with command line: \""
                << child_command_line->GetCommandLineString() << "\".";
      } else {
        sstream << ".";
      }
      LOG(ERROR) << sstream.str();
    }
  }

  base::win::EnumerateChildWindows(
      ::GetDesktopWindow(), base::BindLambdaForTesting([&](HWND hwnd) {
        const bool kContinueIterating = false;

        if (!::IsWindowVisible(hwnd) || ::IsIconic(hwnd) ||
            !base::win::IsTopmostWindow(hwnd)) {
          return kContinueIterating;
        }

        const std::wstring class_name = base::win::GetWindowClass(hwnd);
        if (class_name.empty()) {
          return kContinueIterating;
        }

        // Ignore specific windows owned by the shell.
        if (base::win::IsShellWindow(hwnd)) {
          return kContinueIterating;
        }

        // All other always-on-top windows may be problematic, but in theory
        // tests should not be creating an always on top window that outlives
        // the test. Prepare details of the command line of the test that timed
        // out (if provided), the process owning the window, and the location of
        // a snapshot taken of the screen.
        std::wstring details;
        if (LOG_IS_ON(ERROR)) {
          std::wostringstream sstream;

          if (!base::win::IsSystemDialog(hwnd)) {
            sstream << " window class name: " << class_name << ";";
          }

          if (child_command_line) {
            sstream << " subprocess command line: \""
                    << child_command_line->GetCommandLineString() << "\";";
          }

          // Save a snapshot of the screen if one hasn't already been saved and
          // an output directory was specified.
          base::FilePath snapshot_file;
          if (!saved_snapshot && !output_dir.empty()) {
            snapshot_file = SaveDesktopSnapshot(output_dir);
            if (!snapshot_file.empty()) {
              saved_snapshot = true;
            }
          }

          DWORD process_id = 0;
          GetWindowThreadProcessId(hwnd, &process_id);
          ProcessLineage lineage = ProcessLineage::Create(process_id);
          if (!lineage.IsEmpty()) {
            sstream << " owning process lineage: " << lineage.ToString() << ";";
          }

          if (!snapshot_file.empty()) {
            sstream << " screen snapshot saved to file: \""
                    << snapshot_file.value() << "\";";
          }

          details = sstream.str();
        }

        // System dialogs may be present if a child process triggers an
        // assert(), for example.
        if (base::win::IsSystemDialog(hwnd)) {
          LOG(ERROR) << (run_type == RunType::BEFORE_SHARD
                             ? kDialogFoundBeforeTest
                             : kDialogFoundPostTest)
                     << details;
          // We don't own the dialog, so we can't destroy it. CloseWindow()
          // results in iconifying the window. An alternative may be to focus
          // it, then send return and wait for close. As we reboot machines
          // running interactive ui tests at least every 12 hours we're going
          // with the simple for now.
          CloseWindow(hwnd);
        } else {
          LOG(ERROR) << (run_type == RunType::BEFORE_SHARD
                             ? kWindowFoundBeforeTest
                             : kWindowFoundPostTest)
                     << details;
          // Try to strip the style and iconify the window.
          if (::SetWindowLongPtr(
                  hwnd, GWL_EXSTYLE,
                  ::GetWindowLong(hwnd, GWL_EXSTYLE) & ~WS_EX_TOPMOST)) {
            LOG(ERROR) << "Stripped WS_EX_TOPMOST.";
          } else {
            PLOG(ERROR) << "Failed to strip WS_EX_TOPMOST";
          }
          if (::ShowWindow(hwnd, SW_FORCEMINIMIZE)) {
            LOG(ERROR) << "Minimized window.";
          } else {
            PLOG(ERROR) << "Failed to minimize window";
          }
        }

        return kContinueIterating;
      }));
}