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

Delta Between Two Patch Sets: src/engine/Updater.cpp

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

Powered by Google App Engine
This is Rietveld