4646// Memory AutoRelease pool.
4747static NSAutoreleasePool * g_autopool = nil ;
4848
49+ // Files passed to the app at startup
50+ static NSMutableArray * pendingOpenFiles;
51+ ExtensionString gPendingFilesToOpen ;
52+
4953// Provide the CefAppProtocol implementation required by CEF.
5054@interface ClientApplication : NSApplication <CefAppProtocol> {
5155@private
@@ -286,6 +290,8 @@ - (void)cleanup:(id)window {
286290// Receives notifications from the application. Will delete itself when done.
287291@interface ClientAppDelegate : NSObject
288292- (void )createApp : (id )object ;
293+ - (BOOL )application : (NSApplication *)theApplication openFile : (NSString *)filename ;
294+ - (BOOL )application : (NSApplication *)theApplication openFiles : (NSArray *)filenames ;
289295@end
290296
291297@implementation ClientAppDelegate
@@ -420,8 +426,23 @@ - (void)createApp:(id)object {
420426 window_info.SetAsChild (contentView, 0 , 0 , content_rect.size .width , content_rect.size .height );
421427
422428 NSString * str = [[startupUrl absoluteString ] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
423- CefBrowserHost::CreateBrowser (window_info, g_handler.get (),
429+ CefBrowserHost::CreateBrowserSync (window_info, g_handler.get (),
424430 [str UTF8String ], settings);
431+
432+ if (pendingOpenFiles) {
433+ NSUInteger count = [pendingOpenFiles count ];
434+ gPendingFilesToOpen = " [" ;
435+ for (NSUInteger i = 0 ; i < count; i++) {
436+ NSString * filename = [pendingOpenFiles objectAtIndex: i];
437+
438+ gPendingFilesToOpen += (" \" " + std::string ([filename UTF8String ]) + " \" " );
439+ if (i < count - 1 )
440+ gPendingFilesToOpen += " ," ;
441+ }
442+ gPendingFilesToOpen += " ]" ;
443+ } else {
444+ gPendingFilesToOpen = " []" ;
445+ }
425446
426447 // Show the window.
427448 [mainWnd display ];
@@ -450,21 +471,52 @@ -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theAppli
450471}
451472
452473- (NSApplicationTerminateReply )applicationShouldTerminate : (NSApplication *)theApplication {
453- if (!g_isTerminating && g_handler.get () && !g_handler->AppIsQuitting () && g_handler->HasWindows ()) {
474+ if (!g_isTerminating && g_handler.get () && !g_handler->AppIsQuitting () && g_handler->HasWindows () && [ NSApp keyWindow ] ) {
454475 g_handler->DispatchCloseToNextBrowser ();
455476 return NSTerminateCancel;
456477 }
457478 g_isTerminating = true ;
458479 return NSTerminateNow;
459480}
460481
482+ - (BOOL )application : (NSApplication *)theApplication openFile : (NSString *)filename {
483+ if (g_handler) {
484+ CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow ([NSApp keyWindow ]);
485+ g_handler->SendOpenFileCommand (browser, CefString ([filename UTF8String ]));
486+ } else {
487+ // App is just starting up. Save the filename so we can open it later.
488+ if (!pendingOpenFiles) {
489+ pendingOpenFiles = [[NSMutableArray alloc ] init ];
490+ [pendingOpenFiles addObject: filename];
491+ }
492+ }
493+ return YES ;
494+ }
495+
496+ - (BOOL )application : (NSApplication *)theApplication openFiles : (NSArray *)filenames {
497+ if (g_handler) {
498+ CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow ([NSApp keyWindow ]);
499+ for (NSUInteger i = 0 ; i < [filenames count ]; i++) {
500+ g_handler->SendOpenFileCommand (browser, CefString ([[filenames objectAtIndex: i] UTF8String ]));
501+ }
502+ } else {
503+ // App is just starting up. Save the filenames so we can open them later.
504+ pendingOpenFiles = [[NSMutableArray alloc ] init ];
505+ for (NSUInteger i = 0 ; i < [filenames count ]; i++) {
506+ [pendingOpenFiles addObject: [filenames objectAtIndex: i]];
507+ }
508+ }
509+ return YES ;
510+ }
461511@end
462512
463513
464514int main (int argc, char * argv[]) {
465515 // Initialize the AutoRelease pool.
466516 g_autopool = [[NSAutoreleasePool alloc ] init ];
467517
518+ pendingOpenFiles = nil ;
519+
468520 CefMainArgs main_args (argc, argv);
469521
470522 // Delete Special Characters Palette from Edit menu.
@@ -485,6 +537,8 @@ int main(int argc, char* argv[]) {
485537
486538 // Initialize the ClientApplication instance.
487539 [ClientApplication sharedApplication ];
540+ NSObject * delegate = [[ClientAppDelegate alloc ] init ];
541+ [NSApp setDelegate: delegate];
488542
489543 // Parse command line arguments.
490544 AppInitCommandLine (argc, argv);
@@ -552,7 +606,6 @@ int main(int argc, char* argv[]) {
552606 }
553607
554608 // Create the application delegate and window.
555- NSObject * delegate = [[ClientAppDelegate alloc ] init ];
556609 [delegate performSelectorOnMainThread: @selector (createApp: ) withObject: nil
557610 waitUntilDone: NO ];
558611
0 commit comments