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

Side by Side 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.
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 #include "PluginStdAfx.h" 1 #include "PluginStdAfx.h"
2 2
3 #include "PluginSettings.h" 3 #include "PluginSettings.h"
4 #include "PluginSystem.h" 4 #include "PluginSystem.h"
5 #include "PluginFilter.h" 5 #include "PluginFilter.h"
6 #include "PluginClientFactory.h" 6 #include "PluginClientFactory.h"
7 #include "PluginDictionary.h" 7 #include "PluginDictionary.h"
8 #include "PluginHttpRequest.h" 8 #include "PluginHttpRequest.h"
9 #include "PluginMutex.h" 9 #include "PluginMutex.h"
10 #include "PluginClass.h" 10 #include "PluginClass.h"
11 #include "PluginUtil.h" 11 #include "PluginUtil.h"
12 12
13 #include "AdblockPlusClient.h" 13 #include "AdblockPlusClient.h"
14 14
15 #include "../shared/AutoHandle.h" 15 #include "../shared/AutoHandle.h"
16 #include "../shared/Communication.h" 16 #include "../shared/Communication.h"
17 17
18 namespace 18 namespace
19 { 19 {
20 HANDLE OpenPipe(const std::wstring& name)
21 {
22 if (WaitNamedPipe(name.c_str(), 5000))
23 return CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_E XISTING, 0, 0);
24 return INVALID_HANDLE_VALUE;
25 }
26
27 void SpawnAdblockPlusEngine() 20 void SpawnAdblockPlusEngine()
28 { 21 {
29 std::wstring engineExecutablePath = DllDir() + L"AdblockPlusEngine.exe"; 22 std::wstring engineExecutablePath = DllDir() + L"AdblockPlusEngine.exe";
30 STARTUPINFO startupInfo = {}; 23 STARTUPINFO startupInfo = {};
31 PROCESS_INFORMATION processInformation = {}; 24 PROCESS_INFORMATION processInformation = {};
32 25
33 HANDLE token; 26 HANDLE token;
34 OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &token); 27 OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_ADJUST_DEFAULT | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &token);
35 HANDLE newToken; 28 HANDLE newToken;
36 DuplicateTokenEx(token, 0, 0, SecurityImpersonation, TokenPrimary, &newToken ); 29 DuplicateTokenEx(token, 0, 0, SecurityImpersonation, TokenPrimary, &newToken );
37 30
38 if (!CreateProcessAsUser(newToken, 0, const_cast<wchar_t*>(engineExecutableP ath.c_str()), 0, 0, 0, 0, 0, 0, 31 if (!CreateProcessAsUser(newToken, 0, const_cast<wchar_t*>(engineExecutableP ath.c_str()), 0, 0, 0, 0, 0, 0,
39 &startupInfo, &processInformation)) 32 &startupInfo, &processInformation))
40 { 33 {
41 DWORD error = GetLastError(); 34 DWORD error = GetLastError();
42 throw std::runtime_error("Failed to start Adblock Plus Engine"); 35 throw std::runtime_error("Failed to start Adblock Plus Engine");
43 } 36 }
44 37
45 CloseHandle(processInformation.hProcess); 38 CloseHandle(processInformation.hProcess);
46 CloseHandle(processInformation.hThread); 39 CloseHandle(processInformation.hThread);
47 } 40 }
48 41
49 HANDLE OpenAdblockPlusEnginePipe() 42 std::auto_ptr<Communication::Pipe> OpenAdblockPlusEnginePipe()
50 { 43 {
44 std::auto_ptr<Communication::Pipe> result;
51 try 45 try
52 { 46 {
53 HANDLE pipe = OpenPipe(Communication::pipeName); 47 try
54 if (pipe == INVALID_HANDLE_VALUE) 48 {
49 result.reset(new Communication::Pipe(Communication::pipeName,
50 Communication::Pipe::MODE_CONNECT));
51 }
52 catch (Communication::PipeConnectionError e)
55 { 53 {
56 SpawnAdblockPlusEngine(); 54 SpawnAdblockPlusEngine();
57 55
58 int timeout = 10000; 56 int timeout = 10000;
59 while ((pipe = OpenPipe(Communication::pipeName)) == INVALID_HANDLE_VALU E) 57 const int step = 10;
58 while (!result.get())
60 { 59 {
61 const int step = 10; 60 try
62 Sleep(step); 61 {
63 timeout -= step; 62 result.reset(new Communication::Pipe(Communication::pipeName,
64 if (timeout <= 0) 63 Communication::Pipe::MODE_CONNECT));
65 throw std::runtime_error("Unable to open Adblock Plus Engine pipe"); 64 }
65 catch (Communication::PipeConnectionError e)
66 {
67 Sleep(step);
68 timeout -= step;
69 if (timeout <= 0)
70 throw std::runtime_error("Unable to open Adblock Plus Engine pipe" );
71 }
66 } 72 }
67 } 73 }
68
69 DWORD mode = PIPE_READMODE_MESSAGE;
70 if (!SetNamedPipeHandleState(pipe, &mode, 0, 0))
71 throw std::runtime_error("SetNamedPipeHandleState failed");
72
73 return pipe;
74 } 74 }
75 catch(std::exception e) 75 catch(std::exception e)
76 { 76 {
77 DEBUG_GENERAL(e.what()); 77 DEBUG_GENERAL(e.what());
78 return INVALID_HANDLE_VALUE;
79 } 78 }
79 return result;
80 } 80 }
81 } 81 }
82 82
83 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 83 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
84 84
85 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 85 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
86 { 86 {
87 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 87 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
88 } 88 }
89 89
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 if (status != 0) 205 if (status != 0)
206 { 206 {
207 return 0; 207 return 0;
208 } 208 }
209 RegCloseKey(hKey); 209 RegCloseKey(hKey);
210 return (int)(version[0] - 48); 210 return (int)(version[0] - 48);
211 } 211 }
212 212
213 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::OutputB uffer& message) 213 Communication::InputBuffer CallAdblockPlusEngineProcedure(Communication::OutputB uffer& message)
214 { 214 {
215 AutoHandle pipe(OpenAdblockPlusEnginePipe()); 215 std::auto_ptr<Communication::Pipe> pipe = OpenAdblockPlusEnginePipe();
216 Communication::WriteMessage(pipe.get(), message); 216 pipe->WriteMessage(message);
217 return Communication::ReadMessage(pipe.get()); 217 return pipe->ReadMessage();
218 } 218 }
219 219
220 bool CAdblockPlusClient::Matches(const std::string& url, const std::string& cont entType, const std::string& domain) 220 bool CAdblockPlusClient::Matches(const std::string& url, const std::string& cont entType, const std::string& domain)
221 { 221 {
222 Communication::OutputBuffer request; 222 Communication::OutputBuffer request;
223 request << std::string("Matches") << url << contentType << domain; 223 request << std::string("Matches") << url << contentType << domain;
224 224
225 try 225 try
226 { 226 {
227 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request ); 227 Communication::InputBuffer response = CallAdblockPlusEngineProcedure(request );
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 { 286 {
287 //TODO: implement this 287 //TODO: implement this
288 return std::vector<AdblockPlus::SubscriptionPtr>(); 288 return std::vector<AdblockPlus::SubscriptionPtr>();
289 } 289 }
290 290
291 AdblockPlus::SubscriptionPtr CAdblockPlusClient::GetSubscription(std::string url ) 291 AdblockPlus::SubscriptionPtr CAdblockPlusClient::GetSubscription(std::string url )
292 { 292 {
293 //TODO: imlement this 293 //TODO: imlement this
294 return AdblockPlus::SubscriptionPtr(); 294 return AdblockPlus::SubscriptionPtr();
295 } 295 }
OLDNEW

Powered by Google App Engine
This is Rietveld