Search code examples
objective-cmacosfirefox-addonjsctypes

Message Passing From firefox extension to mac app


I have a mac app which receives broadcast distributed notification messages with code

            NSDistributedNotificationCenter.defaultCenter().addObserver(sender, selector: "testaction:", name: "myNoti", object: nil)

i also have developed firefox extension using Addon SDK

I knows their is javascript - Objc bridge

in which we can import system Frameworks and write objective c code in firefox extension javascript code .

but I tried adding

 Components.utils.import("/path/to/macosx.js");

then

  include("macosx.js")

etc but nothing worked for me attached is the code link : : https://drive.google.com/file/d/0B9ioid0ZPVpaaHBqRzdEV2tGUjg/view?usp=sharing

Can anybody knows how can i pass message from firefox extension to mac app using anything

DistributedNotification seems good and Ok for me using macosx.js but it not working for me

Thanks


Solution

  • Added js-ctypes tag. Join moz irc js-ctypes for more help, I would actually love to chat with you about it, join here: https://client00.chat.mibbit.com/?url=irc%3A%2F%2Firc.mozilla.org%2F%23jsctypes or here irc://moznet/jsctypes . Awesome work you're doing here.

    I'm not going to do this with ObjC Bridge because that has a TON of abstraction. You won't understand the fundamentals needed to run that. So start with no abstraction then later you can move into the abstracted if you want, otherwise troubleshooting will be a headache.

    This is how you post a message with postNotification:object, try this code, I tested it, it ran without error, but I didn't set up an observer so didn't test if it actually worked. It just doesn't crash :P

    Cu.import('resource://gre/modules/ctypes.jsm');
    
    var objc = ctypes.open(ctypes.libraryName('objc'));
    
    // BASIC TYPES
    var BOOL = ctypes.signed_char;
    var CHAR = ctypes.char;
    var ID = ctypes.voidptr_t;
    var SEL = ctypes.voidptr_t;
    var VOID = ctypes.void_t;
    
    // CONSTANTS
    var NIL_ID = ctypes.cast(ctypes.uint64_t(0), ID);
    var YES = BOOL(1);
    var NO = BOOL(0);
    
    // COMMON FUNCTIONS
    var objc_getClass = objc.declare('objc_getClass', ctypes.default_abi, ID, CHAR.ptr);
    var objc_msgSend = objc.declare('objc_msgSend', ctypes.default_abi, ID, ID, SEL, '...');
    var sel_registerName = objc.declare('sel_registerName', ctypes.default_abi, SEL, CHAR.ptr);
    
    // COMMON SELECTORS
    var alloc = sel_registerName('alloc');
    var init = sel_registerName('init');
    var release = sel_registerName('release');
    
    var shutdown = function() {
        if (NSString_myNoti) {
            console.log('releasing NSString_myNoti');
            objc_msgSend(NSString_myNoti, release);
        }
        objc.close();
    
        console.log('succesfully shutdown');
    }
    
    // my globals:
    var NSString_myNoti;
    
    function main() {
    
        // default_center = [NSDistributedNotificationCenter defaultCenter];
        var NSDistributedNotificationCenter = objc_getClass('NSDistributedNotificationCenter');
        var defaultCenter = sel_registerName('defaultCenter');
        var default_center = objc_msgSend(NSDistributedNotificationCenter, defaultCenter);
        console.info('default_center:', default_center, default_center.toString(), uneval(default_center), default_center.isNull());
    
        var NSString = objc_getClass('NSString');
        var initWithUTF8String = sel_registerName('initWithUTF8String:');
        NSString_myNoti = objc_msgSend(objc_msgSend(NSString, alloc), initWithUTF8String, CHAR.array()('myNoti'));
        console.info('NSString_myNoti:', NSString_myNoti, NSString_myNoti.toString(), uneval(NSString_myNoti));
    
        // postNotificationName:object:userInfo:deliverImmediately:
        var postNotificationNameObject = sel_registerName('postNotificationName:object:');
        var rez_postNotificationName = objc_msgSend(default_center, postNotificationNameObject, NSString_myNoti, NIL_ID); // returns void
        //docs say rez_postNotificationName is VOID
        console.log(postNotificationNameObject.toString(), postNotificationNameObject.toString() == YES.toString()); //its always returning 0x0 which iS NO, but its sending, 0x0 is VOID proabably i dont know, but i guess this matches the documentation
    }
    
    try {
        main();
    } catch (ex) {
        console.error('Error Occured:', ex);
    } finally {
        shutdown();
    }
    

    EDIT

    verified the code above works, i tested by adding observer for myNotif also with jsctypes, the addObserver:selector:name:object code is linked from the README. and also in this example i used postNotification:notificationName:object:userInfo:deliverImmediatey so this shows that as an added bonus: GitHubGIST :: Noitidart / _ff-addon-snippet-ObjC-postNotificationNameObjectUserInfoDeliverImmediately