Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 #include <iostream> | 2 #include <iostream> |
3 #include <Lmcons.h> | |
3 #include <ShlObj.h> | 4 #include <ShlObj.h> |
4 #include <sstream> | 5 #include <sstream> |
5 #include <vector> | 6 #include <vector> |
6 #include <Windows.h> | 7 #include <Windows.h> |
7 #include <Sddl.h> | 8 #include <Sddl.h> |
8 | 9 |
9 namespace | 10 namespace |
10 { | 11 { |
11 const std::wstring pipeName = L"\\\\.\\pipe\\adblockplusengine"; | 12 std::wstring GetUserName() |
Wladimir Palant
2013/05/23 14:25:01
I realized that the pipe name should be user-speci
Felix Dahlke
2013/05/23 19:10:44
Oh, you're right. Appended the user name. Consider
Wladimir Palant
2013/05/23 20:10:15
Not sure whether the user name can be changed but
| |
13 { | |
14 const DWORD maxLength = UNLEN + 1; | |
15 std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]); | |
16 DWORD length = maxLength; | |
17 if (!::GetUserName(buffer.get(), &length)) | |
18 { | |
19 std::stringstream stream; | |
20 stream << "Failed to get the current user's name (Error code: " << GetLast Error() << ")"; | |
21 throw std::runtime_error("Failed to get the current user's name"); | |
22 } | |
23 return std::wstring(buffer.get(), length); | |
24 } | |
25 | |
26 const std::wstring pipeName = L"\\\\.\\pipe\\adblockplusengine_" + GetUserName (); | |
12 const int bufferSize = 1024; | 27 const int bufferSize = 1024; |
13 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; | 28 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; |
14 | 29 |
15 class AutoHandle | 30 class AutoHandle |
16 { | 31 { |
17 public: | 32 public: |
18 AutoHandle() | 33 AutoHandle() |
19 { | 34 { |
20 } | 35 } |
21 | 36 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 std::vector<std::string> UnmarshalStrings(const std::string& message) | 85 std::vector<std::string> UnmarshalStrings(const std::string& message) |
71 { | 86 { |
72 std::stringstream stream(message); | 87 std::stringstream stream(message); |
73 std::vector<std::string> strings; | 88 std::vector<std::string> strings; |
74 std::string string; | 89 std::string string; |
75 while (std::getline(stream, string, ';')) | 90 while (std::getline(stream, string, ';')) |
76 strings.push_back(string); | 91 strings.push_back(string); |
77 return strings; | 92 return strings; |
78 } | 93 } |
79 | 94 |
80 std::string ToString(std::wstring value) | 95 std::string ToUtf8String(std::wstring str) |
81 { | 96 { |
82 int size = WideCharToMultiByte(CP_UTF8, 0, value.c_str(), value.length(), 0, 0, 0, 0); | 97 size_t length = str.size(); |
Wladimir Palant
2013/05/23 14:25:01
Error case (size is 0 for a value with non-zero le
Felix Dahlke
2013/05/23 19:10:44
It is, it returns an empty string. I thought it wa
Wladimir Palant
2013/05/23 20:10:15
We should actually crash early here. While I canno
| |
83 std::auto_ptr<char> converted(new char[size]); | 98 if (length == 0) |
84 WideCharToMultiByte(CP_UTF8, 0, value.c_str(), value.length(), converted.get (), size, 0, 0); | 99 return std::string(); |
85 std::string string(converted.get(), size); | 100 |
86 return string; | 101 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length , 0, 0, 0, 0); |
102 if (utf8StringLength == 0) | |
103 throw std::runtime_error("Failed to determine the required buffer size"); | |
104 | |
105 std::string utf8String(utf8StringLength, '\0'); | |
106 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Str ingLength, 0, 0); | |
107 return utf8String; | |
87 } | 108 } |
88 | 109 |
89 std::string ReadMessage(HANDLE pipe) | 110 std::string ReadMessage(HANDLE pipe) |
90 { | 111 { |
91 std::stringstream stream; | 112 std::stringstream stream; |
92 std::auto_ptr<char> buffer(new char[bufferSize]); | 113 std::auto_ptr<char> buffer(new char[bufferSize]); |
93 bool doneReading = false; | 114 bool doneReading = false; |
94 while (!doneReading) | 115 while (!doneReading) |
95 { | 116 { |
96 DWORD bytesRead; | 117 DWORD bytesRead; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 appDataPath.assign(pathBuffer); | 189 appDataPath.assign(pathBuffer); |
169 CoTaskMemFree(pathBuffer); | 190 CoTaskMemFree(pathBuffer); |
170 } | 191 } |
171 else | 192 else |
172 { | 193 { |
173 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); | 194 std::auto_ptr<wchar_t> pathBuffer(new wchar_t[MAX_PATH]); |
174 if (!SHGetSpecialFolderPath(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, true)) | 195 if (!SHGetSpecialFolderPath(0, pathBuffer.get(), CSIDL_LOCAL_APPDATA, true)) |
175 throw std::runtime_error("Unable to find app data directory"); | 196 throw std::runtime_error("Unable to find app data directory"); |
176 appDataPath.assign(pathBuffer.get()); | 197 appDataPath.assign(pathBuffer.get()); |
177 } | 198 } |
178 return std::wstring(appDataPath) + L"\\AdblockPlus"; | 199 return appDataPath + L"\\AdblockPlus"; |
Wladimir Palant
2013/05/23 14:25:01
appDataPath already is a wstring, no need to const
Felix Dahlke
2013/05/23 19:10:44
Done.
| |
179 } | 200 } |
180 | 201 |
181 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() | 202 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() |
182 { | 203 { |
183 // TODO: Pass appInfo in, which should be sent by the client | 204 // TODO: Pass appInfo in, which should be sent by the client |
184 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); | 205 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
185 std::string dataPath = ToString(GetAppDataPath()); | 206 std::string dataPath = ToUtf8String(GetAppDataPath()); |
186 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); | 207 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); |
187 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); | 208 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); |
188 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine->FetchA vailableSubscriptions(); | 209 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine->FetchA vailableSubscriptions(); |
189 // TODO: Select a subscription based on the language, not just the first one. | 210 // TODO: Select a subscription based on the language, not just the first one. |
190 // This should ideally be done in libadblockplus. | 211 // This should ideally be done in libadblockplus. |
191 AdblockPlus::SubscriptionPtr subscription = subscriptions[0]; | 212 AdblockPlus::SubscriptionPtr subscription = subscriptions[0]; |
192 subscription->AddToList(); | 213 subscription->AddToList(); |
193 return filterEngine; | 214 return filterEngine; |
194 } | 215 } |
195 | 216 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pipe) , 0, 0)); | 259 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pipe) , 0, 0)); |
239 if (!thread.get()) | 260 if (!thread.get()) |
240 { | 261 { |
241 LogLastError("CreateThread failed"); | 262 LogLastError("CreateThread failed"); |
242 return 1; | 263 return 1; |
243 } | 264 } |
244 } | 265 } |
245 | 266 |
246 return 0; | 267 return 0; |
247 } | 268 } |
LEFT | RIGHT |