OLD | NEW |
1 #include <AdblockPlus/FileSystem.h> | 1 #include <AdblockPlus/FileSystem.h> |
2 #include <stdexcept> | 2 #include <stdexcept> |
3 #include <sstream> | 3 #include <sstream> |
4 #include <vector> | 4 #include <vector> |
5 | 5 |
6 #include <AdblockPlus/JsEngine.h> | 6 #include <AdblockPlus/JsEngine.h> |
7 #include <AdblockPlus/JsValue.h> | 7 #include <AdblockPlus/JsValue.h> |
8 #include "FileSystemJsObject.h" | 8 #include "FileSystemJsObject.h" |
9 #include "Utils.h" | 9 #include "Utils.h" |
10 #include "Thread.h" | 10 #include "Thread.h" |
11 #include "Utils.h" | 11 #include "Utils.h" |
12 | 12 |
13 using namespace AdblockPlus; | 13 using namespace AdblockPlus; |
14 | 14 |
15 namespace | 15 namespace |
16 { | 16 { |
17 class IoThread : public Thread | 17 class IoThread : public Thread |
18 { | 18 { |
19 public: | 19 public: |
20 IoThread(JsEngine& jsEngine, JsValuePtr callback) | 20 IoThread(JsEngine& jsEngine, JsValuePtr callback) |
21 : jsEngine(jsEngine), fileSystem(jsEngine.GetFileSystem()), | 21 : jsEngine(jsEngine), fileSystem(jsEngine.GetFileSystem()), |
22 callback(callback) | 22 callback(callback) |
23 { | 23 { |
24 } | 24 } |
25 | 25 |
26 protected: | 26 protected: |
27 JsEngine& jsEngine; | 27 JsEngine& jsEngine; |
28 FileSystem& fileSystem; | 28 FileSystemPtr fileSystem; |
29 JsValuePtr callback; | 29 JsValuePtr callback; |
30 }; | 30 }; |
31 | 31 |
32 class ReadThread : public IoThread | 32 class ReadThread : public IoThread |
33 { | 33 { |
34 public: | 34 public: |
35 ReadThread(JsEngine& jsEngine, JsValuePtr callback, | 35 ReadThread(JsEngine& jsEngine, JsValuePtr callback, |
36 const std::string& path) | 36 const std::string& path) |
37 : IoThread(jsEngine, callback), path(path) | 37 : IoThread(jsEngine, callback), path(path) |
38 { | 38 { |
39 } | 39 } |
40 | 40 |
41 void Run() | 41 void Run() |
42 { | 42 { |
43 std::string content; | 43 std::string content; |
44 std::string error; | 44 std::string error; |
45 try | 45 try |
46 { | 46 { |
47 std::tr1::shared_ptr<std::istream> stream = fileSystem.Read(path); | 47 std::tr1::shared_ptr<std::istream> stream = fileSystem->Read(path); |
48 content = Utils::Slurp(*stream); | 48 content = Utils::Slurp(*stream); |
49 } | 49 } |
50 catch (std::exception& e) | 50 catch (std::exception& e) |
51 { | 51 { |
52 error = e.what(); | 52 error = e.what(); |
53 } | 53 } |
54 catch (...) | 54 catch (...) |
55 { | 55 { |
56 error = "Unknown error while reading from " + path; | 56 error = "Unknown error while reading from " + path; |
57 } | 57 } |
(...skipping 21 matching lines...) Expand all Loading... |
79 { | 79 { |
80 } | 80 } |
81 | 81 |
82 void Run() | 82 void Run() |
83 { | 83 { |
84 std::string error; | 84 std::string error; |
85 try | 85 try |
86 { | 86 { |
87 std::tr1::shared_ptr<std::ostream> stream(new std::stringstream); | 87 std::tr1::shared_ptr<std::ostream> stream(new std::stringstream); |
88 *stream << content; | 88 *stream << content; |
89 fileSystem.Write(path, stream); | 89 fileSystem->Write(path, stream); |
90 } | 90 } |
91 catch (std::exception& e) | 91 catch (std::exception& e) |
92 { | 92 { |
93 error = e.what(); | 93 error = e.what(); |
94 } | 94 } |
95 catch (...) | 95 catch (...) |
96 { | 96 { |
97 error = "Unknown error while writing to " + path; | 97 error = "Unknown error while writing to " + path; |
98 } | 98 } |
99 | 99 |
(...skipping 17 matching lines...) Expand all Loading... |
117 const std::string& fromPath, const std::string& toPath) | 117 const std::string& fromPath, const std::string& toPath) |
118 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) | 118 : IoThread(jsEngine, callback), fromPath(fromPath), toPath(toPath) |
119 { | 119 { |
120 } | 120 } |
121 | 121 |
122 void Run() | 122 void Run() |
123 { | 123 { |
124 std::string error; | 124 std::string error; |
125 try | 125 try |
126 { | 126 { |
127 fileSystem.Move(fromPath, toPath); | 127 fileSystem->Move(fromPath, toPath); |
128 } | 128 } |
129 catch (std::exception& e) | 129 catch (std::exception& e) |
130 { | 130 { |
131 error = e.what(); | 131 error = e.what(); |
132 } | 132 } |
133 catch (...) | 133 catch (...) |
134 { | 134 { |
135 error = "Unknown error while moving " + fromPath + " to " + toPath; | 135 error = "Unknown error while moving " + fromPath + " to " + toPath; |
136 } | 136 } |
137 | 137 |
(...skipping 17 matching lines...) Expand all Loading... |
155 const std::string& path) | 155 const std::string& path) |
156 : IoThread(jsEngine, callback), path(path) | 156 : IoThread(jsEngine, callback), path(path) |
157 { | 157 { |
158 } | 158 } |
159 | 159 |
160 void Run() | 160 void Run() |
161 { | 161 { |
162 std::string error; | 162 std::string error; |
163 try | 163 try |
164 { | 164 { |
165 fileSystem.Remove(path); | 165 fileSystem->Remove(path); |
166 } | 166 } |
167 catch (std::exception& e) | 167 catch (std::exception& e) |
168 { | 168 { |
169 error = e.what(); | 169 error = e.what(); |
170 } | 170 } |
171 catch (...) | 171 catch (...) |
172 { | 172 { |
173 error = "Unknown error while removing " + path; | 173 error = "Unknown error while removing " + path; |
174 } | 174 } |
175 | 175 |
(...skipping 17 matching lines...) Expand all Loading... |
193 : IoThread(jsEngine, callback), path(path) | 193 : IoThread(jsEngine, callback), path(path) |
194 { | 194 { |
195 } | 195 } |
196 | 196 |
197 void Run() | 197 void Run() |
198 { | 198 { |
199 std::string error; | 199 std::string error; |
200 FileSystem::StatResult statResult; | 200 FileSystem::StatResult statResult; |
201 try | 201 try |
202 { | 202 { |
203 statResult = fileSystem.Stat(path); | 203 statResult = fileSystem->Stat(path); |
204 } | 204 } |
205 catch (std::exception& e) | 205 catch (std::exception& e) |
206 { | 206 { |
207 error = e.what(); | 207 error = e.what(); |
208 } | 208 } |
209 catch (...) | 209 catch (...) |
210 { | 210 { |
211 error = "Unknown error while calling stat on " + path; | 211 error = "Unknown error while calling stat on " + path; |
212 } | 212 } |
213 | 213 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 317 |
318 JsValuePtr FileSystemJsObject::Setup(JsEngine& jsEngine, JsValuePtr obj) | 318 JsValuePtr FileSystemJsObject::Setup(JsEngine& jsEngine, JsValuePtr obj) |
319 { | 319 { |
320 obj->SetProperty("read", jsEngine.NewCallback(::ReadCallback)); | 320 obj->SetProperty("read", jsEngine.NewCallback(::ReadCallback)); |
321 obj->SetProperty("write", jsEngine.NewCallback(::WriteCallback)); | 321 obj->SetProperty("write", jsEngine.NewCallback(::WriteCallback)); |
322 obj->SetProperty("move", jsEngine.NewCallback(::MoveCallback)); | 322 obj->SetProperty("move", jsEngine.NewCallback(::MoveCallback)); |
323 obj->SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); | 323 obj->SetProperty("remove", jsEngine.NewCallback(::RemoveCallback)); |
324 obj->SetProperty("stat", jsEngine.NewCallback(::StatCallback)); | 324 obj->SetProperty("stat", jsEngine.NewCallback(::StatCallback)); |
325 return obj; | 325 return obj; |
326 } | 326 } |
OLD | NEW |