| Index: src/plugin/AdblockPlusClient.cpp |
| =================================================================== |
| --- a/src/plugin/AdblockPlusClient.cpp |
| +++ b/src/plugin/AdblockPlusClient.cpp |
| @@ -12,23 +12,16 @@ |
| #include "AdblockPlusClient.h" |
| #include "../shared/AutoHandle.h" |
| #include "../shared/Communication.h" |
| namespace |
| { |
| - HANDLE OpenPipe(const std::wstring& name) |
| - { |
| - if (WaitNamedPipe(name.c_str(), 5000)) |
| - return CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); |
| - return INVALID_HANDLE_VALUE; |
| - } |
| - |
| void SpawnAdblockPlusEngine() |
| { |
| std::wstring engineExecutablePath = DllDir() + L"AdblockPlusEngine.exe"; |
| STARTUPINFO startupInfo = {}; |
| PROCESS_INFORMATION processInformation = {}; |
| HANDLE token; |
| OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &token); |
| @@ -41,47 +34,54 @@ namespace |
| DWORD error = GetLastError(); |
| throw std::runtime_error("Failed to start Adblock Plus Engine"); |
| } |
| CloseHandle(processInformation.hProcess); |
| CloseHandle(processInformation.hThread); |
| } |
| - HANDLE OpenAdblockPlusEnginePipe() |
| + std::auto_ptr<Communication::Pipe> OpenAdblockPlusEnginePipe() |
| { |
| + std::auto_ptr<Communication::Pipe> result; |
| try |
| { |
| - HANDLE pipe = OpenPipe(Communication::pipeName); |
| - if (pipe == INVALID_HANDLE_VALUE) |
| + try |
| + { |
| + result.reset(new Communication::Pipe(Communication::pipeName, |
| + Communication::Pipe::MODE_CONNECT)); |
| + } |
| + catch (Communication::PipeConnectionError e) |
| { |
| SpawnAdblockPlusEngine(); |
| int timeout = 10000; |
| - while ((pipe = OpenPipe(Communication::pipeName)) == INVALID_HANDLE_VALUE) |
| + const int step = 10; |
| + while (!result.get()) |
| { |
| - const int step = 10; |
| - Sleep(step); |
| - timeout -= step; |
| - if (timeout <= 0) |
| - throw std::runtime_error("Unable to open Adblock Plus Engine pipe"); |
| + try |
| + { |
| + result.reset(new Communication::Pipe(Communication::pipeName, |
| + Communication::Pipe::MODE_CONNECT)); |
| + } |
| + catch (Communication::PipeConnectionError e) |
| + { |
| + Sleep(step); |
| + timeout -= step; |
| + if (timeout <= 0) |
| + throw std::runtime_error("Unable to open Adblock Plus Engine pipe"); |
| + } |
| } |
| } |
| - |
| - DWORD mode = PIPE_READMODE_MESSAGE; |
| - if (!SetNamedPipeHandleState(pipe, &mode, 0, 0)) |
| - throw std::runtime_error("SetNamedPipeHandleState failed"); |
| - |
| - return pipe; |
| } |
| catch(std::exception e) |
| { |
| DEBUG_GENERAL(e.what()); |
| - return INVALID_HANDLE_VALUE; |
| } |
| + return result; |
| } |
| } |
| CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; |
| CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() |
| { |
| m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); |
| @@ -207,19 +207,19 @@ int CAdblockPlusClient::GetIEVersion() |
| return 0; |
| } |
| RegCloseKey(hKey); |
| return (int)(version[0] - 48); |
| } |
| Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::OutputBuffer& message) |
| { |
| - AutoHandle pipe(OpenAdblockPlusEnginePipe()); |
| - Communication::WriteMessage(pipe.get(), message); |
| - return Communication::ReadMessage(pipe.get()); |
| + std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe(); |
| + pipe->WriteMessage(message); |
| + return pipe->ReadMessage(); |
| } |
| bool CAdblockPlusClient::Matches(const std::string& url, const std::string& contentType, const std::string& domain) |
| { |
| Communication::OutputBuffer request; |
| request << std::string("Matches") << url << contentType << domain; |
| try |