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

Side by Side Diff: src/plugin/AdblockPlusClient.cpp

Issue 10774005: Avoid duplication between AdblockPlus and AdblockPlusEngine (Closed)
Patch Set: Created May 24, 2013, 3:48 p.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"
16 #include "../shared/Communication.h"
17
15 namespace 18 namespace
16 { 19 {
17 // TODO: GetUserName, pipeName, bufferSize, AutoHandle, ReadMessage, WriteMess age, MarshalStrings and UnmarshalStrings are
18 // duplicated in AdblockPlusEngine. We should find a way to reuse them.
19
20 std::wstring GetUserName()
21 {
22 const DWORD maxLength = UNLEN + 1;
23 std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]);
24 DWORD length = maxLength;
25 if (!::GetUserName(buffer.get(), &length))
26 {
27 std::stringstream stream;
28 stream << "Failed to get the current user's name (Error code: " << GetLast Error() << ")";
29 throw std::runtime_error("Failed to get the current user's name");
30 }
31 return std::wstring(buffer.get(), length);
32 }
33
34 const std::wstring pipeName = L"\\\\.\\pipe\\adblockplusengine_" + GetUserName ();
35 const int bufferSize = 1024;
36
37 class AutoHandle
38 {
39 public:
40 AutoHandle()
41 {
42 }
43
44 AutoHandle(HANDLE handle) : handle(handle)
45 {
46 }
47
48 ~AutoHandle()
49 {
50 CloseHandle(handle);
51 }
52
53 HANDLE get()
54 {
55 return handle;
56 }
57
58 private:
59 HANDLE handle;
60
61 AutoHandle(const AutoHandle& autoHandle);
62 AutoHandle& operator=(const AutoHandle& autoHandle);
63 };
64
65 std::string MarshalStrings(const std::vector<std::string>& strings)
66 {
67 // TODO: This is some pretty hacky marshalling, replace it with something mo re robust
68 std::string marshalledStrings;
69 for (std::vector<std::string>::const_iterator it = strings.begin(); it != st rings.end(); it++)
70 marshalledStrings += *it + ';';
71 return marshalledStrings;
72 }
73
74 std::vector<std::string> UnmarshalStrings(const std::string& message)
75 {
76 std::stringstream stream(message);
77 std::vector<std::string> strings;
78 std::string string;
79 while (std::getline(stream, string, ';'))
80 strings.push_back(string);
81 return strings;
82 }
83
84 std::string ReadMessage(HANDLE pipe)
85 {
86 std::stringstream stream;
87 std::auto_ptr<char> buffer(new char[bufferSize]);
88 bool doneReading = false;
89 while (!doneReading)
90 {
91 DWORD bytesRead;
92 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0) )
93 doneReading = true;
94 else if (GetLastError() != ERROR_MORE_DATA)
95 {
96 std::stringstream stream;
97 stream << "Error reading from pipe: " << GetLastError();
98 throw std::runtime_error(stream.str());
99 }
100 stream << std::string(buffer.get(), bytesRead);
101 }
102 return stream.str();
103 }
104
105 void WriteMessage(HANDLE pipe, const std::string& message)
106 {
107 DWORD bytesWritten;
108 if (!WriteFile(pipe, message.c_str(), message.length(), &bytesWritten, 0))
109 throw std::runtime_error("Failed to write to pipe");
110 }
111
112 HANDLE OpenPipe(const std::wstring& name) 20 HANDLE OpenPipe(const std::wstring& name)
113 { 21 {
114 if (WaitNamedPipe(name.c_str(), 5000)) 22 if (WaitNamedPipe(name.c_str(), 5000))
115 return CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_E XISTING, 0, 0); 23 return CreateFile(name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_E XISTING, 0, 0);
116 return INVALID_HANDLE_VALUE; 24 return INVALID_HANDLE_VALUE;
117 } 25 }
118 26
119 void SpawnAdblockPlusEngine() 27 void SpawnAdblockPlusEngine()
120 { 28 {
121 std::wstring engineExecutablePath = DllDir() + L"AdblockPlusEngine.exe"; 29 std::wstring engineExecutablePath = DllDir() + L"AdblockPlusEngine.exe";
(...skipping 13 matching lines...) Expand all
135 } 43 }
136 44
137 CloseHandle(processInformation.hProcess); 45 CloseHandle(processInformation.hProcess);
138 CloseHandle(processInformation.hThread); 46 CloseHandle(processInformation.hThread);
139 } 47 }
140 48
141 HANDLE OpenAdblockPlusEnginePipe() 49 HANDLE OpenAdblockPlusEnginePipe()
142 { 50 {
143 try 51 try
144 { 52 {
145 HANDLE pipe = OpenPipe(pipeName); 53 HANDLE pipe = OpenPipe(Communication::pipeName);
146 if (pipe == INVALID_HANDLE_VALUE) 54 if (pipe == INVALID_HANDLE_VALUE)
147 { 55 {
148 SpawnAdblockPlusEngine(); 56 SpawnAdblockPlusEngine();
149 57
150 int timeout = 10000; 58 int timeout = 10000;
151 while ((pipe = OpenPipe(pipeName)) == INVALID_HANDLE_VALUE) 59 while ((pipe = OpenPipe(Communication::pipeName)) == INVALID_HANDLE_VALU E)
152 { 60 {
153 const int step = 10; 61 const int step = 10;
154 Sleep(step); 62 Sleep(step);
155 timeout -= step; 63 timeout -= step;
156 if (timeout <= 0) 64 if (timeout <= 0)
157 throw std::runtime_error("Unable to open Adblock Plus Engine pipe"); 65 throw std::runtime_error("Unable to open Adblock Plus Engine pipe");
158 } 66 }
159 } 67 }
160 68
161 DWORD mode = PIPE_READMODE_MESSAGE; 69 DWORD mode = PIPE_READMODE_MESSAGE;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 { 206 {
299 return 0; 207 return 0;
300 } 208 }
301 RegCloseKey(hKey); 209 RegCloseKey(hKey);
302 return (int)(version[0] - 48); 210 return (int)(version[0] - 48);
303 } 211 }
304 212
305 std::string CallAdblockPlusEngineProcedure(const std::vector<std::string>& args) 213 std::string CallAdblockPlusEngineProcedure(const std::vector<std::string>& args)
306 { 214 {
307 AutoHandle pipe(OpenAdblockPlusEnginePipe()); 215 AutoHandle pipe(OpenAdblockPlusEnginePipe());
308 WriteMessage(pipe.get(), MarshalStrings(args)); 216 Communication::WriteMessage(pipe.get(), Communication::MarshalStrings(args));
309 return ReadMessage(pipe.get()); 217 return Communication::ReadMessage(pipe.get());
310 } 218 }
311 219
312 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)
313 { 221 {
314 std::vector<std::string> args; 222 std::vector<std::string> args;
315 args.push_back("Matches"); 223 args.push_back("Matches");
316 args.push_back(url); 224 args.push_back(url);
317 args.push_back(contentType); 225 args.push_back(contentType);
318 args.push_back(domain); 226 args.push_back(domain);
319 227
(...skipping 11 matching lines...) Expand all
331 239
332 std::vector<std::string> CAdblockPlusClient::GetElementHidingSelectors(std::stri ng domain) 240 std::vector<std::string> CAdblockPlusClient::GetElementHidingSelectors(std::stri ng domain)
333 { 241 {
334 std::vector<std::string> args; 242 std::vector<std::string> args;
335 args.push_back("GetElementHidingSelectors"); 243 args.push_back("GetElementHidingSelectors");
336 args.push_back(domain); 244 args.push_back(domain);
337 245
338 try 246 try
339 { 247 {
340 std::string response = CallAdblockPlusEngineProcedure(args); 248 std::string response = CallAdblockPlusEngineProcedure(args);
341 return UnmarshalStrings(response); 249 return Communication::UnmarshalStrings(response);
342 } 250 }
343 catch (const std::exception& e) 251 catch (const std::exception& e)
344 { 252 {
345 DEBUG_GENERAL(e.what()); 253 DEBUG_GENERAL(e.what());
346 return std::vector<std::string>(); 254 return std::vector<std::string>();
347 } 255 }
348 } 256 }
349 257
350 std::vector<AdblockPlus::SubscriptionPtr> CAdblockPlusClient::FetchAvailableSubs criptions() 258 std::vector<AdblockPlus::SubscriptionPtr> CAdblockPlusClient::FetchAvailableSubs criptions()
351 { 259 {
(...skipping 17 matching lines...) Expand all
369 { 277 {
370 //TODO: implement this 278 //TODO: implement this
371 return std::vector<AdblockPlus::SubscriptionPtr>(); 279 return std::vector<AdblockPlus::SubscriptionPtr>();
372 } 280 }
373 281
374 AdblockPlus::SubscriptionPtr CAdblockPlusClient::GetSubscription(std::string url ) 282 AdblockPlus::SubscriptionPtr CAdblockPlusClient::GetSubscription(std::string url )
375 { 283 {
376 //TODO: imlement this 284 //TODO: imlement this
377 return AdblockPlus::SubscriptionPtr(); 285 return AdblockPlus::SubscriptionPtr();
378 } 286 }
OLDNEW

Powered by Google App Engine
This is Rietveld