2 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 /* vim: set ts=2 et sw=2 tw=80: */
5 /* This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
7 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "xpcAccessibleMacInterface.h"
11 #include "nsCocoaUtils.h"
12 #include "nsContentUtils.h"
13 #include "nsIObserverService.h"
14 #include "nsISimpleEnumerator.h"
15 #include "nsIXPConnect.h"
16 #include "mozilla/dom/ToJSValue.h"
17 #include "mozilla/Services.h"
19 #include "js/PropertyAndElement.h" // JS_Enumerate, JS_GetElement, JS_GetProperty, JS_GetPropertyById, JS_HasOwnProperty, JS_SetUCProperty
21 #import "mozAccessible.h"
23 using namespace mozilla::a11y;
25 // xpcAccessibleMacNSObjectWrapper
27 NS_IMPL_ISUPPORTS(xpcAccessibleMacNSObjectWrapper, nsIAccessibleMacNSObjectWrapper)
29 xpcAccessibleMacNSObjectWrapper::xpcAccessibleMacNSObjectWrapper(id aNativeObj)
30 : mNativeObject(aNativeObj) {
31 [mNativeObject retain];
34 xpcAccessibleMacNSObjectWrapper::~xpcAccessibleMacNSObjectWrapper() { [mNativeObject release]; }
36 id xpcAccessibleMacNSObjectWrapper::GetNativeObject() { return mNativeObject; }
38 // xpcAccessibleMacInterface
40 NS_IMPL_ISUPPORTS_INHERITED(xpcAccessibleMacInterface, xpcAccessibleMacNSObjectWrapper,
41 nsIAccessibleMacInterface)
43 xpcAccessibleMacInterface::xpcAccessibleMacInterface(Accessible* aObj)
44 : xpcAccessibleMacNSObjectWrapper(GetNativeFromGeckoAccessible(aObj)) {}
47 xpcAccessibleMacInterface::GetAttributeNames(nsTArray<nsString>& aAttributeNames) {
48 NS_OBJC_BEGIN_TRY_BLOCK_RETURN
50 if (!mNativeObject || [mNativeObject isExpired]) {
51 return NS_ERROR_NOT_AVAILABLE;
54 for (NSString* name in [mNativeObject accessibilityAttributeNames]) {
55 nsAutoString attribName;
56 nsCocoaUtils::GetStringForNSString(name, attribName);
57 aAttributeNames.AppendElement(attribName);
62 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
66 xpcAccessibleMacInterface::GetParameterizedAttributeNames(nsTArray<nsString>& aAttributeNames) {
67 NS_OBJC_BEGIN_TRY_BLOCK_RETURN
69 if (!mNativeObject || [mNativeObject isExpired]) {
70 return NS_ERROR_NOT_AVAILABLE;
73 for (NSString* name in [mNativeObject accessibilityParameterizedAttributeNames]) {
74 nsAutoString attribName;
75 nsCocoaUtils::GetStringForNSString(name, attribName);
76 aAttributeNames.AppendElement(attribName);
81 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
85 xpcAccessibleMacInterface::GetActionNames(nsTArray<nsString>& aActionNames) {
86 NS_OBJC_BEGIN_TRY_BLOCK_RETURN
88 if (!mNativeObject || [mNativeObject isExpired]) {
89 return NS_ERROR_NOT_AVAILABLE;
92 for (NSString* name in [mNativeObject accessibilityActionNames]) {
93 nsAutoString actionName;
94 nsCocoaUtils::GetStringForNSString(name, actionName);
95 aActionNames.AppendElement(actionName);
100 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
104 xpcAccessibleMacInterface::PerformAction(const nsAString& aActionName) {
105 NS_OBJC_BEGIN_TRY_BLOCK_RETURN
107 if (!mNativeObject || [mNativeObject isExpired]) {
108 return NS_ERROR_NOT_AVAILABLE;
111 NSString* actionName = nsCocoaUtils::ToNSString(aActionName);
112 [mNativeObject accessibilityPerformAction:actionName];
116 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
120 xpcAccessibleMacInterface::GetAttributeValue(const nsAString& aAttributeName, JSContext* aCx,
121 JS::MutableHandleValue aResult) {
122 NS_OBJC_BEGIN_TRY_BLOCK_RETURN
124 if (!mNativeObject || [mNativeObject isExpired]) {
125 return NS_ERROR_NOT_AVAILABLE;
128 NSString* attribName = nsCocoaUtils::ToNSString(aAttributeName);
129 return NSObjectToJsValue([mNativeObject accessibilityAttributeValue:attribName], aCx, aResult);
131 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE)
135 xpcAccessibleMacInterface::IsAttributeSettable(const nsAString& aAttributeName, bool* aIsSettable) {
136 NS_ENSURE_ARG_POINTER(aIsSettable);
138 NSString* attribName = nsCocoaUtils::ToNSString(aAttributeName);
139 if ([mNativeObject respondsToSelector:@selector(accessibilityIsAttributeSettable:)]) {
140 *aIsSettable = [mNativeObject accessibilityIsAttributeSettable:attribName];
144 return NS_ERROR_NOT_IMPLEMENTED;
148 xpcAccessibleMacInterface::SetAttributeValue(const nsAString& aAttributeName,
149 JS::HandleValue aAttributeValue, JSContext* aCx) {
151 id obj = JsValueToNSObject(aAttributeValue, aCx, &rv);
152 NS_ENSURE_SUCCESS(rv, rv);
154 NSString* attribName = nsCocoaUtils::ToNSString(aAttributeName);
155 if ([mNativeObject respondsToSelector:@selector(accessibilitySetValue:forAttribute:)]) {
156 // The NSObject has an attribute setter, call that.
157 [mNativeObject accessibilitySetValue:obj forAttribute:attribName];
161 return NS_ERROR_NOT_IMPLEMENTED;
165 xpcAccessibleMacInterface::GetParameterizedAttributeValue(const nsAString& aAttributeName,
166 JS::HandleValue aParameter,
168 JS::MutableHandleValue aResult) {
170 id paramObj = JsValueToNSObject(aParameter, aCx, &rv);
171 NS_ENSURE_SUCCESS(rv, rv);
173 NSString* attribName = nsCocoaUtils::ToNSString(aAttributeName);
174 return NSObjectToJsValue(
175 [mNativeObject accessibilityAttributeValue:attribName forParameter:paramObj], aCx, aResult);
178 bool xpcAccessibleMacInterface::SupportsSelector(SEL aSelector) {
179 // return true if we have this selector, and if isAccessibilitySelectorAllowed
180 // is implemented too whether it is "allowed".
181 return [mNativeObject respondsToSelector:aSelector] &&
182 (![mNativeObject respondsToSelector:@selector(isAccessibilitySelectorAllowed:selector:)] ||
183 [mNativeObject isAccessibilitySelectorAllowed:aSelector]);
186 nsresult xpcAccessibleMacInterface::NSObjectToJsValue(id aObj, JSContext* aCx,
187 JS::MutableHandleValue aResult) {
189 aResult.set(JS::NullValue());
190 } else if ([aObj isKindOfClass:[NSString class]]) {
192 nsCocoaUtils::GetStringForNSString((NSString*)aObj, strVal);
193 if (!mozilla::dom::ToJSValue(aCx, strVal, aResult)) {
194 return NS_ERROR_FAILURE;
196 } else if ([aObj isKindOfClass:[NSNumber class]]) {
197 // If the type being held by the NSNumber is a BOOL set js value
198 // to boolean. Otherwise use a double value.
199 if (strcmp([(NSNumber*)aObj objCType], @encode(BOOL)) == 0) {
200 if (!mozilla::dom::ToJSValue(aCx, [(NSNumber*)aObj boolValue], aResult)) {
201 return NS_ERROR_FAILURE;
204 if (!mozilla::dom::ToJSValue(aCx, [(NSNumber*)aObj doubleValue], aResult)) {
205 return NS_ERROR_FAILURE;
208 } else if ([aObj isKindOfClass:[NSValue class]] &&
209 strcmp([(NSValue*)aObj objCType], @encode(NSPoint)) == 0) {
210 NSPoint point = [(NSValue*)aObj pointValue];
211 return NSObjectToJsValue(
212 @[ [NSNumber numberWithDouble:point.x], [NSNumber numberWithDouble:point.y] ], aCx,
214 } else if ([aObj isKindOfClass:[NSValue class]] &&
215 strcmp([(NSValue*)aObj objCType], @encode(NSSize)) == 0) {
216 NSSize size = [(NSValue*)aObj sizeValue];
217 return NSObjectToJsValue(
218 @[ [NSNumber numberWithDouble:size.width], [NSNumber numberWithDouble:size.height] ], aCx,
220 } else if ([aObj isKindOfClass:[NSValue class]] &&
221 strcmp([(NSValue*)aObj objCType], @encode(NSRange)) == 0) {
222 NSRange range = [(NSValue*)aObj rangeValue];
223 return NSObjectToJsValue(@[ @(range.location), @(range.length) ], aCx, aResult);
224 } else if ([aObj isKindOfClass:[NSValue class]] &&
225 strcmp([(NSValue*)aObj objCType], @encode(NSRect)) == 0) {
226 NSRect rect = [(NSValue*)aObj rectValue];
227 return NSObjectToJsValue(@{
228 @"origin" : [NSValue valueWithPoint:rect.origin],
229 @"size" : [NSValue valueWithSize:rect.size]
232 } else if ([aObj isKindOfClass:[NSArray class]]) {
233 NSArray* objArr = (NSArray*)aObj;
235 JS::RootedVector<JS::Value> v(aCx);
236 if (!v.resize([objArr count])) {
237 return NS_ERROR_FAILURE;
239 for (size_t i = 0; i < [objArr count]; ++i) {
240 nsresult rv = NSObjectToJsValue(objArr[i], aCx, v[i]);
241 NS_ENSURE_SUCCESS(rv, rv);
244 JSObject* arrayObj = JS::NewArrayObject(aCx, v);
246 return NS_ERROR_FAILURE;
248 aResult.setObject(*arrayObj);
249 } else if ([aObj isKindOfClass:[NSDictionary class]]) {
250 JS::RootedObject obj(aCx, JS_NewPlainObject(aCx));
251 for (NSString* key in aObj) {
253 nsCocoaUtils::GetStringForNSString(key, strKey);
254 JS::RootedValue value(aCx);
255 nsresult rv = NSObjectToJsValue(aObj[key], aCx, &value);
256 NS_ENSURE_SUCCESS(rv, rv);
257 JS_SetUCProperty(aCx, obj, strKey.get(), strKey.Length(), value);
259 aResult.setObject(*obj);
260 } else if ([aObj isKindOfClass:[NSAttributedString class]]) {
261 NSAttributedString* attrStr = (NSAttributedString*)aObj;
262 __block NSMutableArray* attrRunArray = [[NSMutableArray alloc] init];
265 enumerateAttributesInRange:NSMakeRange(0, [attrStr length])
266 options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
267 usingBlock:^(NSDictionary* attributes, NSRange range, BOOL* stop) {
268 NSString* str = [[attrStr string] substringWithRange:range];
269 if (!str || !attributes) {
273 NSMutableDictionary* attrRun = [attributes mutableCopy];
274 attrRun[@"string"] = str;
276 [attrRunArray addObject:attrRun];
279 // The attributed string is represented in js as an array of objects.
280 // Each object represents a run of text where the "string" property is the
281 // string value and all the AX* properties are the attributes.
282 return NSObjectToJsValue(attrRunArray, aCx, aResult);
283 } else if (CFGetTypeID(aObj) == CGColorGetTypeID()) {
284 const CGFloat* components = CGColorGetComponents((CGColorRef)aObj);
285 NSString* hexString =
286 [NSString stringWithFormat:@"#%02x%02x%02x", (int)(components[0] * 0xff),
287 (int)(components[1] * 0xff), (int)(components[2] * 0xff)];
288 return NSObjectToJsValue(hexString, aCx, aResult);
289 } else if ([aObj respondsToSelector:@selector(isAccessibilityElement)]) {
290 // We expect all of our accessibility objects to implement isAccessibilityElement
291 // at the very least. If it is implemented we will assume its an accessibility object.
292 nsCOMPtr<nsIAccessibleMacInterface> obj = new xpcAccessibleMacInterface(aObj);
293 return nsContentUtils::WrapNative(aCx, obj, &NS_GET_IID(nsIAccessibleMacInterface), aResult);
295 // If this is any other kind of NSObject, just wrap it and return it.
296 // It will be opaque and immutable on the JS side, but it can be
297 // brought back to us in an argument.
298 nsCOMPtr<nsIAccessibleMacNSObjectWrapper> obj = new xpcAccessibleMacNSObjectWrapper(aObj);
299 return nsContentUtils::WrapNative(aCx, obj, &NS_GET_IID(nsIAccessibleMacNSObjectWrapper),
306 id xpcAccessibleMacInterface::JsValueToNSObject(JS::HandleValue aValue, JSContext* aCx,
309 if (aValue.isInt32()) {
310 return [NSNumber numberWithInteger:aValue.toInt32()];
311 } else if (aValue.isBoolean()) {
312 return [NSNumber numberWithBool:aValue.toBoolean()];
313 } else if (aValue.isString()) {
315 if (!temp.init(aCx, aValue)) {
316 NS_WARNING("cannot init string with given value");
317 *aResult = NS_ERROR_FAILURE;
320 return nsCocoaUtils::ToNSString(temp);
321 } else if (aValue.isObject()) {
322 JS::Rooted<JSObject*> obj(aCx, aValue.toObjectOrNull());
325 JS::IsArrayObject(aCx, obj, &isArray);
327 // If this is an array, we construct an NSArray and insert the js
328 // array's elements by recursively calling this function.
330 JS::GetArrayLength(aCx, obj, &len);
331 NSMutableArray* array = [NSMutableArray arrayWithCapacity:len];
332 for (uint32_t i = 0; i < len; i++) {
333 JS::RootedValue v(aCx);
334 JS_GetElement(aCx, obj, i, &v);
335 [array addObject:JsValueToNSObject(v, aCx, aResult)];
336 NS_ENSURE_SUCCESS(*aResult, nil);
343 JS_HasOwnProperty(aCx, obj, "valueType", &hasValueType);
344 JS_HasOwnProperty(aCx, obj, "value", &hasValue);
345 if (hasValueType && hasValue) {
346 // A js object representin an NSValue looks like this:
347 // { valueType: "NSRange", value: [1, 3] }
348 return JsValueToNSValue(obj, aCx, aResult);
353 JS_HasOwnProperty(aCx, obj, "objectType", &hasObjectType);
354 JS_HasOwnProperty(aCx, obj, "object", &hasObject);
355 if (hasObjectType && hasObject) {
356 // A js object representing an NSDictionary looks like this:
357 // { objectType: "NSDictionary", value: {k: v, k: v, ...} }
358 return JsValueToSpecifiedNSObject(obj, aCx, aResult);
361 // This may be another nsIAccessibleMacInterface instance.
362 // If so, return the wrapped NSObject.
363 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
365 nsCOMPtr<nsIXPConnectWrappedNative> wrappedObj;
366 nsresult rv = xpc->GetWrappedNativeOfJSObject(aCx, obj, getter_AddRefs(wrappedObj));
367 NS_ENSURE_SUCCESS(rv, nil);
368 nsCOMPtr<nsIAccessibleMacNSObjectWrapper> macObjIface = do_QueryInterface(wrappedObj->Native());
369 return macObjIface->GetNativeObject();
372 *aResult = NS_ERROR_FAILURE;
376 id xpcAccessibleMacInterface::JsValueToNSValue(JS::HandleObject aObject, JSContext* aCx,
378 *aResult = NS_ERROR_FAILURE;
379 JS::RootedValue valueTypeValue(aCx);
380 if (!JS_GetProperty(aCx, aObject, "valueType", &valueTypeValue)) {
381 NS_WARNING("Could not get valueType");
385 JS::RootedValue valueValue(aCx);
386 if (!JS_GetProperty(aCx, aObject, "value", &valueValue)) {
387 NS_WARNING("Could not get value");
391 nsAutoJSString valueType;
392 if (!valueTypeValue.isString() || !valueType.init(aCx, valueTypeValue)) {
393 NS_WARNING("valueType is not a string");
398 JS::IsArrayObject(aCx, valueValue, &isArray);
400 NS_WARNING("value is not an array");
404 JS::Rooted<JSObject*> value(aCx, valueValue.toObjectOrNull());
406 if (valueType.EqualsLiteral("NSRange")) {
408 JS::GetArrayLength(aCx, value, &len);
410 NS_WARNING("Expected a 2 member array");
414 JS::RootedValue locationValue(aCx);
415 JS_GetElement(aCx, value, 0, &locationValue);
416 JS::RootedValue lengthValue(aCx);
417 JS_GetElement(aCx, value, 1, &lengthValue);
418 if (!locationValue.isInt32() || !lengthValue.isInt32()) {
419 NS_WARNING("Expected an array of integers");
424 return [NSValue valueWithRange:NSMakeRange(locationValue.toInt32(), lengthValue.toInt32())];
430 id xpcAccessibleMacInterface::JsValueToSpecifiedNSObject(JS::HandleObject aObject, JSContext* aCx,
432 *aResult = NS_ERROR_FAILURE;
433 JS::RootedValue objectTypeValue(aCx);
434 if (!JS_GetProperty(aCx, aObject, "objectType", &objectTypeValue)) {
435 NS_WARNING("Could not get objectType");
439 JS::RootedValue objectValue(aCx);
440 if (!JS_GetProperty(aCx, aObject, "object", &objectValue)) {
441 NS_WARNING("Could not get object");
445 nsAutoJSString objectType;
446 if (!objectTypeValue.isString()) {
447 NS_WARNING("objectType is not a string");
451 if (!objectType.init(aCx, objectTypeValue)) {
452 NS_WARNING("cannot init string with object type");
456 bool isObject = objectValue.isObjectOrNull();
458 NS_WARNING("object is not a JSON object");
462 JS::Rooted<JSObject*> object(aCx, objectValue.toObjectOrNull());
464 if (objectType.EqualsLiteral("NSDictionary")) {
465 JS::Rooted<JS::IdVector> ids(aCx, JS::IdVector(aCx));
466 if (!JS_Enumerate(aCx, object, &ids)) {
467 NS_WARNING("Unable to get keys from dictionary object");
471 NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];
473 for (size_t i = 0, n = ids.length(); i < n; i++) {
476 JS::RootedValue currentKey(aCx);
477 JS_IdToValue(aCx, ids[i], ¤tKey);
478 id unwrappedKey = JsValueToNSObject(currentKey, aCx, &rv);
479 NS_ENSURE_SUCCESS(rv, nil);
480 MOZ_ASSERT([unwrappedKey isKindOfClass:[NSString class]]);
482 // get associated value for current key
483 JS::RootedValue currentValue(aCx);
484 JS_GetPropertyById(aCx, object, ids[i], ¤tValue);
485 id unwrappedValue = JsValueToNSObject(currentValue, aCx, &rv);
486 NS_ENSURE_SUCCESS(rv, nil);
487 dict[unwrappedKey] = unwrappedValue;
497 NS_IMPL_ISUPPORTS(xpcAccessibleMacEvent, nsIAccessibleMacEvent)
499 xpcAccessibleMacEvent::xpcAccessibleMacEvent(id aNativeObj, id aData)
500 : mNativeObject(aNativeObj), mData(aData) {
501 [mNativeObject retain];
505 xpcAccessibleMacEvent::~xpcAccessibleMacEvent() {
506 [mNativeObject release];
511 xpcAccessibleMacEvent::GetMacIface(nsIAccessibleMacInterface** aMacIface) {
512 RefPtr<xpcAccessibleMacInterface> macIface = new xpcAccessibleMacInterface(mNativeObject);
513 macIface.forget(aMacIface);
518 xpcAccessibleMacEvent::GetData(JSContext* aCx, JS::MutableHandleValue aData) {
519 return xpcAccessibleMacInterface::NSObjectToJsValue(mData, aCx, aData);
522 void xpcAccessibleMacEvent::FireEvent(id aNativeObj, id aNotification, id aUserInfo) {
523 if (nsCOMPtr<nsIObserverService> obsService = services::GetObserverService()) {
524 nsCOMPtr<nsISimpleEnumerator> observers;
525 // Get all observers for the mac event topic.
526 obsService->EnumerateObservers(NS_ACCESSIBLE_MAC_EVENT_TOPIC, getter_AddRefs(observers));
528 bool hasObservers = false;
529 observers->HasMoreElements(&hasObservers);
530 // If we have observers, notify them.
532 nsCOMPtr<nsIAccessibleMacEvent> xpcIface = new xpcAccessibleMacEvent(aNativeObj, aUserInfo);
533 nsAutoString notificationStr;
534 nsCocoaUtils::GetStringForNSString(aNotification, notificationStr);
535 obsService->NotifyObservers(xpcIface, NS_ACCESSIBLE_MAC_EVENT_TOPIC, notificationStr.get());