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 <sstream>
|
|
|
|
#include "cefclient/cefclient.h"
|
|
|
|
#include "include/cef_app.h"
|
|
|
|
#import "include/cef_application_mac.h"
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_frame.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
#include "cefclient/cefclient_osr_widget_mac.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "cefclient/client_handler.h"
|
2013-04-16 21:23:00 +02:00
|
|
|
#include "cefclient/client_switches.h"
|
2015-01-22 18:55:55 +01:00
|
|
|
#include "cefclient/main_message_loop_std.h"
|
2015-01-22 21:21:21 +01:00
|
|
|
#include "cefclient/resource.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "cefclient/resource_util.h"
|
2015-01-22 21:21:21 +01:00
|
|
|
#include "cefclient/test_runner.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// The global ClientHandler reference.
|
|
|
|
extern CefRefPtr<ClientHandler> g_handler;
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
class MainBrowserProvider : public OSRBrowserProvider {
|
|
|
|
virtual CefRefPtr<CefBrowser> GetBrowser() {
|
|
|
|
if (g_handler.get())
|
|
|
|
return g_handler->GetBrowser();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} g_main_browser_provider;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Sizes for URL bar layout
|
|
|
|
#define BUTTON_HEIGHT 22
|
|
|
|
#define BUTTON_WIDTH 72
|
|
|
|
#define BUTTON_MARGIN 8
|
|
|
|
#define URLBAR_HEIGHT 32
|
|
|
|
|
|
|
|
// Content area size for newly created windows.
|
|
|
|
const int kWindowWidth = 800;
|
|
|
|
const int kWindowHeight = 600;
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
// Receives notifications from the application. Will delete itself when done.
|
|
|
|
@interface ClientAppDelegate : NSObject
|
|
|
|
- (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> {
|
|
|
|
@private
|
|
|
|
BOOL handlingSendEvent_;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ClientApplication
|
|
|
|
- (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 {
|
|
|
|
ClientAppDelegate* delegate =
|
|
|
|
static_cast<ClientAppDelegate*>([NSApp delegate]);
|
|
|
|
[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
|
|
|
|
|
|
|
|
|
|
|
|
// Receives notifications from controls and the browser window. Will delete
|
|
|
|
// itself when done.
|
2014-09-10 23:32:30 +02:00
|
|
|
@interface ClientWindowDelegate : NSObject <NSWindowDelegate> {
|
|
|
|
@private
|
|
|
|
NSWindow* window_;
|
|
|
|
}
|
|
|
|
- (id)initWithWindow:(NSWindow*)window;
|
2012-04-03 03:34:16 +02:00
|
|
|
- (IBAction)goBack:(id)sender;
|
|
|
|
- (IBAction)goForward:(id)sender;
|
|
|
|
- (IBAction)reload:(id)sender;
|
|
|
|
- (IBAction)stopLoading:(id)sender;
|
|
|
|
- (IBAction)takeURLStringValueFrom:(NSTextField *)sender;
|
|
|
|
- (void)alert:(NSString*)title withMessage:(NSString*)message;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation ClientWindowDelegate
|
|
|
|
|
2014-09-10 23:32:30 +02:00
|
|
|
- (id)initWithWindow:(NSWindow*)window {
|
|
|
|
if (self = [super init]) {
|
|
|
|
window_ = window;
|
|
|
|
[window_ setDelegate:self];
|
|
|
|
|
|
|
|
// Register for application hide/unhide notifications.
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(applicationDidHide:)
|
|
|
|
name:NSApplicationDidHideNotification
|
|
|
|
object:nil];
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(applicationDidUnhide:)
|
|
|
|
name:NSApplicationDidUnhideNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
- (IBAction)goBack:(id)sender {
|
|
|
|
if (g_handler.get() && g_handler->GetBrowserId())
|
|
|
|
g_handler->GetBrowser()->GoBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)goForward:(id)sender {
|
|
|
|
if (g_handler.get() && g_handler->GetBrowserId())
|
|
|
|
g_handler->GetBrowser()->GoForward();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)reload:(id)sender {
|
|
|
|
if (g_handler.get() && g_handler->GetBrowserId())
|
|
|
|
g_handler->GetBrowser()->Reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)stopLoading:(id)sender {
|
|
|
|
if (g_handler.get() && g_handler->GetBrowserId())
|
|
|
|
g_handler->GetBrowser()->StopLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)takeURLStringValueFrom:(NSTextField *)sender {
|
|
|
|
if (!g_handler.get() || !g_handler->GetBrowserId())
|
|
|
|
return;
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
NSString *url = [sender stringValue];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// if it doesn't already have a prefix, add http. If we can't parse it,
|
|
|
|
// just don't bother rather than making things worse.
|
|
|
|
NSURL* tempUrl = [NSURL URLWithString:url];
|
|
|
|
if (tempUrl && ![tempUrl scheme])
|
|
|
|
url = [@"http://" stringByAppendingString:url];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
std::string urlStr = [url UTF8String];
|
|
|
|
g_handler->GetBrowser()->GetMainFrame()->LoadURL(urlStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)alert:(NSString*)title withMessage:(NSString*)message {
|
|
|
|
NSAlert *alert = [NSAlert alertWithMessageText:title
|
|
|
|
defaultButton:@"OK"
|
|
|
|
alternateButton:nil
|
|
|
|
otherButton:nil
|
2013-10-14 21:29:39 +02:00
|
|
|
informativeTextWithFormat:@"%@", message];
|
2012-04-03 03:34:16 +02:00
|
|
|
[alert runModal];
|
|
|
|
}
|
|
|
|
|
2014-06-06 21:04:21 +02:00
|
|
|
// Called when we are activated (when we gain focus).
|
2012-04-03 03:34:16 +02:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification*)notification {
|
2014-06-06 21:04:21 +02:00
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
2014-07-01 00:30:29 +02:00
|
|
|
if (browser.get()) {
|
|
|
|
if (AppIsOffScreenRenderingEnabled())
|
|
|
|
browser->GetHost()->SendFocusEvent(true);
|
|
|
|
else
|
|
|
|
browser->GetHost()->SetFocus(true);
|
|
|
|
}
|
2014-06-06 21:04:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when we are deactivated (when we lose focus).
|
|
|
|
- (void)windowDidResignKey:(NSNotification*)notification {
|
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
2014-07-01 00:30:29 +02:00
|
|
|
if (browser.get()) {
|
|
|
|
if (AppIsOffScreenRenderingEnabled())
|
|
|
|
browser->GetHost()->SendFocusEvent(false);
|
|
|
|
else
|
|
|
|
browser->GetHost()->SetFocus(false);
|
|
|
|
}
|
2014-06-06 21:04:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when we have been minimized.
|
|
|
|
- (void)windowDidMiniaturize:(NSNotification *)notification {
|
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
|
|
|
if (browser.get())
|
|
|
|
browser->GetHost()->SetWindowVisibility(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when we have been unminimized.
|
|
|
|
- (void)windowDidDeminiaturize:(NSNotification *)notification {
|
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
|
|
|
if (browser.get())
|
|
|
|
browser->GetHost()->SetWindowVisibility(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the application has been hidden.
|
|
|
|
- (void)applicationDidHide:(NSNotification *)notification {
|
|
|
|
// If the window is miniaturized then nothing has really changed.
|
2014-09-10 23:32:30 +02:00
|
|
|
if (![window_ isMiniaturized]) {
|
2014-06-06 21:04:21 +02:00
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
|
|
|
if (browser.get())
|
|
|
|
browser->GetHost()->SetWindowVisibility(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the application has been unhidden.
|
|
|
|
- (void)applicationDidUnhide:(NSNotification *)notification {
|
|
|
|
// If the window is miniaturized then nothing has really changed.
|
2014-09-10 23:32:30 +02:00
|
|
|
if (![window_ isMiniaturized]) {
|
2014-06-06 21:04:21 +02:00
|
|
|
if (g_handler.get()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
|
|
|
if (browser.get())
|
|
|
|
browser->GetHost()->SetWindowVisibility(true);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the window is about to close. Perform the self-destruction
|
|
|
|
// sequence by getting rid of the window. By returning YES, we allow the window
|
|
|
|
// to be removed from the screen.
|
2013-03-19 23:59:33 +01:00
|
|
|
- (BOOL)windowShouldClose:(id)window {
|
|
|
|
if (g_handler.get() && !g_handler->IsClosing()) {
|
|
|
|
CefRefPtr<CefBrowser> browser = g_handler->GetBrowser();
|
|
|
|
if (browser.get()) {
|
|
|
|
// Notify the browser window that we would like to close it. This
|
|
|
|
// will result in a call to ClientHandler::DoClose() if the
|
|
|
|
// JavaScript 'onbeforeunload' event handler allows it.
|
|
|
|
browser->GetHost()->CloseBrowser(false);
|
|
|
|
|
|
|
|
// Cancel the close.
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Try to make the window go away.
|
|
|
|
[window autorelease];
|
2013-03-19 23:59:33 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Clean ourselves up after clearing the stack of anything that might have the
|
|
|
|
// window on it.
|
|
|
|
[self performSelectorOnMainThread:@selector(cleanup:)
|
|
|
|
withObject:window
|
|
|
|
waitUntilDone:NO];
|
2013-03-19 23:59:33 +01:00
|
|
|
|
|
|
|
// Allow the close.
|
2012-04-03 03:34:16 +02:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes itself.
|
2013-04-16 21:23:00 +02:00
|
|
|
- (void)cleanup:(id)window {
|
2012-04-03 03:34:16 +02:00
|
|
|
[self release];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) {
|
|
|
|
NSButton* button = [[[NSButton alloc] initWithFrame:*rect] autorelease];
|
|
|
|
[button setTitle:title];
|
|
|
|
[button setBezelStyle:NSSmallSquareBezelStyle];
|
|
|
|
[button setAutoresizingMask:(NSViewMaxXMargin | NSViewMinYMargin)];
|
|
|
|
[parent addSubview:button];
|
|
|
|
rect->origin.x += BUTTON_WIDTH;
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
|
|
|
|
NSMenuItem* item = [menu addItemWithTitle:label
|
|
|
|
action:@selector(menuItemSelected:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[item setTag:idval];
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
@implementation ClientAppDelegate
|
|
|
|
|
|
|
|
// Create the application on the UI thread.
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)createApplication:(id)object {
|
2012-04-03 03:34:16 +02:00
|
|
|
[NSApplication sharedApplication];
|
|
|
|
[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.
|
|
|
|
[NSApp setDelegate:self];
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Add the Tests menu.
|
|
|
|
NSMenu* menubar = [NSApp mainMenu];
|
|
|
|
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);
|
|
|
|
AddMenuItem(testMenu, @"Popup Window", ID_TESTS_POPUP);
|
|
|
|
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);
|
|
|
|
AddMenuItem(testMenu, @"Begin Tracing", ID_TESTS_TRACING_BEGIN);
|
|
|
|
AddMenuItem(testMenu, @"End Tracing", ID_TESTS_TRACING_END);
|
|
|
|
AddMenuItem(testMenu, @"Print", ID_TESTS_PRINT);
|
|
|
|
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
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Create the main application window.
|
|
|
|
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame];
|
|
|
|
NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight},
|
|
|
|
{kWindowWidth, kWindowHeight} };
|
2012-04-24 20:01:48 +02:00
|
|
|
NSWindow* mainWnd = [[UnderlayOpenGLHostingWindow alloc]
|
2012-04-03 03:34:16 +02:00
|
|
|
initWithContentRect:window_rect
|
|
|
|
styleMask:(NSTitledWindowMask |
|
|
|
|
NSClosableWindowMask |
|
|
|
|
NSMiniaturizableWindowMask |
|
|
|
|
NSResizableWindowMask )
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
|
|
|
[mainWnd setTitle:@"cefclient"];
|
2014-09-10 23:32:30 +02:00
|
|
|
|
|
|
|
// Create the delegate for control and browser window events.
|
|
|
|
ClientWindowDelegate* delegate =
|
|
|
|
[[ClientWindowDelegate alloc] initWithWindow:mainWnd];
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Rely on the window delegate to clean us up rather than immediately
|
|
|
|
// releasing when the window gets closed. We use the delegate to do
|
|
|
|
// everything from the autorelease pool so the window isn't on the stack
|
|
|
|
// during cleanup (ie, a window close from javascript).
|
|
|
|
[mainWnd setReleasedWhenClosed:NO];
|
|
|
|
|
|
|
|
NSView* contentView = [mainWnd contentView];
|
|
|
|
|
|
|
|
// Create the buttons.
|
|
|
|
NSRect button_rect = [contentView bounds];
|
|
|
|
button_rect.origin.y = window_rect.size.height - URLBAR_HEIGHT +
|
|
|
|
(URLBAR_HEIGHT - BUTTON_HEIGHT) / 2;
|
|
|
|
button_rect.size.height = BUTTON_HEIGHT;
|
|
|
|
button_rect.origin.x += BUTTON_MARGIN;
|
|
|
|
button_rect.size.width = BUTTON_WIDTH;
|
|
|
|
|
|
|
|
NSButton* button = MakeButton(&button_rect, @"Back", contentView);
|
|
|
|
[button setTarget:delegate];
|
|
|
|
[button setAction:@selector(goBack:)];
|
|
|
|
|
|
|
|
button = MakeButton(&button_rect, @"Forward", contentView);
|
|
|
|
[button setTarget:delegate];
|
|
|
|
[button setAction:@selector(goForward:)];
|
|
|
|
|
|
|
|
button = MakeButton(&button_rect, @"Reload", contentView);
|
|
|
|
[button setTarget:delegate];
|
|
|
|
[button setAction:@selector(reload:)];
|
|
|
|
|
|
|
|
button = MakeButton(&button_rect, @"Stop", contentView);
|
|
|
|
[button setTarget:delegate];
|
|
|
|
[button setAction:@selector(stopLoading:)];
|
|
|
|
|
|
|
|
// Create the URL text field.
|
|
|
|
button_rect.origin.x += BUTTON_MARGIN;
|
|
|
|
button_rect.size.width = [contentView bounds].size.width -
|
|
|
|
button_rect.origin.x - BUTTON_MARGIN;
|
|
|
|
NSTextField* editWnd = [[NSTextField alloc] initWithFrame:button_rect];
|
|
|
|
[contentView addSubview:editWnd];
|
|
|
|
[editWnd setAutoresizingMask:(NSViewWidthSizable | NSViewMinYMargin)];
|
|
|
|
[editWnd setTarget:delegate];
|
|
|
|
[editWnd setAction:@selector(takeURLStringValueFrom:)];
|
|
|
|
[[editWnd cell] setWraps:NO];
|
|
|
|
[[editWnd cell] setScrollable:YES];
|
|
|
|
|
|
|
|
// Create the handler.
|
|
|
|
g_handler = new ClientHandler();
|
2014-07-11 22:28:37 +02:00
|
|
|
g_handler->SetMainWindowHandle(contentView);
|
|
|
|
g_handler->SetEditWindowHandle(editWnd);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create the browser view.
|
|
|
|
CefWindowInfo window_info;
|
|
|
|
CefBrowserSettings settings;
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
// Populate the browser settings based on command line arguments.
|
|
|
|
AppGetBrowserSettings(settings);
|
|
|
|
|
|
|
|
if (AppIsOffScreenRenderingEnabled()) {
|
|
|
|
CefRefPtr<CefCommandLine> cmd_line = AppGetCommandLine();
|
2014-12-02 12:23:57 +01:00
|
|
|
const bool transparent =
|
2014-07-01 00:30:29 +02:00
|
|
|
cmd_line->HasSwitch(cefclient::kTransparentPaintingEnabled);
|
2014-12-02 12:23:57 +01:00
|
|
|
const bool show_update_rect =
|
|
|
|
cmd_line->HasSwitch(cefclient::kShowUpdateRect);
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
CefRefPtr<OSRWindow> osr_window =
|
2014-12-02 12:23:57 +01:00
|
|
|
OSRWindow::Create(&g_main_browser_provider, transparent,
|
|
|
|
show_update_rect, contentView,
|
2014-07-01 00:30:29 +02:00
|
|
|
CefRect(0, 0, kWindowWidth, kWindowHeight));
|
|
|
|
window_info.SetAsWindowless(osr_window->GetWindowHandle(), transparent);
|
|
|
|
g_handler->SetOSRHandler(osr_window->GetRenderHandler().get());
|
|
|
|
} else {
|
|
|
|
// Initialize window info to the defaults for a child window.
|
|
|
|
window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight);
|
|
|
|
}
|
2013-04-16 21:23:00 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
|
2013-09-03 18:43:31 +02:00
|
|
|
g_handler->GetStartupURL(), settings, NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Show the window.
|
|
|
|
[mainWnd makeKeyAndOrderFront: nil];
|
|
|
|
|
|
|
|
// Size the window.
|
|
|
|
NSRect r = [mainWnd contentRectForFrameRect:[mainWnd frame]];
|
|
|
|
r.size.width = kWindowWidth;
|
|
|
|
r.size.height = kWindowHeight + URLBAR_HEIGHT;
|
|
|
|
[mainWnd setFrame:[mainWnd frameRectForContentRect:r] display:YES];
|
|
|
|
}
|
|
|
|
|
2014-04-09 20:29:51 +02:00
|
|
|
- (void)tryToTerminateApplication:(NSApplication*)app {
|
|
|
|
if (g_handler.get() && !g_handler->IsClosing())
|
|
|
|
g_handler->CloseAllBrowsers(false);
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
- (IBAction)menuItemSelected:(id)sender {
|
|
|
|
NSMenuItem *item = (NSMenuItem*)sender;
|
2012-04-27 21:12:01 +02:00
|
|
|
if (g_handler.get() && g_handler->GetBrowserId())
|
2015-01-22 21:21:21 +01:00
|
|
|
client::test_runner::RunTest(g_handler->GetBrowser(), [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
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
CefMainArgs main_args(argc, argv);
|
2012-04-12 22:21:50 +02:00
|
|
|
CefRefPtr<ClientApp> app(new ClientApp);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Execute the secondary process, if any.
|
2013-11-15 19:47:02 +01:00
|
|
|
int exit_code = CefExecuteProcess(main_args, app.get(), NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (exit_code >= 0)
|
|
|
|
return exit_code;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Parse command line arguments.
|
|
|
|
AppInitCommandLine(argc, argv);
|
|
|
|
|
|
|
|
CefSettings settings;
|
|
|
|
|
|
|
|
// Populate the settings based on command line arguments.
|
2013-02-07 20:59:40 +01:00
|
|
|
AppGetSettings(settings);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
// Create the main message loop object.
|
|
|
|
scoped_ptr<client::MainMessageLoop> message_loop(
|
|
|
|
new client::MainMessageLoopStd);
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Initialize CEF.
|
2013-11-15 19:47:02 +01:00
|
|
|
CefInitialize(main_args, settings, app.get(), NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
// Register scheme handlers.
|
|
|
|
client::test_runner::RegisterSchemeHandlers();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create the application delegate and window.
|
|
|
|
NSObject* delegate = [[ClientAppDelegate alloc] init];
|
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.
|
|
|
|
CefShutdown();
|
|
|
|
|
2013-04-03 21:29:47 +02:00
|
|
|
// Release the handler.
|
|
|
|
g_handler = NULL;
|
|
|
|
|
|
|
|
// Release the delegate.
|
|
|
|
[delegate release];
|
|
|
|
|
|
|
|
// Release the AutoRelease pool.
|
|
|
|
[autopool release];
|
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
// Release the |message_loop| object.
|
|
|
|
message_loop.reset();
|
|
|
|
|
|
|
|
return result;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Global functions
|
|
|
|
|
|
|
|
std::string AppGetWorkingDirectory() {
|
2015-01-22 22:39:36 +01:00
|
|
|
char szWorkingDir[256];
|
|
|
|
if (getcwd(szWorkingDir, sizeof(szWorkingDir) - 1) == NULL) {
|
|
|
|
szWorkingDir[0] = 0;
|
|
|
|
} else {
|
|
|
|
// Add trailing path separator.
|
|
|
|
size_t len = strlen(szWorkingDir);
|
|
|
|
szWorkingDir[len] = '/';
|
|
|
|
szWorkingDir[len + 1] = 0;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
return szWorkingDir;
|
|
|
|
}
|
2013-03-19 23:59:33 +01:00
|
|
|
|
2015-01-22 21:21:21 +01:00
|
|
|
std::string AppGetDownloadPath(const std::string& file_name) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2013-03-19 23:59:33 +01:00
|
|
|
void AppQuitMessageLoop() {
|
2015-01-22 18:55:55 +01:00
|
|
|
client::MainMessageLoop::Get()->Quit();
|
2013-03-19 23:59:33 +01:00
|
|
|
}
|