trying to get devtools to work

This commit is contained in:
Corey Johnson 2012-08-22 15:29:39 -07:00
parent 1f2453e98c
commit e15897afec
8 changed files with 139 additions and 45 deletions

View File

@ -15,7 +15,8 @@ class ClientHandler;
@property (nonatomic, retain) IBOutlet NSView *webView;
- (void)populateBrowserSettings:(CefBrowserSettings &)settings;
- (id)initWithPath:(NSString *)path atomContext:(CefRefPtr<CefV8Context>)atomContext;
- (id)initSpecsWithAtomContext:(CefRefPtr<CefV8Context>)atomContext;
- (id)initBenchmarksWithAtomContext:(CefRefPtr<CefV8Context>)atomContext;
@end

View File

@ -86,7 +86,7 @@
settings.remote_fonts_disabled = true;
settings.encoding_detector_enabled = false;
settings.javascript_disabled = false;
settings.javascript_open_windows_disallowed = true;
settings.javascript_open_windows_disallowed = false;
settings.javascript_close_windows_disallowed = false;
settings.javascript_access_clipboard_disallowed = false;
settings.dom_paste_disabled = true;

View File

@ -2,10 +2,16 @@
@interface Atom : NSApplication <CefAppProtocol, NSApplicationDelegate> {
@private
NSWindow *_hiddenWindow;
CefRefPtr<ClientHandler> _clientHandler;
BOOL handlingSendEvent_;
}
+ (void)populateAppSettings:(CefSettings &)settings;
- (void)createWindow;
- (void)open:(NSString *)path;
- (IBAction)runSpecs:(id)sender;
- (IBAction)runBenchmarks:(id)sender;
@end

View File

@ -29,17 +29,102 @@
}
// Create the application on the UI thread.
- (void)createWindow {
AtomController *controller = [[AtomController alloc] initWithPath:@"" atomContext:NULL];
- (void)dealloc {
[_hiddenWindow release];
[super dealloc];
}
- (void)createAtomContext {
_clientHandler = new ClientHandler();
CefWindowInfo window_info;
_hiddenWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 0, 0) styleMask:nil backing:nil defer:YES];
window_info.SetAsChild([_hiddenWindow contentView], 0, 0, 0, 0);
CefBrowserSettings settings;
NSURL *resourceDirURL = [[NSBundle mainBundle] resourceURL];
NSString *indexURLString = [[resourceDirURL URLByAppendingPathComponent:@"index.html"] absoluteString];
CefBrowserHost::CreateBrowser(window_info, _clientHandler.get(), [indexURLString UTF8String], settings);
}
- (void)open:(NSString *)path {
[[AtomController alloc] initWithPath:path atomContext:[self atomContext]];
}
- (IBAction)runSpecs:(id)sender {
[[AtomController alloc] initSpecsWithAtomContext:[self atomContext]];
}
- (IBAction)runBenchmarks:(id)sender {
[[AtomController alloc] initBenchmarksWithAtomContext:[self atomContext]];
}
- (void)modifyJavaScript:(void(^)(CefRefPtr<CefV8Context>, CefRefPtr<CefV8Value>))callback {
CefRefPtr<CefV8Context> context = _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context();
CefRefPtr<CefV8Value> global = context->GetGlobal();
context->Enter();
callback(context, global);
context->Exit();
}
- (CefRefPtr<CefV8Context>)atomContext {
return _clientHandler->GetBrowser()->GetMainFrame()->GetV8Context();
}
#pragma mark BrowserDelegate
- (void)loadStart {
[self modifyJavaScript:^(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> global) {
CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString("atom-bootstrap");
global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> atom = CefV8Value::CreateObject(NULL);
global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE);
#ifdef LOAD_RESOURCES_FROM_DIR
char path[] = LOAD_RESOURCES_FROM_DIR;
#else
const char *path = [[[NSBundle mainBundle] resourcePath] UTF8String];
#endif
CefRefPtr<CefV8Value> loadPath = CefV8Value::CreateString(path);
atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE);
}];
}
- (void)loadEnd {
if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--headless"]) {
[self modifyJavaScript:^(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> global) {
CefRefPtr<CefV8Value> atom = context->GetGlobal()->GetValue("atom");
atom->SetValue("headless", CefV8Value::CreateBool(YES), V8_PROPERTY_ATTRIBUTE_NONE);
}];
}
if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--benchmark"]) {
[self runBenchmarks:self];
}
if ([[[NSProcessInfo processInfo] arguments] containsObject:@"--test"]) {
[self runSpecs:self];
}
}
# pragma mark NSApplicationDelegate
// Sent by the default notification center immediately before the application terminates.
- (void)applicationWillTerminate:(NSNotification *)notification {
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
// new NativeHandler();
// new OnigRegexpExtension();
[self createAtomContext];
}
- (void)applicationWillTerminate:(NSNotification *)notification {
CefShutdown();
[self release];
}
# pragma mark CefAppProtocol
@ -54,7 +139,15 @@
- (void)sendEvent:(NSEvent*)event {
CefScopedSendingEvent sendingEventScoper;
[super sendEvent:event];
if ([[self mainMenu] performKeyEquivalent:event]) return;
if (_clientHandler && ![self keyWindow] && [event type] == NSKeyDown) {
[_hiddenWindow makeKeyAndOrderFront:self];
[_hiddenWindow sendEvent:event];
}
else {
[super sendEvent:event];
}
}
@end

View File

@ -9,8 +9,8 @@
#include "include/cef_runnable.h"
#include "atom/client_handler.h"
ClientHandler::ClientHandler()
: m_MainHwnd(NULL) {
ClientHandler::ClientHandler(){
}
ClientHandler::~ClientHandler() {
@ -24,11 +24,6 @@ void ClientHandler::OnBeforeContextMenu(
model->AddItem(MENU_ID_USER_FIRST, "&Show DevTools");
CefString devtools_url = browser->GetHost()->GetDevToolsURL(true);
// Disable the menu option if DevTools isn't enabled or if a window already open for the current URL.
if (devtools_url.empty() || m_OpenDevToolsURLs.find(devtools_url) != m_OpenDevToolsURLs.end()) {
model->SetEnabled(MENU_ID_USER_FIRST, false);
}
}
bool ClientHandler::OnContextMenuCommand(
@ -39,7 +34,7 @@ bool ClientHandler::OnContextMenuCommand(
EventFlags event_flags) {
if (command_id == MENU_ID_USER_FIRST) {
printf("show dev tools stub\n");
ShowDevTools(browser);
return true;
}
else {
@ -60,13 +55,6 @@ bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
if (browser->IsPopup()) {
// Remove the record for DevTools popup windows.
std::set<std::string>::iterator it = m_OpenDevToolsURLs.find(browser->GetMainFrame()->GetURL());
if (it != m_OpenDevToolsURLs.end())
m_OpenDevToolsURLs.erase(it);
}
}
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
@ -101,12 +89,8 @@ void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
}
void ClientHandler::ShowDevTools(CefRefPtr<CefBrowser> browser) {
std::string devtools_url = browser->GetHost()->GetDevToolsURL(true);
std::string devtools_url = "chrome-devtools://devtools/devtools.html?docked=true";//browser->GetHost()->GetDevToolsURL(true);
if (!devtools_url.empty()) {
if (m_OpenDevToolsURLs.find(devtools_url) == m_OpenDevToolsURLs.end()) {
// Open DevTools in a popup window.
m_OpenDevToolsURLs.insert(devtools_url);
browser->GetMainFrame()->ExecuteJavaScript("window.open('" + devtools_url + "');", "about:blank", 0);
}
browser->GetMainFrame()->ExecuteJavaScript("window.open('" + devtools_url + "');", "about:blank", 0);
}
}

View File

@ -27,6 +27,25 @@ class ClientHandler : public CefClient,
CefRefPtr<CefBrowser> GetBrowser() { return m_Browser; }
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
return this;
}
// CefContextMenuHandler methods
virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
@ -59,21 +78,11 @@ class ClientHandler : public CefClient,
const CefString& failedUrl) OVERRIDE;
protected:
// The child browser window
CefRefPtr<CefBrowser> m_Browser;
// The main frame window handle
CefWindowHandle m_MainHwnd;
// List of open DevTools URLs if not using an external browser window.
std::set<std::string> m_OpenDevToolsURLs;
void ShowDevTools(CefRefPtr<CefBrowser> browser);
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(ClientHandler);
// Include the default locking implementation.
IMPLEMENT_LOCKING(ClientHandler);
};

View File

@ -18,7 +18,7 @@ int main(int argc, char* argv[]) {
NSNib *mainNib = [[NSNib alloc] initWithNibNamed:mainNibName bundle:[NSBundle mainBundle]];
[mainNib instantiateNibWithOwner:application topLevelObjects:nil]; // Execute the secondary process, if any.
[application createWindow];
[application open:""];
// Run the application message loop.
CefRunMessageLoop();

View File

@ -5,17 +5,18 @@
<script>
window.onload = function() {
try {
if ($bootstrapScript) require($bootstrapScript);
//if ($bootstrapScript) require($bootstrapScript);
}
catch (error) {
$native.showDevTools();
//c$native.showDevTools();
console.error(error.stack || error);
}
}
</script>
</head>
<body>
MOTHER FUCKER!
MOTHER FUCKER!
<iframe src="chrome-devtools://devtools/devtools.html?docked=true"></iframe>
</body>
</html>