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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/platform/ax_utils_mac.h"
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include "base/apple/scoped_cftyperef.h"
#include "ui/accessibility/ax_range.h"
#include "ui/accessibility/platform/ax_platform_node_base.h"
#include "ui/accessibility/platform/ax_platform_node_cocoa.h"
#include "ui/accessibility/platform/ax_platform_node_delegate.h"
namespace ui {
bool IsAXTextMarker(id object) {
if (object == nil) {
return false;
}
return CFGetTypeID((__bridge CFTypeRef)object) == AXTextMarkerGetTypeID();
}
bool IsAXTextMarkerRange(id object) {
if (object == nil) {
return false;
}
return CFGetTypeID((__bridge CFTypeRef)object) ==
AXTextMarkerRangeGetTypeID();
}
AXPlatformNodeDelegate::AXPosition AXTextMarkerToAXPosition(id text_marker) {
if (!IsAXTextMarker(text_marker)) {
return AXNodePosition::CreateNullPosition();
}
AXTextMarkerRef cf_text_marker = (__bridge AXTextMarkerRef)text_marker;
if (AXTextMarkerGetLength(cf_text_marker) !=
sizeof(AXPlatformNodeDelegate::SerializedPosition)) {
return AXNodePosition::CreateNullPosition();
}
const UInt8* source_buffer = AXTextMarkerGetBytePtr(cf_text_marker);
if (!source_buffer) {
return AXNodePosition::CreateNullPosition();
}
return AXNodePosition::Unserialize(
*reinterpret_cast<const AXPlatformNodeDelegate::SerializedPosition*>(
source_buffer));
}
AXPlatformNodeDelegate::AXRange AXTextMarkerRangeToAXRange(
id text_marker_range) {
if (!IsAXTextMarkerRange(text_marker_range)) {
return AXPlatformNodeDelegate::AXRange();
}
AXTextMarkerRangeRef cf_marker_range =
(__bridge AXTextMarkerRangeRef)text_marker_range;
id start_marker =
CFBridgingRelease(AXTextMarkerRangeCopyStartMarker(cf_marker_range));
id end_marker =
CFBridgingRelease(AXTextMarkerRangeCopyEndMarker(cf_marker_range));
if (!start_marker || !end_marker) {
return AXPlatformNodeDelegate::AXRange();
}
// |AXPlatformNodeDelegate::AXRange| takes ownership of its anchor and focus.
AXPlatformNodeDelegate::AXPosition anchor =
AXTextMarkerToAXPosition(start_marker);
AXPlatformNodeDelegate::AXPosition focus =
AXTextMarkerToAXPosition(end_marker);
return AXPlatformNodeDelegate::AXRange(std::move(anchor), std::move(focus));
}
id AXPositionToAXTextMarker(AXPlatformNodeDelegate::AXPosition position) {
// AXTextMarkerCreate is a system function that makes a copy of the data
// buffer given to it.
AXPlatformNodeDelegate::SerializedPosition serialized = position->Serialize();
return CFBridgingRelease(AXTextMarkerCreate(
kCFAllocatorDefault, reinterpret_cast<const UInt8*>(&serialized),
sizeof(AXPlatformNodeDelegate::SerializedPosition)));
}
id AXRangeToAXTextMarkerRange(AXPlatformNodeDelegate::AXRange range) {
AXPlatformNodeDelegate::SerializedPosition serialized_anchor =
range.anchor()->Serialize();
AXPlatformNodeDelegate::SerializedPosition serialized_focus =
range.focus()->Serialize();
base::apple::ScopedCFTypeRef<AXTextMarkerRef> start_marker(AXTextMarkerCreate(
kCFAllocatorDefault, reinterpret_cast<const UInt8*>(&serialized_anchor),
sizeof(AXPlatformNodeDelegate::SerializedPosition)));
base::apple::ScopedCFTypeRef<AXTextMarkerRef> end_marker(AXTextMarkerCreate(
kCFAllocatorDefault, reinterpret_cast<const UInt8*>(&serialized_focus),
sizeof(AXPlatformNodeDelegate::SerializedPosition)));
return CFBridgingRelease(AXTextMarkerRangeCreate(
kCFAllocatorDefault, start_marker.get(), end_marker.get()));
}
id AXTextMarkerFrom(AXPlatformNodeCocoa* anchor,
int offset,
ax::mojom::TextAffinity affinity) {
AXPlatformNode* anchor_platform_node = anchor.node;
AXPlatformNodeDelegate* anchor_node = anchor_platform_node->GetDelegate();
AXPlatformNodeDelegate::AXPosition position =
anchor_node->CreateTextPositionAt(offset, affinity);
return AXPositionToAXTextMarker(std::move(position));
}
id AXTextMarkerRangeFrom(id start_textmarker, id end_textmarker) {
return CFBridgingRelease(AXTextMarkerRangeCreate(
kCFAllocatorDefault, (__bridge AXTextMarkerRef)start_textmarker,
(__bridge AXTextMarkerRef)end_textmarker));
}
id AXTextMarkerRangeStart(id text_marker_range) {
return CFBridgingRelease(AXTextMarkerRangeCopyStartMarker(
(__bridge AXTextMarkerRangeRef)text_marker_range));
}
id AXTextMarkerRangeEnd(id text_marker_range) {
return CFBridgingRelease(AXTextMarkerRangeCopyEndMarker(
(__bridge AXTextMarkerRangeRef)text_marker_range));
}
} // namespace ui
|