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

Side by Side Diff: src/engine/main.cpp

Issue 10800092: Use libadblockplus update checker (Closed)
Patch Set: Addressed review comments Created June 7, 2013, 5:27 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 <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <functional>
2 #include <vector> 3 #include <vector>
3 #include <Windows.h> 4 #include <Windows.h>
4 5
5 #include "../shared/AutoHandle.h" 6 #include "../shared/AutoHandle.h"
6 #include "../shared/Communication.h" 7 #include "../shared/Communication.h"
7 #include "../shared/Utils.h" 8 #include "../shared/Utils.h"
8 #include "../shared/Version.h" 9 #include "../shared/Version.h"
9 #include "Debug.h" 10 #include "Debug.h"
11 #include "Updater.h"
10 12
11 namespace 13 namespace
12 { 14 {
13 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; 15 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine;
14 16
15 std::string ToUtf8String(std::wstring str)
16 {
17 size_t length = str.size();
18 if (length == 0)
19 return std::string();
20
21 DWORD utf8StringLength = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length , 0, 0, 0, 0);
22 if (utf8StringLength == 0)
23 throw std::runtime_error("Failed to determine the required buffer size");
24
25 std::string utf8String(utf8StringLength, '\0');
26 WideCharToMultiByte(CP_UTF8, 0, str.c_str(), length, &utf8String[0], utf8Str ingLength, 0, 0);
27 return utf8String;
28 }
29
30 void WriteStrings(Communication::OutputBuffer& response, 17 void WriteStrings(Communication::OutputBuffer& response,
31 const std::vector<std::string>& strings) 18 const std::vector<std::string>& strings)
32 { 19 {
33 int32_t count = strings.size(); 20 int32_t count = strings.size();
34 response << count; 21 response << count;
35 for (int32_t i = 0; i < count; i++) 22 for (int32_t i = 0; i < count; i++)
36 response << strings[i]; 23 response << strings[i];
37 } 24 }
38 25
39 void WriteSubscriptions(Communication::OutputBuffer& response, 26 void WriteSubscriptions(Communication::OutputBuffer& response,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 143 }
157 catch (const std::exception& e) 144 catch (const std::exception& e)
158 { 145 {
159 DebugException(e); 146 DebugException(e);
160 } 147 }
161 148
162 // TODO: Keep the pipe open until the client disconnects 149 // TODO: Keep the pipe open until the client disconnects
163 150
164 return 0; 151 return 0;
165 } 152 }
153
154 void OnUpdateAvailable(AdblockPlus::JsEnginePtr jsEngine, AdblockPlus::JsValue List& params)
155 {
156 if (params.size() < 1)
157 {
158 Debug("updateAvailable event missing URL");
159 return;
160 }
161
162 Updater updater(jsEngine, params[0]->AsString());
163 updater.Update();
164 }
166 } 165 }
167 166
168 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine(const std::wstring& locale) 167 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine(const std::wstring& locale)
169 { 168 {
170 AdblockPlus::AppInfo appInfo; 169 AdblockPlus::AppInfo appInfo;
171 appInfo.version = ToUtf8String(IEPLUGIN_VERSION); 170 appInfo.version = ToUtf8String(IEPLUGIN_VERSION);
172 appInfo.name = "adblockplusie"; 171 appInfo.name = "adblockplusie";
173 appInfo.platform = "msie"; 172 appInfo.platform = "msie";
174 appInfo.locale = ToUtf8String(locale); 173 appInfo.locale = ToUtf8String(locale);
175 #ifdef ADBLOCK_PLUS_TEST_MODE 174 #ifdef ADBLOCK_PLUS_TEST_MODE
176 appInfo.developmentBuild = true; 175 appInfo.developmentBuild = true;
177 #else 176 #else
178 appInfo.developmentBuild = false; 177 appInfo.developmentBuild = false;
179 #endif 178 #endif
180 179
181 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(appInfo); 180 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(appInfo);
181 jsEngine->SetEventCallback("updateAvailable",
182 std::bind(&OnUpdateAvailable, jsEngine, std::placeholders::_1));
183
182 std::string dataPath = ToUtf8String(GetAppDataPath()); 184 std::string dataPath = ToUtf8String(GetAppDataPath());
183 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); 185 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath);
184 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); 186 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine));
185 return filterEngine; 187 return filterEngine;
186 } 188 }
187 189
188 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 190 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
189 { 191 {
190 // TODO: Attempt to create the pipe first, and exit immediately if this 192 // TODO: Attempt to create the pipe first, and exit immediately if this
191 // fails. Since multiple instances of the engine could be running, 193 // fails. Since multiple instances of the engine could be running,
(...skipping 27 matching lines...) Expand all
219 } 221 }
220 catch (std::runtime_error e) 222 catch (std::runtime_error e)
221 { 223 {
222 DebugException(e); 224 DebugException(e);
223 return 1; 225 return 1;
224 } 226 }
225 } 227 }
226 228
227 return 0; 229 return 0;
228 } 230 }
OLDNEW

Powered by Google App Engine
This is Rietveld