Commit 36fe18f7 authored by avi@google.com's avatar avi@google.com

Switch to a raw kAEOpenDocuments AppleEvent handler for opening dropped files...

Switch to a raw kAEOpenDocuments AppleEvent handler for opening dropped files to avoid crashes when Cocoa feeds us files specified on the command line.

Review URL: http://codereview.chromium.org/100142

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14876 0039d316-1c4b-4281-b951-d872f2087c98
parent 99fa8776
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
- (void)initMenuState; - (void)initMenuState;
- (void)getUrl:(NSAppleEventDescriptor*)event - (void)getUrl:(NSAppleEventDescriptor*)event
withReply:(NSAppleEventDescriptor*)reply; withReply:(NSAppleEventDescriptor*)reply;
- (void)openFiles:(NSAppleEventDescriptor*)event
withReply:(NSAppleEventDescriptor*)reply;
@end @end
@implementation AppController @implementation AppController
...@@ -47,6 +49,10 @@ ...@@ -47,6 +49,10 @@
andSelector:@selector(getUrl:withReply:) andSelector:@selector(getUrl:withReply:)
forEventClass:'WWW!' // A particularly ancient AppleEvent that dates forEventClass:'WWW!' // A particularly ancient AppleEvent that dates
andEventID:'OURL']; // back to the Spyglass days. andEventID:'OURL']; // back to the Spyglass days.
[em setEventHandler:self
andSelector:@selector(openFiles:withReply:)
forEventClass:kCoreEventClass
andEventID:kAEOpenDocuments];
} }
- (void)dealloc { - (void)dealloc {
...@@ -72,6 +78,8 @@ ...@@ -72,6 +78,8 @@
andEventID:kAEGetURL]; andEventID:kAEGetURL];
[em removeEventHandlerForEventClass:'WWW!' [em removeEventHandlerForEventClass:'WWW!'
andEventID:'OURL']; andEventID:'OURL'];
[em removeEventHandlerForEventClass:kCoreEventClass
andEventID:kAEOpenDocuments];
// TODO(pinkerton): Not sure where this should live, including it here // TODO(pinkerton): Not sure where this should live, including it here
// causes all sorts of asserts from the open renderers. On Windows, it // causes all sorts of asserts from the open renderers. On Windows, it
...@@ -190,13 +198,33 @@ void OpenURLs(const std::vector<GURL>& urls) { ...@@ -190,13 +198,33 @@ void OpenURLs(const std::vector<GURL>& urls) {
OpenURLs(gurlVector); OpenURLs(gurlVector);
} }
- (void)application:(NSApplication*)sender - (void)openFiles:(NSAppleEventDescriptor*)event
openFiles:(NSArray*)filenames { withReply:(NSAppleEventDescriptor*)reply {
// Ordinarily we'd use the NSApplication delegate method
// -application:openFiles:, but Cocoa tries to be smart and it sends files
// specified on the command line into that delegate method. That's too smart
// for us (our setup isn't done by the time Cocoa triggers the delegate method
// and we crash). Since all we want are files dropped on the app icon, and we
// have cross-platform code to handle the command-line files anyway, an Apple
// Event handler fits the bill just right.
NSAppleEventDescriptor* fileList =
[event paramDescriptorForKeyword:keyDirectObject];
if (!fileList)
return;
std::vector<GURL> gurlVector; std::vector<GURL> gurlVector;
for (NSString* filename in filenames) { for (NSInteger i = 1; i <= [fileList numberOfItems]; ++i) {
NSURL* fileURL = [NSURL fileURLWithPath:filename]; NSAppleEventDescriptor* fileAliasDesc = [fileList descriptorAtIndex:i];
GURL gurl(base::SysNSStringToUTF8([fileURL absoluteString])); if (!fileAliasDesc)
continue;
NSAppleEventDescriptor* fileURLDesc =
[fileAliasDesc coerceToDescriptorType:typeFileURL];
if (!fileURLDesc)
continue;
NSData* fileURLData = [fileURLDesc data];
if (!fileURLData)
continue;
GURL gurl(std::string((char*)[fileURLData bytes], [fileURLData length]));
gurlVector.push_back(gurl); gurlVector.push_back(gurl);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment