Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/Platform.cpp

Issue 29535600: Issue 5617 - Provide with async executor bound to Platform in the future (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created Sept. 4, 2017, 12:03 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present 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 #include <AdblockPlus/Platform.h> 17 #include <AdblockPlus/Platform.h>
18 #include <AdblockPlus/JsEngine.h> 18 #include <AdblockPlus/JsEngine.h>
19 #include <AdblockPlus/FilterEngine.h> 19 #include <AdblockPlus/FilterEngine.h>
20 #include <AdblockPlus/DefaultLogSystem.h> 20 #include <AdblockPlus/DefaultLogSystem.h>
21 #include "DefaultTimer.h" 21 #include "DefaultTimer.h"
22 #include "DefaultWebRequest.h" 22 #include "DefaultWebRequest.h"
23 #include "DefaultFileSystem.h" 23 #include "DefaultFileSystem.h"
24 #include <stdexcept>
24 25
25 using namespace AdblockPlus; 26 using namespace AdblockPlus;
26 27
27 namespace 28 namespace
28 { 29 {
29 void DummyScheduler(const AdblockPlus::SchedulerTask& task) 30 void DummyScheduler(const AdblockPlus::SchedulerTask& task)
30 { 31 {
31 std::thread(task).detach(); 32 std::thread(task).detach();
32 } 33 }
34
35 template<typename T>
36 void ValidatePlatformCreationParameter(const std::unique_ptr<T>& param, const char* paramName)
37 {
38 if (!param)
39 throw std::logic_error(paramName + std::string(" must not be nullptr"));
40 }
33 } 41 }
34 42
35 TimerPtr AdblockPlus::CreateDefaultTimer() 43 #define ASSIGN_PLATFORM_PARAM(param) ValidatePlatformCreationParameter(param = s td::move(creationParameters.param), #param)
36 {
37 return TimerPtr(new DefaultTimer());
38 }
39
40 FileSystemPtr AdblockPlus::CreateDefaultFileSystem(const Scheduler& scheduler, c onst std::string& basePath)
41 {
42 return FileSystemPtr(new DefaultFileSystem(scheduler, std::unique_ptr<DefaultF ileSystemSync>(new DefaultFileSystemSync(basePath))));
43 }
44
45 WebRequestPtr AdblockPlus::CreateDefaultWebRequest(const Scheduler& scheduler, W ebRequestSyncPtr syncImpl)
46 {
47 if (!syncImpl)
48 syncImpl.reset(new DefaultWebRequestSync());
49 return WebRequestPtr(new DefaultWebRequest(scheduler, std::move(syncImpl)));
50 }
51
52 LogSystemPtr AdblockPlus::CreateDefaultLogSystem()
53 {
54 return LogSystemPtr(new DefaultLogSystem());
55 }
56 44
57 Platform::Platform(CreationParameters&& creationParameters) 45 Platform::Platform(CreationParameters&& creationParameters)
58 { 46 {
59 logSystem = creationParameters.logSystem ? std::move(creationParameters.logSys tem) : CreateDefaultLogSystem(); 47 ASSIGN_PLATFORM_PARAM(logSystem);
60 timer = creationParameters.timer ? std::move(creationParameters.timer) : Creat eDefaultTimer(); 48 ASSIGN_PLATFORM_PARAM(timer);
61 fileSystem = creationParameters.fileSystem ? std::move(creationParameters.file System) : CreateDefaultFileSystem(::DummyScheduler); 49 ASSIGN_PLATFORM_PARAM(fileSystem);
62 webRequest = creationParameters.webRequest ? std::move(creationParameters.webR equest) : CreateDefaultWebRequest(::DummyScheduler); 50 ASSIGN_PLATFORM_PARAM(webRequest);
63 } 51 }
64 52
65 Platform::~Platform() 53 Platform::~Platform()
66 { 54 {
67 } 55 }
68 56
69 void Platform::SetUpJsEngine(const AppInfo& appInfo, std::unique_ptr<IV8IsolateP rovider> isolate) 57 void Platform::SetUpJsEngine(const AppInfo& appInfo, std::unique_ptr<IV8IsolateP rovider> isolate)
70 { 58 {
71 std::lock_guard<std::mutex> lock(modulesMutex); 59 std::lock_guard<std::mutex> lock(modulesMutex);
72 if (jsEngine) 60 if (jsEngine)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 106 }
119 107
120 IWebRequest& Platform::GetWebRequest() 108 IWebRequest& Platform::GetWebRequest()
121 { 109 {
122 return *webRequest; 110 return *webRequest;
123 } 111 }
124 112
125 LogSystem& Platform::GetLogSystem() 113 LogSystem& Platform::GetLogSystem()
126 { 114 {
127 return *logSystem; 115 return *logSystem;
116 }
117
118 namespace
119 {
120 class DefaultPlatform : public Platform
121 {
122 public:
123 typedef std::shared_ptr<Scheduler> AsyncExecutorPtr;
124 explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationPara meters&& creationParams)
125 : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor)
126 {
127 }
128 ~DefaultPlatform()
129 {
130 asyncExecutor.reset();
sergei 2017/09/04 12:16:27 While the actual executor is not ready yet (https:
131 }
132 private:
133 AsyncExecutorPtr asyncExecutor;
134 };
135 }
136
137 Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor()
138 {
139 if (!defaultScheduler)
140 {
141 asyncExecutor = std::make_shared<Scheduler>(::DummyScheduler);
142 std::weak_ptr<Scheduler> weakExecutor = asyncExecutor;
143 defaultScheduler = [weakExecutor](const SchedulerTask& task)
144 {
145 if (auto executor = weakExecutor.lock())
146 {
147 (*executor)(task);
148 }
149 };
150 }
151 return defaultScheduler;
152 }
153
154 void DefaultPlatformBuilder::CreateDefaultTimer()
155 {
156 timer.reset(new DefaultTimer());
157 }
158
159 void DefaultPlatformBuilder::CreateDefaultFileSystem(const std::string& basePath )
160 {
161 fileSystem.reset(new DefaultFileSystem(GetDefaultAsyncExecutor(), std::unique_ ptr<DefaultFileSystemSync>(new DefaultFileSystemSync(basePath))));
162 }
163
164 void DefaultPlatformBuilder::CreateDefaultWebRequest(std::unique_ptr<IWebRequest Sync> webRequest)
165 {
166 if (!webRequest)
167 webRequest.reset(new DefaultWebRequestSync());
168 this->webRequest.reset(new DefaultWebRequest(GetDefaultAsyncExecutor(), std::m ove(webRequest)));
169 }
170
171 void DefaultPlatformBuilder::CreateDefaultLogSystem()
172 {
173 logSystem.reset(new DefaultLogSystem());
174 }
175
176 std::unique_ptr<Platform> DefaultPlatformBuilder::CreatePlatform()
177 {
178 if (!logSystem)
179 CreateDefaultLogSystem();
180 if (!timer)
181 CreateDefaultTimer();
182 if (!fileSystem)
183 CreateDefaultFileSystem();
184 if (!webRequest)
185 CreateDefaultWebRequest();
186
187 std::unique_ptr<Platform> platform(new DefaultPlatform(asyncExecutor, std::mov e(*this)));
188 asyncExecutor.reset();
sergei 2017/09/04 12:16:27 This `reset` is merely to ensure that async execut
189 return platform;
128 } 190 }
OLDNEW
« no previous file with comments | « shell/src/Main.cpp ('k') | test/DefaultFileSystem.cpp » ('j') | test/WebRequest.cpp » ('J')

Powered by Google App Engine
This is Rietveld