| 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-2013 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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 #ifndef ADBLOCK_PLUS_JS_ENGINE_H | 18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H |
| 19 #define ADBLOCK_PLUS_JS_ENGINE_H | 19 #define ADBLOCK_PLUS_JS_ENGINE_H |
| 20 | 20 |
| 21 #include <map> | 21 #include <map> |
| 22 #include <stdexcept> | 22 #include <stdexcept> |
| 23 #include <stdint.h> | 23 #include <stdint.h> |
| 24 #include <string> | 24 #include <string> |
| 25 #include <v8.h> | |
| 26 #include <AdblockPlus/AppInfo.h> | 25 #include <AdblockPlus/AppInfo.h> |
| 27 #include <AdblockPlus/tr1_functional.h> | 26 #include <AdblockPlus/tr1_functional.h> |
| 28 #include <AdblockPlus/LogSystem.h> | 27 #include <AdblockPlus/LogSystem.h> |
| 29 #include <AdblockPlus/FileSystem.h> | 28 #include <AdblockPlus/FileSystem.h> |
| 30 #include <AdblockPlus/JsValue.h> | 29 #include <AdblockPlus/JsValue.h> |
| 31 #include <AdblockPlus/WebRequest.h> | 30 #include <AdblockPlus/WebRequest.h> |
| 32 | 31 |
| 33 #include "tr1_memory.h" | 32 #include "tr1_memory.h" |
| 34 | 33 |
| 34 namespace v8 |
| 35 { |
| 36 class Arguments; |
| 37 class Isolate; |
| 38 class Context; |
| 39 template <class T> class Handle; |
| 40 template <class T> class Persistent; |
| 41 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); |
| 42 } |
| 43 |
| 35 namespace AdblockPlus | 44 namespace AdblockPlus |
| 36 { | 45 { |
| 37 class JsError : public std::runtime_error | |
| 38 { | |
| 39 public: | |
| 40 JsError(const v8::Handle<v8::Value> exception, | |
| 41 const v8::Handle<v8::Message> message); | |
| 42 }; | |
| 43 | |
| 44 class JsEngine; | 46 class JsEngine; |
| 45 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 47 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; |
| 46 | 48 |
| 47 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> | 49 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> |
| 48 { | 50 { |
| 49 friend class JsValue; | 51 friend class JsValue; |
| 52 friend class JsContext; |
| 50 | 53 |
| 51 public: | 54 public: |
| 52 typedef std::tr1::function<void()> EventCallback; | 55 typedef std::tr1::function<void()> EventCallback; |
| 53 typedef std::map<std::string, EventCallback> EventMap; | 56 typedef std::map<std::string, EventCallback> EventMap; |
| 54 | 57 |
| 55 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 58 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
| 56 void SetEventCallback(const std::string& eventName, EventCallback callback); | 59 void SetEventCallback(const std::string& eventName, EventCallback callback); |
| 57 void RemoveEventCallback(const std::string& eventName); | 60 void RemoveEventCallback(const std::string& eventName); |
| 58 void TriggerEvent(const std::string& eventName); | 61 void TriggerEvent(const std::string& eventName); |
| 59 JsValuePtr Evaluate(const std::string& source, | 62 JsValuePtr Evaluate(const std::string& source, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 81 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 84 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 82 JsValueList ConvertArguments(const v8::Arguments& arguments); | 85 JsValueList ConvertArguments(const v8::Arguments& arguments); |
| 83 | 86 |
| 84 FileSystemPtr GetFileSystem(); | 87 FileSystemPtr GetFileSystem(); |
| 85 void SetFileSystem(FileSystemPtr val); | 88 void SetFileSystem(FileSystemPtr val); |
| 86 WebRequestPtr GetWebRequest(); | 89 WebRequestPtr GetWebRequest(); |
| 87 void SetWebRequest(WebRequestPtr val); | 90 void SetWebRequest(WebRequestPtr val); |
| 88 LogSystemPtr GetLogSystem(); | 91 LogSystemPtr GetLogSystem(); |
| 89 void SetLogSystem(LogSystemPtr val); | 92 void SetLogSystem(LogSystemPtr val); |
| 90 | 93 |
| 91 class Context | |
| 92 { | |
| 93 public: | |
| 94 Context(const JsEnginePtr jsEngine); | |
| 95 virtual inline ~Context() {}; | |
| 96 | |
| 97 private: | |
| 98 const v8::Locker locker; | |
| 99 const v8::HandleScope handleScope; | |
| 100 const v8::Context::Scope contextScope; | |
| 101 }; | |
| 102 | |
| 103 private: | 94 private: |
| 104 JsEngine(); | 95 JsEngine(); |
| 105 | 96 |
| 106 FileSystemPtr fileSystem; | 97 FileSystemPtr fileSystem; |
| 107 WebRequestPtr webRequest; | 98 WebRequestPtr webRequest; |
| 108 LogSystemPtr logSystem; | 99 LogSystemPtr logSystem; |
| 109 v8::Isolate* isolate; | 100 v8::Isolate* isolate; |
| 110 v8::Persistent<v8::Context> context; | 101 std::auto_ptr<v8::Persistent<v8::Context> > context; |
| 111 EventMap eventCallbacks; | 102 EventMap eventCallbacks; |
| 112 }; | 103 }; |
| 113 } | 104 } |
| 114 | 105 |
| 115 #endif | 106 #endif |
| OLD | NEW |