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