1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//------------------------------------------------------------------------
#include "nsIFile.h"
#include "mozilla/ModuleUtils.h"
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsDirectoryServiceDefs.h"
#include "nsLiteralString.h"
#include "nsReadableUtils.h"
#include "nsIStringBundle.h"
#include "mozilla/Services.h"
#define INCL_WIN
#define INCL_DOS
#include <os2.h>
// nsRwsService must be included _after_ os2.h
#include "nsRwsService.h"
#include "rwserr.h"
#include "nsOS2Uni.h"
#include "prenv.h"
#include <stdio.h>
//------------------------------------------------------------------------
// debug macros
//------------------------------------------------------------------------
#ifdef DEBUG
#define RWS_DEBUG
#endif
// use this to force debug msgs in non-debug builds
//#ifndef RWS_DEBUG
// #define RWS_DEBUG
//#endif
#ifdef RWS_DEBUG
#define ERRMSG(x,y) { printf(y " failed - rc= %d\n", (int)x); }
#define ERRBREAK(x,y) if (x) { ERRMSG(x,y); break; }
#define ERRPRINTF(x,y) { printf(y "\n", x); }
#else
#define ERRMSG(x,y) ;
#define ERRBREAK(x,y) if (x) break;
#define ERRPRINTF(x,y) ;
#endif
//------------------------------------------------------------------------
// function prototypes
//------------------------------------------------------------------------
static nsresult IsDescendedFrom(uint32_t wpsFilePtr, const char *pszClassname);
static nsresult CreateFileForExtension(const char *aFileExt, nsACString& aPath);
static nsresult DeleteFileForExtension(const char *aPath);
static void AssignNLSString(const PRUnichar *aKey, nsAString& _retval);
static nsresult AssignTitleString(const char *aTitle, nsAString& result);
//------------------------------------------------------------------------
// module-level variables - not declared as members of nsRws
//------------------------------------------------------------------------
static nsRwsService *sRwsInstance = 0; // our singleton instance
static bool sInit = FALSE; // have we tried to load RWS
// RWS administrative functions
static ULONG (* _System sRwsClientInit)(BOOL);
static ULONG (* _System sRwsGetTimeout)(PULONG, PULONG);
static ULONG (* _System sRwsSetTimeout)(ULONG);
static ULONG (* _System sRwsClientTerminate)(void);
// RWS task-oriented functions
static ULONG (* _System sRwsCall)(PRWSHDR*, ULONG, ULONG, ULONG, ULONG, ULONG, ...);
static ULONG (* _System sRwsCallIndirect)(PRWSBLD);
static ULONG (* _System sRwsFreeMem)(PRWSHDR);
static ULONG (* _System sRwsGetResult)(PRWSHDR, ULONG, PULONG);
static ULONG (* _System sRwsGetArgPtr)(PRWSHDR, ULONG, PRWSDSC*);
//------------------------------------------------------------------------
// ExtCache - caches the default icons & handlers for file extensions
//------------------------------------------------------------------------
typedef struct _ExtInfo
{
char ext[8];
uint32_t icon;
uint32_t mini;
uint32_t handler;
PRUnichar *title;
} ExtInfo;
#define kGrowBy 8
#define kMutexTimeout 500
class ExtCache
{
public:
ExtCache();
~ExtCache();
nsresult GetIcon(const char *aExt, bool aNeedMini, uint32_t *oIcon);
nsresult SetIcon(const char *aExt, bool aIsMini, uint32_t aIcon);
nsresult GetHandler(const char *aExt, uint32_t *oHandle, nsAString& oTitle);
nsresult SetHandler(const char *aExt, uint32_t aHandle, nsAString& aTitle);
void EmptyCache();
protected:
ExtInfo *FindExtension(const char *aExt, bool aSet = false);
uint32_t mPid;
uint32_t mMutex;
uint32_t mCount;
uint32_t mSize;
ExtInfo *mExtInfo;
};
//------------------------------------------------------------------------
// nsIRwsService implementation
//------------------------------------------------------------------------
NS_IMPL_ISUPPORTS2(nsRwsService, nsIRwsService, nsIObserver)
nsRwsService::nsRwsService()
{
mExtCache = new ExtCache();
if (!mExtCache)
ERRPRINTF("", "nsRwsService - unable to allocate mExtArray%s");
}
nsRwsService::~nsRwsService()
{
}
//------------------------------------------------------------------------
// provides the default icon associated with a given extension; if the
// icon isn't in the cache, it creates a temp file with that extension,
// retrieves its icon, deletes the temp file, then caches the icon
NS_IMETHODIMP
nsRwsService::IconFromExtension(const char *aExt, bool aNeedMini,
uint32_t *_retval)
{
if (!aExt || !*aExt || !_retval)
return NS_ERROR_INVALID_ARG;
nsresult rv = mExtCache->GetIcon(aExt, aNeedMini, _retval);
if (NS_SUCCEEDED(rv))
return rv;
nsAutoCString path;
rv = CreateFileForExtension(aExt, path);
if (NS_SUCCEEDED(rv)) {
rv = IconFromPath(path.get(), false, aNeedMini, _retval);
DeleteFileForExtension(path.get());
if (NS_SUCCEEDED(rv))
mExtCache->SetIcon(aExt, aNeedMini, *_retval);
}
return rv;
}
//------------------------------------------------------------------------
// retrieves an object's icon using its fully-qualified path; aAbstract
// indicates whether this is a Filesystem object or an Abstract or
// Transient object; locating non-file objects is fairly expensive
NS_IMETHODIMP
nsRwsService::IconFromPath(const char *aPath, bool aAbstract,
bool aNeedMini, uint32_t *_retval)
{
if (!aPath || !*aPath || !_retval)
return NS_ERROR_INVALID_ARG;
uint32_t rwsType;
if (aAbstract)
rwsType = (aNeedMini ? RWSC_OFTITLE_OMINI : RWSC_OFTITLE_OICON);
else
rwsType = (aNeedMini ? RWSC_OPATH_OMINI : RWSC_OPATH_OICON);
return RwsConvert(rwsType, (uint32_t)aPath, _retval);
}
//------------------------------------------------------------------------
// retrieve's an object's icon using its persistent handle
NS_IMETHODIMP
nsRwsService::IconFromHandle(uint32_t aHandle, bool aNeedMini,
uint32_t *_retval)
{
if (!aHandle || !_retval)
return NS_ERROR_INVALID_ARG;
return RwsConvert( (aNeedMini ? RWSC_OHNDL_OMINI : RWSC_OHNDL_OICON),
aHandle, _retval);
}
//------------------------------------------------------------------------
// retrieve's an object's title using its persistent handle
NS_IMETHODIMP
nsRwsService::TitleFromHandle(uint32_t aHandle, nsAString& _retval)
{
if (!aHandle)
return NS_ERROR_INVALID_ARG;
return RwsConvert(RWSC_OHNDL_OTITLE, aHandle, _retval);
}
//------------------------------------------------------------------------
// provides the default handler associated with a given extension; if the
// info isn't in the cache, it creates a temp file with that extension,
// retrieves the handler's title & possibly its object handle, deletes the
// temp file, then caches the info.
NS_IMETHODIMP
nsRwsService::HandlerFromExtension(const char *aExt, uint32_t *aHandle,
nsAString& _retval)
{
if (!aExt || !*aExt || !aHandle)
return NS_ERROR_INVALID_ARG;
nsresult rv = mExtCache->GetHandler(aExt, aHandle, _retval);
if (NS_SUCCEEDED(rv))
return rv;
nsAutoCString path;
rv = CreateFileForExtension(aExt, path);
if (NS_SUCCEEDED(rv)) {
rv = HandlerFromPath(path.get(), aHandle, _retval);
DeleteFileForExtension(path.get());
if (NS_SUCCEEDED(rv))
mExtCache->SetHandler(aExt, *aHandle, _retval);
}
return rv;
}
//------------------------------------------------------------------------
// identifies the default WPS handler for a given file; if the handler
// is an associated program, returns its title & object handle; if the
// handler is an integrated class viewer or player, it constructs a title
// and sets the object handle to zero
NS_IMETHODIMP
nsRwsService::HandlerFromPath(const char *aPath, uint32_t *aHandle,
nsAString& _retval)
{
if (!aPath || !*aPath || !aHandle)
return NS_ERROR_INVALID_ARG;
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
uint32_t rc;
_retval.Truncate();
*aHandle = 0;
// this dummy do{...}while(0) loop lets us bail out early
// while ensuring pHdr gets freed
do {
// get the default view for this file
rc = sRwsCall(&pHdr,
RWSP_MNAM, (ULONG)"wpQueryDefaultView",
RWSR_ASIS, 0, 1,
RWSI_OPATH, 0, (ULONG)aPath);
ERRBREAK(rc, "wpQueryDefaultView")
uint32_t defView = sRwsGetResult(pHdr, 0, 0);
if (defView == (uint32_t)-1)
break;
// if the default view is OPEN_SETTINGS ('2'),
// change it to OPEN_RUNNING ('4')
if (defView == 2)
defView = 4;
// to improve efficiency, retrieve & reuse the file's wpObject
// ptr rather than repeatedly converting the name to an object
uint32_t wpsFilePtr = sRwsGetResult(pHdr, 1, 0);
// free pHdr before the next call
sRwsFreeMem(pHdr);
pHdr = 0;
// for default view values less than OPEN_USER, see if there
// is a program or program object associated with this file
if (defView < 0x6500) {
rc = sRwsCall(&pHdr,
RWSP_MNAM, (ULONG)"wpQueryAssociatedProgram",
RWSR_OHNDL, 0, 6,
RWSI_ASIS, 0, wpsFilePtr,
RWSI_ASIS, 0, defView,
RWSI_PULONG, 0, 0,
RWSI_PBUF, 32, 0,
RWSI_ASIS, 0, 32,
RWSI_ASIS, 0, (ULONG)-1);
// if there's no associated program, create dummy values
// (if chosen, the WPS will open the file's Properties notebook)
if (rc) {
if (rc == RWSSRV_FUNCTIONFAILED) {
*aHandle = 0;
AssignNLSString(NS_LITERAL_STRING("wpsDefaultOS2").get(), _retval);
rv = NS_OK;
}
else
ERRMSG(rc, "wpQueryAssociatedProgram")
break;
}
// get a ptr to the return value's data structure; then get
// both the object handle we requested & the raw WPS object ptr
PRWSDSC pRtn;
rc = sRwsGetArgPtr(pHdr, 0, &pRtn);
ERRBREAK(rc, "GetArgPtr")
*aHandle = *((uint32_t*)pRtn->pget);
uint32_t wpsPgmPtr = pRtn->value;
// free pHdr before the next call
sRwsFreeMem(pHdr);
pHdr = 0;
// convert the object to its title (using Rws' conversion
// feature is more efficient than calling wpQueryTitle)
rc = sRwsCall(&pHdr,
RWSP_CONV, 0,
RWSR_ASIS, 0, 1,
RWSC_OBJ_OTITLE, 0, wpsPgmPtr);
ERRBREAK(rc, "convert pgm object to title")
// retrieve the title, massage it & assign to _retval, then exit
// N.B. no need to free pHdr here, it will be freed below
char *pszTitle = (char*)sRwsGetResult(pHdr, 1, 0);
if (pszTitle != (char*)-1)
rv = AssignTitleString(pszTitle, _retval);
break;
}
// the default view is provided by the file's WPS class;
// in this case, *aHandle is 0
// see if it's a known view that can be given a meaningful name
switch (defView) {
case 0xbc2b: {
rv = IsDescendedFrom(wpsFilePtr, "MMImage");
if (NS_SUCCEEDED(rv))
AssignNLSString(NS_LITERAL_STRING("mmImageViewerOS2").get(), _retval);
break;
}
case 0xbc0d: // Audio editor
case 0xbc21: // Video editor
case 0xbc17: // Midi editor
case 0xbbef: // Play
case 0xbbe5: { // Player
rv = IsDescendedFrom(wpsFilePtr, "MMAudio");
if (NS_SUCCEEDED(rv)) {
AssignNLSString(NS_LITERAL_STRING("mmAudioPlayerOS2").get(), _retval);
break;
}
rv = IsDescendedFrom(wpsFilePtr, "MMVideo");
if (NS_SUCCEEDED(rv)) {
AssignNLSString(NS_LITERAL_STRING("mmVideoPlayerOS2").get(), _retval);
break;
}
rv = IsDescendedFrom(wpsFilePtr, "MMMIDI");
if (NS_SUCCEEDED(rv))
AssignNLSString(NS_LITERAL_STRING("mmMidiPlayerOS2").get(), _retval);
break;
}
case 0x7701: {
rv = IsDescendedFrom(wpsFilePtr, "TSArcMgr");
if (NS_SUCCEEDED(rv))
AssignNLSString(NS_LITERAL_STRING("odZipFolderOS2").get(), _retval);
break;
}
// this has to go last because TSEnhDataFile replaces
// WPDataFile; if present, all data files are descended from it
case 0xb742:
case 0xa742: {
rv = IsDescendedFrom(wpsFilePtr, "TSEnhDataFile");
if (NS_SUCCEEDED(rv))
AssignNLSString(NS_LITERAL_STRING("odTextViewOS2").get(), _retval);
break;
}
} // end switch
if (NS_SUCCEEDED(rv))
break;
// the name of this view is unknown, so create a generic name
// (i.e. "Viewer for Class WPSomething")
// use the file's object ptr to get the name of its class
rc = sRwsCall(&pHdr,
RWSP_CONV, 0,
RWSR_ASIS, 0, 1,
RWSC_OBJ_CNAME, 0, wpsFilePtr);
ERRBREAK(rc, "convert object to classname")
char *pszTitle = (char*)sRwsGetResult(pHdr, 1, 0);
if (pszTitle == (char*)-1)
break;
nsAutoChar16Buffer buffer;
int32_t bufLength;
rv = MultiByteToWideChar(0, pszTitle, strlen(pszTitle),
buffer, bufLength);
if (NS_FAILED(rv))
break;
nsAutoString classViewer;
AssignNLSString(NS_LITERAL_STRING("classViewerOS2").get(), classViewer);
int pos = -1;
if ((pos = classViewer.Find("%S")) > -1)
classViewer.Replace(pos, 2, buffer.Elements());
_retval.Assign(classViewer);
rv = NS_OK;
} while (0);
// free the pHdr allocated by the final call
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// retrieves an object's menu using its fully-qualified path; aAbstract
// indicates whether this is a Filesystem object or an Abstract or
// Transient object; locating non-file objects is fairly expensive
NS_IMETHODIMP
nsRwsService::MenuFromPath(const char *aPath, bool aAbstract)
{
if (!aPath || !*aPath)
return NS_ERROR_INVALID_ARG;
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
uint32_t type = (aAbstract ? RWSI_OFTITLE : RWSI_OPATH);
uint32_t rc;
POINTL ptl;
HWND hTgt = 0;
// try to identify the window where the click occurred
if (WinQueryMsgPos(0, &ptl)) {
hTgt = WinQueryFocus(HWND_DESKTOP);
if (hTgt)
WinMapWindowPoints(HWND_DESKTOP, hTgt, &ptl, 1);
}
// if we have the window & coordinates, use them; otherwise,
// let RWS position the menu at the current pointer position
// (specifying the window ensures the focus will return to it)
if (hTgt)
rc = sRwsCall(&pHdr,
RWSP_CMD, RWSCMD_POPUPMENU,
RWSR_ASIS, 0, 3,
type, 0, (ULONG)aPath,
RWSI_ASIS, 0, hTgt,
RWSI_PBUF, sizeof(POINTL), (ULONG)&ptl);
else
rc = sRwsCall(&pHdr,
RWSP_CMD, RWSCMD_POPUPMENU,
RWSR_ASIS, 0, 3,
type, 0, (ULONG)aPath,
RWSI_ASIS, 0, 0,
RWSI_ASIS, 0, 0);
if (rc)
ERRMSG(rc, "RWSCMD_POPUPMENU")
else
rv = NS_OK;
// free pHdr
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// causes the WPS to open a file using the program specified by AppPath;
// this is identical to dropping the file on the program's WPS icon
NS_IMETHODIMP
nsRwsService::OpenWithAppHandle(const char *aFilePath, uint32_t aAppHandle)
{
if (!aFilePath || !*aFilePath || !aAppHandle)
return NS_ERROR_INVALID_ARG;
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
uint32_t rc;
rc = sRwsCall(&pHdr,
RWSP_CMD, RWSCMD_OPENUSING,
RWSR_ASIS, 0, 2,
RWSI_OPATH, 0, aFilePath,
RWSI_OHNDL, 0, aAppHandle);
if (rc)
ERRMSG(rc, "RWSCMD_OPENUSING")
else
rv = NS_OK;
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// causes the WPS to open a file using the program specified by AppPath;
// this is identical to dropping the file on the program's WPS icon
NS_IMETHODIMP
nsRwsService::OpenWithAppPath(const char *aFilePath, const char *aAppPath)
{
if (!aFilePath || !*aFilePath || !aAppPath || !*aAppPath)
return NS_ERROR_INVALID_ARG;
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
uint32_t rc;
rc = sRwsCall(&pHdr,
RWSP_CMD, RWSCMD_OPENUSING,
RWSR_ASIS, 0, 2,
RWSI_OPATH, 0, aFilePath,
RWSI_OPATH, 0, aAppPath);
if (rc)
ERRMSG(rc, "RWSCMD_OPENUSING")
else
rv = NS_OK;
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// nsRwsService additional methods
//------------------------------------------------------------------------
// uses RwsConvert to retrieve a result that can be handled as a ULONG;
// type identifies the conversion, value can be anything (ULONG, char*, etc)
//static
nsresult
nsRwsService::RwsConvert(uint32_t type, uint32_t value, uint32_t *result)
{
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
*result = 0;
uint32_t rc = sRwsCall(&pHdr,
RWSP_CONV, 0,
RWSR_ASIS, 0, 1,
type, 0, value);
if (rc)
ERRMSG(rc, "RwsConvert to ULONG")
else {
*result = sRwsGetResult(pHdr, 1, 0);
if (*result == (uint32_t)-1)
*result = 0;
else
rv = NS_OK;
}
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// uses RwsConvert to retrieve a result that can be handled as a
// Unicode string; type identifies the conversion, value can be
// anything (ULONG, char*, etc)
//static
nsresult
nsRwsService::RwsConvert(uint32_t type, uint32_t value, nsAString& result)
{
nsresult rv = NS_ERROR_FAILURE;
PRWSHDR pHdr = 0;
result.Truncate();
uint32_t rc = sRwsCall(&pHdr,
RWSP_CONV, 0,
RWSR_ASIS, 0, 1,
type, 0, value);
if (rc)
ERRMSG(rc, "RwsConvert to string")
else {
char *string = (char*)sRwsGetResult(pHdr, 1, 0);
if (string != (char*)-1)
rv = AssignTitleString(string, result);
}
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// nsIObserver implementation
//------------------------------------------------------------------------
// when the app shuts down, advise RwsClient; if this is the
// last app using RwsServer, the WPS will unload the class's dll;
// also, empty the extension cache to unlock & delete its icons
NS_IMETHODIMP
nsRwsService::Observe(nsISupports *aSubject, const char *aTopic,
const PRUnichar *aSomeData)
{
if (strcmp(aTopic, "quit-application") == 0) {
uint32_t rc = sRwsClientTerminate();
if (rc)
ERRMSG(rc, "RwsClientTerminate");
if (mExtCache)
mExtCache->EmptyCache();
}
return NS_OK;
}
//------------------------------------------------------------------------
// nsRwsService static helper functions
//------------------------------------------------------------------------
// this wrapper for somIsA() makes HandlerFromPath() easier to read
static nsresult IsDescendedFrom(uint32_t wpsFilePtr, const char *pszClassname)
{
PRWSHDR pHdr = 0;
nsresult rv = NS_ERROR_FAILURE;
uint32_t rc = sRwsCall(&pHdr,
RWSP_MNAMI, (ULONG)"somIsA",
RWSR_ASIS, 0, 2,
RWSI_ASIS, 0, wpsFilePtr,
RWSI_CNAME, 0, pszClassname);
if (rc)
ERRMSG(rc, "somIsA")
else
if (sRwsGetResult(pHdr, 0, 0) == TRUE)
rv = NS_OK;
sRwsFreeMem(pHdr);
return rv;
}
//------------------------------------------------------------------------
// create a temp file with the specified extension, then return its path
static nsresult CreateFileForExtension(const char *aFileExt,
nsACString& aPath)
{
nsresult rv = NS_ERROR_FAILURE;
aPath.Truncate();
nsCOMPtr<nsIFile> tempFile;
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(tempFile));
if (NS_FAILED(rv))
return rv;
nsAutoCString pathStr(NS_LITERAL_CSTRING("nsrws."));
if (*aFileExt == '.')
aFileExt++;
pathStr.Append(aFileExt);
rv = tempFile->AppendNative(pathStr);
if (NS_FAILED(rv))
return rv;
tempFile->GetNativePath(pathStr);
FILE *fp = fopen(pathStr.get(), "wb+");
if (fp) {
fclose(fp);
aPath.Assign(pathStr);
rv = NS_OK;
}
return rv;
}
//------------------------------------------------------------------------
// delete a temp file created earlier
static nsresult DeleteFileForExtension(const char *aPath)
{
if (!aPath || !*aPath)
return NS_ERROR_INVALID_ARG;
remove(aPath);
return NS_OK;
}
//------------------------------------------------------------------------
// returns a localized string from unknownContentType.properties;
// if there's a failure, returns "WPS Default"
static void AssignNLSString(const PRUnichar *aKey, nsAString& result)
{
nsresult rv;
nsXPIDLString title;
do {
nsCOMPtr<nsIStringBundleService> bundleSvc =
mozilla::services::GetStringBundleService();
if (!bundleSvc)
break;
nsCOMPtr<nsIStringBundle> bundle;
rv = bundleSvc->CreateBundle(
"chrome://mozapps/locale/downloads/unknownContentType.properties",
getter_AddRefs(bundle));
if (NS_FAILED(rv))
break;
// if we can't fetch the requested string, try to get "WPS Default"
rv = bundle->GetStringFromName(aKey, getter_Copies(title));
if (NS_FAILED(rv))
rv = bundle->GetStringFromName(NS_LITERAL_STRING("wpsDefaultOS2").get(),
getter_Copies(title));
} while (0);
if (NS_SUCCEEDED(rv))
result.Assign(title);
else
result.Assign(NS_LITERAL_STRING("WPS Default"));
}
//------------------------------------------------------------------------
// converts a native string (presumably a WPS object's title) to
// Unicode, removes line breaks, and compresses whitespace
static nsresult AssignTitleString(const char *aTitle, nsAString& result)
{
nsAutoChar16Buffer buffer;
int32_t bufLength;
// convert the title to Unicode
if (NS_FAILED(MultiByteToWideChar(0, aTitle, strlen(aTitle),
buffer, bufLength)))
return NS_ERROR_FAILURE;
PRUnichar *pSrc;
PRUnichar *pDst;
bool fSkip;
// remove line breaks, leading whitespace, & extra embedded whitespace
// (primitive, but gcc 3.2.2 doesn't support wchar)
for (fSkip=true, pSrc=pDst=buffer.Elements(); *pSrc; pSrc++) {
if (*pSrc == ' ' || *pSrc == '\r' || *pSrc == '\n' || *pSrc == '\t') {
if (!fSkip)
*pDst++ = ' ';
fSkip = true;
}
else {
if (pDst != pSrc)
*pDst = *pSrc;
pDst++;
fSkip = false;
}
}
// remove the single trailing space, if needed
if (fSkip && pDst > buffer.Elements())
pDst--;
*pDst = 0;
result.Assign(buffer.Elements());
return NS_OK;
}
//------------------------------------------------------------------------
// ExtCache implementation
//------------------------------------------------------------------------
ExtCache::ExtCache() : mCount(0), mSize(0), mExtInfo(0)
{
PTIB ptib;
PPIB ppib;
// the PID is needed to lock/unlock icons
DosGetInfoBlocks(&ptib, &ppib);
mPid = ppib->pib_ulpid;
uint32_t rc = DosCreateMutexSem(0, (PHMTX)&mMutex, 0, 0);
if (rc)
ERRMSG(rc, "DosCreateMutexSem")
}
ExtCache::~ExtCache() {}
//------------------------------------------------------------------------
// retrieve the WPS's default icon for files with this extension
nsresult ExtCache::GetIcon(const char *aExt, bool aNeedMini,
uint32_t *oIcon)
{
uint32_t rc = DosRequestMutexSem(mMutex, kMutexTimeout);
if (rc) {
ERRMSG(rc, "DosRequestMutexSem")
return NS_ERROR_FAILURE;
}
ExtInfo *info = FindExtension(aExt);
if (info) {
if (aNeedMini)
*oIcon = info->mini;
else
*oIcon = info->icon;
}
else
*oIcon = 0;
rc = DosReleaseMutexSem(mMutex);
if (rc)
ERRMSG(rc, "DosReleaseMutexSem")
return (*oIcon ? NS_OK : NS_ERROR_FAILURE);
}
//------------------------------------------------------------------------
// save the WPS's default icon for files with this extension
nsresult ExtCache::SetIcon(const char *aExt, bool aIsMini,
uint32_t aIcon)
{
uint32_t rc = DosRequestMutexSem(mMutex, kMutexTimeout);
if (rc) {
ERRMSG(rc, "DosRequestMutexSem")
return NS_ERROR_FAILURE;
}
ExtInfo *info = FindExtension(aExt, true);
if (!info)
return NS_ERROR_FAILURE;
// the icon has to be made non-deletable or else
// it will be destroyed if the WPS terminates
if (!WinSetPointerOwner(aIcon, mPid, FALSE)) {
ERRPRINTF(info->ext, "WinSetPointerOwner failed for %s icon")
return NS_ERROR_FAILURE;
}
if (aIsMini)
info->mini = aIcon;
else
info->icon = aIcon;
ERRPRINTF(info->ext, "ExtCache - added icon for %s")
rc = DosReleaseMutexSem(mMutex);
if (rc)
ERRMSG(rc, "DosReleaseMutexSem")
return NS_OK;
}
//------------------------------------------------------------------------
// retrieve the WPS default handler's title & object handle (if any)
nsresult ExtCache::GetHandler(const char *aExt, uint32_t *oHandle,
nsAString& oTitle)
{
uint32_t rc = DosRequestMutexSem(mMutex, kMutexTimeout);
if (rc) {
ERRMSG(rc, "DosRequestMutexSem")
return NS_ERROR_FAILURE;
}
nsresult rv = NS_ERROR_FAILURE;
ExtInfo *info = FindExtension(aExt);
// if there's no title, the handle isn't useful
if (info && info->title) {
oTitle.Assign(info->title);
*oHandle = info->handler;
rv = NS_OK;
}
rc = DosReleaseMutexSem(mMutex);
if (rc)
ERRMSG(rc, "DosReleaseMutexSem")
return rv;
}
//------------------------------------------------------------------------
// save the WPS default handler's title & object handle (if any)
nsresult ExtCache::SetHandler(const char *aExt, uint32_t aHandle,
nsAString& aTitle)
{
uint32_t rc = DosRequestMutexSem(mMutex, kMutexTimeout);
if (rc) {
ERRMSG(rc, "DosRequestMutexSem")
return NS_ERROR_FAILURE;
}
nsresult rv = NS_ERROR_FAILURE;
ExtInfo *info = FindExtension(aExt, true);
// if the title can't be saved, don't save the handle
if (info) {
info->title = ToNewUnicode(aTitle);
if (info->title) {
info->handler = aHandle;
rv = NS_OK;
ERRPRINTF(info->ext, "ExtCache - added handler for %s")
}
}
rc = DosReleaseMutexSem(mMutex);
if (rc)
ERRMSG(rc, "DosReleaseMutexSem")
return rv;
}
//------------------------------------------------------------------------
// find the entry for the requested extension; if not found,
// create a new entry, expanding the array as needed
ExtInfo *ExtCache::FindExtension(const char *aExt, bool aSet)
{
// eliminate any leading dot & null extensions
if (*aExt == '.')
aExt++;
if (*aExt == 0)
return 0;
// too long to cache
if (strlen(aExt) >= 8)
return 0;
// uppercase extension, then confirm it still fits
char extUpper[16];
strcpy(extUpper, aExt);
// XXX WinUpper() can crash with high-memory
// could we just store as-is and instead use stricmp() for
// compare operations?
if (WinUpper(0, 0, 0, extUpper) >= 8)
return 0;
// a minor optimization: if we're setting a value, it probably
// belongs to the entry added most recently (i.e. the last one)
if (aSet && mCount && !strcmp(extUpper, (&mExtInfo[mCount-1])->ext))
return &mExtInfo[mCount-1];
ExtInfo *info;
uint32_t ctr;
// look for the extension in the array, return if found
for (ctr = 0, info = mExtInfo; ctr < mCount; ctr++, info++)
if (!strcmp(extUpper, info->ext))
return info;
// if a new entry won't fit into the current array, realloc
if (mCount >= mSize) {
uint32_t newSize = mSize + kGrowBy;
info = (ExtInfo*) NS_Realloc(mExtInfo, newSize * sizeof(ExtInfo));
if (!info)
return 0;
memset(&info[mSize], 0, kGrowBy * sizeof(ExtInfo));
mExtInfo = info;
mSize = newSize;
}
// point at the next entry & store the extension
info = &mExtInfo[mCount++];
strcpy(info->ext, extUpper);
return info;
}
//------------------------------------------------------------------------
// clear out the cache - since this is only called at shutdown,
// the primary concern is that the icons get unlocked & deleted
void ExtCache::EmptyCache()
{
if (!mExtInfo)
return;
uint32_t rc = DosRequestMutexSem(mMutex, kMutexTimeout);
if (rc) {
ERRMSG(rc, "DosRequestMutexSem")
return;
}
uint32_t saveMutex = mMutex;
mMutex = 0;
uint32_t ctr;
ExtInfo *info;
for (ctr = 0, info = mExtInfo; ctr < mCount; ctr++, info++) {
ERRPRINTF(info->ext, "ExtCache - deleting entry for %s")
if (info->icon) {
if (WinSetPointerOwner(info->icon, mPid, TRUE) == FALSE ||
WinDestroyPointer(info->icon) == FALSE)
ERRPRINTF(info->ext, "unable to destroy icon for %s")
}
if (info->mini) {
if (WinSetPointerOwner(info->mini, mPid, TRUE) == FALSE ||
WinDestroyPointer(info->mini) == FALSE)
ERRPRINTF(info->ext, "unable to destroy mini for %s")
}
if (info->title)
NS_Free(info->title);
}
mCount = 0;
mSize = 0;
NS_Free(mExtInfo);
mExtInfo = 0;
rc = DosReleaseMutexSem(saveMutex);
if (rc)
ERRMSG(rc, "DosReleaseMutexSem")
rc = DosCloseMutexSem(saveMutex);
if (rc)
ERRMSG(rc, "DosCloseMutexSem")
}
//------------------------------------------------------------------------
// Module & Factory stuff
//------------------------------------------------------------------------
// this is the "getter proc" for nsRwsServiceConstructor(); it makes a
// single attempt to load the RWS libraries and, if successful, creates
// our singleton object; thereafter, it returns the existing object or
// NS_ERROR_NOT_AVAILABLE
static nsresult nsRwsServiceInit(nsRwsService **aClass)
{
// init already done - return what we've got or an error
if (sInit) {
*aClass = sRwsInstance;
if (*aClass == 0)
return NS_ERROR_NOT_AVAILABLE;
NS_ADDREF(*aClass);
return NS_OK;
}
sInit = TRUE;
*aClass = 0;
// don't load RWS if "MOZ_NO_RWS" is found in the environment
if (PR_GetEnv("MOZ_NO_RWS"))
return NS_ERROR_NOT_AVAILABLE;
char errBuf[16];
HMODULE hmod;
// try to load RwsCliXX.dll; first, see if the RWS WPS class is
// registered by f/q name - if so, look for RwsCli in the same
// directory; the goal is to consistently use the same pair of
// dlls if the user has multiple copies
uint32_t rc = 1;
// get the list of registered WPS classes
ULONG cbClass;
if (!WinEnumObjectClasses(NULL, &cbClass))
return NS_ERROR_NOT_AVAILABLE;
char *pBuf = (char*)NS_Alloc(cbClass + CCHMAXPATH);
if (!pBuf)
return NS_ERROR_OUT_OF_MEMORY;
POBJCLASS pClass = (POBJCLASS)&pBuf[CCHMAXPATH];
if (!WinEnumObjectClasses(pClass, &cbClass)) {
NS_Free(pBuf);
return NS_ERROR_NOT_AVAILABLE;
}
// look for RWSxx
while (pClass) {
if (!strcmp(pClass->pszClassName, RWSCLASSNAME))
break;
pClass = pClass->pNext;
}
// if the class was found & it was registered with a f/q name,
// try to load RwsCliXX from the same directory
if (pClass && pClass->pszModName[1] == ':') {
strcpy(pBuf, pClass->pszModName);
char *ptr = strrchr(pBuf, '\\');
if (ptr) {
strcpy(ptr+1, RWSCLIDLL);
rc = DosLoadModule(errBuf, sizeof(errBuf), pBuf, &hmod);
}
}
NS_Free(pBuf);
// if RwsCli couldn't be found, look for it on the LIBPATH
if (rc)
rc = DosLoadModule(errBuf, sizeof(errBuf), RWSCLIMOD, &hmod);
// the dll couldn't be found, so exit
if (rc) {
ERRPRINTF(RWSCLIDLL, "nsRwsServiceInit - unable to locate %s");
return NS_ERROR_NOT_AVAILABLE;
}
// get the addresses of the procs we'll be using;
if (DosQueryProcAddr(hmod, ORD_RWSCALL, 0, (PFN*)&sRwsCall) ||
DosQueryProcAddr(hmod, ORD_RWSCALLINDIRECT,0, (PFN*)&sRwsCallIndirect) ||
DosQueryProcAddr(hmod, ORD_RWSFREEMEM, 0, (PFN*)&sRwsFreeMem) ||
DosQueryProcAddr(hmod, ORD_RWSGETRESULT, 0, (PFN*)&sRwsGetResult) ||
DosQueryProcAddr(hmod, ORD_RWSGETARGPTR, 0, (PFN*)&sRwsGetArgPtr) ||
DosQueryProcAddr(hmod, ORD_RWSCLIENTINIT, 0, (PFN*)&sRwsClientInit) ||
DosQueryProcAddr(hmod, ORD_RWSGETTIMEOUT, 0, (PFN*)&sRwsGetTimeout) ||
DosQueryProcAddr(hmod, ORD_RWSSETTIMEOUT, 0, (PFN*)&sRwsSetTimeout) ||
DosQueryProcAddr(hmod, ORD_RWSCLIENTTERMINATE, 0, (PFN*)&sRwsClientTerminate)) {
DosFreeModule(hmod);
ERRPRINTF("", "nsRwsServiceInit - DosQueryProcAddr failed%s")
return NS_ERROR_NOT_AVAILABLE;
}
// init RWS and have it register the WPS class if needed
rc = sRwsClientInit(TRUE);
if (rc) {
ERRMSG(rc, "RwsClientInit")
return NS_ERROR_NOT_AVAILABLE;
}
// if the user hasn't set a timeout, reset it to 2 seconds
// (the default is 20 seconds)
uint32_t currentTO;
uint32_t userTO;
rc = sRwsGetTimeout((PULONG)¤tTO, (PULONG)&userTO);
if (rc)
ERRMSG(rc, "RwsGetTimeout")
else
if (userTO == 0) {
rc = sRwsSetTimeout(2);
if (rc)
ERRMSG(rc, "RwsSetTimeout")
}
// create an instance of nsRwsService
sRwsInstance = new nsRwsService();
if (sRwsInstance == 0)
return NS_ERROR_OUT_OF_MEMORY;
*aClass = sRwsInstance;
NS_ADDREF(*aClass);
// set the class up as a shutdown observer
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os)
os->AddObserver(*aClass, "quit-application", false);
return NS_OK;
}
//------------------------------------------------------------------------
// this is a variation on NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR();
// the only difference is that the _GetterProc returns an nsresult to
// provide a more appropriate error code (typically NS_ERROR_NOT_AVAILABLE)
NS_IMETHODIMP nsRwsServiceConstructor(nsISupports *aOuter, REFNSIID aIID,
void **aResult)
{
nsresult rv;
nsRwsService *inst;
*aResult = NULL;
if (aOuter) {
rv = NS_ERROR_NO_AGGREGATION;
return rv;
}
rv = nsRwsServiceInit(&inst);
if (NS_FAILED(rv)) {
ERRPRINTF(rv, "==>> nsRwsServiceInit failed - rv= %x");
return rv;
}
rv = inst->QueryInterface(aIID, aResult);
NS_RELEASE(inst);
return rv;
}
//------------------------------------------------------------------------
|