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