OLD | NEW |
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 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 onCreated(*filterEngine); | 88 onCreated(*filterEngine); |
89 }, parameters); | 89 }, parameters); |
90 } | 90 } |
91 | 91 |
92 FilterEngine& Platform::GetFilterEngine() | 92 FilterEngine& Platform::GetFilterEngine() |
93 { | 93 { |
94 CreateFilterEngineAsync(); | 94 CreateFilterEngineAsync(); |
95 return *std::shared_future<FilterEnginePtr>(filterEngine).get(); | 95 return *std::shared_future<FilterEnginePtr>(filterEngine).get(); |
96 } | 96 } |
97 | 97 |
98 ITimer& Platform::GetTimer() | 98 void Platform::WithTimer(const WithTimerCallback& callback) |
99 { | 99 { |
100 return *timer; | 100 if (timer && callback) |
| 101 callback(*timer); |
101 } | 102 } |
102 | 103 |
103 IFileSystem& Platform::GetFileSystem() | 104 void Platform::WithFileSystem(const WithFileSystemCallback& callback) |
104 { | 105 { |
105 return *fileSystem; | 106 if (fileSystem && callback) |
| 107 callback(*fileSystem); |
106 } | 108 } |
107 | 109 |
108 IWebRequest& Platform::GetWebRequest() | 110 void Platform::WithWebRequest(const WithWebRequestCallback& callback) |
109 { | 111 { |
110 return *webRequest; | 112 if (webRequest && callback) |
| 113 callback(*webRequest); |
111 } | 114 } |
112 | 115 |
113 LogSystem& Platform::GetLogSystem() | 116 void Platform::WithLogSystem(const WithLogSystemCallback& callback) |
114 { | 117 { |
115 return *logSystem; | 118 if (logSystem && callback) |
| 119 callback(*logSystem); |
116 } | 120 } |
117 | 121 |
118 namespace | 122 namespace |
119 { | 123 { |
120 class DefaultPlatform : public Platform | 124 class DefaultPlatform : public Platform |
121 { | 125 { |
122 public: | 126 public: |
123 typedef std::shared_ptr<Scheduler> AsyncExecutorPtr; | 127 typedef std::shared_ptr<Scheduler> AsyncExecutorPtr; |
124 explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationPara
meters&& creationParams) | 128 explicit DefaultPlatform(const AsyncExecutorPtr& asyncExecutor, CreationPara
meters&& creationParams) |
125 : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor) | 129 : Platform(std::move(creationParams)), asyncExecutor(asyncExecutor) |
126 { | 130 { |
127 } | 131 } |
128 ~DefaultPlatform() | 132 ~DefaultPlatform(); |
129 { | 133 |
130 asyncExecutor.reset(); | 134 void WithTimer(const WithTimerCallback&) override; |
131 } | 135 void WithFileSystem(const WithFileSystemCallback&) override; |
| 136 void WithWebRequest(const WithWebRequestCallback&) override; |
| 137 void WithLogSystem(const WithLogSystemCallback&) override; |
| 138 |
132 private: | 139 private: |
133 AsyncExecutorPtr asyncExecutor; | 140 AsyncExecutorPtr asyncExecutor; |
| 141 std::recursive_mutex interfacesMutex; |
134 }; | 142 }; |
| 143 |
| 144 DefaultPlatform::~DefaultPlatform() |
| 145 { |
| 146 asyncExecutor.reset(); |
| 147 LogSystemPtr tmpLogSystem; |
| 148 TimerPtr tmpTimer; |
| 149 FileSystemPtr tmpFileSystem; |
| 150 WebRequestPtr tmpWebRequest; |
| 151 { |
| 152 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); |
| 153 tmpLogSystem = std::move(logSystem); |
| 154 tmpTimer = std::move(timer); |
| 155 tmpFileSystem = std::move(fileSystem); |
| 156 tmpWebRequest = std::move(webRequest); |
| 157 } |
| 158 } |
| 159 |
| 160 void DefaultPlatform::WithTimer(const WithTimerCallback& callback) |
| 161 { |
| 162 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); |
| 163 Platform::WithTimer(callback); |
| 164 } |
| 165 |
| 166 void DefaultPlatform::WithFileSystem(const WithFileSystemCallback& callback) |
| 167 { |
| 168 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); |
| 169 Platform::WithFileSystem(callback); |
| 170 } |
| 171 |
| 172 void DefaultPlatform::WithWebRequest(const WithWebRequestCallback& callback) |
| 173 { |
| 174 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); |
| 175 Platform::WithWebRequest(callback); |
| 176 } |
| 177 |
| 178 void DefaultPlatform::WithLogSystem(const WithLogSystemCallback& callback) |
| 179 { |
| 180 std::lock_guard<std::recursive_mutex> lock(interfacesMutex); |
| 181 Platform::WithLogSystem(callback); |
| 182 } |
135 } | 183 } |
136 | 184 |
137 Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor() | 185 Scheduler DefaultPlatformBuilder::GetDefaultAsyncExecutor() |
138 { | 186 { |
139 if (!defaultScheduler) | 187 if (!defaultScheduler) |
140 { | 188 { |
141 asyncExecutor = std::make_shared<Scheduler>(::DummyScheduler); | 189 asyncExecutor = std::make_shared<Scheduler>(::DummyScheduler); |
142 std::weak_ptr<Scheduler> weakExecutor = asyncExecutor; | 190 std::weak_ptr<Scheduler> weakExecutor = asyncExecutor; |
143 defaultScheduler = [weakExecutor](const SchedulerTask& task) | 191 defaultScheduler = [weakExecutor](const SchedulerTask& task) |
144 { | 192 { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 if (!timer) | 228 if (!timer) |
181 CreateDefaultTimer(); | 229 CreateDefaultTimer(); |
182 if (!fileSystem) | 230 if (!fileSystem) |
183 CreateDefaultFileSystem(); | 231 CreateDefaultFileSystem(); |
184 if (!webRequest) | 232 if (!webRequest) |
185 CreateDefaultWebRequest(); | 233 CreateDefaultWebRequest(); |
186 | 234 |
187 std::unique_ptr<Platform> platform(new DefaultPlatform(asyncExecutor, std::mov
e(*this))); | 235 std::unique_ptr<Platform> platform(new DefaultPlatform(asyncExecutor, std::mov
e(*this))); |
188 asyncExecutor.reset(); | 236 asyncExecutor.reset(); |
189 return platform; | 237 return platform; |
190 } | 238 } |
OLD | NEW |