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

Powered by Google App Engine
This is Rietveld