Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fuzzy on memory/sharing objects between Obj-C and Javascript



As far as I know, sharing with JavaScript shouldn't introduce any differences in retain/release semantics in cocoa. You should just treat these as ordinary cocoa methods. So, yes, you should autorelease the string allocated and returned by styleAction because the cocoa convention is that accessor methods return autoreleased objects with a retain count of 1. (Even better, you could use an NSString convenience method, like +stringWithString:, to get such an object.) The @"" convention is just like calling +[NSString stringWithCharacters:length:], which will do the right thing.

As a side note, the behavior you've implemented is just like always returning NO from isSelectorExcludedFromWebScript. You can pass cocoaFunction:withString: any method name, and it will execute that method. This is a security hole that would allow JavaScript to execute arbitrary methods in Objective-C. The purpose of isSelectorExcludedFromWebScript is to avoid such security holes. If you want to avoid constantly having to edit the method during development, consider setting it to return NO unconditionally while you develop, and change it to return NO only for the mehods you want to expose before shipping.

Cheers.

Geoff

On Dec 15, 2005, at 12:08 PM, Jeffrey Johnson wrote:

Hello,

I have a JavaScript function like this:

function callCocoaWithString(functionName, argument) {
var theBroker = window.myMessageBroker;
var returnMsg = theBroker.cocoaFunctionWithString (functionName,argument);
}


where myMessageBroker is an NSObject attached in webView:windowScriptObjectAvailable: with a [wso setValue:msgBroker forKey:@"myMessageBroker" ].

I have the following methods in msgBroker.m:

+ (NSString *) webScriptNameForSelector:(SEL)sel
{
    if (sel == @selector(cocoaFunction:withString:))
           return @"cocoaFunctionWithString";
    return nil;
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)selector
{
    if (selector == @selector(cocoaFunction:withString:)) {
        return NO;
    }
    return YES;
}

- (NSString *)cocoaFunction:(NSString*)functionName withString: (NSString *)dataString
{
NSString * someString;


NSString * selName = [ functionName stringByAppendingString:@":" ];
SEL sel = NSSelectorFromString(selName);


if ( [self respondsToSelector:sel] ) {
someString = [self performSelector:sel withObject:dataString];
}
else {
NSLog(@"javascript called cocoaFunction: with unknown selector: |%@|", functionName);
return @"%UNKNOWNFUNCTION";
}


    return someString;
}


- (NSString *) styleAction:(NSString *)dataString
{
NSString * returnString = [[[NSString alloc] initWithString:dataString] autorelease] ;


    return returnString;
}

styleAction: doesn't really do anything yet, obviously, and I'm doing the NSSelectorFromString() thing so that we can add new functions without having to constantly be editing webScriptNameForSelector: and isSelectorExcludedFromWebScript:.

Given a JavaScript
 callCocoaWithString("styleAction","a String")

am I doing the right thing with regards to autoreleasing the string returned by styleAction: ? What about the static string I return in cocoaFunction:withString: for unknown selectors?


Thanks
Jeffrey Johnson
Macintosh Development
Wavefunction, Inc.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webkitsdk-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webkitsdk-dev/ggaren% 40apple.com


This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Webkitsdk-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webkitsdk-dev/email@hidden

This email sent to email@hidden
References: 
 >Fuzzy on memory/sharing objects between Obj-C and Javascript (From: Jeffrey Johnson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.