libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
Platform.h
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  struct IV8IsolateProvider;
34  class JsEngine;
35  class OptionalAsyncExecutor;
36 
44  class Platform
45  {
46  public:
56  {
57  LogSystemPtr logSystem;
58  TimerPtr timer;
59  WebRequestPtr webRequest;
60  FileSystemPtr fileSystem;
61  };
62 
66  typedef std::function<void(const FilterEngine&)> OnFilterEngineCreatedCallback;
67 
74  explicit Platform(CreationParameters&& creationParameters = CreationParameters());
75  virtual ~Platform();
76 
85  void SetUpJsEngine(const AppInfo& appInfo = AppInfo(), std::unique_ptr<IV8IsolateProvider> isolate = nullptr);
86 
92 
101  const OnFilterEngineCreatedCallback& onCreated = OnFilterEngineCreatedCallback());
102 
110 
111  typedef std::function<void(ITimer&)> WithTimerCallback;
112  virtual void WithTimer(const WithTimerCallback&);
113 
114  typedef std::function<void(IFileSystem&)> WithFileSystemCallback;
115  virtual void WithFileSystem(const WithFileSystemCallback&);
116 
117  typedef std::function<void(IWebRequest&)> WithWebRequestCallback;
118  virtual void WithWebRequest(const WithWebRequestCallback&);
119 
120  typedef std::function<void(LogSystem&)> WithLogSystemCallback;
121  virtual void WithLogSystem(const WithLogSystemCallback&);
122 
123  protected:
124  LogSystemPtr logSystem;
125  TimerPtr timer;
126  FileSystemPtr fileSystem;
127  WebRequestPtr webRequest;
128  private:
129  // used for creation and deletion of modules.
130  std::mutex modulesMutex;
131  std::shared_ptr<JsEngine> jsEngine;
132  std::shared_future<FilterEnginePtr> filterEngine;
133  };
134 
140  {
141  public:
145  typedef std::shared_ptr<OptionalAsyncExecutor> AsyncExecutorPtr;
146 
148 
156 
160  void CreateDefaultTimer();
161 
166  void CreateDefaultFileSystem(const std::string& basePath = std::string());
167 
171  void CreateDefaultWebRequest(WebRequestSyncPtr webRequest = nullptr);
172 
176  void CreateDefaultLogSystem();
177 
182  std::unique_ptr<Platform> CreatePlatform();
183  private:
184  AsyncExecutorPtr sharedAsyncExecutor;
185  Scheduler defaultScheduler;
186  };
187 }
188 
189 #endif // ADBLOCK_PLUS_PLATFORM_H
JsEngine & GetJsEngine()
Retrieves the JsEngine instance.
std::unique_ptr< ITimer > TimerPtr
Unique smart pointer to an instance of ITimer implementation.
Definition: ITimer.h:49
Platform creation parameters.
Definition: Platform.h:55
std::unique_ptr< IWebRequest > WebRequestPtr
Unique smart pointer to an instance of IWebRequest implementation.
Definition: IWebRequest.h:116
void CreateFilterEngineAsync(const FilterEngine::CreationParameters &parameters=FilterEngine::CreationParameters(), const OnFilterEngineCreatedCallback &onCreated=OnFilterEngineCreatedCallback())
Ensures that FilterEngine is constructed.
void CreateDefaultLogSystem()
Constructs default implementation of LogSystem.
std::shared_ptr< OptionalAsyncExecutor > AsyncExecutorPtr
Private.
Definition: Platform.h:145
Information about the app using libadblockplus.
Definition: AppInfo.h:28
JavaScript engine used by FilterEngine, wraps v8.
Definition: JsEngine.h:73
std::function< void(const SchedulerTask &)> Scheduler
Scheduler object executing tasks asynchronously.
Definition: Scheduler.h:31
A helper class allowing to construct a default Platform and to obtain the Scheduler used by Platform ...
Definition: Platform.h:139
Platform(CreationParameters &&creationParameters=CreationParameters())
Platform constructor.
Main component of libadblockplus.
Definition: FilterEngine.h:192
void CreateDefaultWebRequest(WebRequestSyncPtr webRequest=nullptr)
Constructs default implementation of IWebRequest.
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
AdblockPlus platform is the main component providing access to other modules.
Definition: Platform.h:44
std::function< void(const FilterEngine &)> OnFilterEngineCreatedCallback
Callback type invoked when FilterEngine is created.
Definition: Platform.h:66
std::unique_ptr< LogSystem > LogSystemPtr
Smart pointer to a LogSystem instance.
Definition: LogSystem.h:53
FilterEngine & GetFilterEngine()
Synchronous equivalent of CreateFilterEngineAsync.
std::unique_ptr< IFileSystem > FileSystemPtr
Unique smart pointer to to a IFileSystem instance.
Definition: IFileSystem.h:132
void CreateDefaultTimer()
Constructs default implementation of ITimer.
void CreateDefaultFileSystem(const std::string &basePath=std::string())
Constructs default implementation of IFileSystem.
void SetUpJsEngine(const AppInfo &appInfo=AppInfo(), std::unique_ptr< IV8IsolateProvider > isolate=nullptr)
Ensures that JsEngine is constructed.
FilterEngine creation parameters.
Definition: FilterEngine.h:270
Scheduler GetDefaultAsyncExecutor()
Constructs a default executor for asynchronous tasks.
std::unique_ptr< Platform > CreatePlatform()
Constructs Platform with default implementations of platform interfaces when a corresponding field is...