2013-11-26 23:02:14 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright (c) 2010 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "include/cef_application_mac.h"
|
2020-07-06 20:14:57 +02:00
|
|
|
#include "include/cef_command_line.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2018-07-27 17:39:53 +02:00
|
|
|
#include "include/wrapper/cef_library_loader.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "tests/cefsimple/simple_app.h"
|
|
|
|
#include "tests/cefsimple/simple_handler.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
// Receives notifications from the application.
|
2021-02-23 21:08:33 +01:00
|
|
|
@interface SimpleAppDelegate : NSObject <NSApplicationDelegate>
|
2020-07-06 20:14:57 +02:00
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)createApplication:(id)object;
|
|
|
|
- (void)tryToTerminateApplication:(NSApplication*)app;
|
|
|
|
@end
|
|
|
|
|
2013-11-26 23:02:14 +01:00
|
|
|
// Provide the CefAppProtocol implementation required by CEF.
|
2019-04-23 19:17:56 +02:00
|
|
|
@interface SimpleApplication : NSApplication <CefAppProtocol> {
|
2017-05-17 11:29:28 +02:00
|
|
|
@private
|
2013-11-26 23:02:14 +01:00
|
|
|
BOOL handlingSendEvent_;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SimpleApplication
|
|
|
|
- (BOOL)isHandlingSendEvent {
|
|
|
|
return handlingSendEvent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setHandlingSendEvent:(BOOL)handlingSendEvent {
|
|
|
|
handlingSendEvent_ = handlingSendEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendEvent:(NSEvent*)event {
|
|
|
|
CefScopedSendingEvent sendingEventScoper;
|
|
|
|
[super sendEvent:event];
|
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
// |-terminate:| is the entry point for orderly "quit" operations in Cocoa. This
|
|
|
|
// includes the application menu's quit menu item and keyboard equivalent, the
|
|
|
|
// application's dock icon menu's quit menu item, "quit" (not "force quit") in
|
|
|
|
// the Activity Monitor, and quits triggered by user logout and system restart
|
|
|
|
// and shutdown.
|
|
|
|
//
|
|
|
|
// The default |-terminate:| implementation ends the process by calling exit(),
|
|
|
|
// and thus never leaves the main run loop. This is unsuitable for Chromium
|
|
|
|
// since Chromium depends on leaving the main run loop to perform an orderly
|
|
|
|
// shutdown. We support the normal |-terminate:| interface by overriding the
|
|
|
|
// default implementation. Our implementation, which is very specific to the
|
|
|
|
// needs of Chromium, works by asking the application delegate to terminate
|
|
|
|
// using its |-tryToTerminateApplication:| method.
|
|
|
|
//
|
|
|
|
// |-tryToTerminateApplication:| differs from the standard
|
|
|
|
// |-applicationShouldTerminate:| in that no special event loop is run in the
|
|
|
|
// case that immediate termination is not possible (e.g., if dialog boxes
|
|
|
|
// allowing the user to cancel have to be shown). Instead, this method tries to
|
|
|
|
// close all browsers by calling CloseBrowser(false) via
|
|
|
|
// ClientHandler::CloseAllBrowsers. Calling CloseBrowser will result in a call
|
|
|
|
// to ClientHandler::DoClose and execution of |-performClose:| on the NSWindow.
|
|
|
|
// DoClose sets a flag that is used to differentiate between new close events
|
|
|
|
// (e.g., user clicked the window close button) and in-progress close events
|
|
|
|
// (e.g., user approved the close window dialog). The NSWindowDelegate
|
|
|
|
// |-windowShouldClose:| method checks this flag and either calls
|
|
|
|
// CloseBrowser(false) in the case of a new close event or destructs the
|
|
|
|
// NSWindow in the case of an in-progress close event.
|
|
|
|
// ClientHandler::OnBeforeClose will be called after the CEF NSView hosted in
|
|
|
|
// the NSWindow is dealloc'ed.
|
|
|
|
//
|
|
|
|
// After the final browser window has closed ClientHandler::OnBeforeClose will
|
|
|
|
// begin actual tear-down of the application by calling CefQuitMessageLoop.
|
|
|
|
// This ends the NSApplication event loop and execution then returns to the
|
|
|
|
// main() function for cleanup before application termination.
|
|
|
|
//
|
|
|
|
// The standard |-applicationShouldTerminate:| is not supported, and code paths
|
|
|
|
// leading to it must be redirected.
|
|
|
|
- (void)terminate:(id)sender {
|
|
|
|
SimpleAppDelegate* delegate =
|
|
|
|
static_cast<SimpleAppDelegate*>([NSApp delegate]);
|
|
|
|
[delegate tryToTerminateApplication:self];
|
|
|
|
// Return, don't exit. The application is responsible for exiting on its own.
|
|
|
|
}
|
2013-11-26 23:02:14 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SimpleAppDelegate
|
|
|
|
|
|
|
|
// Create the application on the UI thread.
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)createApplication:(id)object {
|
2021-02-23 21:08:33 +01:00
|
|
|
[[NSBundle mainBundle] loadNibNamed:@"MainMenu"
|
|
|
|
owner:NSApp
|
|
|
|
topLevelObjects:nil];
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
// Set the delegate for application events.
|
2015-08-26 22:38:43 +02:00
|
|
|
[[NSApplication sharedApplication] setDelegate:self];
|
2013-11-26 23:02:14 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)tryToTerminateApplication:(NSApplication*)app {
|
|
|
|
SimpleHandler* handler = SimpleHandler::GetInstance();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (handler && !handler->IsClosing()) {
|
2013-11-26 23:02:14 +01:00
|
|
|
handler->CloseAllBrowsers(false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-11-26 23:02:14 +01:00
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:
|
2017-05-17 11:29:28 +02:00
|
|
|
(NSApplication*)sender {
|
2014-04-09 20:29:51 +02:00
|
|
|
return NSTerminateNow;
|
2013-11-26 23:02:14 +01:00
|
|
|
}
|
2024-01-29 19:38:25 +01:00
|
|
|
|
|
|
|
// Called when the user clicks the app dock icon while the application is
|
|
|
|
// already running.
|
|
|
|
- (BOOL)applicationShouldHandleReopen:(NSApplication*)theApplication
|
|
|
|
hasVisibleWindows:(BOOL)flag {
|
|
|
|
SimpleHandler* handler = SimpleHandler::GetInstance();
|
|
|
|
if (handler && !handler->IsClosing()) {
|
|
|
|
handler->ShowMainWindow();
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
2013-11-26 23:02:14 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
// Entry point function for the browser process.
|
|
|
|
int main(int argc, char* argv[]) {
|
2018-07-27 17:39:53 +02:00
|
|
|
// Load the CEF framework library at runtime instead of linking directly
|
|
|
|
// as required by the macOS sandbox implementation.
|
|
|
|
CefScopedLibraryLoader library_loader;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!library_loader.LoadInMain()) {
|
2018-07-27 17:39:53 +02:00
|
|
|
return 1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-07-27 17:39:53 +02:00
|
|
|
|
2013-11-26 23:02:14 +01:00
|
|
|
// Provide CEF with command-line arguments.
|
|
|
|
CefMainArgs main_args(argc, argv);
|
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
@autoreleasepool {
|
|
|
|
// Initialize the SimpleApplication instance.
|
|
|
|
[SimpleApplication sharedApplication];
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2020-07-06 20:14:57 +02:00
|
|
|
// If there was an invocation to NSApp prior to this method, then the NSApp
|
|
|
|
// will not be a SimpleApplication, but will instead be an NSApplication.
|
|
|
|
// This is undesirable and we must enforce that this doesn't happen.
|
|
|
|
CHECK([NSApp isKindOfClass:[SimpleApplication class]]);
|
|
|
|
|
|
|
|
// Parse command-line arguments for use in this method.
|
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::CreateCommandLine();
|
|
|
|
command_line->InitFromArgv(argc, argv);
|
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
// Specify CEF global settings here.
|
|
|
|
CefSettings settings;
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2024-01-17 23:44:33 +01:00
|
|
|
// Use the CEF Chrome runtime if "--enable-chrome-runtime" is specified via
|
|
|
|
// the command-line. Otherwise, use the CEF Alloy runtime. For more
|
|
|
|
// information about CEF runtimes see
|
|
|
|
// https://bitbucket.org/chromiumembedded/cef/wiki/Architecture.md#markdown-header-cef3
|
|
|
|
if (command_line->HasSwitch("enable-chrome-runtime")) {
|
2020-07-06 20:14:57 +02:00
|
|
|
settings.chrome_runtime = true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
// When generating projects with CMake the CEF_USE_SANDBOX value will be
|
|
|
|
// defined automatically. Pass -DUSE_SANDBOX=OFF to the CMake command-line
|
|
|
|
// to disable use of the sandbox.
|
2018-07-27 23:28:12 +02:00
|
|
|
#if !defined(CEF_USE_SANDBOX)
|
2019-04-23 19:17:56 +02:00
|
|
|
settings.no_sandbox = true;
|
2018-07-27 23:28:12 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
// SimpleApp implements application-level callbacks for the browser process.
|
|
|
|
// It will create the first browser instance in OnContextInitialized() after
|
|
|
|
// CEF has initialized.
|
|
|
|
CefRefPtr<SimpleApp> app(new SimpleApp);
|
|
|
|
|
2023-11-29 02:33:44 +01:00
|
|
|
// Initialize the CEF browser process. May return false if initialization
|
|
|
|
// fails or if early exit is desired (for example, due to process singleton
|
|
|
|
// relaunch behavior).
|
|
|
|
if (!CefInitialize(main_args, settings, app.get(), nullptr)) {
|
2024-03-12 20:47:10 +01:00
|
|
|
return CefGetExitCode();
|
2023-11-29 02:33:44 +01:00
|
|
|
}
|
2019-04-23 19:17:56 +02:00
|
|
|
|
|
|
|
// Create the application delegate.
|
2024-01-29 19:38:25 +01:00
|
|
|
SimpleAppDelegate* delegate = [[SimpleAppDelegate alloc] init];
|
|
|
|
// Set as the delegate for application events.
|
|
|
|
NSApp.delegate = delegate;
|
|
|
|
|
2019-04-23 19:17:56 +02:00
|
|
|
[delegate performSelectorOnMainThread:@selector(createApplication:)
|
|
|
|
withObject:nil
|
|
|
|
waitUntilDone:NO];
|
|
|
|
|
|
|
|
// Run the CEF message loop. This will block until CefQuitMessageLoop() is
|
|
|
|
// called.
|
|
|
|
CefRunMessageLoop();
|
|
|
|
|
|
|
|
// Shut down CEF.
|
|
|
|
CefShutdown();
|
|
|
|
|
|
|
|
// Release the delegate.
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
[delegate release];
|
|
|
|
#endif // !__has_feature(objc_arc)
|
|
|
|
delegate = nil;
|
|
|
|
} // @autoreleasepool
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|