OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 #ifndef ADBLOCK_PLUS_PLATFORM_H |
| 19 #define ADBLOCK_PLUS_PLATFORM_H |
| 20 |
| 21 #include "LogSystem.h" |
| 22 #include "ITimer.h" |
| 23 #include "IFileSystem.h" |
| 24 #include "IWebRequest.h" |
| 25 #include "AppInfo.h" |
| 26 #include "Scheduler.h" |
| 27 #include "FilterEngine.h" |
| 28 #include <mutex> |
| 29 #include <future> |
| 30 |
| 31 namespace AdblockPlus |
| 32 { |
| 33 class IV8IsolateProvider; |
| 34 class JsEngine; |
| 35 |
| 36 /** |
| 37 * AdblockPlus platform is the main component providing access to other |
| 38 * modules. |
| 39 * |
| 40 * It manages the functionality modules, e.g. JsEngine and FilterEngine as |
| 41 * well as allows to correctly work with asynchronous functionality. |
| 42 */ |
| 43 class Platform |
| 44 { |
| 45 public: |
| 46 /** |
| 47 * Platform creation parameters. |
| 48 * |
| 49 * @param logSystem Implementation of log system. |
| 50 * @param timer Implementation of timer. |
| 51 * @param webRequest Implementation of web request. |
| 52 * @param fileSystem Implementation of filesystem. |
| 53 */ |
| 54 struct CreationParameters |
| 55 { |
| 56 LogSystemPtr logSystem; |
| 57 TimerPtr timer; |
| 58 WebRequestPtr webRequest; |
| 59 FileSystemPtr fileSystem; |
| 60 }; |
| 61 |
| 62 /** |
| 63 * Callback type invoked when FilterEngine is created. |
| 64 */ |
| 65 typedef std::function<void(const FilterEngine&)> OnFilterEngineCreatedCallba
ck; |
| 66 |
| 67 /** |
| 68 * Platform constructor. |
| 69 * |
| 70 * When a parameter value is nullptr the corresponding default |
| 71 * implementation is chosen. |
| 72 */ |
| 73 explicit Platform(CreationParameters&& creationParameters = CreationParamete
rs()); |
| 74 virtual ~Platform(); |
| 75 |
| 76 /** |
| 77 * Ensures that JsEngine is constructed. If JsEngine is already present |
| 78 * then the parameters are ignored. |
| 79 * |
| 80 * @param appInfo Information about the app, |
| 81 * @param isolate A provider of v8::Isolate, if the value is nullptr then |
| 82 * a default implementation is used. |
| 83 */ |
| 84 void SetUpJsEngine(const AppInfo& appInfo = AppInfo(), std::unique_ptr<IV8Is
olateProvider> isolate = nullptr); |
| 85 |
| 86 /** |
| 87 * Retrieves the `JsEngine` instance. It calls SetUpJsEngine if JsEngine is |
| 88 * not initialized yet. |
| 89 */ |
| 90 JsEngine& GetJsEngine(); |
| 91 |
| 92 /** |
| 93 * Ensures that FilterEngine is constructed. Only the first call is effectiv
e. |
| 94 * |
| 95 * @param parameters optional creation parameters. |
| 96 * @param onCreated A callback which is called when FilterEngine is ready |
| 97 * for use. |
| 98 */ |
| 99 void CreateFilterEngineAsync(const FilterEngine::CreationParameters& paramet
ers = FilterEngine::CreationParameters(), |
| 100 const OnFilterEngineCreatedCallback& onCreated = OnFilterEngineCreatedCall
back()); |
| 101 |
| 102 /** |
| 103 * Synchronous equivalent of `CreateFilterEngineAsync`. |
| 104 * Internally it blocks and waits for finishing of certain asynchronous |
| 105 * operations, please ensure that provided implementation does not lead to |
| 106 * a dead lock. |
| 107 */ |
| 108 FilterEngine& GetFilterEngine(); |
| 109 |
| 110 /** |
| 111 * @return The asynchronous ITimer implementation. |
| 112 */ |
| 113 ITimer& GetTimer(); |
| 114 |
| 115 /** |
| 116 * @return The asynchronous IFileSystem implementation. |
| 117 */ |
| 118 IFileSystem& GetFileSystem(); |
| 119 |
| 120 /** |
| 121 * @return The asynchronous IWebRequest implementation. |
| 122 */ |
| 123 IWebRequest& GetWebRequest(); |
| 124 |
| 125 /** |
| 126 * @return The LogSystem implementation. |
| 127 */ |
| 128 LogSystem& GetLogSystem(); |
| 129 |
| 130 private: |
| 131 LogSystemPtr logSystem; |
| 132 TimerPtr timer; |
| 133 FileSystemPtr fileSystem; |
| 134 WebRequestPtr webRequest; |
| 135 // used for creation and deletion of modules. |
| 136 std::mutex modulesMutex; |
| 137 std::shared_ptr<JsEngine> jsEngine; |
| 138 std::shared_future<FilterEnginePtr> filterEngine; |
| 139 }; |
| 140 |
| 141 /** |
| 142 * A helper class allowing to construct a default Platform and to obtain |
| 143 * the Scheduler used by Platform before the latter is constructed. |
| 144 */ |
| 145 class DefaultPlatformBuilder : public Platform::CreationParameters |
| 146 { |
| 147 public: |
| 148 /** |
| 149 * Constructs a default executor for asynchronous tasks. When Platform |
| 150 * is being destroyed it starts to ignore new tasks and waits for finishing |
| 151 * of already running tasks. |
| 152 * @return Scheduler allowing to execute tasks asynchronously. |
| 153 */ |
| 154 Scheduler GetDefaultAsyncExecutor(); |
| 155 |
| 156 /** |
| 157 * Constructs default implementation of `ITimer`. |
| 158 */ |
| 159 void CreateDefaultTimer(); |
| 160 |
| 161 /** |
| 162 * Constructs default implementation of `IFileSystem`. |
| 163 * @param basePath A working directory for file system operations. |
| 164 */ |
| 165 void CreateDefaultFileSystem(const std::string& basePath = std::string()); |
| 166 |
| 167 /** |
| 168 * Constructs default implementation of `IWebRequest`. |
| 169 */ |
| 170 void CreateDefaultWebRequest(WebRequestSyncPtr webRequest = nullptr); |
| 171 |
| 172 /** |
| 173 * Constructs default implementation of `LogSystem`. |
| 174 */ |
| 175 void CreateDefaultLogSystem(); |
| 176 |
| 177 /** |
| 178 * Constructs Platform with default implementations of platform interfaces |
| 179 * when a corresponding field is nullptr and with a default Scheduler. |
| 180 */ |
| 181 std::unique_ptr<Platform> CreatePlatform(); |
| 182 private: |
| 183 std::shared_ptr<Scheduler> asyncExecutor; |
| 184 Scheduler defaultScheduler; |
| 185 }; |
| 186 } |
| 187 |
| 188 #endif // ADBLOCK_PLUS_PLATFORM_H |
OLD | NEW |