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

Side by Side Diff: test/BaseJsTest.h

Issue 29731562: Issue 6477 - separate done and error callbacks in IFileSystem::Read (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git@c0a6434596a83383e37678ef3b6ecef00ed6a261
Patch Set: Created March 23, 2018, 10:58 a.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
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 void operator()(LogLevel logLevel, const std::string& message, 101 void operator()(LogLevel logLevel, const std::string& message,
102 const std::string& source) 102 const std::string& source)
103 { 103 {
104 throw std::runtime_error("Unexpected error: " + message); 104 throw std::runtime_error("Unexpected error: " + message);
105 } 105 }
106 }; 106 };
107 107
108 class ThrowingFileSystem : public AdblockPlus::IFileSystem 108 class ThrowingFileSystem : public AdblockPlus::IFileSystem
109 { 109 {
110 public: 110 public:
111 void Read(const std::string& fileName, const ReadCallback& callback) const ove rride 111 void Read(const std::string& fileName, const ReadCallback& callback, const Cal lback& errorCallback) const override
112 { 112 {
113 throw std::runtime_error("Not implemented"); 113 throw std::runtime_error("Not implemented");
114 } 114 }
115 115
116 void Write(const std::string& fileName, const IOBuffer& data, 116 void Write(const std::string& fileName, const IOBuffer& data,
117 const Callback& callback) override 117 const Callback& callback) override
118 { 118 {
119 throw std::runtime_error("Not implemented"); 119 throw std::runtime_error("Not implemented");
120 } 120 }
121 121
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 static void ExecuteImmediately(const Task& task) 153 static void ExecuteImmediately(const Task& task)
154 { 154 {
155 if (task) 155 if (task)
156 task(); 156 task();
157 } 157 }
158 explicit LazyFileSystem(const Scheduler& scheduler = LazyFileSystem::ExecuteIm mediately) 158 explicit LazyFileSystem(const Scheduler& scheduler = LazyFileSystem::ExecuteIm mediately)
159 : scheduler(scheduler) 159 : scheduler(scheduler)
160 { 160 {
161 } 161 }
162 162
163 void Read(const std::string& fileName, const ReadCallback& callback) const ove rride 163 void Read(const std::string& fileName, const ReadCallback& callback, const Cal lback& errorCallback) const override
164 { 164 {
165 scheduler([fileName, callback] 165 scheduler([fileName, callback, errorCallback]
166 { 166 {
sergei 2018/03/23 11:27:19 Strictly speaking, now in accordance with the inte
167 if (fileName == "patterns.ini") 167 if (fileName == "patterns.ini")
168 { 168 {
169 std::string dummyData = "# Adblock Plus preferences\n[Subscription]\nurl =~user~0000"; 169 std::string dummyData = "# Adblock Plus preferences\n[Subscription]\nurl =~user~0000";
170 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()), ""); 170 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()));
171 } 171 }
172 else if (fileName == "prefs.json") 172 else if (fileName == "prefs.json")
173 { 173 {
174 std::string dummyData = "{}"; 174 std::string dummyData = "{}";
175 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()), ""); 175 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()));
176 } 176 }
177 else
178 errorCallback("File not found, " + fileName);
177 }); 179 });
178 } 180 }
179 181
180 void Write(const std::string& fileName, const IOBuffer& data, 182 void Write(const std::string& fileName, const IOBuffer& data,
181 const Callback& callback) override 183 const Callback& callback) override
182 { 184 {
183 } 185 }
184 186
185 187
186 void Move(const std::string& fromFileName, const std::string& toFileName, 188 void Move(const std::string& fromFileName, const std::string& toFileName,
(...skipping 19 matching lines...) Expand all
206 } 208 }
207 public: 209 public:
208 Scheduler scheduler; 210 Scheduler scheduler;
209 }; 211 };
210 212
211 class InMemoryFileSystem : public LazyFileSystem 213 class InMemoryFileSystem : public LazyFileSystem
212 { 214 {
213 std::map<std::string, IOBuffer> files; 215 std::map<std::string, IOBuffer> files;
214 public: 216 public:
215 using LazyFileSystem::LazyFileSystem; 217 using LazyFileSystem::LazyFileSystem;
216 void Read(const std::string& fileName, const ReadCallback& callback) const ove rride 218 void Read(const std::string& fileName, const ReadCallback& callback, const Cal lback& errorCallback) const override
217 { 219 {
218 scheduler([this, fileName, callback]() 220 scheduler([this, fileName, callback, errorCallback]()
219 { 221 {
220 auto ii_file = files.find(fileName); 222 auto ii_file = files.find(fileName);
221 if (ii_file == files.end()) 223 if (ii_file != files.end())
222 { 224 callback(IOBuffer(ii_file->second));
223 callback(IOBuffer(), "File not found, " + fileName); 225 else
224 return; 226 errorCallback("File not found, " + fileName);
225 }
226 callback(IOBuffer(ii_file->second), "");
227 }); 227 });
228 } 228 }
229 229
230 void Write(const std::string& fileName, const IOBuffer& data, 230 void Write(const std::string& fileName, const IOBuffer& data,
231 const Callback& callback) override 231 const Callback& callback) override
232 { 232 {
233 scheduler([this, fileName, data, callback]() 233 scheduler([this, fileName, data, callback]()
234 { 234 {
235 files[fileName] = data; 235 files[fileName] = data;
236 callback(""); 236 callback("");
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 } 341 }
342 342
343 void TearDown() override 343 void TearDown() override
344 { 344 {
345 if (platform) 345 if (platform)
346 platform.reset(); 346 platform.reset();
347 } 347 }
348 }; 348 };
349 349
350 #endif 350 #endif
OLDNEW
« src/FileSystemJsObject.cpp ('K') | « src/JsEngine.cpp ('k') | test/DefaultFileSystem.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld