File: window_contents_as_string_win.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (124 lines) | stat: -rw-r--r-- 3,888 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
// Copyright 2019 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/window_contents_as_string_win.h"

// Needed for <uiautomation.h>
#include <objbase.h>

#include <wrl/client.h>

#include <utility>

#include "base/check.h"
#include "base/win/com_init_util.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"

#include <uiautomation.h>

namespace {

// IDs corresponding to the properties read by CachedElementContentsAsString.
constexpr long kCachedProperties[] = {
    UIA_LocalizedControlTypePropertyId,
    UIA_NamePropertyId,
    UIA_IsInvokePatternAvailablePropertyId,
};

// Returns a string representation of the cached properties of |element|.
std::wstring CachedElementContentsAsString(IUIAutomationElement* element) {
  std::wstring contents;

  base::win::ScopedVariant variant;
  HRESULT result = element->GetCachedPropertyValue(
      UIA_IsInvokePatternAvailablePropertyId, variant.Receive());
  if (SUCCEEDED(result) && variant.type() == VT_BOOL &&
      V_BOOL(variant.ptr()) == VARIANT_TRUE) {
    contents.append(L"[invokable] ");
  }

  base::win::ScopedBstr value;
  result = element->get_CachedLocalizedControlType(value.Receive());
  if (SUCCEEDED(result))
    contents.append(L"type: ").append(value.Get());

  value.Reset();
  result = element->get_CachedName(value.Receive());
  if (SUCCEEDED(result)) {
    if (!contents.empty())
      contents.append(L", ");
    contents.append(L"name: ").append(value.Get());
  }

  return contents;
}

void TreeAsString(IUIAutomationTreeWalker* walker,
                  IUIAutomationCacheRequest* cache_request,
                  IUIAutomationElement* element,
                  const std::wstring& padding,
                  std::wstring* contents) {
  contents->append(padding)
      .append(CachedElementContentsAsString(element))
      .append(L"\n");

  Microsoft::WRL::ComPtr<IUIAutomationElement> child_element;
  HRESULT result = walker->GetFirstChildElementBuildCache(
      element, cache_request, &child_element);
  if (FAILED(result))
    return;
  const std::wstring next_padding = padding + L"  ";
  while (child_element.Get()) {
    TreeAsString(walker, cache_request, child_element.Get(), next_padding,
                 contents);
    Microsoft::WRL::ComPtr<IUIAutomationElement> next_element;
    result = walker->GetNextSiblingElementBuildCache(
        child_element.Get(), cache_request, &next_element);
    if (FAILED(result))
      return;
    child_element = std::move(next_element);
  }
}

}  // namespace

std::wstring WindowContentsAsString(HWND window_handle) {
  DCHECK(window_handle);
  base::win::AssertComInitialized();

  Microsoft::WRL::ComPtr<IUIAutomation> automation;

  HRESULT result =
      ::CoCreateInstance(CLSID_CUIAutomation, nullptr, CLSCTX_INPROC_SERVER,
                         IID_PPV_ARGS(&automation));
  if (FAILED(result))
    return std::wstring();

  Microsoft::WRL::ComPtr<IUIAutomationCacheRequest> cache_request;
  result = automation->CreateCacheRequest(&cache_request);
  if (FAILED(result))
    return std::wstring();
  for (auto property_id : kCachedProperties)
    cache_request->AddProperty(property_id);

  Microsoft::WRL::ComPtr<IUIAutomationElement> window_element;
  result = automation->ElementFromHandleBuildCache(
      window_handle, cache_request.Get(), &window_element);
  if (FAILED(result))
    return std::wstring();

  Microsoft::WRL::ComPtr<IUIAutomationTreeWalker> walker;
  result = automation->get_RawViewWalker(&walker);
  if (FAILED(result))
    return std::wstring();

  std::wstring contents;
  TreeAsString(walker.Get(), cache_request.Get(), window_element.Get(),
               std::wstring(), &contents);
  // Strip the trailing newline.
  if (!contents.empty())
    contents.pop_back();
  return contents;
}