| OLD | NEW | 
|---|
| 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-2017 eyeo GmbH | 3  * Copyright (C) 2006-2017 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 #include "GlobalJsObject.h" | 19 #include "GlobalJsObject.h" | 
| 20 #include "JsContext.h" | 20 #include "JsContext.h" | 
| 21 #include "JsError.h" | 21 #include "JsError.h" | 
| 22 #include "Utils.h" | 22 #include "Utils.h" | 
|  | 23 #include "DefaultTimer.h" | 
| 23 | 24 | 
| 24 namespace | 25 namespace | 
| 25 { | 26 { | 
| 26   v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 27   v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 
| 27     const std::string& source, const std::string& filename) | 28     const std::string& source, const std::string& filename) | 
| 28   { | 29   { | 
| 29     using AdblockPlus::Utils::ToV8String; | 30     using AdblockPlus::Utils::ToV8String; | 
| 30     const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); | 31     const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); | 
| 31     if (filename.length()) | 32     if (filename.length()) | 
| 32     { | 33     { | 
| (...skipping 26 matching lines...) Expand all  Loading... | 
| 59     { | 60     { | 
| 60       // it's threadsafe since C++11 and it will be instantiated only once and | 61       // it's threadsafe since C++11 and it will be instantiated only once and | 
| 61       // destroyed at the application exit | 62       // destroyed at the application exit | 
| 62       static V8Initializer initializer; | 63       static V8Initializer initializer; | 
| 63     } | 64     } | 
| 64   }; | 65   }; | 
| 65 } | 66 } | 
| 66 | 67 | 
| 67 using namespace AdblockPlus; | 68 using namespace AdblockPlus; | 
| 68 | 69 | 
|  | 70 TimerPtr AdblockPlus::CreateDefaultTimer() | 
|  | 71 { | 
|  | 72   return TimerPtr(new DefaultTimer()); | 
|  | 73 } | 
|  | 74 | 
| 69 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | 75 AdblockPlus::ScopedV8Isolate::ScopedV8Isolate() | 
| 70 { | 76 { | 
| 71   V8Initializer::Init(); | 77   V8Initializer::Init(); | 
| 72   isolate = v8::Isolate::New(); | 78   isolate = v8::Isolate::New(); | 
| 73 } | 79 } | 
| 74 | 80 | 
| 75 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 81 AdblockPlus::ScopedV8Isolate::~ScopedV8Isolate() | 
| 76 { | 82 { | 
| 77   isolate->Dispose(); | 83   isolate->Dispose(); | 
| 78   isolate = nullptr; | 84   isolate = nullptr; | 
| 79 } | 85 } | 
| 80 | 86 | 
| 81 JsEngine::TimerTaskInfo::~TimerTaskInfo() | 87 JsEngine::TimerTask::~TimerTask() | 
| 82 { | 88 { | 
| 83   for (auto& arg : arguments) | 89   for (auto& arg : arguments) | 
| 84     arg->Dispose(); | 90     arg->Dispose(); | 
| 85 } | 91 } | 
| 86 | 92 | 
| 87 JsEngine::TimerTask JsEngine::CreateTimerTask(const v8::Arguments& arguments) | 93 void JsEngine::ScheduleTimer(const v8::Arguments& arguments) | 
| 88 { | 94 { | 
|  | 95   auto jsEngine = FromArguments(arguments); | 
| 89   if (arguments.Length() < 2) | 96   if (arguments.Length() < 2) | 
| 90     throw std::runtime_error("setTimeout requires at least 2 parameters"); | 97     throw std::runtime_error("setTimeout requires at least 2 parameters"); | 
| 91 | 98 | 
| 92   if (!arguments[0]->IsFunction()) | 99   if (!arguments[0]->IsFunction()) | 
| 93     throw std::runtime_error("First argument to setTimeout must be a function"); | 100     throw std::runtime_error("First argument to setTimeout must be a function"); | 
| 94 | 101 | 
| 95   auto timerTaskInfoIterator = timerTaskInfos.emplace(timerTaskInfos.end()); | 102   auto timerTaskIterator = jsEngine->timerTasks.emplace(jsEngine->timerTasks.end
     ()); | 
| 96   timerTaskInfoIterator->delay = arguments[1]->IntegerValue(); |  | 
| 97 | 103 | 
| 98   for (int i = 0; i < arguments.Length(); i++) | 104   for (int i = 0; i < arguments.Length(); i++) | 
| 99     timerTaskInfoIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(
     GetIsolate(), arguments[i])); | 105     timerTaskIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(jsEn
     gine->GetIsolate(), arguments[i])); | 
| 100   TimerTask retValue = { shared_from_this(), timerTaskInfoIterator }; | 106 | 
| 101   return retValue; | 107   std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 
|  | 108   jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
     ()), [weakJsEngine, timerTaskIterator] | 
|  | 109   { | 
|  | 110     if (auto jsEngine = weakJsEngine.lock()) | 
|  | 111       jsEngine->CallTimerTask(timerTaskIterator); | 
|  | 112   }); | 
| 102 } | 113 } | 
| 103 | 114 | 
| 104 void JsEngine::CallTimerTask(TimerTaskInfos::const_iterator timerTaskInfoIterato
     r) | 115 void JsEngine::CallTimerTask(TimerTasks::const_iterator timerTaskIterator) | 
| 105 { | 116 { | 
| 106   const JsContext context(shared_from_this()); | 117   const JsContext context(shared_from_this()); | 
| 107   JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
     timerTaskInfoIterator->arguments[0])); | 118   JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
     timerTaskIterator->arguments[0])); | 
| 108   JsConstValueList callbackArgs; | 119   JsConstValueList callbackArgs; | 
| 109   for (int i = 2; i < timerTaskInfoIterator->arguments.size(); i++) | 120   for (int i = 2; i < timerTaskIterator->arguments.size(); i++) | 
| 110     callbackArgs.emplace_back(new JsValue(shared_from_this(), | 121     callbackArgs.emplace_back(new JsValue(shared_from_this(), | 
| 111     v8::Local<v8::Value>::New(GetIsolate(), *timerTaskInfoIterator->arguments[i]
     ))); | 122     v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))); | 
| 112   callback.Call(callbackArgs); | 123   callback.Call(callbackArgs); | 
| 113   timerTaskInfos.erase(timerTaskInfoIterator); | 124   timerTasks.erase(timerTaskIterator); | 
| 114 } | 125 } | 
| 115 | 126 | 
| 116 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate) | 127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time
     r) | 
| 117   : isolate(isolate) | 128   : isolate(isolate) | 
|  | 129   , timer(std::move(timer)) | 
| 118 { | 130 { | 
| 119 } | 131 } | 
| 120 | 132 | 
| 121 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons
     t ScopedV8IsolatePtr& isolate) | 133 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, | 
|  | 134   TimerPtr timer, | 
|  | 135   const ScopedV8IsolatePtr& isolate) | 
| 122 { | 136 { | 
| 123   JsEnginePtr result(new JsEngine(isolate)); | 137   JsEnginePtr result(new JsEngine(isolate, std::move(timer))); | 
| 124 | 138 | 
| 125   const v8::Locker locker(result->GetIsolate()); | 139   const v8::Locker locker(result->GetIsolate()); | 
| 126   const v8::Isolate::Scope isolateScope(result->GetIsolate()); | 140   const v8::Isolate::Scope isolateScope(result->GetIsolate()); | 
| 127   const v8::HandleScope handleScope(result->GetIsolate()); | 141   const v8::HandleScope handleScope(result->GetIsolate()); | 
| 128 | 142 | 
| 129   result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), | 143   result->context.reset(new v8::Persistent<v8::Context>(result->GetIsolate(), | 
| 130     v8::Context::New(result->GetIsolate()))); | 144     v8::Context::New(result->GetIsolate()))); | 
| 131   AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->GetGlobalObject())
     ; | 145   AdblockPlus::GlobalJsObject::Setup(result, appInfo, result->GetGlobalObject())
     ; | 
| 132   return result; | 146   return result; | 
| 133 } | 147 } | 
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 315 | 329 | 
| 316 | 330 | 
| 317 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 331 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 
| 318                                               AdblockPlus::JsValuePtr value) | 332                                               AdblockPlus::JsValuePtr value) | 
| 319 { | 333 { | 
| 320   auto global = GetGlobalObject(); | 334   auto global = GetGlobalObject(); | 
| 321   if (!global) | 335   if (!global) | 
| 322     throw std::runtime_error("Global object cannot be null"); | 336     throw std::runtime_error("Global object cannot be null"); | 
| 323   global->SetProperty(name, value); | 337   global->SetProperty(name, value); | 
| 324 } | 338 } | 
| OLD | NEW | 
|---|