| OLD | NEW |
| 1 #include "stdafx.h" | 1 #include "stdafx.h" |
| 2 | 2 |
| 3 #include "../shared/AutoHandle.h" | 3 #include "../shared/AutoHandle.h" |
| 4 #include "../shared/Communication.h" | 4 #include "../shared/Communication.h" |
| 5 #include "Debug.h" |
| 6 #include "Utils.h" |
| 5 | 7 |
| 6 namespace | 8 namespace |
| 7 { | 9 { |
| 8 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; | 10 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; |
| 9 | 11 |
| 10 void Log(const std::string& message) | |
| 11 { | |
| 12 // TODO: Log to a log file | |
| 13 MessageBoxA(0, ("AdblockPlusEngine: " + message).c_str(), "", MB_OK); | |
| 14 } | |
| 15 | |
| 16 void LogLastError(const std::string& message) | |
| 17 { | |
| 18 std::stringstream stream; | |
| 19 stream << message << " (Error code: " << GetLastError() << ")"; | |
| 20 Log(stream.str()); | |
| 21 } | |
| 22 | |
| 23 void LogException(const std::exception& exception) | |
| 24 { | |
| 25 Log(std::string("An exception occurred: ") + exception.what()); | |
| 26 } | |
| 27 | |
| 28 std::string ToUtf8String(std::wstring str) | 12 std::string ToUtf8String(std::wstring str) |
| 29 { | 13 { |
| 30 size_t length = str.size(); | 14 size_t length = str.size(); |
| 31 if (length == 0) | 15 if (length == 0) |
| 32 return std::string(); | 16 return std::string(); |
| 33 | 17 |
| 34 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length
, 0, 0, 0, 0); | 18 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length
, 0, 0, 0, 0); |
| 35 if (utf8StringLength == 0) | 19 if (utf8StringLength == 0) |
| 36 throw std::runtime_error("Failed to determine the required buffer size"); | 20 throw std::runtime_error("Failed to determine the required buffer size"); |
| 37 | 21 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa
ram)); | 134 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa
ram)); |
| 151 | 135 |
| 152 try | 136 try |
| 153 { | 137 { |
| 154 Communication::InputBuffer message = pipe->ReadMessage(); | 138 Communication::InputBuffer message = pipe->ReadMessage(); |
| 155 Communication::OutputBuffer response = HandleRequest(message); | 139 Communication::OutputBuffer response = HandleRequest(message); |
| 156 pipe->WriteMessage(response); | 140 pipe->WriteMessage(response); |
| 157 } | 141 } |
| 158 catch (const std::exception& e) | 142 catch (const std::exception& e) |
| 159 { | 143 { |
| 160 LogException(e); | 144 DebugException(e); |
| 161 } | 145 } |
| 162 | 146 |
| 163 // TODO: Keep the pipe open until the client disconnects | 147 // TODO: Keep the pipe open until the client disconnects |
| 164 | 148 |
| 165 return 0; | 149 return 0; |
| 166 } | 150 } |
| 167 | |
| 168 bool IsWindowsVistaOrLater() | |
| 169 { | |
| 170 OSVERSIONINFOEX osvi; | |
| 171 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); | |
| 172 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); | |
| 173 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); | |
| 174 return osvi.dwMajorVersion >= 6; | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 std::wstring GetAppDataPath() | |
| 179 { | |
| 180 std::wstring appDataPath; | |
| 181 if (IsWindowsVistaOrLater()) | |
| 182 { | |
| 183 WCHAR* pathBuffer; | |
| 184 if (FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, 0, &pathBuffer)
)) | |
| 185 throw std::runtime_error("Unable to find app data directory"); | |
| 186 appDataPath.assign(pathBuffer); | |
| 187 CoTaskMemFree(pathBuffer); | |
| 188 } | |
| 189 else | |
| 190 { | |
| 191 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); | |
| 192 if (!SHGetSpecialFolderPath(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, true)) | |
| 193 throw std::runtime_error("Unable to find app data directory"); | |
| 194 appDataPath.assign(pathBuffer.get()); | |
| 195 } | |
| 196 return appDataPath + L"\\AdblockPlus"; | |
| 197 } | 151 } |
| 198 | 152 |
| 199 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() | 153 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() |
| 200 { | 154 { |
| 201 // TODO: Pass appInfo in, which should be sent by the client | 155 // TODO: Pass appInfo in, which should be sent by the client |
| 202 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); | 156 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
| 203 std::string dataPath = ToUtf8String(GetAppDataPath()); | 157 std::string dataPath = ToUtf8String(GetAppDataPath()); |
| 204 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get())
->SetBasePath(dataPath); | 158 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get())
->SetBasePath(dataPath); |
| 205 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE
ngine(jsEngine)); | 159 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE
ngine(jsEngine)); |
| 206 return filterEngine; | 160 return filterEngine; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 222 try | 176 try |
| 223 { | 177 { |
| 224 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam
e, | 178 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam
e, |
| 225 Communication::Pipe::MODE_CREATE); | 179 Communication::Pipe::MODE_CREATE); |
| 226 | 180 |
| 227 // TODO: Count established connections, kill the engine when none are left | 181 // TODO: Count established connections, kill the engine when none are left |
| 228 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip
e), 0, 0)); | 182 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip
e), 0, 0)); |
| 229 if (!thread.get()) | 183 if (!thread.get()) |
| 230 { | 184 { |
| 231 delete pipe; | 185 delete pipe; |
| 232 LogLastError("CreateThread failed"); | 186 DebugLastError("CreateThread failed"); |
| 233 return 1; | 187 return 1; |
| 234 } | 188 } |
| 235 } | 189 } |
| 236 catch (std::runtime_error e) | 190 catch (std::runtime_error e) |
| 237 { | 191 { |
| 238 LogException(e); | 192 DebugException(e); |
| 239 return 1; | 193 return 1; |
| 240 } | 194 } |
| 241 } | 195 } |
| 242 | 196 |
| 243 return 0; | 197 return 0; |
| 244 } | 198 } |
| OLD | NEW |