| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2013 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 "debug.h" | |
| 21 #include "ops.h" | |
| 22 | |
| 23 typedef struct __QueueEntry | |
| 24 { | |
| 25 v8::Persistent<v8::Function> callback; | |
| 26 int64_t delay; | |
| 27 } QueueEntry; | |
| 28 | |
| 29 static std::list<QueueEntry*> queue; | |
| 30 | |
| 31 v8::Handle<v8::Value> setTimeoutImpl(const v8::Arguments& args) | |
| 32 { | |
| 33 D(D_WARN, "setTimeout()"); | |
| 34 v8::HandleScope handle_scope; | |
| 35 | |
| 36 JNIEnv* jniEnv = NULL; | |
| 37 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0) | |
| 38 return v8::ThrowException(v8::String::New("Failed to get JNI environment")); | |
| 39 | |
| 40 if (args.Length() < 2) | |
| 41 return v8::ThrowException(v8::String::New("Not enough parameters")); | |
| 42 | |
| 43 if (!args[0]->IsFunction()) | |
| 44 return v8::ThrowException(v8::String::New("Parameter 0 must be a function"))
; | |
| 45 if (!args[1]->IsInt32()) | |
| 46 return v8::ThrowException(v8::String::New("Parameter 1 must be an integer"))
; | |
| 47 | |
| 48 v8::Handle<v8::Function> callback = v8::Handle<v8::Function>::Cast(args[0]); | |
| 49 | |
| 50 QueueEntry* entry = new QueueEntry; | |
| 51 entry->callback = v8::Persistent<v8::Function>::New(callback); | |
| 52 entry->delay = (v8::Handle<v8::Integer>::Cast(args[1]))->Value(); | |
| 53 | |
| 54 queue.push_back(entry); | |
| 55 | |
| 56 jlong jnum = entry->delay; | |
| 57 | |
| 58 static jclass cls = jniEnv->GetObjectClass(jniCallback); | |
| 59 static jmethodID mid = jniEnv->GetMethodID(cls, "notify", "(J)V"); | |
| 60 if (mid) | |
| 61 jniEnv->CallVoidMethod(jniCallback, mid, jnum); | |
| 62 | |
| 63 return v8::Undefined(); | |
| 64 } | |
| 65 | |
| 66 long RunNextCallback(v8::Handle<v8::Context> context) | |
| 67 { | |
| 68 D(D_WARN, "RunNextCallback()"); | |
| 69 if (queue.size() == 0) | |
| 70 return -1; | |
| 71 | |
| 72 std::list<QueueEntry*>::iterator minEntry = queue.begin(); | |
| 73 for (std::list<QueueEntry*>::iterator it = minEntry; it != queue.end(); it++) | |
| 74 if ((*it)->delay < (*minEntry)->delay) | |
| 75 minEntry = it; | |
| 76 | |
| 77 int64_t delay = (*minEntry)->delay; | |
| 78 if (delay > 0) | |
| 79 { | |
| 80 // Here we assume that we will be called next time after the specified delay
, | |
| 81 // caller should ensure this | |
| 82 for (std::list<QueueEntry*>::iterator it = queue.begin(); it != queue.end();
it++) | |
| 83 (*it)->delay -= delay; | |
| 84 return delay; | |
| 85 } | |
| 86 | |
| 87 QueueEntry* entry = *minEntry; | |
| 88 queue.erase(minEntry); | |
| 89 | |
| 90 entry->callback->Call(context->Global(), 0, NULL); | |
| 91 entry->callback.Dispose(); | |
| 92 delete entry; | |
| 93 | |
| 94 return 0; | |
| 95 } | |
| 96 | |
| 97 void ClearQueue() | |
| 98 { | |
| 99 queue.clear(); | |
| 100 } | |
| OLD | NEW |