1010#include " mongoose_server.h"
1111#include " settings.h"
1212
13+ #include " include/cef_application_mac.h"
1314#include " include/base/cef_logging.h"
1415#include " include/wrapper/cef_helpers.h"
1516#import " include/wrapper/cef_library_loader.h"
2425// Globals
2526std::string g_cgi_env_from_argv = " " ;
2627
27- void create_browser ()
28- {
29- // The call to CreateBrowserSync cannot be in the same block scope
30- // as the call to CefShutdown otherwise it results in segmentation
31- // fault with the stack trace as seen below. Making a call to
32- // browser->Release() did not help.
33- // ----
34- // #0 MaybeSendDestroyedNotification () at
35- // ./../../chrome/browser/profiles/profile.cc:294
36- // #1 0x00007ffff34c74b5 in Shutdown () at
37- // ../../cef/libcef/browser/browser_context.cc:81
38- // ----
39- json_value* app_settings = Settings ();
40- CefBrowserSettings browser_settings;
41- CefWindowInfo window_info;
42- std::string runtime_style ((*app_settings)[" chrome" ][" runtime_style" ]);
43- if (runtime_style == " alloy" ) {
44- window_info.runtime_style = CEF_RUNTIME_STYLE_ALLOY;
45- LOG (INFO) << " Runtime style: Alloy" ;
46- } else if (runtime_style == " chrome" ) {
47- window_info.runtime_style = CEF_RUNTIME_STYLE_CHROME;
48- LOG (INFO) << " Runtime style: Chrome" ;
49- } else {
50- LOG (WARNING) << " Invalid runtime style in settings.json: " << runtime_style;
51- window_info.runtime_style = CEF_RUNTIME_STYLE_ALLOY;
52- }
53- int default_width = static_cast <int >(
54- (*app_settings)[" main_window" ][" default_size" ][0 ]);
55- int default_height = static_cast <int >(
56- (*app_settings)[" main_window" ][" default_size" ][1 ]);
57- CefRect browser_rect (0 , 0 , default_width, default_height);
58- window_info.SetAsChild (nullptr , browser_rect);
59- CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync (
60- window_info,
61- Client::GetInstance (),
62- mongoose_get_url (),
63- browser_settings,
64- nullptr ,
65- nullptr );
66- LOG (INFO) << " Browser window handle=" << browser->GetHost ()->GetWindowHandle ();
28+ @interface SharedAppDelegate : NSObject <NSApplicationDelegate >
29+ - (void )createApplication:(id )object;
30+ - (void )tryToTerminateApplication:(NSApplication *)app;
31+ @end
32+
33+ @interface SharedApplication : NSApplication <CefAppProtocol> {
34+ @private
35+ BOOL handlingSendEvent_;
6736}
37+ @end
38+
39+ @implementation SharedApplication
40+ - (BOOL )isHandlingSendEvent {
41+ return handlingSendEvent_;
42+ }
43+
44+ - (void )setHandlingSendEvent:(BOOL )handlingSendEvent {
45+ handlingSendEvent_ = handlingSendEvent;
46+ }
47+
48+ - (void )sendEvent:(NSEvent *)event {
49+ CefScopedSendingEvent sendingEventScoper;
50+ [super sendEvent: event];
51+ }
52+
53+ - (void )terminate:(id )sender {
54+ SharedAppDelegate* delegate = static_cast <SharedAppDelegate*>([NSApp delegate ]);
55+ [delegate tryToTerminateApplication: self ];
56+ }
57+ @end
58+
59+ @implementation SharedAppDelegate
60+ - (void )createApplication:(id )object {
61+ [NSApplication sharedApplication ];
62+ [[NSBundle mainBundle ] loadNibNamed: @" MainMenu"
63+ owner: NSApp
64+ topLevelObjects: nil ];
65+ [[NSApplication sharedApplication ] setDelegate: self ];
66+ }
67+
68+ - (void )tryToTerminateApplication:(NSApplication *)app {
69+ Client* client = Client::GetInstance ();
70+ if (client && !client->IsClosing ()) {
71+ client->CloseAllBrowsers (false );
72+ }
73+ }
74+
75+ - (NSApplicationTerminateReply )applicationShouldTerminate:
76+ (NSApplication *)sender {
77+ return NSTerminateNow;
78+ }
79+ @end
6880
6981int main (int argc, char **argv) {
7082 // Load the CEF framework library at runtime instead of linking directly
@@ -74,6 +86,8 @@ int main(int argc, char **argv) {
7486 return 1 ;
7587 }
7688
89+ @autoreleasepool {
90+
7791 // Passing ENV variables to PHP using the --cgi-environment
7892 // command line arg passed to app.
7993 if (argv) {
@@ -221,28 +235,25 @@ int remote_debugging_port(
221235 cef_settings.remote_debugging_port = remote_debugging_port;
222236 }
223237
224- // App implements application-level callbacks for the browser
225- // process.
238+ // App implements application-level callbacks for the browser process.
226239 CefRefPtr<App> app (new App);
227240
241+ [SharedApplication sharedApplication ];
242+
228243 // Log messages created by LOG() macro will be written to debug.log
229244 // file only after CEF was initialized. Before CEF is initialized
230245 // all logs are only printed to console.
231246 LOG (INFO) << " Initialize CEF" ;
232- CefInitialize (main_args, cef_settings, app.get (), nullptr );
233-
234- // Create window TODO
235- std::string app_icon_path ((*app_settings)[" main_window" ][" icon" ]);
236- app_icon_path = GetFullPath (app_icon_path);
237- bool center_on_screen = (*app_settings)[" main_window" ][" center_on_screen" ];
238- int default_width = static_cast <int >(
239- (*app_settings)[" main_window" ][" default_size" ][0 ]);
240- int default_height = static_cast <int >(
241- (*app_settings)[" main_window" ][" default_size" ][1 ]);
247+ if (!CefInitialize (main_args, cef_settings, app.get (), nullptr )) {
248+ LOG (ERROR) << " Failed to initialize CEF" ;
249+ return 1 ;
250+ }
242251
243- // Create browser
244- LOG (INFO) << " Create browser" ;
245- create_browser ();
252+ // Create the application delegate.
253+ NSObject * delegate = [[SharedAppDelegate alloc ] init ];
254+ [delegate performSelectorOnMainThread: @selector (createApplication: )
255+ withObject: nil
256+ waitUntilDone: NO ];
246257
247258 LOG (INFO) << " Run CEF message loop" ;
248259 CefRunMessageLoop ();
@@ -253,5 +264,7 @@ int remote_debugging_port(
253264 LOG (INFO) << " Shutdown CEF" ;
254265 CefShutdown ();
255266
267+ } // end @autoreleasepool
268+
256269 return 0 ;
257270}
0 commit comments