| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * This file is part of Adblock Plus <http://adblockplus.org/>, |    2  * This file is part of Adblock Plus <http://adblockplus.org/>, | 
|    3  * Copyright (C) 2006-2014 Eyeo GmbH |    3  * Copyright (C) 2006-2014 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 <libplatform/libplatform.h> | 
|   19  |   20  | 
|   20 #include "GlobalJsObject.h" |   21 #include "GlobalJsObject.h" | 
|   21 #include "JsContext.h" |   22 #include "JsContext.h" | 
|   22 #include "JsError.h" |   23 #include "JsError.h" | 
|   23 #include "Utils.h" |   24 #include "Utils.h" | 
|   24  |   25  | 
|   25 namespace |   26 namespace | 
|   26 { |   27 { | 
|   27   v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str
     ing& filename) |   28   const int g_kIdleNotificationTimeuotMsec = 20; | 
 |   29   v8::Handle<v8::Script> CompileScript(v8::Isolate* isolate, const std::string& 
     source, const std::string& filename) | 
|   28   { |   30   { | 
|   29     const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); |   31     const v8::Handle<v8::String> v8Source = AdblockPlus::Utils::ToV8String(isola
     te, source); | 
|   30     if (filename.length()) |   32     if (filename.length()) | 
|   31     { |   33     { | 
|   32       const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str()
     ); |   34       const v8::Handle<v8::String> v8Filename = AdblockPlus::Utils::ToV8String(i
     solate, filename); | 
|   33       return v8::Script::Compile(v8Source, v8Filename); |   35       return v8::Script::Compile(v8Source, v8Filename); | 
|   34     } |   36     } | 
|   35     else |   37     else | 
|   36       return v8::Script::Compile(v8Source); |   38       return v8::Script::Compile(v8Source); | 
|   37   } |   39   } | 
|   38  |   40  | 
|   39   void CheckTryCatch(const v8::TryCatch& tryCatch) |   41   void CheckTryCatch(const v8::TryCatch& tryCatch) | 
|   40   { |   42   { | 
|   41     if (tryCatch.HasCaught()) |   43     if (tryCatch.HasCaught()) | 
|   42       throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); |   44       throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); | 
|   43   } |   45   } | 
|   44 } |   46  | 
|   45  |   47   class V8Initializer | 
|   46 AdblockPlus::JsEngine::JsEngine() |   48   { | 
|   47   : isolate(v8::Isolate::GetCurrent()) |   49     V8Initializer() | 
 |   50     { | 
 |   51       v8::Platform* platform = v8::platform::CreateDefaultPlatform(); | 
 |   52       v8::V8::InitializePlatform(platform); | 
 |   53       v8::V8::Initialize(); | 
 |   54     } | 
 |   55  | 
 |   56     ~V8Initializer() | 
 |   57     { | 
 |   58       v8::V8::Dispose(); | 
 |   59       v8::V8::ShutdownPlatform(); | 
 |   60     } | 
 |   61     std::unique_ptr<v8::Platform> platform; | 
 |   62   public: | 
 |   63     static void Init() | 
 |   64     { | 
 |   65       // it's threadsafe since C++11 and it will be instantiated only once and d
     estroyed at the | 
 |   66       // application exit | 
 |   67       static V8Initializer initializer; | 
 |   68     } | 
 |   69   }; | 
 |   70  | 
 |   71 } | 
 |   72  | 
 |   73 AdblockPlus::JsEngine::JsEngine(PrivateCtrArg) | 
|   48 { |   74 { | 
|   49 } |   75 } | 
|   50  |   76  | 
|   51 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) |   77 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 
|   52 { |   78 { | 
|   53   JsEnginePtr result(new JsEngine()); |   79   V8Initializer::Init(); | 
|   54  |   80   auto result = std::make_shared<JsEngine>(PrivateCtrArg{}); | 
|   55   const v8::Locker locker(result->isolate); |   81  | 
|   56   const v8::HandleScope handleScope; |   82   const v8::Locker locker{result->isolate}; | 
|   57  |   83   const v8::HandleScope handleScope{result->isolate}; | 
|   58   result->context.reset(result->isolate, v8::Context::New(result->isolate)); |   84   result->context = v8::UniquePersistent<v8::Context>{result->isolate, v8::Conte
     xt::New(result->isolate)}; | 
|   59   AdblockPlus::GlobalJsObject::Setup(result, appInfo, |   85   auto localContext = v8::Local<v8::Object>::Cast(v8::Local<v8::Context>::New(re
     sult->isolate, result->context)->Global()); | 
|   60       JsValuePtr(new JsValue(result, v8::Local<v8::Context>::New(result->isolate
     , result->context)->Global(), JsValue::Private::CtrArg()))); |   86  | 
 |   87   v8::Context::Scope context_scope{v8::Local<v8::Context>::New(result->isolate, 
     result->context)}; | 
 |   88   JsValue jsValueContext(result, localContext, JsValue::Private::CtrArg{}); | 
 |   89   AdblockPlus::GlobalJsObject::Setup(result, appInfo, jsValueContext); | 
 |   90  | 
|   61   return result; |   91   return result; | 
|   62 } |   92 } | 
|   63  |   93  | 
 |   94 AdblockPlus::JsEngine::~JsEngine() | 
 |   95 { | 
 |   96 } | 
 |   97  | 
|   64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
     e, |   98 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
     e, | 
|   65     const std::string& filename) |   99     const std::string& filename) | 
|   66 { |  100 { | 
|   67   const JsContext context(shared_from_this()); |  101   const JsContext context{*this}; | 
|   68   const v8::TryCatch tryCatch; |  102   const v8::TryCatch tryCatch; | 
|   69   const v8::Handle<v8::Script> script = CompileScript(source, filename); |  103   const v8::Handle<v8::Script> script = CompileScript(isolate, source, filename)
     ; | 
|   70   CheckTryCatch(tryCatch); |  104   CheckTryCatch(tryCatch); | 
|   71   v8::Local<v8::Value> result = script->Run(); |  105   v8::Local<v8::Value> result = script->Run(); | 
|   72   CheckTryCatch(tryCatch); |  106   CheckTryCatch(tryCatch); | 
|   73   return JsValuePtr(new JsValue(shared_from_this(), result, JsValue::Private::Ct
     rArg())); |  107   return std::make_shared<JsValue>(shared_from_this(), result, JsValue::Private:
     :CtrArg{}); | 
|   74 } |  108 } | 
|   75  |  109  | 
|   76 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |  110 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 
|   77     AdblockPlus::JsEngine::EventCallback callback) |  111     AdblockPlus::JsEngine::EventCallback callback) | 
|   78 { |  112 { | 
|   79   eventCallbacks[eventName] = callback; |  113   eventCallbacks[eventName] = callback; | 
|   80 } |  114 } | 
|   81  |  115  | 
|   82 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) |  116 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) | 
|   83 { |  117 { | 
|   84   eventCallbacks.erase(eventName); |  118   eventCallbacks.erase(eventName); | 
|   85 } |  119 } | 
|   86  |  120  | 
|   87 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
     us::JsValueList& params) |  121 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
     us::JsValueList& params) | 
|   88 { |  122 { | 
|   89   EventMap::iterator it = eventCallbacks.find(eventName); |  123   EventMap::iterator it = eventCallbacks.find(eventName); | 
|   90   if (it != eventCallbacks.end()) |  124   if (it != eventCallbacks.end()) | 
|   91     it->second(params); |  125     it->second(params); | 
|   92 } |  126 } | 
|   93  |  127  | 
|   94 void AdblockPlus::JsEngine::Gc() |  128 void AdblockPlus::JsEngine::Gc() | 
|   95 { |  129 { | 
|   96   while (!v8::V8::IdleNotification()); |  130   while (!isolate->IdleNotification(g_kIdleNotificationTimeuotMsec)); | 
|   97 } |  131 } | 
|   98  |  132  | 
|   99 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |  133 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 
|  100 { |  134 { | 
|  101   const JsContext context(shared_from_this()); |  135   const JsContext context{*this}; | 
|  102   return JsValuePtr(new JsValue(shared_from_this(), |  136   return std::make_shared<JsValue>(shared_from_this(), | 
|  103       v8::String::New(val.c_str(), val.length()), JsValue::Private::CtrArg())); |  137     AdblockPlus::Utils::ToV8String(isolate, val), | 
 |  138     JsValue::Private::CtrArg{}); | 
|  104 } |  139 } | 
|  105  |  140  | 
|  106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |  141 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 
|  107 { |  142 { | 
|  108   const JsContext context(shared_from_this()); |  143   const JsContext context{*this}; | 
|  109   return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val), JsValu
     e::Private::CtrArg())); |  144   return std::make_shared<JsValue>(shared_from_this(), v8::Number::New(isolate, 
     val), | 
 |  145     JsValue::Private::CtrArg{}); | 
|  110 } |  146 } | 
|  111  |  147  | 
|  112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |  148 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 
|  113 { |  149 { | 
|  114   const JsContext context(shared_from_this()); |  150   const JsContext context{*this}; | 
|  115   return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val), JsVal
     ue::Private::CtrArg())); |  151   return std::make_shared<JsValue>(shared_from_this(), v8::Boolean::New(isolate,
      val), | 
 |  152     JsValue::Private::CtrArg{}); | 
|  116 } |  153 } | 
|  117  |  154  | 
|  118 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |  155 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 
|  119 { |  156 { | 
|  120   const JsContext context(shared_from_this()); |  157   const JsContext context{*this}; | 
|  121   return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(), JsValue::
     Private::CtrArg())); |  158   return std::make_shared<JsValue>(shared_from_this(), v8::Object::New(isolate), | 
 |  159     JsValue::Private::CtrArg{}); | 
|  122 } |  160 } | 
|  123  |  161  | 
|  124 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |  162 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 
|  125     v8::InvocationCallback callback) |  163   v8::FunctionCallback callback) | 
|  126 { |  164 { | 
|  127   const JsContext context(shared_from_this()); |  165   const JsContext context{*this}; | 
|  128  |  166  | 
|  129   // Note: we are leaking this weak pointer, no obvious way to destroy it when |  167   // Note: we are leaking this weak pointer, no obvious way to destroy it when | 
|  130   // it's no longer used |  168   // it's no longer used | 
|  131   std::tr1::weak_ptr<JsEngine>* data = |  169   std::tr1::weak_ptr<JsEngine>* data = | 
|  132       new std::tr1::weak_ptr<JsEngine>(shared_from_this()); |  170       new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 
|  133   v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |  171   v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate, cal
     lback, | 
|  134       v8::External::New(data)); |  172       v8::External::New(isolate, data)); | 
|  135   return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction(), JsValu
     e::Private::CtrArg())); |  173   return std::make_shared<JsValue>(shared_from_this(), templ->GetFunction(), JsV
     alue::Private::CtrArg{}); | 
|  136 } |  174 } | 
|  137  |  175  | 
|  138 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
     s& arguments) |  176 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Function
     CallbackInfo<v8::Value>& arguments) | 
|  139 { |  177 { | 
|  140   const v8::Local<const v8::External> external = |  178   const v8::Local<const v8::External> external = | 
|  141       v8::Local<const v8::External>::Cast(arguments.Data()); |  179       v8::Local<const v8::External>::Cast(arguments.Data()); | 
|  142   std::tr1::weak_ptr<JsEngine>* data = |  180   std::tr1::weak_ptr<JsEngine>* data = | 
|  143       static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); |  181       static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 
|  144   JsEnginePtr result = data->lock(); |  182   JsEnginePtr result = data->lock(); | 
|  145   if (!result) |  183   if (!result) | 
|  146     throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
     ; |  184     throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
     ; | 
|  147   return result; |  185   return result; | 
|  148 } |  186 } | 
|  149  |  187  | 
|  150 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
     ents& arguments) |  188 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Funct
     ionCallbackInfo<v8::Value>& arguments) | 
|  151 { |  189 { | 
|  152   const JsContext context(shared_from_this()); |  190   const JsContext context{*this}; | 
|  153   JsValueList list; |  191   JsValueList list; | 
|  154   for (int i = 0; i < arguments.Length(); i++) |  192   for (int i = 0; i < arguments.Length(); i++) | 
|  155     list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i], JsVa
     lue::Private::CtrArg()))); |  193     list.emplace_back(std::make_shared<JsValue>(shared_from_this(), arguments[i]
     , JsValue::Private::CtrArg{})); | 
|  156   return list; |  194   return list; | 
|  157 } |  195 } | 
|  158  |  196  | 
|  159 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() |  197 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | 
|  160 { |  198 { | 
|  161   if (!fileSystem) |  199   if (!fileSystem) | 
|  162     fileSystem.reset(new DefaultFileSystem()); |  200     fileSystem.reset(new DefaultFileSystem()); | 
|  163   return fileSystem; |  201   return fileSystem; | 
|  164 } |  202 } | 
|  165  |  203  | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|  193   return logSystem; |  231   return logSystem; | 
|  194 } |  232 } | 
|  195  |  233  | 
|  196 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) |  234 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 
|  197 { |  235 { | 
|  198   if (!val) |  236   if (!val) | 
|  199     throw std::runtime_error("LogSystem cannot be null"); |  237     throw std::runtime_error("LogSystem cannot be null"); | 
|  200  |  238  | 
|  201   logSystem = val; |  239   logSystem = val; | 
|  202 } |  240 } | 
| OLD | NEW |