OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of the Adblock Plus, |
| 3 * Copyright (C) 2006-2012 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include <unistd.h> |
| 19 #include <list> |
| 20 #include "ops.h" |
| 21 |
| 22 typedef struct __QueueEntry |
| 23 { |
| 24 v8::Persistent<v8::Function> callback; |
| 25 int64_t delay; |
| 26 } QueueEntry; |
| 27 |
| 28 static std::list<QueueEntry*> queue; |
| 29 |
| 30 v8::Handle<v8::Value> setTimeoutImpl(const v8::Arguments& args) |
| 31 { |
| 32 v8::HandleScope handle_scope; |
| 33 |
| 34 if (args.Length() < 2) |
| 35 return v8::ThrowException(v8::String::New("Not enough parameters")); |
| 36 |
| 37 if (!args[0]->IsFunction()) |
| 38 return v8::ThrowException(v8::String::New("Parameter 0 must be a function"))
; |
| 39 if (!args[1]->IsInt32()) |
| 40 return v8::ThrowException(v8::String::New("Parameter 1 must be an integer"))
; |
| 41 |
| 42 v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast(args[0]); |
| 43 |
| 44 QueueEntry* entry = new QueueEntry; |
| 45 entry->callback = v8::Persistent<v8::Function>::New(callback); |
| 46 entry->delay = (v8::Handle<v8::Integer>::Cast(args[1]))->Value(); |
| 47 |
| 48 queue.push_back(entry); |
| 49 |
| 50 jlong jnum = entry->delay; |
| 51 |
| 52 static jclass cls = jniEnv->GetObjectClass(jniCallback); |
| 53 static jmethodID mid = jniEnv->GetMethodID(cls, "notify", "(J)V"); |
| 54 if (mid) |
| 55 jniEnv->CallVoidMethod(jniCallback, mid, jnum); |
| 56 |
| 57 return v8::Undefined(); |
| 58 } |
| 59 |
| 60 long RunNextCallback(v8::Handle<v8::Context> context) |
| 61 { |
| 62 if (queue.size() == 0) |
| 63 return -1; |
| 64 |
| 65 std::list<QueueEntry*>::iterator minEntry = queue.begin(); |
| 66 for (std::list<QueueEntry*>::iterator it = minEntry; it != queue.end(); it++) |
| 67 if ((*it)->delay < (*minEntry)->delay) |
| 68 minEntry = it; |
| 69 |
| 70 int64_t delay = (*minEntry)->delay; |
| 71 if (delay > 0) |
| 72 { |
| 73 // Here we assume that we will be called next time after the specified delay
, |
| 74 // caller should ensure this |
| 75 for (std::list<QueueEntry*>::iterator it = queue.begin(); it != queue.end();
it++) |
| 76 (*it)->delay -= delay; |
| 77 return delay; |
| 78 } |
| 79 |
| 80 QueueEntry* entry = *minEntry; |
| 81 queue.erase(minEntry); |
| 82 |
| 83 entry->callback->Call(context->Global(), 0, NULL); |
| 84 entry->callback.Dispose(); |
| 85 delete entry; |
| 86 |
| 87 return 0; |
| 88 } |
| 89 |
| 90 void ClearQueue() |
| 91 { |
| 92 queue.clear(); |
| 93 } |
OLD | NEW |