Skip to content

Commit 3366127

Browse files
committed
Fix context menu and implement CEF app protocol. Use autoreleasepool.
1 parent 292cba9 commit 3366127

File tree

5 files changed

+139
-59
lines changed

5 files changed

+139
-59
lines changed

app.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,54 @@
33
// Project website: https://github.com/cztomczak/phpdesktop
44

55
#include "app.h"
6+
#include "client.h"
7+
#include "mongoose_server.h"
68
#include "settings.h"
79

810
#include "include/wrapper/cef_helpers.h"
911

12+
void CreateMainBrowser()
13+
{
14+
LOG(INFO) << "Create main browser";
15+
16+
json_value* app_settings = Settings();
17+
CefBrowserSettings browser_settings;
18+
19+
CefWindowInfo window_info;
20+
std::string runtime_style((*app_settings)["chrome"]["runtime_style"]);
21+
if (runtime_style == "alloy") {
22+
window_info.runtime_style = CEF_RUNTIME_STYLE_ALLOY;
23+
LOG(INFO) << "Runtime style: alloy";
24+
} else if (runtime_style == "chrome") {
25+
window_info.runtime_style = CEF_RUNTIME_STYLE_CHROME;
26+
LOG(INFO) << "Runtime style: chrome";
27+
} else {
28+
LOG(WARNING) << "Invalid runtime style in settings.json: " << runtime_style;
29+
window_info.runtime_style = CEF_RUNTIME_STYLE_ALLOY;
30+
}
31+
32+
// Create window explicitilly TODO
33+
// std::string app_icon_path((*app_settings)["main_window"]["icon"]);
34+
// app_icon_path = GetFullPath(app_icon_path);
35+
// bool center_on_screen = (*app_settings)["main_window"]["center_on_screen"];
36+
// const char* title = (*Settings())["main_window"]["title"];
37+
38+
int default_width = static_cast<int>(
39+
(*app_settings)["main_window"]["default_size"][0]);
40+
int default_height = static_cast<int>(
41+
(*app_settings)["main_window"]["default_size"][1]);
42+
CefRect browser_rect(0, 0, default_width, default_height);
43+
window_info.SetAsChild(nullptr, browser_rect);
44+
45+
CefBrowserHost::CreateBrowser(
46+
window_info,
47+
Client::GetInstance(),
48+
mongoose_get_url(),
49+
browser_settings,
50+
nullptr,
51+
nullptr);
52+
}
53+
1054
App::App()
1155
{
1256
}
@@ -62,5 +106,5 @@ void App::OnBeforeCommandLineProcessing(const CefString& process_type,
62106
void App::OnContextInitialized()
63107
{
64108
CEF_REQUIRE_UI_THREAD();
65-
const char* title = (*Settings())["main_window"]["title"];
109+
CreateMainBrowser();
66110
}

client.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ Client* Client::GetInstance() {
3232
}
3333
}
3434

35+
bool Client::IsClosing()
36+
{
37+
return is_closing;
38+
}
39+
40+
void Client::CloseAllBrowsers(bool force_close)
41+
{
42+
CEF_REQUIRE_UI_THREAD();
43+
LOG(INFO) << "Close all browsers";
44+
45+
if (browser_list_.empty()) {
46+
return;
47+
}
48+
49+
BrowserList::const_iterator it = browser_list_.begin();
50+
for (; it != browser_list_.end(); ++it) {
51+
(*it)->GetHost()->CloseBrowser(force_close);
52+
}
53+
}
54+
3555
// CefContextMenuHandler.
3656

3757
#define _MENU_ID_DEVTOOLS MENU_ID_USER_FIRST + 1
@@ -143,8 +163,7 @@ void Client::OnBeforeClose(CefRefPtr<CefBrowser> browser)
143163

144164
// Cookies are not flushed to disk when closing app immediately.
145165
// Need to call FlushStore manually when browser is closing.
146-
browser->GetHost()->GetRequestContext()->GetCookieManager(nullptr)
147-
->FlushStore(nullptr);
166+
browser->GetHost()->GetRequestContext()->GetCookieManager(nullptr)->FlushStore(nullptr);
148167

149168
// Remove from the list of existing browsers.
150169
BrowserList::iterator bit = browser_list_.begin();

client.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Client : public CefClient,
2121

2222
// Provide access to the single global instance of this object.
2323
static Client* GetInstance();
24+
bool IsClosing();
25+
void CloseAllBrowsers(bool force_close);
2426

2527
// CefClient.
2628
CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() override {
@@ -84,6 +86,8 @@ class Client : public CefClient,
8486
typedef std::list<CefRefPtr<CefBrowser>> BrowserList;
8587
BrowserList browser_list_;
8688

89+
bool is_closing = false;
90+
8791
// Include the default reference counting implementation.
8892
IMPLEMENT_REFCOUNTING(Client);
8993
};

main.mm

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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"
@@ -24,47 +25,58 @@
2425
// Globals
2526
std::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

6981
int 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
}

settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"enable_menu": true,
3434
"navigation": true,
3535
"print": true,
36-
"view_source": true,
36+
"view_source": false,
3737
"devtools": true
3838
}
3939
}

0 commit comments

Comments
 (0)