| 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-2015 Eyeo GmbH | 3  * Copyright (C) 2006-2015 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 | 19 | 
| 20 #include "GlobalJsObject.h" | 20 #include "GlobalJsObject.h" | 
| 21 #include "JsContext.h" | 21 #include "JsContext.h" | 
| 22 #include "JsError.h" | 22 #include "JsError.h" | 
| 23 #include "Utils.h" | 23 #include "Utils.h" | 
|  | 24 #include <libplatform/libplatform.h> | 
| 24 | 25 | 
| 25 namespace | 26 namespace | 
| 26 { | 27 { | 
| 27   v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 28   v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, | 
| 28     const std::string& source, const std::string& filename) | 29     const std::string& source, const std::string& filename) | 
| 29   { | 30   { | 
| 30     using AdblockPlus::Utils::ToV8String; | 31     using AdblockPlus::Utils::ToV8String; | 
| 31     const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); | 32     const v8::Handle<v8::String> v8Source = ToV8String(isolate, source); | 
| 32     if (filename.length()) | 33     if (filename.length()) | 
| 33     { | 34     { | 
| 34       const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); | 35       const v8::Handle<v8::String> v8Filename = ToV8String(isolate, filename); | 
| 35       return v8::Script::Compile(v8Source, v8Filename); | 36       return v8::Script::Compile(v8Source, v8Filename); | 
| 36     } | 37     } | 
| 37     else | 38     else | 
| 38       return v8::Script::Compile(v8Source); | 39       return v8::Script::Compile(v8Source); | 
| 39   } | 40   } | 
| 40 | 41 | 
| 41   void CheckTryCatch(const v8::TryCatch& tryCatch) | 42   void CheckTryCatch(const v8::TryCatch& tryCatch) | 
| 42   { | 43   { | 
| 43     if (tryCatch.HasCaught()) | 44     if (tryCatch.HasCaught()) | 
| 44       throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 45       throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 
| 45   } | 46   } | 
| 46 | 47 | 
| 47   class V8Initializer | 48   class V8Initializer | 
| 48   { | 49   { | 
| 49     V8Initializer() | 50     V8Initializer() | 
|  | 51       : platform(v8::platform::CreateDefaultPlatform()) | 
| 50     { | 52     { | 
|  | 53       v8::V8::InitializePlatform(platform); | 
| 51       v8::V8::Initialize(); | 54       v8::V8::Initialize(); | 
| 52     } | 55     } | 
| 53 | 56 | 
| 54     ~V8Initializer() | 57     ~V8Initializer() | 
| 55     { | 58     { | 
| 56       v8::V8::Dispose(); | 59       v8::V8::Dispose(); | 
|  | 60       v8::V8::ShutdownPlatform(); | 
|  | 61       delete platform; | 
| 57     } | 62     } | 
|  | 63     v8::Platform* platform; | 
| 58   public: | 64   public: | 
| 59     static void Init() | 65     static void Init() | 
| 60     { | 66     { | 
| 61       // it's threadsafe since C++11 and it will be instantiated only once and | 67       // it's threadsafe since C++11 and it will be instantiated only once and | 
| 62       // destroyed at the application exit | 68       // destroyed at the application exit | 
| 63       static V8Initializer initializer; | 69       static V8Initializer initializer; | 
| 64     } | 70     } | 
| 65   }; | 71   }; | 
| 66 } | 72 } | 
| 67 | 73 | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
| 82 | 88 | 
| 83 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 89 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 
| 84 { | 90 { | 
| 85   V8Initializer::Init(); | 91   V8Initializer::Init(); | 
| 86   JsEnginePtr result(new JsEngine()); | 92   JsEnginePtr result(new JsEngine()); | 
| 87 | 93 | 
| 88   const v8::Locker locker(result->isolate); | 94   const v8::Locker locker(result->isolate); | 
| 89   const v8::Isolate::Scope isolateScope(result->isolate); | 95   const v8::Isolate::Scope isolateScope(result->isolate); | 
| 90   const v8::HandleScope handleScope(result->isolate); | 96   const v8::HandleScope handleScope(result->isolate); | 
| 91 | 97 | 
| 92   result->context.reset(new v8::Persistent<v8::Context>(result->isolate, | 98   result->context.reset(new v8::UniquePersistent<v8::Context>(result->isolate, | 
| 93     v8::Context::New(result->isolate))); | 99     v8::Context::New(result->isolate))); | 
| 94   v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( | 100   v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( | 
| 95     result->isolate, *result->context)->Global(); | 101     result->isolate, *result->context)->Global(); | 
| 96   AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 102   AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 
| 97     JsValuePtr(new JsValue(result, globalContext))); | 103     JsValuePtr(new JsValue(result, globalContext))); | 
| 98   return result; | 104   return result; | 
| 99 } | 105 } | 
| 100 | 106 | 
| 101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
     e, | 107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
     e, | 
| 102     const std::string& filename) | 108     const std::string& filename) | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
| 124 | 130 | 
| 125 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) | 
| 126 { | 132 { | 
| 127   EventMap::iterator it = eventCallbacks.find(eventName); | 133   EventMap::iterator it = eventCallbacks.find(eventName); | 
| 128   if (it != eventCallbacks.end()) | 134   if (it != eventCallbacks.end()) | 
| 129     it->second(params); | 135     it->second(params); | 
| 130 } | 136 } | 
| 131 | 137 | 
| 132 void AdblockPlus::JsEngine::Gc() | 138 void AdblockPlus::JsEngine::Gc() | 
| 133 { | 139 { | 
| 134   while (!v8::V8::IdleNotification()); | 140   while (!isolate->IdleNotification(1000)); | 
| 135 } | 141 } | 
| 136 | 142 | 
| 137 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 143 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 
| 138 { | 144 { | 
| 139   const JsContext context(shared_from_this()); | 145   const JsContext context(shared_from_this()); | 
| 140   return JsValuePtr(new JsValue(shared_from_this(), | 146   return JsValuePtr(new JsValue(shared_from_this(), | 
| 141     Utils::ToV8String(isolate, val))); | 147     Utils::ToV8String(isolate, val))); | 
| 142 } | 148 } | 
| 143 | 149 | 
| 144 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 150 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 
| 145 { | 151 { | 
| 146   const JsContext context(shared_from_this()); | 152   const JsContext context(shared_from_this()); | 
| 147   return JsValuePtr(new JsValue(shared_from_this(), | 153   return JsValuePtr(new JsValue(shared_from_this(), | 
| 148     v8::Number::New(isolate, val))); | 154     v8::Number::New(isolate, val))); | 
| 149 } | 155 } | 
| 150 | 156 | 
| 151 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 
| 152 { | 158 { | 
| 153   const JsContext context(shared_from_this()); | 159   const JsContext context(shared_from_this()); | 
| 154   return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); | 160   return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(isolate, va
     l))); | 
| 155 } | 161 } | 
| 156 | 162 | 
| 157 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 
| 158 { | 164 { | 
| 159   const JsContext context(shared_from_this()); | 165   const JsContext context(shared_from_this()); | 
| 160   return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); | 166   return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(isolate))); | 
| 161 } | 167 } | 
| 162 | 168 | 
| 163 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 169 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 
| 164     v8::InvocationCallback callback) | 170     v8::FunctionCallback callback) | 
| 165 { | 171 { | 
| 166   const JsContext context(shared_from_this()); | 172   const JsContext context(shared_from_this()); | 
| 167 | 173 | 
| 168   // 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 | 
| 169   // it's no longer used | 175   // it's no longer used | 
| 170   std::tr1::weak_ptr<JsEngine>* data = | 176   std::tr1::weak_ptr<JsEngine>* data = | 
| 171       new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 177       new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 
| 172   v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 178   v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate, cal
     lback, | 
| 173       v8::External::New(data)); | 179       v8::External::New(isolate, data)); | 
| 174   return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 180   return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 
| 175 } | 181 } | 
| 176 | 182 | 
| 177 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
     s& arguments) | 183 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
     CallbackInfo<v8::Value>& arguments) | 
| 178 { | 184 { | 
| 179   const v8::Local<const v8::External> external = | 185   const v8::Local<const v8::External> external = | 
| 180       v8::Local<const v8::External>::Cast(arguments.Data()); | 186       v8::Local<const v8::External>::Cast(arguments.Data()); | 
| 181   std::tr1::weak_ptr<JsEngine>* data = | 187   std::tr1::weak_ptr<JsEngine>* data = | 
| 182       static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 188       static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 
| 183   JsEnginePtr result = data->lock(); | 189   JsEnginePtr result = data->lock(); | 
| 184   if (!result) | 190   if (!result) | 
| 185     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?")
     ; | 
| 186   return result; | 192   return result; | 
| 187 } | 193 } | 
| 188 | 194 | 
| 189 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
     ents& arguments) | 195 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct
     ionCallbackInfo<v8::Value>& arguments) | 
| 190 { | 196 { | 
| 191   const JsContext context(shared_from_this()); | 197   const JsContext context(shared_from_this()); | 
| 192   JsValueList list; | 198   JsValueList list; | 
| 193   for (int i = 0; i < arguments.Length(); i++) | 199   for (int i = 0; i < arguments.Length(); i++) | 
| 194     list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); | 200     list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); | 
| 195   return list; | 201   return list; | 
| 196 } | 202 } | 
| 197 | 203 | 
| 198 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | 204 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | 
| 199 { | 205 { | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 232   return logSystem; | 238   return logSystem; | 
| 233 } | 239 } | 
| 234 | 240 | 
| 235 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 241 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 
| 236 { | 242 { | 
| 237   if (!val) | 243   if (!val) | 
| 238     throw std::runtime_error("LogSystem cannot be null"); | 244     throw std::runtime_error("LogSystem cannot be null"); | 
| 239 | 245 | 
| 240   logSystem = val; | 246   logSystem = val; | 
| 241 } | 247 } | 
| OLD | NEW | 
|---|