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

Unified Diff: src/plugin/AdblockPlusClient.cpp

Issue 10786016: Moved all pipe functionality into a self-containing Communication::Pipe class (Closed)
Patch Set: Created May 31, 2013, 11:48 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld