| LEFT | RIGHT |
| 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 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) | 51 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo) |
| 52 { | 52 { |
| 53 JsEnginePtr result(new JsEngine()); | 53 JsEnginePtr result(new JsEngine()); |
| 54 | 54 |
| 55 const v8::Locker locker(result->isolate); | 55 const v8::Locker locker(result->isolate); |
| 56 const v8::HandleScope handleScope; | 56 const v8::HandleScope handleScope; |
| 57 | 57 |
| 58 result->context.reset(result->isolate, v8::Context::New(result->isolate)); | 58 result->context.reset(result->isolate, v8::Context::New(result->isolate)); |
| 59 v8::Local<v8::Object> globalContext = v8::Local<v8::Context>::New( |
| 60 result->isolate, result->context)->Global(); |
| 59 AdblockPlus::GlobalJsObject::Setup(result, appInfo, | 61 AdblockPlus::GlobalJsObject::Setup(result, appInfo, |
| 60 JsValuePtr(new JsValue(result, v8::Local<v8::Context>::New(result->isolate
, result->context)->Global(), JsValue::Private::CtrArg()))); | 62 JsValuePtr(new JsValue(result, globalContext))); |
| 61 return result; | 63 return result; |
| 62 } | 64 } |
| 63 | 65 |
| 64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, | 66 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc
e, |
| 65 const std::string& filename) | 67 const std::string& filename) |
| 66 { | 68 { |
| 67 const JsContext context(shared_from_this()); | 69 const JsContext context(shared_from_this()); |
| 68 const v8::TryCatch tryCatch; | 70 const v8::TryCatch tryCatch; |
| 69 const v8::Handle<v8::Script> script = CompileScript(source, filename); | 71 const v8::Handle<v8::Script> script = CompileScript(source, filename); |
| 70 CheckTryCatch(tryCatch); | 72 CheckTryCatch(tryCatch); |
| 71 v8::Local<v8::Value> result = script->Run(); | 73 v8::Local<v8::Value> result = script->Run(); |
| 72 CheckTryCatch(tryCatch); | 74 CheckTryCatch(tryCatch); |
| 73 return JsValuePtr(new JsValue(shared_from_this(), result, JsValue::Private::Ct
rArg())); | 75 return JsValuePtr(new JsValue(shared_from_this(), result)); |
| 74 } | 76 } |
| 75 | 77 |
| 76 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 78 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
| 77 AdblockPlus::JsEngine::EventCallback callback) | 79 AdblockPlus::JsEngine::EventCallback callback) |
| 78 { | 80 { |
| 79 eventCallbacks[eventName] = callback; | 81 eventCallbacks[eventName] = callback; |
| 80 } | 82 } |
| 81 | 83 |
| 82 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) | 84 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) |
| 83 { | 85 { |
| 84 eventCallbacks.erase(eventName); | 86 eventCallbacks.erase(eventName); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) | 89 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) |
| 88 { | 90 { |
| 89 EventMap::iterator it = eventCallbacks.find(eventName); | 91 EventMap::iterator it = eventCallbacks.find(eventName); |
| 90 if (it != eventCallbacks.end()) | 92 if (it != eventCallbacks.end()) |
| 91 it->second(params); | 93 it->second(params); |
| 92 } | 94 } |
| 93 | 95 |
| 94 void AdblockPlus::JsEngine::Gc() | 96 void AdblockPlus::JsEngine::Gc() |
| 95 { | 97 { |
| 96 while (!v8::V8::IdleNotification()); | 98 while (!v8::V8::IdleNotification()); |
| 97 } | 99 } |
| 98 | 100 |
| 99 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) | 101 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) |
| 100 { | 102 { |
| 101 const JsContext context(shared_from_this()); | 103 const JsContext context(shared_from_this()); |
| 102 return JsValuePtr(new JsValue(shared_from_this(), | 104 return JsValuePtr(new JsValue(shared_from_this(), |
| 103 v8::String::New(val.c_str(), val.length()), JsValue::Private::CtrArg())); | 105 v8::String::New(val.c_str(), val.length()))); |
| 104 } | 106 } |
| 105 | 107 |
| 106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) | 108 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) |
| 107 { | 109 { |
| 108 const JsContext context(shared_from_this()); | 110 const JsContext context(shared_from_this()); |
| 109 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val), JsValu
e::Private::CtrArg())); | 111 return JsValuePtr(new JsValue(shared_from_this(), v8::Number::New(val))); |
| 110 } | 112 } |
| 111 | 113 |
| 112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) | 114 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) |
| 113 { | 115 { |
| 114 const JsContext context(shared_from_this()); | 116 const JsContext context(shared_from_this()); |
| 115 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val), JsVal
ue::Private::CtrArg())); | 117 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); |
| 116 } | 118 } |
| 117 | 119 |
| 118 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 120 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
| 119 { | 121 { |
| 120 const JsContext context(shared_from_this()); | 122 const JsContext context(shared_from_this()); |
| 121 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New(), JsValue::
Private::CtrArg())); | 123 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); |
| 122 } | 124 } |
| 123 | 125 |
| 124 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 126 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
| 125 v8::InvocationCallback callback) | 127 v8::InvocationCallback callback) |
| 126 { | 128 { |
| 127 const JsContext context(shared_from_this()); | 129 const JsContext context(shared_from_this()); |
| 128 | 130 |
| 129 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 131 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
| 130 // it's no longer used | 132 // it's no longer used |
| 131 std::tr1::weak_ptr<JsEngine>* data = | 133 std::tr1::weak_ptr<JsEngine>* data = |
| 132 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); | 134 new std::tr1::weak_ptr<JsEngine>(shared_from_this()); |
| 133 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 135 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |
| 134 v8::External::New(data)); | 136 v8::External::New(data)); |
| 135 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction(), JsValu
e::Private::CtrArg())); | 137 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); |
| 136 } | 138 } |
| 137 | 139 |
| 138 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) | 140 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument
s& arguments) |
| 139 { | 141 { |
| 140 const v8::Local<const v8::External> external = | 142 const v8::Local<const v8::External> external = |
| 141 v8::Local<const v8::External>::Cast(arguments.Data()); | 143 v8::Local<const v8::External>::Cast(arguments.Data()); |
| 142 std::tr1::weak_ptr<JsEngine>* data = | 144 std::tr1::weak_ptr<JsEngine>* data = |
| 143 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); | 145 static_cast<std::tr1::weak_ptr<JsEngine>*>(external->Value()); |
| 144 JsEnginePtr result = data->lock(); | 146 JsEnginePtr result = data->lock(); |
| 145 if (!result) | 147 if (!result) |
| 146 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; | 148 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?")
; |
| 147 return result; | 149 return result; |
| 148 } | 150 } |
| 149 | 151 |
| 150 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) | 152 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum
ents& arguments) |
| 151 { | 153 { |
| 152 const JsContext context(shared_from_this()); | 154 const JsContext context(shared_from_this()); |
| 153 JsValueList list; | 155 JsValueList list; |
| 154 for (int i = 0; i < arguments.Length(); i++) | 156 for (int i = 0; i < arguments.Length(); i++) |
| 155 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i], JsVa
lue::Private::CtrArg()))); | 157 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); |
| 156 return list; | 158 return list; |
| 157 } | 159 } |
| 158 | 160 |
| 159 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() | 161 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() |
| 160 { | 162 { |
| 161 if (!fileSystem) | 163 if (!fileSystem) |
| 162 fileSystem.reset(new DefaultFileSystem()); | 164 fileSystem.reset(new DefaultFileSystem()); |
| 163 return fileSystem; | 165 return fileSystem; |
| 164 } | 166 } |
| 165 | 167 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 193 return logSystem; | 195 return logSystem; |
| 194 } | 196 } |
| 195 | 197 |
| 196 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 198 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) |
| 197 { | 199 { |
| 198 if (!val) | 200 if (!val) |
| 199 throw std::runtime_error("LogSystem cannot be null"); | 201 throw std::runtime_error("LogSystem cannot be null"); |
| 200 | 202 |
| 201 logSystem = val; | 203 logSystem = val; |
| 202 } | 204 } |
| LEFT | RIGHT |