Permalink
Browse files

initial working version on OSX

  • Loading branch information...
1 parent 8ae77ce commit 0546f7033e46dcd27cd7e2254c44cc077c57a20e @rogerwang committed Apr 26, 2012
View
@@ -138,9 +138,9 @@ deps = {
'src/v8':
Var('github_url') + '/v8.git@f63ee59aaac8849a6168cd43aed519d755232621',
'src/third_party/libuv':
- Var('github_url') + '/libuv.git@866cb059e7fd5f6426ad8e529870df0e152416bb',
+ Var('github_url') + '/libuv.git@30ccfa490f189833a386c8c16c8c86596ef92c37',
'src/third_party/node':
- Var('github_url') + '/node.git@6ae8a6572da836fac95c29f1ca789da6407989f4',
+ Var('github_url') + '/node.git@5f3dd6bf511974c95db3e1dd151bb05a7c19e0a0',
'src/third_party/nwebkit':
Var('github_url') + '/node-webkit.git@a3a13eb0ec131c9202b0804c62449efa5dca87b5',
}
View
@@ -146,6 +146,7 @@ MessageLoop::MessageLoop(Type type)
#define MESSAGE_PUMP_IO new base::MessagePumpForIO()
#elif defined(OS_MACOSX)
#define MESSAGE_PUMP_UI base::MessagePumpMac::Create()
+#define MESSAGE_PUMP_NODE base::MessagePumpMac::Create(true)
#define MESSAGE_PUMP_IO new base::MessagePumpLibevent()
#elif defined(OS_NACL)
// Currently NaCl doesn't have a UI or an IO MessageLoop.
@@ -169,7 +170,11 @@ MessageLoop::MessageLoop(Type type)
} else if (type_ == TYPE_IO) {
pump_ = MESSAGE_PUMP_IO;
} else if (type_ == TYPE_NODE) {
+#if defined(OS_MACOSX)
+ pump_ = MESSAGE_PUMP_NODE;
+#else
pump_ = MESSAGE_PUMP_UV;
+#endif
} else {
DCHECK_EQ(TYPE_DEFAULT, type_);
pump_ = new base::MessagePumpDefault();
View
@@ -227,7 +227,7 @@ class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase {
class MessagePumpNSApplication : public MessagePumpCFRunLoopBase {
public:
- MessagePumpNSApplication();
+ MessagePumpNSApplication(bool forNode = false);
virtual void DoRun(Delegate* delegate) OVERRIDE;
virtual void Quit() OVERRIDE;
@@ -242,12 +242,13 @@ class MessagePumpNSApplication : public MessagePumpCFRunLoopBase {
// in DoRun.
bool running_own_loop_;
+ bool for_node_;
DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication);
};
class MessagePumpCrApplication : public MessagePumpNSApplication {
public:
- MessagePumpCrApplication();
+ MessagePumpCrApplication(bool forNode = false);
protected:
// Returns nil if NSApp is currently in the middle of calling
@@ -268,7 +269,7 @@ class MessagePumpMac {
//
// Otherwise creates an instance of MessagePumpNSApplication using a
// default NSApplication.
- static MessagePump* Create();
+ static MessagePump* Create(bool forNode = false);
// If a pump is created before the required CrAppProtocol is
// created, the wrong MessagePump subclass could be used.
View
@@ -11,6 +11,8 @@
#include "base/logging.h"
#include "base/time.h"
+#include "third_party/libuv/include/uv-private/ev.h"
+#include "third_party/node/src/node.h"
namespace {
@@ -26,6 +28,21 @@ void NoOp(void* info) {
} // namespace
+static void PumpNode() {
+ ev_now_update(EV_DEFAULT_UC); // Bring the clock forward since the last ev_loop().
+ ev_loop(EV_DEFAULT_UC_ EVLOOP_NONBLOCK);
+ while(ev_backend_changecount(EV_DEFAULT_UC) != 0) {
+ ev_loop(EV_DEFAULT_UC_ EVLOOP_NONBLOCK);
+ }
+}
+
+static void KqueueCallback(CFFileDescriptorRef backend_cffd,
+ CFOptionFlags callBackTypes,
+ void* info) {
+ PumpNode();
+ CFFileDescriptorEnableCallBacks(backend_cffd, kCFFileDescriptorReadCallBack);
+}
+
namespace base {
// A scoper for autorelease pools created from message pump run loops.
@@ -85,6 +102,25 @@ explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
&source_context);
CFRunLoopAddSource(run_loop_, idle_work_source_, kCFRunLoopCommonModes);
+ int backend_fd = ev_backend_fd(EV_DEFAULT);
+
+ CFFileDescriptorRef backend_cffd =
+ CFFileDescriptorCreate(NULL, backend_fd, true, &KqueueCallback, NULL);
+ CFRunLoopSourceRef backend_rlsr =
+ CFFileDescriptorCreateRunLoopSource(NULL, backend_cffd, 0);
+ CFRunLoopAddSource(run_loop_,
+ backend_rlsr,
+ kCFRunLoopDefaultMode);
+ CFRelease(backend_rlsr);
+ CFFileDescriptorEnableCallBacks(backend_cffd, kCFFileDescriptorReadCallBack);
+
+
+ // source_context.perform = RunUVWorkSource;
+ // uv_work_source_ = CFRunLoopSourceCreate(NULL, // allocator
+ // 1, // priority
+ // &source_context);
+ // CFRunLoopAddSource(run_loop_, uv_work_source_, kCFRunLoopCommonModes);
+
source_context.perform = RunNestingDeferredWorkSource;
nesting_deferred_work_source_ = CFRunLoopSourceCreate(NULL, // allocator
0, // priority
@@ -537,9 +573,10 @@ explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
CFRunLoopWakeUp(run_loop());
}
-MessagePumpNSApplication::MessagePumpNSApplication()
+MessagePumpNSApplication::MessagePumpNSApplication(bool forNode)
: keep_running_(true),
- running_own_loop_(false) {
+ running_own_loop_(false),
+ for_node_(forNode) {
}
void MessagePumpNSApplication::DoRun(Delegate* delegate) {
@@ -552,13 +589,46 @@ explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
// RegisterCrApp() or RegisterBrowserCrApp().
CHECK(NSApp);
- if (![NSApp isRunning]) {
+ if (!for_node_ && ![NSApp isRunning]) {
+ // if (0) {
running_own_loop_ = false;
// NSApplication manages autorelease pools itself when run this way.
[NSApp run];
+ } else if (for_node_) {
+ running_own_loop_ = true;
+ // NSDate* distant_future = [NSDate distantFuture];
+ int argc = 1;
+ char argv0[] = "node";
+ char* argv[] = {argv0, NULL};
+ node::Start0(argc, argv);
+ {
+ v8::Locker locker;
+ v8::HandleScope handle_scope;
+ node::Start(argc, argv);
+
+ while (keep_running_) {
+ MessagePumpScopedAutoreleasePool autorelease_pool(this);
+ PumpNode();
+ double next_waittime = ev_next_waittime();
+ // TODO(deanm): Fix loop integration with newest version of Node.
+ next_waittime = 0.01;
+ NSDate* next_date = [NSDate dateWithTimeIntervalSinceNow:next_waittime];
+ // printf("Running a loop iteration with timeout %f\n", next_waittime);
+ NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
+ // untilDate:distant_future
+ untilDate:next_date
+ inMode:NSDefaultRunLoopMode
+ dequeue:YES];
+ if (event) {
+ [NSApp sendEvent:event];
+ }
+ }
+ keep_running_ = true;
+ }
} else {
running_own_loop_ = true;
NSDate* distant_future = [NSDate distantFuture];
+
while (keep_running_) {
MessagePumpScopedAutoreleasePool autorelease_pool(this);
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
@@ -595,7 +665,8 @@ explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
atStart:NO];
}
-MessagePumpCrApplication::MessagePumpCrApplication() {
+MessagePumpCrApplication::MessagePumpCrApplication(bool forNode)
+ : MessagePumpNSApplication(forNode) {
}
// Prevents an autorelease pool from being created if the app is in the midst of
@@ -636,18 +707,18 @@ explicit MessagePumpScopedAutoreleasePool(MessagePumpCFRunLoopBase* pump) :
}
// static
-MessagePump* MessagePumpMac::Create() {
+MessagePump* MessagePumpMac::Create(bool forNode) {
if ([NSThread isMainThread]) {
if ([NSApp conformsToProtocol:@protocol(CrAppProtocol)])
- return new MessagePumpCrApplication;
+ return new MessagePumpCrApplication(forNode);
// The main-thread MessagePump implementations REQUIRE an NSApp.
// Executables which have specific requirements for their
// NSApplication subclass should initialize appropriately before
// creating an event loop.
[NSApplication sharedApplication];
not_using_crapp = true;
- return new MessagePumpNSApplication;
+ return new MessagePumpNSApplication(forNode);
}
return new MessagePumpNSRunLoop;
@@ -881,7 +881,9 @@ void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
EnsureWebKitInitialized();
// When bringing in render_view, also bring in webkit's glue and jsbindings.
+#if !defined(OS_MACOSX)
RenderViewImpl* impl =
+#endif
RenderViewImpl::Create(
params.parent_window,
MSG_ROUTING_NONE,
@@ -896,7 +898,9 @@ void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
params.screen_info,
params.guest);
+#if !defined(OS_MACOSX)
MessageLoopForUV::current()->pump_uv()->onRenderViewCreated(impl);
+#endif
}
GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
@@ -169,7 +169,7 @@ int RendererMain(const content::MainFunctionParams& parameters) {
#if defined(OS_MACOSX)
// As long as we use Cocoa in the renderer (for the forseeable future as of
// now; see http://crbug.com/13890 for info) we need to have a UI loop.
- MessageLoop main_message_loop(MessageLoop::TYPE_UI);
+ MessageLoop main_message_loop(MessageLoop::TYPE_NODE);
#else
// The main message loop of the renderer services doesn't have IO or UI tasks,
// unless in-process-plugins is used.

0 comments on commit 0546f70

Please sign in to comment.