1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #ifndef CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_UIA_WIN_H_
5 #define CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_UIA_WIN_H_
6 
7 #include "content/browser/accessibility/accessibility_tree_formatter_base.h"
8 
9 #include <ole2.h>
10 #include <stdint.h>
11 #include <uiautomation.h>
12 #include <wrl/client.h>
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "base/win/scoped_variant.h"
19 
20 namespace content {
21 
22 class AccessibilityTreeFormatterUia : public AccessibilityTreeFormatterBase {
23  public:
24   AccessibilityTreeFormatterUia();
25   ~AccessibilityTreeFormatterUia() override;
26 
27   static std::unique_ptr<AccessibilityTreeFormatter> CreateUia();
28 
29   static void SetUpCommandLineForTestPass(base::CommandLine* command_line);
30 
31   // AccessibilityTreeFormatterBase:
32   void AddDefaultFilters(
33       std::vector<PropertyFilter>* property_filters) override;
34   base::FilePath::StringType GetExpectedFileSuffix() override;
35   base::FilePath::StringType GetVersionSpecificExpectedFileSuffix() override;
36   std::unique_ptr<base::DictionaryValue> BuildAccessibilityTree(
37       BrowserAccessibility* start) override;
38   std::unique_ptr<base::DictionaryValue> BuildAccessibilityTreeForProcess(
39       base::ProcessId pid) override;
40   std::unique_ptr<base::DictionaryValue> BuildAccessibilityTreeForWindow(
41       gfx::AcceleratedWidget hwnd) override;
42   std::unique_ptr<base::DictionaryValue> BuildAccessibilityTreeForPattern(
43       const base::StringPiece& pattern) override;
44 
45  private:
46   static const long properties_[];
47   static const long patterns_[];
48   static const long pattern_properties_[];
49   void RecursiveBuildAccessibilityTree(IUIAutomationElement* node,
50                                        int root_x,
51                                        int root_y,
52                                        base::DictionaryValue* dict);
53   void BuildCacheRequests();
54   void AddProperties(IUIAutomationElement* node,
55                      int root_x,
56                      int root_y,
57                      base::DictionaryValue* dict);
58   void AddExpandCollapseProperties(IUIAutomationElement* node,
59                                    base::DictionaryValue* dict);
60   void AddGridProperties(IUIAutomationElement* node,
61                          base::DictionaryValue* dict);
62   void AddGridItemProperties(IUIAutomationElement* node,
63                              base::DictionaryValue* dict);
64   void AddRangeValueProperties(IUIAutomationElement* node,
65                                base::DictionaryValue* dict);
66   void AddScrollProperties(IUIAutomationElement* node,
67                            base::DictionaryValue* dict);
68   void AddSelectionProperties(IUIAutomationElement* node,
69                               base::DictionaryValue* dict);
70   void AddSelectionItemProperties(IUIAutomationElement* node,
71                                   base::DictionaryValue* dict);
72   void AddTableProperties(IUIAutomationElement* node,
73                           base::DictionaryValue* dict);
74   void AddToggleProperties(IUIAutomationElement* node,
75                            base::DictionaryValue* dict);
76   void AddValueProperties(IUIAutomationElement* node,
77                           base::DictionaryValue* dict);
78   void AddWindowProperties(IUIAutomationElement* node,
79                            base::DictionaryValue* dict);
80   void WriteProperty(long propertyId,
81                      const base::win::ScopedVariant& var,
82                      int root_x,
83                      int root_y,
84                      base::DictionaryValue* dict);
85   // UIA enums have type I4, print formatted string for these when possible
86   void WriteI4Property(long propertyId, long lval, base::DictionaryValue* dict);
87   void WriteUnknownProperty(long propertyId,
88                             IUnknown* unk,
89                             base::DictionaryValue* dict);
90   void WriteRectangleProperty(long propertyId,
91                               const VARIANT& value,
92                               int root_x,
93                               int root_y,
94                               base::DictionaryValue* dict);
95   void WriteElementArray(long propertyId,
96                          IUIAutomationElementArray* array,
97                          base::DictionaryValue* dict);
98   base::string16 GetNodeName(IUIAutomationElement* node);
99   const std::string GetAllowEmptyString() override;
100   const std::string GetAllowString() override;
101   const std::string GetDenyString() override;
102   const std::string GetDenyNodeString() override;
103   base::string16 ProcessTreeForOutput(
104       const base::DictionaryValue& node,
105       base::DictionaryValue* filtered_result = nullptr) override;
106   void ProcessPropertyForOutput(const std::string& property_name,
107                                 const base::DictionaryValue& dict,
108                                 base::string16& line,
109                                 base::DictionaryValue* filtered_result);
110   void ProcessValueForOutput(const std::string& name,
111                              const base::Value* value,
112                              base::string16& line,
113                              base::DictionaryValue* filtered_result);
114   Microsoft::WRL::ComPtr<IUIAutomation> uia_;
115   Microsoft::WRL::ComPtr<IUIAutomationCacheRequest> element_cache_request_;
116   Microsoft::WRL::ComPtr<IUIAutomationCacheRequest> children_cache_request_;
117 };
118 
119 }  // namespace content
120 
121 #endif  // CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_FORMATTER_UIA_WIN_H_
122