LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 <AdblockPlus.h> | 18 #include <AdblockPlus.h> |
19 | |
20 #include "GlobalJsObject.h" | 19 #include "GlobalJsObject.h" |
21 #include "JsContext.h" | 20 #include "JsContext.h" |
22 #include "JsError.h" | 21 #include "JsError.h" |
23 #include "Utils.h" | 22 #include "Utils.h" |
24 #include <libplatform/libplatform.h> | 23 #include <libplatform/libplatform.h> |
25 | 24 |
26 namespace | 25 namespace |
27 { | 26 { |
28 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 27 v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, |
29 const std::string& source, const std::string& filename) | 28 const std::string& source, const std::string& filename) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 static void Init() | 64 static void Init() |
66 { | 65 { |
67 // it's threadsafe since C++11 and it will be instantiated only once and | 66 // it's threadsafe since C++11 and it will be instantiated only once and |
68 // destroyed at the application exit | 67 // destroyed at the application exit |
69 static V8Initializer initializer; | 68 static V8Initializer initializer; |
70 } | 69 } |
71 }; | 70 }; |
72 } | 71 } |
73 | 72 |
74 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | 73 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() |
75 : isolate(v8::Isolate::New()) | 74 { |
76 { | 75 V8Initializer::Init(); |
| 76 isolate = v8::Isolate::New(); |
77 } | 77 } |
78 | 78 |
79 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 79 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() |
80 { | 80 { |
81 isolate->Dispose(); | 81 isolate->Dispose(); |
82 isolate = nullptr; | 82 isolate = nullptr; |
83 } | 83 } |
84 | 84 |
85 AdblockPlus::JsEngine::JsEngine() | 85 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) |
86 { | 86 : isolate(isolate) |
87 } | 87 { |
88 | 88 } |
89 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 89 |
90 { | 90 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
t ScopedV8IsolatePtr& isolate) |
91 V8Initializer::Init(); | 91 { |
92 JsEnginePtr result(new JsEngine()); | 92 JsEnginePtr result(new JsEngine(isolate)); |
93 | 93 |
94 const v8::Locker locker(result->isolate); | 94 const v8::Locker locker(result->GetIsolate()); |
95 const v8::Isolate::Scope isolateScope(result->isolate); | 95 const v8::Isolate::Scope isolateScope(result->GetIsolate()); |
96 const v8::HandleScope handleScope(result->isolate); | 96 const v8::HandleScope handleScope(result->GetIsolate()); |
97 | 97 |
98 result->context.reset(new v8::UniquePersistent<v8::Context>(result->isolate, | 98 result->context.reset(new v8::UniquePersistent<v8::Context>(result->GetIsolate
(), |
99 v8::Context::New(result->isolate))); | 99 v8::Context::New(result->GetIsolate()))); |
100 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( | 100 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( |
101 result->isolate, *result->context)->Global(); | 101 result->GetIsolate(), *result->context)->Global(); |
102 AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 102 result->globalJsObject = JsValuePtr(new JsValue(result, globalContext)); |
103 JsValuePtr(new JsValue(result, globalContext))); | 103 AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->globalJsObject); |
104 return result; | 104 return result; |
105 } | 105 } |
106 | 106 |
107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
108 const std::string& filename) | 108 const std::string& filename) |
109 { | 109 { |
110 const JsContext context(shared_from_this()); | 110 const JsContext context(shared_from_this()); |
111 const v8::TryCatch tryCatch; | 111 const v8::TryCatch tryCatch; |
112 const v8::Handle<v8::Script> script = CompileScript(isolate, source, | 112 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, |
113 filename); | 113 filename); |
114 CheckTryCatch(tryCatch); | 114 CheckTryCatch(tryCatch); |
115 v8::Local<v8::Value> result = script->Run(); | 115 v8::Local<v8::Value> result = script->Run(); |
116 CheckTryCatch(tryCatch); | 116 CheckTryCatch(tryCatch); |
117 return JsValuePtr(new JsValue(shared_from_this(), result)); | 117 return JsValuePtr(new JsValue(shared_from_this(), result)); |
118 } | 118 } |
119 | 119 |
120 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 120 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
121 AdblockPlus::JsEngine::EventCallback callback) | 121 AdblockPlus::JsEngine::EventCallback callback) |
122 { | 122 { |
123 eventCallbacks[eventName] = callback; | 123 eventCallbacks[eventName] = callback; |
124 } | 124 } |
125 | 125 |
126 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) | 126 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) |
127 { | 127 { |
128 eventCallbacks.erase(eventName); | 128 eventCallbacks.erase(eventName); |
129 } | 129 } |
130 | 130 |
131 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) | 131 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) |
132 { | 132 { |
133 EventMap::iterator it = eventCallbacks.find(eventName); | 133 EventMap::iterator it = eventCallbacks.find(eventName); |
134 if (it != eventCallbacks.end()) | 134 if (it != eventCallbacks.end()) |
135 it->second(params); | 135 it->second(params); |
136 } | 136 } |
137 | 137 |
138 void AdblockPlus::JsEngine::Gc() | 138 void AdblockPlus::JsEngine::Gc() |
139 { | 139 { |
140 while (!isolate->IdleNotification(1000)); | 140 while (!GetIsolate()->IdleNotification(1000)); |
141 } | 141 } |
142 | 142 |
143 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 143 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
144 { | 144 { |
145 const JsContext context(shared_from_this()); | 145 const JsContext context(shared_from_this()); |
146 return JsValuePtr(new JsValue(shared_from_this(), | 146 return JsValuePtr(new JsValue(shared_from_this(), |
147 Utils::ToV8String(isolate, val))); | 147 Utils::ToV8String(GetIsolate(), val))); |
148 } | 148 } |
149 | 149 |
150 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 150 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
151 { | 151 { |
152 const JsContext context(shared_from_this()); | 152 const JsContext context(shared_from_this()); |
153 return JsValuePtr(new JsValue(shared_from_this(), | 153 return JsValuePtr(new JsValue(shared_from_this(), |
154 v8::Number::New(isolate, val))); | 154 v8::Number::New(GetIsolate(), val))); |
155 } | 155 } |
156 | 156 |
157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
158 { | 158 { |
159 const JsContext context(shared_from_this()); | 159 const JsContext context(shared_from_this()); |
160 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(isolate, va
l))); | 160 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(GetIsolate(
), val))); |
161 } | 161 } |
162 | 162 |
163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
164 { | 164 { |
165 const JsContext context(shared_from_this()); | 165 const JsContext context(shared_from_this()); |
166 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(isolate))); | 166 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(GetIsolate()
))); |
167 } | 167 } |
168 | 168 |
169 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 169 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
170 v8::FunctionCallback callback) | 170 v8::FunctionCallback callback) |
171 { | 171 { |
172 const JsContext context(shared_from_this()); | 172 const JsContext context(shared_from_this()); |
173 | 173 |
174 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 174 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
175 // it's no longer used | 175 // it's no longer used |
176 std::tr1::weak_ptr<JsEngine>* data = | 176 std::weak_ptr<JsEngine>* data = |
177 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 177 new std::weak_ptr<JsEngine>(shared_from_this()); |
178 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate, cal
lback, | 178 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(GetIsolate()
, callback, |
179 v8::External::New(isolate, data)); | 179 v8::External::New(GetIsolate(), data)); |
180 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 180 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); |
181 } | 181 } |
182 | 182 |
183 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
CallbackInfo<v8::Value>& arguments) | 183 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
CallbackInfo<v8::Value>& arguments) |
184 { | 184 { |
185 const v8::Local<const v8::External> external = | 185 const v8::Local<const v8::External> external = |
186 v8::Local<const v8::External>::Cast(arguments.Data()); | 186 v8::Local<const v8::External>::Cast(arguments.Data()); |
187 std::tr1::weak_ptr<JsEngine>* data = | 187 std::weak_ptr<JsEngine>* data = |
188 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 188 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); |
189 JsEnginePtr result = data->lock(); | 189 JsEnginePtr result = data->lock(); |
190 if (!result) | 190 if (!result) |
191 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 191 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
192 return result; | 192 return result; |
193 } | 193 } |
194 | 194 |
195 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct
ionCallbackInfo<v8::Value>& arguments) | 195 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct
ionCallbackInfo<v8::Value>& arguments) |
196 { | 196 { |
197 const JsContext context(shared_from_this()); | 197 const JsContext context(shared_from_this()); |
198 JsValueList list; | 198 JsValueList list; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 return logSystem; | 238 return logSystem; |
239 } | 239 } |
240 | 240 |
241 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 241 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) |
242 { | 242 { |
243 if (!val) | 243 if (!val) |
244 throw std::runtime_error("LogSystem cannot be null"); | 244 throw std::runtime_error("LogSystem cannot be null"); |
245 | 245 |
246 logSystem = val; | 246 logSystem = val; |
247 } | 247 } |
| 248 |
| 249 |
| 250 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
| 251 AdblockPlus::JsValuePtr value) |
| 252 { |
| 253 if (!globalJsObject) |
| 254 throw std::runtime_error("Global object cannot be null"); |
| 255 |
| 256 globalJsObject->SetProperty(name, value); |
| 257 } |
LEFT | RIGHT |