Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: chrome/app_installer/win/app_installer_main.cc

Issue 423293004: Move app_installer into chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Make app_installer.gyp a gypi. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6 #include <initguid.h>
7 #include <urlmon.h>
8 #pragma comment(lib, "urlmon.lib")
9
10 #include "base/at_exit.h"
11 #include "base/base_paths.h"
12 #include "base/basictypes.h"
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/logging_win.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h"
19 #include "base/process/launch.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/string_split.h"
22 #include "base/strings/string_util.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/installer/launcher_support/chrome_launcher_support.h"
25 #include "chrome/installer/util/google_update_util.h"
26 #include "chrome/installer/util/html_dialog.h"
27 #include "chrome/installer/util/util_constants.h"
28 #include "third_party/omaha/src/omaha/base/extractor.h"
29
30 namespace app_installer {
31
32 enum ExitCode {
33 SUCCESS = 0,
34 COULD_NOT_GET_FILE_PATH,
35 COULD_NOT_READ_TAG,
36 COULD_NOT_PARSE_TAG,
37 INVALID_APP_ID,
38 EULA_CANCELLED,
39 COULD_NOT_FIND_CHROME,
40 COULD_NOT_GET_TMP_FILE_PATH,
41 FAILED_TO_DOWNLOAD_CHROME_SETUP,
42 FAILED_TO_LAUNCH_CHROME_SETUP,
43 };
44
45 namespace {
46
47 // Log provider UUID. Required for logging to Sawbuck.
48 // {d82c3b59-bacd-4625-8282-4d570c4dad12}
49 DEFINE_GUID(kAppInstallerLogProvider,
50 0xd82c3b59,
51 0xbacd,
52 0x4625,
53 0x82, 0x82, 0x4d, 0x57, 0x0c, 0x4d, 0xad, 0x12);
54
55 const int kMaxTagLength = 4096;
56
57 const char kInstallChromeApp[] = "install-chrome-app";
58
59 const wchar_t kDownloadAndEulaPage[] =
60 L"https://tools.google.com/dlpage/chromeappinstaller";
61
62 const wchar_t kSxSDownloadAndEulaPage[] =
63 L"https://tools.google.com/dlpage/chromeappinstaller?sxs=true";
64
65 const wchar_t kDialogDimensions[] = L"dialogWidth:750px;dialogHeight:500px";
66
67 // This uses HTMLDialog to show a Chrome download page as a modal dialog.
68 // The page includes the EULA and returns a download URL.
69 class DownloadAndEulaHTMLDialog {
70 public:
71 explicit DownloadAndEulaHTMLDialog(bool is_canary) {
72 dialog_.reset(installer::CreateNativeHTMLDialog(
73 is_canary ? kSxSDownloadAndEulaPage : kDownloadAndEulaPage,
74 base::string16()));
75 }
76 ~DownloadAndEulaHTMLDialog() {}
77
78 // Shows the dialog and blocks for user input. Returns the string passed back
79 // by the web page via |window.returnValue|.
80 base::string16 ShowModal() {
81 Customizer customizer;
82 dialog_->ShowModal(NULL, &customizer);
83 return dialog_->GetExtraResult();
84 }
85
86 private:
87 class Customizer : public installer::HTMLDialog::CustomizationCallback {
88 public:
89 virtual void OnBeforeCreation(wchar_t** extra) OVERRIDE {
90 *extra = const_cast<wchar_t*>(kDialogDimensions);
91 }
92
93 virtual void OnBeforeDisplay(void* window) OVERRIDE {
94 // Customize the window by removing the close button and replacing the
95 // existing 'e' icon with the standard informational icon.
96 if (!window)
97 return;
98 HWND top_window = static_cast<HWND>(window);
99 LONG_PTR style = GetWindowLongPtrW(top_window, GWL_STYLE);
100 SetWindowLongPtrW(top_window, GWL_STYLE, style & ~WS_SYSMENU);
101 HICON ico = LoadIcon(NULL, IDI_INFORMATION);
102 SendMessageW(top_window, WM_SETICON, ICON_SMALL,
103 reinterpret_cast<LPARAM>(ico));
104 }
105 };
106
107 scoped_ptr<installer::HTMLDialog> dialog_;
108 DISALLOW_COPY_AND_ASSIGN(DownloadAndEulaHTMLDialog);
109 };
110
111 // Gets the tag attached to a file by dl.google.com. This uses the same format
112 // as Omaha. Returns the empty string on failure.
113 std::string GetTag(const base::FilePath& file_name_path) {
114 base::string16 file_name = file_name_path.value();
115 omaha::TagExtractor extractor;
116 if (!extractor.OpenFile(file_name.c_str()))
117 return std::string();
118
119 int tag_buffer_size = 0;
120 if (!extractor.ExtractTag(NULL, &tag_buffer_size) || tag_buffer_size <= 1)
121 return std::string();
122
123 if (tag_buffer_size - 1 > kMaxTagLength) {
124 LOG(ERROR) << "Tag length (" << tag_buffer_size - 1 << ") exceeds maximum ("
125 << kMaxTagLength << ").";
126 return std::string();
127 }
128
129 scoped_ptr<char[]> tag_buffer(new char[tag_buffer_size]);
130 extractor.ExtractTag(tag_buffer.get(), &tag_buffer_size);
131
132 return std::string(tag_buffer.get(), tag_buffer_size - 1);
133 }
134
135 bool IsStringPrintable(const std::string& s) {
136 return std::find_if_not(s.begin(), s.end(), isprint) == s.end();
137 }
138
139 bool IsLegalDataKeyChar(int c) {
140 return isalnum(c) || c == '-' || c == '_' || c == '$';
141 }
142
143 bool IsDataKeyValid(const std::string& key) {
144 return std::find_if_not(key.begin(), key.end(), IsLegalDataKeyChar) ==
145 key.end();
146 }
147
148 // Parses |tag| as key-value pairs and overwrites |parsed_pairs| with
149 // the result. |tag| should be a '&'-delimited list of '='-separated
150 // key-value pairs, e.g. "key1=value1&key2=value2".
151 // Returns true if the data could be parsed.
152 bool ParseTag(
153 const std::string& tag,
154 std::map<std::string, std::string>* parsed_pairs) {
155 DCHECK(parsed_pairs);
156
157 std::vector<std::pair<std::string, std::string> > kv_pairs;
158 if (!base::SplitStringIntoKeyValuePairs(tag, '=', '&', &kv_pairs)) {
159 LOG(ERROR) << "Failed to parse tag: " << tag;
160 return false;
161 }
162
163 parsed_pairs->clear();
164 for (const auto& pair : kv_pairs) {
165 const std::string& key(pair.first);
166 const std::string& value(pair.second);
167 if (IsDataKeyValid(key) && IsStringPrintable(value)) {
168 (*parsed_pairs)[key] = value;
169 } else {
170 LOG(ERROR) << "Illegal character found in tag.";
171 return false;
172 }
173 }
174 return true;
175 }
176
177 bool IsValidAppId(const std::string& app_id) {
178 if (app_id.size() != 32)
179 return false;
180
181 for (size_t i = 0; i < app_id.size(); ++i) {
182 char c = base::ToLowerASCII(app_id[i]);
183 if (c < 'a' || c > 'p')
184 return false;
185 }
186
187 return true;
188 }
189
190 base::FilePath GetChromeExePath(bool is_canary) {
191 return is_canary ?
192 chrome_launcher_support::GetAnyChromeSxSPath() :
193 chrome_launcher_support::GetAnyChromePath();
194 }
195
196 ExitCode GetChrome(bool is_canary) {
197 // Show UI to install Chrome. The UI returns a download URL.
198 base::string16 download_url =
199 DownloadAndEulaHTMLDialog(is_canary).ShowModal();
200 if (download_url.empty())
201 return EULA_CANCELLED;
202
203 DVLOG(1) << "Chrome download url: " << download_url;
204
205 // Get a temporary file path.
206 base::FilePath setup_file;
207 if (!base::CreateTemporaryFile(&setup_file))
208 return COULD_NOT_GET_TMP_FILE_PATH;
209
210 // Download the Chrome installer.
211 HRESULT hr = URLDownloadToFile(
212 NULL, download_url.c_str(), setup_file.value().c_str(), 0, NULL);
213 if (FAILED(hr)) {
214 LOG(ERROR) << "Download failed: Error " << std::hex << hr << ". "
215 << setup_file.value();
216 return FAILED_TO_DOWNLOAD_CHROME_SETUP;
217 }
218
219 // Install Chrome. Wait for the installer to finish before returning.
220 base::LaunchOptions options;
221 options.wait = true;
222 if (!base::LaunchProcess(CommandLine(setup_file), options, NULL)) {
223 base::DeleteFile(setup_file, false);
224 return FAILED_TO_LAUNCH_CHROME_SETUP;
225 }
226
227 base::DeleteFile(setup_file, false);
228 return SUCCESS;
229 }
230
231 } // namespace
232
233 extern "C"
234 int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance,
235 wchar_t* command_line, int show_command) {
236 base::AtExitManager exit_manager;
237 CommandLine::Init(0, NULL);
238 logging::LogEventProvider::Initialize(kAppInstallerLogProvider);
239 logging::LoggingSettings settings;
240 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
241 logging::InitLogging(settings);
242
243 std::string app_id = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
244 switches::kAppId);
245 const char* sxs = installer::switches::kChromeSxS;
246 // --chrome-sxs on the command line takes precedence over chrome-sxs in the
247 // tag.
248 bool is_canary = CommandLine::ForCurrentProcess()->HasSwitch(sxs);
249
250 // --app-id on the command line inhibits tag parsing altogether.
251 if (app_id.empty()) {
252 base::FilePath current_exe;
253 if (!PathService::Get(base::FILE_EXE, &current_exe))
254 return COULD_NOT_GET_FILE_PATH;
255
256 // Get the tag added by dl.google.com. Note that this is passed in via URL
257 // parameters when requesting a file to download, so it must be validated
258 // before use.
259 std::string tag = GetTag(current_exe);
260 if (tag.empty())
261 return COULD_NOT_READ_TAG;
262
263 DVLOG(1) << "Tag: " << tag;
264
265 std::map<std::string, std::string> parsed_pairs;
266 if (!ParseTag(tag, &parsed_pairs))
267 return COULD_NOT_PARSE_TAG;
268
269 app_id = parsed_pairs[switches::kAppId];
270
271 if (!is_canary)
272 is_canary = parsed_pairs[sxs] == "1";
273 }
274
275 if (!IsValidAppId(app_id))
276 return INVALID_APP_ID;
277
278 base::FilePath chrome_path = GetChromeExePath(is_canary);
279 // If none found, show EULA, download, and install Chrome.
280 if (chrome_path.empty()) {
281 ExitCode get_chrome_result = GetChrome(is_canary);
282 if (get_chrome_result != SUCCESS)
283 return get_chrome_result;
284
285 chrome_path = GetChromeExePath(is_canary);
286 if (chrome_path.empty())
287 return COULD_NOT_FIND_CHROME;
288 }
289
290 CommandLine cmd(chrome_path);
291 cmd.AppendSwitchASCII(kInstallChromeApp, app_id);
292 DVLOG(1) << "Install command: " << cmd.GetCommandLineString();
293 bool launched = base::LaunchProcess(cmd, base::LaunchOptions(), NULL);
294 DVLOG(1) << "Launch " << (launched ? "success." : "failed.");
295
296 return SUCCESS;
297 }
298
299 } // namespace app_installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698