1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "nsArrayEnumerator.h"
8 #include "nsCOMArray.h"
9 #include "nsLocalFile.h"
10 #include "nsMIMEInfoWin.h"
11 #include "nsNetUtil.h"
12 #include <windows.h>
13 #include <shellapi.h>
14 #include "nsAutoPtr.h"
15 #include "nsIMutableArray.h"
16 #include "nsTArray.h"
17 #include "shlobj.h"
18 #include "windows.h"
19 #include "nsIWindowsRegKey.h"
20 #include "nsIProcess.h"
21 #include "nsUnicharUtils.h"
22 #include "nsITextToSubURI.h"
23 #include "nsVariant.h"
24 #include "mozilla/UniquePtrExtensions.h"
25
26 #define RUNDLL32_EXE L"\\rundll32.exe"
27
NS_IMPL_ISUPPORTS_INHERITED(nsMIMEInfoWin,nsMIMEInfoBase,nsIPropertyBag)28 NS_IMPL_ISUPPORTS_INHERITED(nsMIMEInfoWin, nsMIMEInfoBase, nsIPropertyBag)
29
30 nsMIMEInfoWin::~nsMIMEInfoWin() {}
31
LaunchDefaultWithFile(nsIFile * aFile)32 nsresult nsMIMEInfoWin::LaunchDefaultWithFile(nsIFile* aFile) {
33 // Launch the file, unless it is an executable.
34 bool executable = true;
35 aFile->IsExecutable(&executable);
36 if (executable) return NS_ERROR_FAILURE;
37
38 return aFile->Launch();
39 }
40
41 NS_IMETHODIMP
LaunchWithFile(nsIFile * aFile)42 nsMIMEInfoWin::LaunchWithFile(nsIFile* aFile) {
43 nsresult rv;
44
45 // it doesn't make any sense to call this on protocol handlers
46 NS_ASSERTION(mClass == eMIMEInfo,
47 "nsMIMEInfoBase should have mClass == eMIMEInfo");
48
49 if (mPreferredAction == useSystemDefault) {
50 return LaunchDefaultWithFile(aFile);
51 }
52
53 if (mPreferredAction == useHelperApp) {
54 if (!mPreferredApplication) return NS_ERROR_FILE_NOT_FOUND;
55
56 // at the moment, we only know how to hand files off to local handlers
57 nsCOMPtr<nsILocalHandlerApp> localHandler =
58 do_QueryInterface(mPreferredApplication, &rv);
59 NS_ENSURE_SUCCESS(rv, rv);
60
61 nsCOMPtr<nsIFile> executable;
62 rv = localHandler->GetExecutable(getter_AddRefs(executable));
63 NS_ENSURE_SUCCESS(rv, rv);
64
65 nsAutoString path;
66 aFile->GetPath(path);
67
68 // Deal with local dll based handlers
69 nsCString filename;
70 executable->GetNativeLeafName(filename);
71 if (filename.Length() > 4) {
72 nsCString extension(Substring(filename, filename.Length() - 4, 4));
73
74 if (extension.LowerCaseEqualsLiteral(".dll")) {
75 nsAutoString args;
76
77 // executable is rundll32, everything else is a list of parameters,
78 // including the dll handler.
79 if (!GetDllLaunchInfo(executable, aFile, args, false))
80 return NS_ERROR_INVALID_ARG;
81
82 WCHAR rundll32Path[MAX_PATH + sizeof(RUNDLL32_EXE) / sizeof(WCHAR) +
83 1] = {L'\0'};
84 if (!GetSystemDirectoryW(rundll32Path, MAX_PATH)) {
85 return NS_ERROR_FILE_NOT_FOUND;
86 }
87 lstrcatW(rundll32Path, RUNDLL32_EXE);
88
89 SHELLEXECUTEINFOW seinfo;
90 memset(&seinfo, 0, sizeof(seinfo));
91 seinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
92 seinfo.fMask = 0;
93 seinfo.hwnd = nullptr;
94 seinfo.lpVerb = nullptr;
95 seinfo.lpFile = rundll32Path;
96 seinfo.lpParameters = args.get();
97 seinfo.lpDirectory = nullptr;
98 seinfo.nShow = SW_SHOWNORMAL;
99 if (ShellExecuteExW(&seinfo)) return NS_OK;
100
101 switch ((LONG_PTR)seinfo.hInstApp) {
102 case 0:
103 case SE_ERR_OOM:
104 return NS_ERROR_OUT_OF_MEMORY;
105 case SE_ERR_ACCESSDENIED:
106 return NS_ERROR_FILE_ACCESS_DENIED;
107 case SE_ERR_ASSOCINCOMPLETE:
108 case SE_ERR_NOASSOC:
109 return NS_ERROR_UNEXPECTED;
110 case SE_ERR_DDEBUSY:
111 case SE_ERR_DDEFAIL:
112 case SE_ERR_DDETIMEOUT:
113 return NS_ERROR_NOT_AVAILABLE;
114 case SE_ERR_DLLNOTFOUND:
115 return NS_ERROR_FAILURE;
116 case SE_ERR_SHARE:
117 return NS_ERROR_FILE_IS_LOCKED;
118 default:
119 switch (GetLastError()) {
120 case ERROR_FILE_NOT_FOUND:
121 return NS_ERROR_FILE_NOT_FOUND;
122 case ERROR_PATH_NOT_FOUND:
123 return NS_ERROR_FILE_UNRECOGNIZED_PATH;
124 case ERROR_BAD_FORMAT:
125 return NS_ERROR_FILE_CORRUPTED;
126 }
127 }
128 return NS_ERROR_FILE_EXECUTION_FAILED;
129 }
130 }
131 return LaunchWithIProcess(executable, path);
132 }
133
134 return NS_ERROR_INVALID_ARG;
135 }
136
137 NS_IMETHODIMP
GetHasDefaultHandler(bool * _retval)138 nsMIMEInfoWin::GetHasDefaultHandler(bool* _retval) {
139 // We have a default application if we have a description
140 // We can ShellExecute anything; however, callers are probably interested if
141 // there is really an application associated with this type of file
142 *_retval = !mDefaultAppDescription.IsEmpty();
143 return NS_OK;
144 }
145
146 NS_IMETHODIMP
GetEnumerator(nsISimpleEnumerator ** _retval)147 nsMIMEInfoWin::GetEnumerator(nsISimpleEnumerator** _retval) {
148 nsCOMArray<nsIVariant> properties;
149
150 nsCOMPtr<nsIVariant> variant;
151 GetProperty(NS_LITERAL_STRING("defaultApplicationIconURL"),
152 getter_AddRefs(variant));
153 if (variant) properties.AppendObject(variant);
154
155 GetProperty(NS_LITERAL_STRING("customApplicationIconURL"),
156 getter_AddRefs(variant));
157 if (variant) properties.AppendObject(variant);
158
159 return NS_NewArrayEnumerator(_retval, properties);
160 }
161
GetIconURLVariant(nsIFile * aApplication,nsIVariant ** _retval)162 static nsresult GetIconURLVariant(nsIFile* aApplication, nsIVariant** _retval) {
163 nsAutoCString fileURLSpec;
164 NS_GetURLSpecFromFile(aApplication, fileURLSpec);
165 nsAutoCString iconURLSpec;
166 iconURLSpec.AssignLiteral("moz-icon://");
167 iconURLSpec += fileURLSpec;
168 RefPtr<nsVariant> writable(new nsVariant());
169 writable->SetAsAUTF8String(iconURLSpec);
170 writable.forget(_retval);
171 return NS_OK;
172 }
173
174 NS_IMETHODIMP
GetProperty(const nsAString & aName,nsIVariant ** _retval)175 nsMIMEInfoWin::GetProperty(const nsAString& aName, nsIVariant** _retval) {
176 nsresult rv;
177 if (mDefaultApplication &&
178 aName.EqualsLiteral(PROPERTY_DEFAULT_APP_ICON_URL)) {
179 rv = GetIconURLVariant(mDefaultApplication, _retval);
180 NS_ENSURE_SUCCESS(rv, rv);
181 } else if (mPreferredApplication &&
182 aName.EqualsLiteral(PROPERTY_CUSTOM_APP_ICON_URL)) {
183 nsCOMPtr<nsILocalHandlerApp> localHandler =
184 do_QueryInterface(mPreferredApplication, &rv);
185 NS_ENSURE_SUCCESS(rv, rv);
186
187 nsCOMPtr<nsIFile> executable;
188 rv = localHandler->GetExecutable(getter_AddRefs(executable));
189 NS_ENSURE_SUCCESS(rv, rv);
190
191 rv = GetIconURLVariant(executable, _retval);
192 NS_ENSURE_SUCCESS(rv, rv);
193 }
194
195 return NS_OK;
196 }
197
198 // this implementation was pretty much copied verbatime from
199 // Tony Robinson's code in nsExternalProtocolWin.cpp
LoadUriInternal(nsIURI * aURL)200 nsresult nsMIMEInfoWin::LoadUriInternal(nsIURI* aURL) {
201 nsresult rv = NS_OK;
202
203 // 1. Find the default app for this protocol
204 // 2. Set up the command line
205 // 3. Launch the app.
206
207 // For now, we'll just cheat essentially, check for the command line
208 // then just call ShellExecute()!
209
210 if (aURL) {
211 // extract the url spec from the url
212 nsAutoCString urlSpec;
213 aURL->GetAsciiSpec(urlSpec);
214
215 // Unescape non-ASCII characters in the URL
216 nsAutoString utf16Spec;
217
218 nsCOMPtr<nsITextToSubURI> textToSubURI =
219 do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
220 NS_ENSURE_SUCCESS(rv, rv);
221
222 if (NS_FAILED(textToSubURI->UnEscapeNonAsciiURI(NS_LITERAL_CSTRING("UTF-8"),
223 urlSpec, utf16Spec))) {
224 CopyASCIItoUTF16(urlSpec, utf16Spec);
225 }
226
227 static const wchar_t cmdVerb[] = L"open";
228 SHELLEXECUTEINFOW sinfo;
229 memset(&sinfo, 0, sizeof(sinfo));
230 sinfo.cbSize = sizeof(sinfo);
231 sinfo.fMask = SEE_MASK_FLAG_DDEWAIT;
232 sinfo.hwnd = nullptr;
233 sinfo.lpVerb = (LPWSTR)&cmdVerb;
234 sinfo.nShow = SW_SHOWNORMAL;
235
236 LPITEMIDLIST pidl = nullptr;
237 SFGAOF sfgao;
238
239 // Bug 394974
240 if (SUCCEEDED(
241 SHParseDisplayName(utf16Spec.get(), nullptr, &pidl, 0, &sfgao))) {
242 sinfo.lpIDList = pidl;
243 sinfo.fMask |= SEE_MASK_INVOKEIDLIST;
244 } else {
245 // SHParseDisplayName failed. Bailing out as work around for
246 // Microsoft Security Bulletin MS07-061
247 rv = NS_ERROR_FAILURE;
248 }
249 if (NS_SUCCEEDED(rv)) {
250 BOOL result = ShellExecuteExW(&sinfo);
251 if (!result || ((LONG_PTR)sinfo.hInstApp) < 32) rv = NS_ERROR_FAILURE;
252 }
253 if (pidl) CoTaskMemFree(pidl);
254 }
255
256 return rv;
257 }
258
259 // Given a path to a local file, return its nsILocalHandlerApp instance.
GetLocalHandlerApp(const nsAString & aCommandHandler,nsCOMPtr<nsILocalHandlerApp> & aApp)260 bool nsMIMEInfoWin::GetLocalHandlerApp(const nsAString& aCommandHandler,
261 nsCOMPtr<nsILocalHandlerApp>& aApp) {
262 nsCOMPtr<nsIFile> locfile;
263 nsresult rv = NS_NewLocalFile(aCommandHandler, true, getter_AddRefs(locfile));
264 if (NS_FAILED(rv)) return false;
265
266 aApp = do_CreateInstance("@mozilla.org/uriloader/local-handler-app;1");
267 if (!aApp) return false;
268
269 aApp->SetExecutable(locfile);
270 return true;
271 }
272
273 // Return the cleaned up file path associated with a command verb
274 // located in root/Applications.
GetAppsVerbCommandHandler(const nsAString & appExeName,nsAString & applicationPath,bool edit)275 bool nsMIMEInfoWin::GetAppsVerbCommandHandler(const nsAString& appExeName,
276 nsAString& applicationPath,
277 bool edit) {
278 nsCOMPtr<nsIWindowsRegKey> appKey =
279 do_CreateInstance("@mozilla.org/windows-registry-key;1");
280 if (!appKey) return false;
281
282 // HKEY_CLASSES_ROOT\Applications\iexplore.exe
283 nsAutoString applicationsPath;
284 applicationsPath.AppendLiteral("Applications\\");
285 applicationsPath.Append(appExeName);
286
287 nsresult rv =
288 appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, applicationsPath,
289 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
290 if (NS_FAILED(rv)) return false;
291
292 // Check for the NoOpenWith flag, if it exists
293 uint32_t value;
294 if (NS_SUCCEEDED(
295 appKey->ReadIntValue(NS_LITERAL_STRING("NoOpenWith"), &value)) &&
296 value == 1)
297 return false;
298
299 nsAutoString dummy;
300 if (NS_SUCCEEDED(
301 appKey->ReadStringValue(NS_LITERAL_STRING("NoOpenWith"), dummy)))
302 return false;
303
304 appKey->Close();
305
306 // HKEY_CLASSES_ROOT\Applications\iexplore.exe\shell\open\command
307 applicationsPath.AssignLiteral("Applications\\");
308 applicationsPath.Append(appExeName);
309 if (!edit)
310 applicationsPath.AppendLiteral("\\shell\\open\\command");
311 else
312 applicationsPath.AppendLiteral("\\shell\\edit\\command");
313
314 rv = appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, applicationsPath,
315 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
316 if (NS_FAILED(rv)) return false;
317
318 nsAutoString appFilesystemCommand;
319 if (NS_SUCCEEDED(
320 appKey->ReadStringValue(EmptyString(), appFilesystemCommand))) {
321 // Expand environment vars, clean up any misc.
322 if (!nsLocalFile::CleanupCmdHandlerPath(appFilesystemCommand)) return false;
323
324 applicationPath = appFilesystemCommand;
325 return true;
326 }
327 return false;
328 }
329
330 // Return a fully populated command string based on
331 // passing information. Used in launchWithFile to trace
332 // back to the full handler path based on the dll.
333 // (dll, targetfile, return args, open/edit)
GetDllLaunchInfo(nsIFile * aDll,nsIFile * aFile,nsAString & args,bool edit)334 bool nsMIMEInfoWin::GetDllLaunchInfo(nsIFile* aDll, nsIFile* aFile,
335 nsAString& args, bool edit) {
336 if (!aDll || !aFile) return false;
337
338 nsString appExeName;
339 aDll->GetLeafName(appExeName);
340
341 nsCOMPtr<nsIWindowsRegKey> appKey =
342 do_CreateInstance("@mozilla.org/windows-registry-key;1");
343 if (!appKey) return false;
344
345 // HKEY_CLASSES_ROOT\Applications\iexplore.exe
346 nsAutoString applicationsPath;
347 applicationsPath.AppendLiteral("Applications\\");
348 applicationsPath.Append(appExeName);
349
350 nsresult rv =
351 appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, applicationsPath,
352 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
353 if (NS_FAILED(rv)) return false;
354
355 // Check for the NoOpenWith flag, if it exists
356 uint32_t value;
357 rv = appKey->ReadIntValue(NS_LITERAL_STRING("NoOpenWith"), &value);
358 if (NS_SUCCEEDED(rv) && value == 1) return false;
359
360 nsAutoString dummy;
361 if (NS_SUCCEEDED(
362 appKey->ReadStringValue(NS_LITERAL_STRING("NoOpenWith"), dummy)))
363 return false;
364
365 appKey->Close();
366
367 // HKEY_CLASSES_ROOT\Applications\iexplore.exe\shell\open\command
368 applicationsPath.AssignLiteral("Applications\\");
369 applicationsPath.Append(appExeName);
370 if (!edit)
371 applicationsPath.AppendLiteral("\\shell\\open\\command");
372 else
373 applicationsPath.AppendLiteral("\\shell\\edit\\command");
374
375 rv = appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, applicationsPath,
376 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
377 if (NS_FAILED(rv)) return false;
378
379 nsAutoString appFilesystemCommand;
380 if (NS_SUCCEEDED(
381 appKey->ReadStringValue(EmptyString(), appFilesystemCommand))) {
382 // Replace embedded environment variables.
383 uint32_t bufLength =
384 ::ExpandEnvironmentStringsW(appFilesystemCommand.get(), nullptr, 0);
385 if (bufLength == 0) // Error
386 return false;
387
388 auto destination = mozilla::MakeUniqueFallible<wchar_t[]>(bufLength);
389 if (!destination) return false;
390 if (!::ExpandEnvironmentStringsW(appFilesystemCommand.get(),
391 destination.get(), bufLength))
392 return false;
393
394 appFilesystemCommand.Assign(destination.get());
395
396 // C:\Windows\System32\rundll32.exe "C:\Program Files\Windows
397 // Photo Gallery\PhotoViewer.dll", ImageView_Fullscreen %1
398 nsAutoString params;
399 NS_NAMED_LITERAL_STRING(rundllSegment, "rundll32.exe ");
400 int32_t index = appFilesystemCommand.Find(rundllSegment);
401 if (index > kNotFound) {
402 params.Append(
403 Substring(appFilesystemCommand, index + rundllSegment.Length()));
404 } else {
405 params.Append(appFilesystemCommand);
406 }
407
408 // check to make sure we have a %1 and fill it
409 NS_NAMED_LITERAL_STRING(percentOneParam, "%1");
410 index = params.Find(percentOneParam);
411 if (index == kNotFound) // no parameter
412 return false;
413
414 nsString target;
415 aFile->GetTarget(target);
416 params.Replace(index, 2, target);
417
418 args = params;
419
420 return true;
421 }
422 return false;
423 }
424
425 // Return the cleaned up file path associated with a progid command
426 // verb located in root.
GetProgIDVerbCommandHandler(const nsAString & appProgIDName,nsAString & applicationPath,bool edit)427 bool nsMIMEInfoWin::GetProgIDVerbCommandHandler(const nsAString& appProgIDName,
428 nsAString& applicationPath,
429 bool edit) {
430 nsCOMPtr<nsIWindowsRegKey> appKey =
431 do_CreateInstance("@mozilla.org/windows-registry-key;1");
432 if (!appKey) return false;
433
434 nsAutoString appProgId(appProgIDName);
435
436 // HKEY_CLASSES_ROOT\Windows.XPSReachViewer\shell\open\command
437 if (!edit)
438 appProgId.AppendLiteral("\\shell\\open\\command");
439 else
440 appProgId.AppendLiteral("\\shell\\edit\\command");
441
442 nsresult rv = appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, appProgId,
443 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
444 if (NS_FAILED(rv)) return false;
445
446 nsAutoString appFilesystemCommand;
447 if (NS_SUCCEEDED(
448 appKey->ReadStringValue(EmptyString(), appFilesystemCommand))) {
449 // Expand environment vars, clean up any misc.
450 if (!nsLocalFile::CleanupCmdHandlerPath(appFilesystemCommand)) return false;
451
452 applicationPath = appFilesystemCommand;
453 return true;
454 }
455 return false;
456 }
457
458 // Helper routine used in tracking app lists. Converts path
459 // entries to lower case and stores them in the trackList array.
ProcessPath(nsCOMPtr<nsIMutableArray> & appList,nsTArray<nsString> & trackList,const nsAString & appFilesystemCommand)460 void nsMIMEInfoWin::ProcessPath(nsCOMPtr<nsIMutableArray>& appList,
461 nsTArray<nsString>& trackList,
462 const nsAString& appFilesystemCommand) {
463 nsAutoString lower(appFilesystemCommand);
464 ToLowerCase(lower);
465
466 // Don't include firefox.exe in the list
467 WCHAR exe[MAX_PATH + 1];
468 uint32_t len = GetModuleFileNameW(nullptr, exe, MAX_PATH);
469 if (len < MAX_PATH && len != 0) {
470 int32_t index = lower.Find(exe);
471 if (index != -1) return;
472 }
473
474 nsCOMPtr<nsILocalHandlerApp> aApp;
475 if (!GetLocalHandlerApp(appFilesystemCommand, aApp)) return;
476
477 // Save in our main tracking arrays
478 appList->AppendElement(aApp);
479 trackList.AppendElement(lower);
480 }
481
482 // Helper routine that handles a compare between a path
483 // and an array of paths.
IsPathInList(nsAString & appPath,nsTArray<nsString> & trackList)484 static bool IsPathInList(nsAString& appPath, nsTArray<nsString>& trackList) {
485 // trackList data is always lowercase, see ProcessPath
486 // above.
487 nsAutoString tmp(appPath);
488 ToLowerCase(tmp);
489
490 for (uint32_t i = 0; i < trackList.Length(); i++) {
491 if (tmp.Equals(trackList[i])) return true;
492 }
493 return false;
494 }
495
496 /**
497 * Returns a list of nsILocalHandlerApp objects containing local
498 * handlers associated with this mimeinfo. Implemented per
499 * platform using information in this object to generate the
500 * best list. Typically used for an "open with" style user
501 * option.
502 *
503 * @return nsIArray of nsILocalHandlerApp
504 */
505 NS_IMETHODIMP
GetPossibleLocalHandlers(nsIArray ** _retval)506 nsMIMEInfoWin::GetPossibleLocalHandlers(nsIArray** _retval) {
507 nsresult rv;
508
509 *_retval = nullptr;
510
511 nsCOMPtr<nsIMutableArray> appList = do_CreateInstance("@mozilla.org/array;1");
512
513 if (!appList) return NS_ERROR_FAILURE;
514
515 nsTArray<nsString> trackList;
516
517 nsAutoCString fileExt;
518 GetPrimaryExtension(fileExt);
519
520 nsCOMPtr<nsIWindowsRegKey> regKey =
521 do_CreateInstance("@mozilla.org/windows-registry-key;1");
522 if (!regKey) return NS_ERROR_FAILURE;
523 nsCOMPtr<nsIWindowsRegKey> appKey =
524 do_CreateInstance("@mozilla.org/windows-registry-key;1");
525 if (!appKey) return NS_ERROR_FAILURE;
526
527 nsAutoString workingRegistryPath;
528
529 bool extKnown = false;
530 if (fileExt.IsEmpty()) {
531 extKnown = true;
532 // Mime type discovery is possible in some cases, through
533 // HKEY_CLASSES_ROOT\MIME\Database\Content Type, however, a number
534 // of file extensions related to mime type are simply not defined,
535 // (application/rss+xml & application/atom+xml are good examples)
536 // in which case we can only provide a generic list.
537 nsAutoCString mimeType;
538 GetMIMEType(mimeType);
539 if (!mimeType.IsEmpty()) {
540 workingRegistryPath.AppendLiteral("MIME\\Database\\Content Type\\");
541 workingRegistryPath.Append(NS_ConvertASCIItoUTF16(mimeType));
542
543 rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
544 workingRegistryPath,
545 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
546 if (NS_SUCCEEDED(rv)) {
547 nsAutoString mimeFileExt;
548 if (NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(), mimeFileExt))) {
549 CopyUTF16toUTF8(mimeFileExt, fileExt);
550 extKnown = false;
551 }
552 }
553 }
554 }
555
556 nsAutoString fileExtToUse;
557 if (!fileExt.IsEmpty() && fileExt.First() != '.') {
558 fileExtToUse = char16_t('.');
559 }
560 fileExtToUse.Append(NS_ConvertUTF8toUTF16(fileExt));
561
562 // Note, the order in which these occur has an effect on the
563 // validity of the resulting display list.
564
565 if (!extKnown) {
566 // 1) Get the default handler if it exists
567 workingRegistryPath = fileExtToUse;
568
569 rv =
570 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
571 workingRegistryPath, nsIWindowsRegKey::ACCESS_QUERY_VALUE);
572 if (NS_SUCCEEDED(rv)) {
573 nsAutoString appProgId;
574 if (NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(), appProgId))) {
575 // Bug 358297 - ignore the embedded internet explorer handler
576 if (appProgId != NS_LITERAL_STRING("XPSViewer.Document")) {
577 nsAutoString appFilesystemCommand;
578 if (GetProgIDVerbCommandHandler(appProgId, appFilesystemCommand,
579 false) &&
580 !IsPathInList(appFilesystemCommand, trackList)) {
581 ProcessPath(appList, trackList, appFilesystemCommand);
582 }
583 }
584 }
585 regKey->Close();
586 }
587
588 // 2) list HKEY_CLASSES_ROOT\.ext\OpenWithList
589
590 workingRegistryPath = fileExtToUse;
591 workingRegistryPath.AppendLiteral("\\OpenWithList");
592
593 rv =
594 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
595 workingRegistryPath, nsIWindowsRegKey::ACCESS_QUERY_VALUE);
596 if (NS_SUCCEEDED(rv)) {
597 uint32_t count = 0;
598 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
599 for (uint32_t index = 0; index < count; index++) {
600 nsAutoString appName;
601 if (NS_FAILED(regKey->GetValueName(index, appName))) continue;
602
603 // HKEY_CLASSES_ROOT\Applications\firefox.exe = "path params"
604 nsAutoString appFilesystemCommand;
605 if (!GetAppsVerbCommandHandler(appName, appFilesystemCommand,
606 false) ||
607 IsPathInList(appFilesystemCommand, trackList))
608 continue;
609 ProcessPath(appList, trackList, appFilesystemCommand);
610 }
611 }
612 regKey->Close();
613 }
614
615 // 3) List HKEY_CLASSES_ROOT\.ext\OpenWithProgids, with the
616 // different step of resolving the progids for the command handler.
617
618 workingRegistryPath = fileExtToUse;
619 workingRegistryPath.AppendLiteral("\\OpenWithProgids");
620
621 rv =
622 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
623 workingRegistryPath, nsIWindowsRegKey::ACCESS_QUERY_VALUE);
624 if (NS_SUCCEEDED(rv)) {
625 uint32_t count = 0;
626 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
627 for (uint32_t index = 0; index < count; index++) {
628 // HKEY_CLASSES_ROOT\.ext\OpenWithProgids\Windows.XPSReachViewer
629 nsAutoString appProgId;
630 if (NS_FAILED(regKey->GetValueName(index, appProgId))) continue;
631
632 nsAutoString appFilesystemCommand;
633 if (!GetProgIDVerbCommandHandler(appProgId, appFilesystemCommand,
634 false) ||
635 IsPathInList(appFilesystemCommand, trackList))
636 continue;
637 ProcessPath(appList, trackList, appFilesystemCommand);
638 }
639 }
640 regKey->Close();
641 }
642
643 // 4) Add any non configured applications located in the MRU list
644
645 // HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
646 // \Explorer\FileExts\.ext\OpenWithList
647 workingRegistryPath = NS_LITERAL_STRING(
648 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\");
649 workingRegistryPath += fileExtToUse;
650 workingRegistryPath.AppendLiteral("\\OpenWithList");
651
652 rv =
653 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER,
654 workingRegistryPath, nsIWindowsRegKey::ACCESS_QUERY_VALUE);
655 if (NS_SUCCEEDED(rv)) {
656 uint32_t count = 0;
657 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
658 for (uint32_t index = 0; index < count; index++) {
659 nsAutoString appName, appValue;
660 if (NS_FAILED(regKey->GetValueName(index, appName))) continue;
661 if (appName.EqualsLiteral("MRUList")) continue;
662 if (NS_FAILED(regKey->ReadStringValue(appName, appValue))) continue;
663
664 // HKEY_CLASSES_ROOT\Applications\firefox.exe = "path params"
665 nsAutoString appFilesystemCommand;
666 if (!GetAppsVerbCommandHandler(appValue, appFilesystemCommand,
667 false) ||
668 IsPathInList(appFilesystemCommand, trackList))
669 continue;
670 ProcessPath(appList, trackList, appFilesystemCommand);
671 }
672 }
673 }
674
675 // 5) Add any non configured progids in the MRU list, with the
676 // different step of resolving the progids for the command handler.
677
678 // HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
679 // \Explorer\FileExts\.ext\OpenWithProgids
680 workingRegistryPath = NS_LITERAL_STRING(
681 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\");
682 workingRegistryPath += fileExtToUse;
683 workingRegistryPath.AppendLiteral("\\OpenWithProgids");
684
685 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, workingRegistryPath,
686 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
687 if (NS_SUCCEEDED(rv)) {
688 uint32_t count = 0;
689 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
690 for (uint32_t index = 0; index < count; index++) {
691 nsAutoString appIndex, appProgId;
692 if (NS_FAILED(regKey->GetValueName(index, appProgId))) continue;
693
694 nsAutoString appFilesystemCommand;
695 if (!GetProgIDVerbCommandHandler(appProgId, appFilesystemCommand,
696 false) ||
697 IsPathInList(appFilesystemCommand, trackList))
698 continue;
699 ProcessPath(appList, trackList, appFilesystemCommand);
700 }
701 }
702 regKey->Close();
703 }
704
705 // 6) Check the perceived type value, and use this to lookup the
706 // perceivedtype open with list.
707 // http://msdn2.microsoft.com/en-us/library/aa969373.aspx
708
709 workingRegistryPath = fileExtToUse;
710
711 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, workingRegistryPath,
712 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
713 if (NS_SUCCEEDED(rv)) {
714 nsAutoString perceivedType;
715 rv = regKey->ReadStringValue(NS_LITERAL_STRING("PerceivedType"),
716 perceivedType);
717 if (NS_SUCCEEDED(rv)) {
718 nsAutoString openWithListPath(
719 NS_LITERAL_STRING("SystemFileAssociations\\"));
720 openWithListPath.Append(perceivedType); // no period
721 openWithListPath.AppendLiteral("\\OpenWithList");
722
723 nsresult rv = appKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
724 openWithListPath,
725 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
726 if (NS_SUCCEEDED(rv)) {
727 uint32_t count = 0;
728 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
729 for (uint32_t index = 0; index < count; index++) {
730 nsAutoString appName;
731 if (NS_FAILED(regKey->GetValueName(index, appName))) continue;
732
733 // HKEY_CLASSES_ROOT\Applications\firefox.exe = "path params"
734 nsAutoString appFilesystemCommand;
735 if (!GetAppsVerbCommandHandler(appName, appFilesystemCommand,
736 false) ||
737 IsPathInList(appFilesystemCommand, trackList))
738 continue;
739 ProcessPath(appList, trackList, appFilesystemCommand);
740 }
741 }
742 }
743 }
744 }
745 } // extKnown == false
746
747 // 7) list global HKEY_CLASSES_ROOT\*\OpenWithList
748 // Listing general purpose handlers, not specific to a mime type or file
749 // extension
750
751 workingRegistryPath = NS_LITERAL_STRING("*\\OpenWithList");
752
753 rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
754 workingRegistryPath, nsIWindowsRegKey::ACCESS_QUERY_VALUE);
755 if (NS_SUCCEEDED(rv)) {
756 uint32_t count = 0;
757 if (NS_SUCCEEDED(regKey->GetValueCount(&count)) && count > 0) {
758 for (uint32_t index = 0; index < count; index++) {
759 nsAutoString appName;
760 if (NS_FAILED(regKey->GetValueName(index, appName))) continue;
761
762 // HKEY_CLASSES_ROOT\Applications\firefox.exe = "path params"
763 nsAutoString appFilesystemCommand;
764 if (!GetAppsVerbCommandHandler(appName, appFilesystemCommand, false) ||
765 IsPathInList(appFilesystemCommand, trackList))
766 continue;
767 ProcessPath(appList, trackList, appFilesystemCommand);
768 }
769 }
770 regKey->Close();
771 }
772
773 // 8) General application's list - not file extension specific on windows
774 workingRegistryPath = NS_LITERAL_STRING("Applications");
775
776 rv =
777 regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, workingRegistryPath,
778 nsIWindowsRegKey::ACCESS_ENUMERATE_SUB_KEYS |
779 nsIWindowsRegKey::ACCESS_QUERY_VALUE);
780 if (NS_SUCCEEDED(rv)) {
781 uint32_t count = 0;
782 if (NS_SUCCEEDED(regKey->GetChildCount(&count)) && count > 0) {
783 for (uint32_t index = 0; index < count; index++) {
784 nsAutoString appName;
785 if (NS_FAILED(regKey->GetChildName(index, appName))) continue;
786
787 // HKEY_CLASSES_ROOT\Applications\firefox.exe = "path params"
788 nsAutoString appFilesystemCommand;
789 if (!GetAppsVerbCommandHandler(appName, appFilesystemCommand, false) ||
790 IsPathInList(appFilesystemCommand, trackList))
791 continue;
792 ProcessPath(appList, trackList, appFilesystemCommand);
793 }
794 }
795 }
796
797 // Return to the caller
798 *_retval = appList;
799 NS_ADDREF(*_retval);
800
801 return NS_OK;
802 }
803