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

Side by Side Diff: Shared/AdblockPlusClient.cpp

Issue 10540013: Proof of concept: Use a single FilterEngine instance (Closed)
Patch Set: Created May 3, 2013, 3:58 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
« no previous file with comments | « Shared/AdblockPlusClient.h ('k') | Shared/PluginFilter.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 11
12 #include "AdblockPlusClient.h" 12 #include "AdblockPlusClient.h"
13 13
14 namespace
15 {
16 const LPCWSTR responseClassName = L"FilterEngineResponse";
17 std::string response;
18 bool responseReady;
19
20 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine()
21 {
22 try
23 {
24 DEBUG_GENERAL("Building client");
25 AdblockPlus::AppInfo appInfo;
26 appInfo.name = "adblockplusie";
27 appInfo.version = CT2CA(_T(IEPLUGIN_VERSION), CP_UTF8);
28 appInfo.platform = "msie";
29
30 DEBUG_GENERAL(L"Building engine");
31
32 JsEnginePtr jsEngine(AdblockPlus::JsEngine::New(appInfo));
33 return std::auto_ptr<AdblockPlus::FilterEngine>(new AdblockPlus::FilterEng ine(jsEngine));
34 }
35 catch(std::exception ex)
36 {
37 DEBUG_GENERAL(ex.what());
38 }
39 }
40
41 template<typename T>
42 std::wstring ToWString(const T& value)
43 {
44 std::wstringstream stream;
45 stream << value;
46 return stream.str();
47 }
48
49 std::wstring ToWString(LPCSTR value)
50 {
51 USES_CONVERSION;
52 return ToWString(A2W(value));
53 }
54
55 template<>
56 std::wstring ToWString(const std::string& value)
57 {
58 return ToWString(value.c_str());
59 }
60
61 template<typename T>
62 std::string ToString(const T& value)
63 {
64 std::stringstream stream;
65 stream << value;
66 return stream.str();
67 }
68
69 std::string ToString(LPCWSTR value)
70 {
71 USES_CONVERSION;
72 return ToString(W2A(value));
73 }
74
75 template<>
76 std::string ToString(const std::wstring& value)
77 {
78 return ToString(value.c_str());
79 }
80
81 std::string MarshalStrings(const std::vector<std::string>& strings)
82 {
83 std::string marshalledStrings;
84 for (std::vector<std::string>::const_iterator it = strings.begin(); it != st rings.end(); it++)
85 marshalledStrings += *it + '\0';
86 return marshalledStrings;
87 }
88
89 std::vector<std::string> UnmarshalStrings(std::string::value_type* message, in t count)
90 {
91 std::vector<std::string> strings;
92 for (int i = 0; i < count; i++)
93 {
94 std::string::value_type* part = message;
95 strings.push_back(message);
96 message += strlen(part) + 1;
97 }
98 return strings;
99 }
100
101 void SendStringMessage(HWND messageWindow, const std::string& message)
102 {
103 COPYDATASTRUCT data;
104 data.cbData = message.length() * sizeof(std::string::value_type);
105 data.lpData = (PVOID) message.c_str();
106 SendMessage(messageWindow, WM_COPYDATA, (WPARAM) messageWindow, (LPARAM)(LPV OID) &data);
107 }
108
109 LRESULT CALLBACK RequestWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, L PARAM lParam)
110 {
111 if (uMsg == WM_COPYDATA)
112 {
113 COPYDATASTRUCT* data = (COPYDATASTRUCT*) lParam;
114 std::vector<std::string> args = UnmarshalStrings((std::string::value_type* ) data->lpData, 4);
115
116 FilterEngine* filterEngine = (FilterEngine*) GetWindowLongPtr(hWnd, GWLP_U SERDATA);
117 bool matches = filterEngine->Matches(args[1], args[2], args[3]);
118
119 std::wstring responseWindowName = ToWString(args[0]);
120 HWND responseWindow = FindWindowEx(HWND_MESSAGE, 0, responseClassName, res ponseWindowName.c_str());
121 if (!responseWindow)
122 {
123 DEBUG_GENERAL(L"Failed to find the filter engine response window");
124 }
125 else
126 {
127 SendStringMessage(responseWindow, ToString(matches));
128 }
129 }
130 return DefWindowProc(hWnd, uMsg, wParam, lParam);
131 }
132
133 LRESULT CALLBACK ResponseWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
134 {
135 if (uMsg == WM_COPYDATA)
136 {
137 COPYDATASTRUCT* data = (COPYDATASTRUCT*) lParam;
138 response = (std::string::value_type*) data->lpData;
139 responseReady = true;
140 }
141 return DefWindowProc(hWnd, uMsg, wParam, lParam);
142 }
143 }
14 144
15 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL; 145 CAdblockPlusClient* CAdblockPlusClient::s_instance = NULL;
16 146
17
18 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase() 147 CAdblockPlusClient::CAdblockPlusClient() : CPluginClientBase()
19 { 148 {
20 try 149 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter());
150
151 // TODO: Only create a single filter engine instance, share them across all
152 // processes. For this we need to invoke all filter engine methods via
153 // IPC. This is already done for FilterEngine::Matches().
154 filterEngine = CreateFilterEngine();
155
156 const LPCWSTR requestClassName = L"FilterEngineRequest";
157 WNDCLASS requestClass = {};
158 requestClass.lpfnWndProc = (WNDPROC) RequestWindowProcedure;
159 requestClass.lpszClassName = requestClassName;
160 if (!RegisterClass(&requestClass))
21 { 161 {
22 DEBUG_GENERAL("Building client"); 162 DEBUG_GENERAL(L"Failed to register FilterEngineRequest class");
23 m_filter = std::auto_ptr<CPluginFilter>(new CPluginFilter()); 163 return;
24 AdblockPlus::AppInfo appInfo; 164 }
25 appInfo.name = "adblockplusie";
26 appInfo.version = CT2CA(_T(IEPLUGIN_VERSION), CP_UTF8);
27 appInfo.platform = "msie";
28
29 DEBUG_GENERAL(L"Building engine");
30 165
31 JsEnginePtr jsEngine(AdblockPlus::JsEngine::New(appInfo)); 166 filterEngineRequestWindow = FindWindowEx(HWND_MESSAGE, 0, requestClassName, 0) ;
32 filterEngine = std::auto_ptr<AdblockPlus::FilterEngine>(new AdblockPlus::Fil terEngine(jsEngine)); 167 if (!filterEngineRequestWindow)
168 {
169 filterEngineRequestWindow = CreateWindow(requestClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
170 if (!filterEngineRequestWindow)
171 {
172 DEBUG_GENERAL(L"Failed to create filter engine request window");
173 return;
174 }
175 SetWindowLongPtr(filterEngineRequestWindow, GWLP_USERDATA, (LONG_PTR) filter Engine.get());
176 }
33 177
178 WNDCLASS responseClass = {};
179 responseClass.lpfnWndProc = (WNDPROC) ResponseWindowProcedure;
180 responseClass.lpszClassName = responseClassName;
181 if (!RegisterClass(&responseClass))
182 {
183 DEBUG_GENERAL(L"Failed to register FilterEngineRequest class");
184 return;
34 } 185 }
35 catch(std::exception ex) 186
36 { 187 filterEngineResponseWindowName = ToWString(GetCurrentThreadId());
37 DEBUG_GENERAL(ex.what()); 188 filterEngineResponseWindow = CreateWindow(responseClassName, filterEngineRespo nseWindowName.c_str(), 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
38 } 189 if (!filterEngineResponseWindow)
190 DEBUG_GENERAL(L"Failed to create filter engine response window");
39 } 191 }
192
40 CAdblockPlusClient::~CAdblockPlusClient() 193 CAdblockPlusClient::~CAdblockPlusClient()
41 { 194 {
42 s_instance = NULL; 195 s_instance = NULL;
43 } 196 }
44 197
45 198
46 CAdblockPlusClient* CAdblockPlusClient::GetInstance() 199 CAdblockPlusClient* CAdblockPlusClient::GetInstance()
47 { 200 {
48 CAdblockPlusClient* instance = NULL; 201 CAdblockPlusClient* instance = NULL;
49 202
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 BYTE version[50]; 309 BYTE version[50];
157 cbData = 50; 310 cbData = 50;
158 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a); 311 status = RegQueryValueEx(hKey, L"Version", NULL, &type, (BYTE*)version, &cbDat a);
159 if (status != 0) 312 if (status != 0)
160 { 313 {
161 return 0; 314 return 0;
162 } 315 }
163 RegCloseKey(hKey); 316 RegCloseKey(hKey);
164 return (int)(version[0] - 48); 317 return (int)(version[0] - 48);
165 } 318 }
319
320 bool CAdblockPlusClient::Matches(const std::string& url, const std::string& cont entType, const std::string& domain)
321 {
322 std::vector<std::string> args;
323 args.push_back(ToString(filterEngineResponseWindowName));
324 args.push_back(url);
325 args.push_back(contentType);
326 args.push_back(domain);
327 responseReady = false;
328 SendStringMessage(filterEngineRequestWindow, MarshalStrings(args));
329 while (!responseReady)
330 Sleep(10);
331 return response == "1";
332 }
OLDNEW
« no previous file with comments | « Shared/AdblockPlusClient.h ('k') | Shared/PluginFilter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld