2013-04-03 20:20:59 +02:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors.
|
2012-04-03 03:34:16 +02:00
|
|
|
// 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_app.h"
|
|
|
|
#import "include/cef_application_mac.h"
|
2015-01-31 05:41:36 +01:00
|
|
|
#include "cefclient/browser/client_app_browser.h"
|
2015-01-31 01:55:56 +01:00
|
|
|
#include "cefclient/browser/main_context_impl.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
#include "cefclient/browser/main_message_loop_external_pump.h"
|
2015-01-31 01:55:56 +01:00
|
|
|
#include "cefclient/browser/main_message_loop_std.h"
|
|
|
|
#include "cefclient/browser/resource.h"
|
|
|
|
#include "cefclient/browser/root_window.h"
|
|
|
|
#include "cefclient/browser/test_runner.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "cefclient/common/client_switches.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
|
|
|
|
NSMenuItem* item = [menu addItemWithTitle:label
|
|
|
|
action:@selector(menuItemSelected:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[item setTag:idval];
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
// Receives notifications from the application. Will delete itself when done.
|
2015-01-30 19:07:13 +01:00
|
|
|
@interface ClientAppDelegate : NSObject<NSApplicationDelegate> {
|
|
|
|
@private
|
2016-01-19 21:09:01 +01:00
|
|
|
bool with_controls_;
|
2015-01-30 19:07:13 +01:00
|
|
|
bool with_osr_;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
- (id)initWithControls:(bool)with_controls andOsr:(bool)with_osr;
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)createApplication:(id)object;
|
|
|
|
- (void)tryToTerminateApplication:(NSApplication*)app;
|
2015-01-22 21:21:21 +01:00
|
|
|
- (IBAction)menuItemSelected:(id)sender;
|
2014-04-09 20:29:51 +02:00
|
|
|
@end
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Provide the CefAppProtocol implementation required by CEF.
|
|
|
|
@interface ClientApplication : NSApplication<CefAppProtocol> {
|
2015-01-30 19:07:13 +01:00
|
|
|
@private
|
2012-04-03 03:34:16 +02:00
|
|
|
BOOL handlingSendEvent_;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ClientApplication
|
2015-01-30 19:07:13 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
- (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 {
|
2015-01-30 19:07:13 +01:00
|
|
|
ClientAppDelegate* delegate = static_cast<ClientAppDelegate*>(
|
|
|
|
[[NSApplication sharedApplication] delegate]);
|
2014-04-09 20:29:51 +02:00
|
|
|
[delegate tryToTerminateApplication:self];
|
|
|
|
// Return, don't exit. The application is responsible for exiting on its own.
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ClientAppDelegate
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
- (id)initWithControls:(bool)with_controls andOsr:(bool)with_osr {
|
2015-01-30 19:13:39 +01:00
|
|
|
if (self = [super init]) {
|
2016-01-19 21:09:01 +01:00
|
|
|
with_controls_ = with_controls;
|
2015-01-30 19:13:39 +01:00
|
|
|
with_osr_ = with_osr;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Create the application on the UI thread.
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)createApplication:(id)object {
|
2015-01-30 19:07:13 +01:00
|
|
|
NSApplication* application = [NSApplication sharedApplication];
|
2012-04-03 03:34:16 +02:00
|
|
|
[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Set the delegate for application events.
|
2015-01-30 19:07:13 +01:00
|
|
|
[application setDelegate:self];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Add the Tests menu.
|
2015-01-30 19:07:13 +01:00
|
|
|
NSMenu* menubar = [application mainMenu];
|
2012-04-03 03:34:16 +02:00
|
|
|
NSMenuItem *testItem = [[[NSMenuItem alloc] initWithTitle:@"Tests"
|
|
|
|
action:nil
|
|
|
|
keyEquivalent:@""] autorelease];
|
|
|
|
NSMenu *testMenu = [[[NSMenu alloc] initWithTitle:@"Tests"] autorelease];
|
2015-01-22 21:21:21 +01:00
|
|
|
AddMenuItem(testMenu, @"Get Text", ID_TESTS_GETSOURCE);
|
|
|
|
AddMenuItem(testMenu, @"Get Source", ID_TESTS_GETTEXT);
|
2015-03-02 21:25:14 +01:00
|
|
|
AddMenuItem(testMenu, @"New Window", ID_TESTS_WINDOW_NEW);
|
|
|
|
AddMenuItem(testMenu, @"Popup Window", ID_TESTS_WINDOW_POPUP);
|
2015-01-22 21:21:21 +01:00
|
|
|
AddMenuItem(testMenu, @"Request", ID_TESTS_REQUEST);
|
|
|
|
AddMenuItem(testMenu, @"Plugin Info", ID_TESTS_PLUGIN_INFO);
|
|
|
|
AddMenuItem(testMenu, @"Zoom In", ID_TESTS_ZOOM_IN);
|
|
|
|
AddMenuItem(testMenu, @"Zoom Out", ID_TESTS_ZOOM_OUT);
|
|
|
|
AddMenuItem(testMenu, @"Zoom Reset", ID_TESTS_ZOOM_RESET);
|
2015-05-13 17:43:50 +02:00
|
|
|
if (with_osr_) {
|
2015-08-10 22:49:45 +02:00
|
|
|
AddMenuItem(testMenu, @"Set FPS", ID_TESTS_OSR_FPS);
|
|
|
|
AddMenuItem(testMenu, @"Set Scale Factor", ID_TESTS_OSR_DSF);
|
2015-05-13 17:43:50 +02:00
|
|
|
}
|
2015-01-22 21:21:21 +01:00
|
|
|
AddMenuItem(testMenu, @"Begin Tracing", ID_TESTS_TRACING_BEGIN);
|
|
|
|
AddMenuItem(testMenu, @"End Tracing", ID_TESTS_TRACING_END);
|
|
|
|
AddMenuItem(testMenu, @"Print", ID_TESTS_PRINT);
|
2015-03-24 16:40:08 +01:00
|
|
|
AddMenuItem(testMenu, @"Print to PDF", ID_TESTS_PRINT_TO_PDF);
|
2015-01-22 21:21:21 +01:00
|
|
|
AddMenuItem(testMenu, @"Other Tests", ID_TESTS_OTHER_TESTS);
|
2012-04-03 03:34:16 +02:00
|
|
|
[testItem setSubmenu:testMenu];
|
|
|
|
[menubar addItem:testItem];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2015-01-30 19:07:13 +01:00
|
|
|
// Create the first window.
|
|
|
|
client::MainContext::Get()->GetRootWindowManager()->CreateRootWindow(
|
2016-01-19 21:09:01 +01:00
|
|
|
with_controls_, // Show controls.
|
2015-01-30 19:07:13 +01:00
|
|
|
with_osr_, // Use off-screen rendering.
|
|
|
|
CefRect(), // Use default system size.
|
|
|
|
std::string()); // Use default URL.
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)tryToTerminateApplication:(NSApplication*)app {
|
2015-01-30 19:07:13 +01:00
|
|
|
client::MainContext::Get()->GetRootWindowManager()->CloseAllWindows(false);
|
2014-04-09 20:29:51 +02:00
|
|
|
}
|
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
- (IBAction)menuItemSelected:(id)sender {
|
2015-01-30 19:07:13 +01:00
|
|
|
// Retrieve the active RootWindow.
|
|
|
|
NSWindow* key_window = [[NSApplication sharedApplication] keyWindow];
|
|
|
|
if (!key_window)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_refptr<client::RootWindow> root_window =
|
|
|
|
client::RootWindow::GetForNSWindow(key_window);
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowser> browser = root_window->GetBrowser();
|
|
|
|
if (browser.get()) {
|
|
|
|
NSMenuItem *item = (NSMenuItem*)sender;
|
|
|
|
client::test_runner::RunTest(browser, [item tag]);
|
|
|
|
}
|
2013-04-03 20:20:59 +02:00
|
|
|
}
|
|
|
|
|
2013-04-03 21:29:47 +02:00
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:
|
|
|
|
(NSApplication *)sender {
|
2014-04-09 20:29:51 +02:00
|
|
|
return NSTerminateNow;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-01-23 20:09:34 +01:00
|
|
|
namespace client {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
int RunMain(int argc, char* argv[]) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefMainArgs main_args(argc, argv);
|
|
|
|
|
|
|
|
// Initialize the AutoRelease pool.
|
2013-04-03 21:29:47 +02:00
|
|
|
NSAutoreleasePool* autopool = [[NSAutoreleasePool alloc] init];
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Initialize the ClientApplication instance.
|
|
|
|
[ClientApplication sharedApplication];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
// Parse command-line arguments.
|
|
|
|
CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
|
|
|
|
command_line->InitFromArgv(argc, argv);
|
|
|
|
|
|
|
|
// Create a ClientApp of the correct type.
|
|
|
|
CefRefPtr<CefApp> app;
|
|
|
|
ClientApp::ProcessType process_type = ClientApp::GetProcessType(command_line);
|
|
|
|
if (process_type == ClientApp::BrowserProcess)
|
|
|
|
app = new ClientAppBrowser();
|
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
// Create the main context object.
|
2015-01-31 05:41:36 +01:00
|
|
|
scoped_ptr<MainContextImpl> context(new MainContextImpl(command_line, true));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefSettings settings;
|
|
|
|
|
|
|
|
// Populate the settings based on command line arguments.
|
2015-01-23 00:11:30 +01:00
|
|
|
context->PopulateSettings(&settings);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
// Create the main message loop object.
|
2016-05-04 20:00:03 +02:00
|
|
|
scoped_ptr<MainMessageLoop> message_loop;
|
|
|
|
if (settings.external_message_pump)
|
|
|
|
message_loop = MainMessageLoopExternalPump::Create();
|
|
|
|
else
|
|
|
|
message_loop.reset(new MainMessageLoopStd);
|
2015-01-22 18:55:55 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Initialize CEF.
|
2015-01-31 05:41:36 +01:00
|
|
|
context->Initialize(main_args, settings, app, NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
// Register scheme handlers.
|
2015-01-23 20:09:34 +01:00
|
|
|
test_runner::RegisterSchemeHandlers();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create the application delegate and window.
|
2015-01-30 19:13:39 +01:00
|
|
|
ClientAppDelegate* delegate = [[ClientAppDelegate alloc]
|
2016-01-19 21:09:01 +01:00
|
|
|
initWithControls:!command_line->HasSwitch(switches::kHideControls)
|
|
|
|
andOsr:settings.windowless_rendering_enabled ? true : false];
|
2014-04-09 20:29:51 +02:00
|
|
|
[delegate performSelectorOnMainThread:@selector(createApplication:)
|
|
|
|
withObject:nil
|
2012-04-03 03:34:16 +02:00
|
|
|
waitUntilDone:NO];
|
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
// Run the message loop. This will block until Quit() is called.
|
|
|
|
int result = message_loop->Run();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-03-19 23:59:33 +01:00
|
|
|
// Shut down CEF.
|
2015-01-29 21:53:53 +01:00
|
|
|
context->Shutdown();
|
2013-03-19 23:59:33 +01:00
|
|
|
|
2015-01-23 00:11:30 +01:00
|
|
|
// Release objects in reverse order of creation.
|
2013-04-03 21:29:47 +02:00
|
|
|
[delegate release];
|
2015-01-22 18:55:55 +01:00
|
|
|
message_loop.reset();
|
2015-01-23 00:11:30 +01:00
|
|
|
context.reset();
|
|
|
|
[autopool release];
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
return result;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2015-01-23 20:09:34 +01:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace client
|
|
|
|
|
|
|
|
|
|
|
|
// Program entry point function.
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
return client::RunMain(argc, argv);
|
|
|
|
}
|