Rippin' and tearin'

This commit is contained in:
Corey Johnson
2012-08-20 15:25:17 -07:00
parent 9c3b0c1b4c
commit b68937fb2a
6 changed files with 206 additions and 1160 deletions

View File

@@ -183,11 +183,13 @@
],
},
'sources': [
'tests/cefclient/main_mac.mm',
'tests/cefclient/cefclient_mac.h',
'tests/cefclient/cefclient_mac.mm',
'tests/cefclient/client_handler_mac.mm',
'include/cef_application_mac.h',
'include/internal/cef_mac.h',
'include/internal/cef_types_mac.h',
'tests/cefclient/cefclient_mac.mm',
'tests/cefclient/client_handler_mac.mm',
],
}],
[ 'OS=="linux" or OS=="freebsd" or OS=="openbsd"', {
@@ -266,15 +268,15 @@
],
},
'sources': [
'tests/cefclient/client_handler_mac.mm',
'tests/cefclient/process_helper_mac.cpp',
'tests/cefclient/client_app.cpp',
'tests/cefclient/client_app.h',
'tests/cefclient/client_app_delegates.cpp',
'tests/cefclient/client_handler.cpp',
'tests/cefclient/client_handler.h',
'tests/cefclient/client_handler_mac.mm',
'tests/cefclient/client_renderer.cpp',
'tests/cefclient/client_renderer.h',
'tests/cefclient/process_helper_mac.cpp',
'tests/cefclient/util.h',
],
# TODO(mark): Come up with a fancier way to do this. It should only

View File

@@ -0,0 +1,26 @@
#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"
#include "include/cef_runnable.h"
#include "cefclient/client_handler.h"
@interface ClientApplication : NSApplication <CefAppProtocol, NSWindowDelegate> {
@private
BOOL handlingSendEvent_;
}
- (void)createWindow;
- (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;
- (void)createApp:(id)object;
- (void)populateBrowserSettings:(CefBrowserSettings &)settings;
@end

View File

@@ -1,8 +1,3 @@
// Copyright (c) 2010 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 <sstream>
#include "cefclient/cefclient.h"
@@ -12,11 +7,18 @@
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "cefclient/client_handler.h"
#include "cefclient/cefclient_mac.h"
// The global ClientHandler reference.
extern CefRefPtr<ClientHandler> g_handler;
char szWorkingDir[512]; // The current working directory
char szWorkingDir[512] = {0}; // The current working directory
// Global functions
std::string AppGetWorkingDirectory() {
if (!szWorkingDir[0]) getcwd(szWorkingDir, sizeof(szWorkingDir));
return szWorkingDir;
}
// Sizes for URL bar layout
#define BUTTON_HEIGHT 22
@@ -28,17 +30,70 @@ char szWorkingDir[512]; // The current working directory
const int kWindowWidth = 800;
const int kWindowHeight = 600;
// Memory AutoRelease pool.
static NSAutoreleasePool* g_autopool = nil;
// Provide the CefAppProtocol implementation required by CEF.
@interface ClientApplication : NSApplication<CefAppProtocol> {
@private
BOOL handlingSendEvent_;
}
@end
@implementation ClientApplication
+ (id)sharedApplication {
id atomApp = [super sharedApplication];
CefSettings settings;
CefMainArgs mainArgs(0, NULL);
CefRefPtr<ClientApp> app(new ClientApp);
CefInitialize(mainArgs, settings, app.get());
return atomApp;
}
// Create the application on the UI thread.
- (void)createWindow {
// Create the main application window.
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame];
NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight},
{kWindowWidth, kWindowHeight} };
NSWindow* mainWnd = [[UnderlayOpenGLHostingWindow alloc]
initWithContentRect:window_rect
styleMask:(NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask )
backing:NSBackingStoreBuffered
defer:NO];
[mainWnd setTitle:@"cefclient"];
[mainWnd setDelegate:self];
// 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 handler.
g_handler = new ClientHandler();
g_handler->SetMainHwnd(contentView);
// Create the browser view.
CefWindowInfo window_info;
CefBrowserSettings settings;
[self populateBrowserSettings:settings];
window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight);
CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
g_handler->GetStartupURL(), settings);
// 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];
}
- (BOOL)isHandlingSendEvent {
return handlingSendEvent_;
}
@@ -51,24 +106,6 @@ static NSAutoreleasePool* g_autopool = nil;
CefScopedSendingEvent sendingEventScoper;
[super sendEvent:event];
}
@end
// Receives notifications from controls and the browser window. Will delete
// itself when done.
@interface ClientWindowDelegate : NSObject <NSWindowDelegate>
- (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;
- (void)notifyConsoleMessage:(id)object;
- (void)notifyDownloadComplete:(id)object;
- (void)notifyDownloadError:(id)object;
@end
@implementation ClientWindowDelegate
- (IBAction)goBack:(id)sender {
if (g_handler.get() && g_handler->GetBrowserId())
@@ -143,122 +180,6 @@ static NSAutoreleasePool* g_autopool = nil;
[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;
}
// Receives notifications from the application. Will delete itself when done.
@interface ClientAppDelegate : NSObject
- (void)createApp:(id)object;
- (void)populateBrowserSettings:(CefBrowserSettings &)settings;
@end
@implementation ClientAppDelegate
// Create the application on the UI thread.
- (void)createApp:(id)object {
[NSApplication sharedApplication];
[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];
// Set the delegate for application events.
[NSApp setDelegate:self];
// Create the delegate for control and browser window events.
ClientWindowDelegate* delegate = [[ClientWindowDelegate alloc] init];
// Create the main application window.
NSRect screen_rect = [[NSScreen mainScreen] visibleFrame];
NSRect window_rect = { {0, screen_rect.size.height - kWindowHeight},
{kWindowWidth, kWindowHeight} };
NSWindow* mainWnd = [[UnderlayOpenGLHostingWindow alloc]
initWithContentRect:window_rect
styleMask:(NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask )
backing:NSBackingStoreBuffered
defer:NO];
[mainWnd setTitle:@"cefclient"];
[mainWnd setDelegate:delegate];
// 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();
g_handler->SetMainHwnd(contentView);
g_handler->SetEditHwnd(editWnd);
// Create the browser view.
CefWindowInfo window_info;
CefBrowserSettings settings;
[self populateBrowserSettings:settings];
window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight);
CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
g_handler->GetStartupURL(), settings);
// 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];
}
- (void)populateBrowserSettings:(CefBrowserSettings &)settings {
CefString(&settings.default_encoding) = "UTF-8";
settings.remote_fonts_disabled = true;
@@ -305,55 +226,11 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) {
// Shut down CEF.
g_handler = NULL;
CefShutdown();
[self release];
// Release the AutoRelease pool.
[g_autopool release];
}
@end
int main(int argc, char* argv[]) {
CefMainArgs main_args(argc, argv);
CefRefPtr<ClientApp> app(new ClientApp);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, app.get());
if (exit_code >= 0)
return exit_code;
// Retrieve the current working directory.
getcwd(szWorkingDir, sizeof(szWorkingDir));
// Initialize the AutoRelease pool.
g_autopool = [[NSAutoreleasePool alloc] init];
// Initialize the ClientApplication instance.
[ClientApplication sharedApplication];
CefSettings settings;
// Initialize CEF.
CefInitialize(main_args, settings, app.get());
// Create the application delegate and window.
NSObject* delegate = [[ClientAppDelegate alloc] init];
[delegate performSelectorOnMainThread:@selector(createApp:) withObject:nil
waitUntilDone:NO];
// Run the application message loop.
CefRunMessageLoop();
// Don't put anything below this line because it won't be executed.
return 0;
}
// Global functions
std::string AppGetWorkingDirectory() {
return szWorkingDir;
}

File diff suppressed because it is too large Load Diff

View File

@@ -23,6 +23,6 @@
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<string>ClientApplicatoin</string>
</dict>
</plist>

View File

@@ -0,0 +1,38 @@
#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"
#include "include/cef_runnable.h"
#include "cefclient/client_handler.h"
#include "cefclient/cefclient_mac.h"
int main(int argc, char* argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CefMainArgs mainArgs(argc, argv);
CefRefPtr<ClientApp> app(new ClientApp);
int exit_code = CefExecuteProcess(mainArgs, app.get());
if (exit_code >= 0){
return exit_code;
}
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
ClientApplication *application = (ClientApplication *)[ClientApplication sharedApplication];
NSString *mainNibName = [infoDictionary objectForKey:@"NSMainNibFile"];
NSNib *mainNib = [[NSNib alloc] initWithNibNamed:mainNibName bundle:[NSBundle mainBundle]];
[mainNib instantiateNibWithOwner:application topLevelObjects:nil]; // Execute the secondary process, if any.
[application createWindow];
// Run the application message loop.
CefRunMessageLoop();
[pool release];
// Don't put anything below this line because it won't be executed.
return 0;
}