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

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

Issue 10800092: Use libadblockplus update checker (Closed)
Patch Set: Created June 6, 2013, 3:18 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
(Empty)
1 #include <functional>
2 #include <sstream>
3 #include <AdblockPlus/FileSystem.h>
4 #include <AdblockPlus/WebRequest.h>
5
6 #include "../shared/Utils.h"
7 #include "Debug.h"
8 #include "Resource.h"
9 #include "Updater.h"
10
11 namespace
12 {
13 typedef std::function<void()> CallbackType;
14
15 LRESULT CALLBACK UpdateDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPar am)
16 {
17 switch (msg)
18 {
19 case WM_INITDIALOG:
20 // TODO: Localize dialog strings
21 return TRUE;
22 case WM_COMMAND:
23 if (wParam == IDOK || wParam == IDCANCEL)
24 {
25 EndDialog(hWnd, wParam);
26 return TRUE;
27 }
28 break;
29 }
30
31 return FALSE;
32 }
33
34 LRESULT CALLBACK DownloadDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lP aram)
35 {
36 // TODO: Indicate progress
37
38 switch (msg)
39 {
40 case WM_INITDIALOG:
41 // TODO: Localize dialog strings
42 reinterpret_cast<Updater*>(lParam)->StartDownload(hWnd);
43 return TRUE;
44 case WM_COMMAND:
45 if (wParam == IDCANCEL)
46 {
47 EndDialog(hWnd, wParam);
48 return TRUE;
49 }
50 break;
51 }
52 return FALSE;
53 }
54
55 DWORD RunThread(LPVOID param)
56 {
57 CallbackType* callback = reinterpret_cast<CallbackType*>(param);
Felix Dahlke 2013/06/06 17:44:00 I'd use an auto_ptr. What if there's an exception
58 (*callback)();
59 delete callback;
60 return 0;
61 }
62 }
63
64 Updater::Updater(AdblockPlus::JsEnginePtr jsEngine, const std::string& url)
65 : jsEngine(jsEngine), url(url), tempFile(GetAppDataPath() + L"\\update.exe")
66 {
67 }
68
69 void Updater::StartUpdate()
70 {
71 Debug("Update available: " + url);
72
73 if (DialogBox(NULL, MAKEINTRESOURCE(IDD_UPDATEDIALOG), GetDesktopWindow(),
74 reinterpret_cast<DLGPROC>(&UpdateDlgProc)) == IDOK)
75 {
76 Debug("User accepted update");
77
78 int result = DialogBoxParam(NULL, MAKEINTRESOURCE(IDD_DOWNLOADDIALOG), GetDe sktopWindow(),
79 reinterpret_cast<DLGPROC>(&DownloadDlgProc),
80 reinterpret_cast<LPARAM>(this));
81 if (result == -1)
82 {
83 // TODO: Localize
84 MessageBoxW(NULL, L"Download failed", L"Downloading update", 0);
85 }
86 if (result != IDOK)
87 return;
88
89 PROCESS_INFORMATION pi;
90 STARTUPINFO si;
91 ::ZeroMemory(&si, sizeof(si));
92 si.cb = sizeof(si);
93 si.wShowWindow = FALSE;
94
95 if (!::CreateProcessW(tempFile.c_str(), NULL, NULL, NULL, FALSE, CREATE_BREA KAWAY_FROM_JOB, NULL, NULL, &si, &pi))
96 {
97 // TODO: Localize
98 MessageBoxW(NULL, L"Failed to run updater", L"Downloading update", 0);
99 DebugLastError("Creating updater process failed");
100 return;
101 }
102 ::CloseHandle(pi.hProcess);
103 ::CloseHandle(pi.hThread);
104 }
105 }
106
107 void Updater::StartDownload(HWND dialog)
108 {
109 this->dialog = dialog;
110 CallbackType* callback = new CallbackType(std::bind(&Updater::RunDownload, thi s));
111 ::CreateThread(NULL, 0, reinterpret_cast<LPTHREAD_START_ROUTINE>(&RunThread),
112 callback, 0, NULL);
113 }
114
115 void Updater::RunDownload()
116 {
117 AdblockPlus::ServerResponse response = jsEngine->GetWebRequest()->GET(url, Adb lockPlus::HeaderList());
118 if (response.status != AdblockPlus::WebRequest::NS_OK ||
119 response.responseStatus != 200)
120 {
121 std::stringstream ss;
122 ss << "Update download failed (status: " << response.status << ", ";
123 ss << "response: " << response.responseStatus << ")";
124 Debug(ss.str());
125
126 EndDialog(dialog, -1);
Felix Dahlke 2013/06/06 17:44:00 How about using a constant for this result? downlo
127 return;
128 }
129
130 AdblockPlus::FileSystemPtr fileSystem = jsEngine->GetFileSystem();
131 std::string utfTempFile = ToUtf8String(tempFile);
132 try
133 {
134 // Remove left-overs from previous update attempts
135 fileSystem->Remove(utfTempFile);
136 }
137 catch (const std::exception&)
138 {
139 }
140
141 try
142 {
143 std::tr1::shared_ptr<std::istream> fileData(new std::istringstream(response. responseText));
144 fileSystem->Write(utfTempFile, fileData);
145 }
146 catch (const std::exception& e)
147 {
148 DebugException(e);
149 EndDialog(dialog, -1);
150 return;
151 }
152
153 EndDialog(dialog, IDOK);
154 return;
Felix Dahlke 2013/06/06 17:44:00 Not really necessary
155 }
OLDNEW
« src/engine/Updater.h ('K') | « src/engine/Updater.h ('k') | src/engine/engine.rc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld