Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event loop #42

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@ try {
loadDefaultChimera();
}

setInterval(webkit.processEvents, 50);

var exports = module.exports;
var timer;
var processingEvents = true;
var timer = 0;
var processingEvents = false;
var referenceCount = 0;

function stop() {
processingEvents = false;
clearInterval(timer);
--referenceCount;
if (referenceCount === 0) {
processingEvents = false;
clearInterval(timer);
}
};

function start() {
if (!processingEvents) {
processingEvents = true;
timer = setInterval(webkit.processEvents, 50);
}
++referenceCount;
};

function Chimera(options) {
Expand All @@ -51,14 +54,7 @@ function Chimera(options) {
var cookies = options.cookies || '';
var disableImages = !!(options.disableImages);
var shouldStart = false;
if (processingEvents) {
shouldStart = true;
// stop();
}
this.browser = new webkit.Browser(userAgent, libraryCode, cookies, disableImages);
if (shouldStart) {
// start();
}
var proxy = options.proxy;
if (proxy) {
this.setProxy(proxy.type, proxy.host, proxy.port, proxy.username, proxy.password)
Expand Down Expand Up @@ -112,8 +108,10 @@ Chimera.prototype.perform = function(options) {
var locals = options.locals || {};
var run = 'with('+JSON.stringify(locals)+'){(' + (options.run || function(callback) {callback(null, "");}) + ')(function(err,res) {window.chimera.callback(JSON.stringify(err), JSON.stringify(res));})}';
var callback = options.callback || function(err, result) {};
start();
this.browser.open(url, run, function(errStr, resStr) {
callback(parseOrReturn(errStr), parseOrReturn(resStr));
stop();
});
}

Expand Down
22 changes: 9 additions & 13 deletions src/browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ Browser::Browser(QString userAgent, QString libraryCode, QString cookies, bool d
libraryCode_ = libraryCode;
cookies_ = cookies;
disableImages_ = disableImages;
chimera_ = 0;
}

Browser::~Browser() {
delete chimera_;
}

void Browser::Initialize(Handle<Object> target) {
Expand Down Expand Up @@ -51,8 +50,6 @@ Handle<Value> Browser::New(const Arguments& args) {
void AsyncWork(uv_work_t* req) {
BWork* work = static_cast<BWork*>(req->data);
work->chimera->wait();
work->result = work->chimera->getResult();
work->errorResult = work->chimera->getError();
}

void AsyncAfter(uv_work_t* req) {
Expand All @@ -71,6 +68,9 @@ void AsyncAfter(uv_work_t* req) {
node::FatalException(try_catch);
}
} else {
work->result = work->chimera->getResult();
work->errorResult = work->chimera->getError();

const unsigned argc = 2;
Local<Value> argv[argc] = {
Local<Value>::New(Null()),
Expand All @@ -84,11 +84,8 @@ void AsyncAfter(uv_work_t* req) {
}
}

uv_queue_work(uv_default_loop(), &work->request, AsyncWork, AsyncAfter);

// TODO: we need to figure out where to dispose the callback & free up work
// work->callback.Dispose();
// delete work;
work->callback.Dispose();
delete work;
}

Handle<Value> Browser::Cookies(const Arguments& args) {
Expand Down Expand Up @@ -167,11 +164,10 @@ Handle<Value> Browser::Close(const Arguments& args) {

Browser* w = ObjectWrap::Unwrap<Browser>(args.This());
Chimera* chimera = w->getChimera();
if (0 != chimera) {

if (NULL != chimera) {
chimera->exit(1);
w->setChimera(0);
chimera->deleteLater();
w->setChimera(NULL);
}

return scope.Close(Undefined());
Expand Down
8 changes: 5 additions & 3 deletions src/browser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef BROWSER_H
#define BROWSER_H

#include <boost/tr1/memory.hpp>

#include <node.h>
#include <QApplication>
#include <QTimer>
Expand All @@ -21,8 +23,8 @@ struct BWork {
class Browser : public node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
Chimera* getChimera() const { return chimera_; };
void setChimera(Chimera *chimera) { chimera_ = chimera; };
Chimera* getChimera() const { return chimera_.get(); };
void setChimera(Chimera *chimera) { chimera_.reset(chimera); };

QString userAgent() {return userAgent_; };
QString libraryCode() {return libraryCode_; };
Expand All @@ -40,7 +42,7 @@ class Browser : public node::ObjectWrap {
static v8::Handle<v8::Value> SetCookies(const v8::Arguments& args);
static v8::Handle<v8::Value> SetProxy(const v8::Arguments& args);

Chimera* chimera_;
std::tr1::shared_ptr<Chimera> chimera_;
QString libraryCode_;
QString userAgent_;
QString cookies_;
Expand Down
20 changes: 13 additions & 7 deletions src/chimera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ void Chimera::setEmbedScript(const QString &jscode)

void Chimera::callback(const QString &errorResult, const QString &result)
{
QMutexLocker locker(&m_mutex);
m_errors.enqueue(errorResult);
m_results.enqueue(result);
m_mutex.unlock();
m_loading.wakeAll();
}

Expand All @@ -174,13 +174,15 @@ void Chimera::exit(int code)
{
m_page.triggerAction(QWebPage::Stop);
m_returnValue = code;
QMutexLocker locker(&m_mutex);
m_loading.wakeAll();
locker.unlock();
disconnect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(finish(bool)));
}

void Chimera::execute()
{
std::cout << "debug -- about to lock" << std::endl;
m_mutex.tryLock();
std::cout << "debug -- about to evaluate" << std::endl;
m_page.mainFrame()->evaluateJavaScript(m_script);
std::cout << "debug -- done evaluating" << std::endl;
Expand All @@ -207,17 +209,13 @@ void Chimera::open(const QString &address)
{
m_page.triggerAction(QWebPage::Stop);
m_loadStatus = "loading";
m_mutex.lock();
m_page.mainFrame()->setUrl(QUrl(address));
}

void Chimera::wait()
{
if (m_mutex.tryLock()) {
m_mutex.unlock();
} else {
QMutexLocker locker(&m_mutex);
m_loading.wait(&m_mutex);
}
}

bool Chimera::capture(const QString &fileName)
Expand Down Expand Up @@ -259,11 +257,19 @@ int Chimera::returnValue() const

QString Chimera::getResult()
{
if (m_results.isEmpty()) {
// TODO may cause by Browser::Close
return "";
}
return m_results.dequeue();
}

QString Chimera::getError()
{
if (m_errors.isEmpty()) {
// TODO may cause by Browser::Close
return "";
}
return m_errors.dequeue();
}

Expand Down