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 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG /* Allow logging in the release build */
#endif
#include "nsIGenericFactory.h"
#include "nsRegistry.h"
#include "nsIEnumerator.h"
#include "nsDirectoryService.h"
#include "nsDirectoryServiceDefs.h"
#include "nsAppDirectoryServiceDefs.h"
#include "NSReg.h"
#include "prmem.h"
#include "prlock.h"
#include "prlog.h"
#include "prprf.h"
#include "nsCRT.h"
#include "nsMemory.h"
#include "nsCOMPtr.h"
#include "nsILocalFile.h"
#include "nsIServiceManager.h"
#include "nsTextFormatter.h"
#ifdef XP_BEOS
#include <FindDirectory.h>
#include <Path.h>
#endif
/* extra locking for the paranoid */
/* #define EXTRA_THREADSAFE */
#ifndef EXTRA_THREADSAFE
#define PR_Lock(x) (void)0
#define PR_Unlock(x) (void)0
#endif
// Logging of debug output
extern NS_COM PRLogModuleInfo *nsComponentManagerLog;
PRUnichar widestrFormat[] = { PRUnichar('%'),PRUnichar('s'),PRUnichar(0)};
/*-------------------------------- nsRegistry ----------------------------------
| This class implements the nsIRegistry interface using the functions |
| provided by libreg (as declared in mozilla/modules/libreg/include/NSReg.h). |
| |
| Since that interface is designed to match the libreg function, this class |
| is implemented with each member function being a simple wrapper for the |
| corresponding libreg function. |
| |
| #define EXTRA_THREADSAFE if you are worried about libreg thread safety. |
| It should not be necessary, but I'll leave in the code for the paranoid. |
------------------------------------------------------------------------------*/
#define NS_MOZILLA_DIR_PERMISSION 00700
#include "nsRegistry.h"
/*
struct nsRegistry : public nsIRegistry {
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
// This class implements the nsIRegistry interface functions.
NS_DECL_NSIREGISTRY
// ctor/dtor
nsRegistry();
private:
~nsRegistry();
protected:
HREG mReg; // Registry handle.
#ifdef EXTRA_THREADSAFE
PRLock *mregLock; // libreg isn't threadsafe. Use locks to synchronize.
#endif
char *mCurRegFile; // these are to prevent open from opening the registry again
nsWellKnownRegistry mCurRegID;
NS_IMETHOD Close();
}; // nsRegistry
*/
#include "nsIFactory.h"
/*----------------------------- nsRegistryFactory ------------------------------
| Class factory for nsRegistry objects. |
------------------------------------------------------------------------------*/
struct nsRegistryFactory : public nsIFactory {
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
// nsIFactory methods
NS_IMETHOD CreateInstance(nsISupports *,const nsIID &,void **);
NS_IMETHOD LockFactory(PRBool aLock);
// ctor
nsRegistryFactory();
};
/*--------------------------- nsRegSubtreeEnumerator ---------------------------
| This class implements the nsIEnumerator interface and is used to implement |
| the nsRegistry EnumerateSubtrees and EnumerateAllSubtrees functions. |
------------------------------------------------------------------------------*/
struct nsRegSubtreeEnumerator : public nsIRegistryEnumerator {
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
// This class implements the nsIEnumerator interface functions.
NS_DECL_NSIENUMERATOR
// And our magic behind-the-back fast-path thing.
NS_DECL_NSIREGISTRYENUMERATOR
// ctor/dtor
nsRegSubtreeEnumerator( HREG hReg, RKEY rKey, PRBool all );
// virtual dtor since subclasses call our Release()
virtual ~nsRegSubtreeEnumerator();
protected:
NS_IMETHOD advance(); // Implementation file; does appropriate NR_RegEnum call.
HREG mReg; // Handle to registry we're affiliated with.
RKEY mKey; // Base key being enumerated.
char mName[MAXREGPATHLEN]; // The name of the current key which is in mNext
REGENUM mEnum; // Corresponding libreg "enumerator".
REGENUM mNext; // Lookahead value.
PRUint32 mStyle; // Style (indicates all or some);
PRBool mDone; // Done flag.
#ifdef EXTRA_THREADSAFE
PRLock *mregLock;
#endif
}; // nsRegSubtreeEnumerator
/*--------------------------- nsRegValueEnumerator -----------------------------
| This class is a variation on nsRegSubtreeEnumerator that allocates |
| nsRegistryValue objects rather than nsRegistryNode objects. It also |
| overrides certain functions to make sure the "value" oriented libreg |
| functions used rather than the subtree oriented ones. |
------------------------------------------------------------------------------*/
struct nsRegValueEnumerator : public nsRegSubtreeEnumerator {
// Override CurrentItem to allocate nsRegistryValue objects.
NS_IMETHOD CurrentItem( nsISupports **result );
// Override advance() to use proper NR_RegEnumEntries.
NS_IMETHOD advance();
// ctor/dtor
nsRegValueEnumerator( HREG hReg, RKEY rKey );
}; // nsRegValueEnumerator
/*------------------------------ nsRegistryNode --------------------------------
| This class implements the nsIRegistryNode interface. Instances are |
| allocated by nsRegSubtreeEnumerator::CurrentItem. |
------------------------------------------------------------------------------*/
struct nsRegistryNode : public nsIRegistryNode {
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
// This class implements the nsIRegistryNode interface functions.
NS_DECL_NSIREGISTRYNODE
// ctor
nsRegistryNode( HREG hReg, char *name, RKEY childKey );
private:
~nsRegistryNode();
protected:
HREG mReg; // Handle to registry this node is part of.
char mName[MAXREGPATHLEN]; // Buffer to hold name.
RKEY mChildKey; // Key corresponding to mName
#ifdef EXTRA_THREADSAFE
PRLock *mregLock;
#endif
}; // nsRegistryNode
/*------------------------------ nsRegistryValue -------------------------------
| This class implements the nsIRegistryValue interface. Instances are |
| allocated by nsRegValueEnumerator::CurrentItem. |
------------------------------------------------------------------------------*/
struct nsRegistryValue : public nsIRegistryValue {
// This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS
// This class implements the nsIRegistryValue interface functions.
NS_DECL_NSIREGISTRYVALUE
// ctor
nsRegistryValue( HREG hReg, RKEY key, REGENUM slot );
private:
~nsRegistryValue();
protected:
nsresult getInfo(); // Get registry info.
HREG mReg; // Handle to registry this node is part of.
RKEY mKey; // Key this node is under.
REGENUM mEnum; // Copy of corresponding content of parent enumerator.
REGINFO mInfo; // Value info.
char mName[MAXREGNAMELEN]; // Buffer to hold name.
REGERR mErr; // XXX This causes this class to be NON THREAD SAFE
#ifdef EXTRA_THREADSAFE
PRLock *mregLock;
#endif
}; // nsRegistryValue
/*----------------------------- regerr2nsresult --------------------------------
| This utility function maps a REGERR value to a corresponding nsresult |
| error code. |
------------------------------------------------------------------------------*/
static nsresult regerr2nsresult( REGERR err ) {
nsresult rv = NS_ERROR_UNEXPECTED;
switch( err ) {
case REGERR_OK:
rv = NS_OK;
break;
case REGERR_FAIL:
rv = NS_ERROR_FAILURE;
break;
case REGERR_NOMORE:
rv = NS_ERROR_REG_NO_MORE;
break;
case REGERR_NOFIND:
rv = NS_ERROR_REG_NOT_FOUND;
break;
case REGERR_PARAM:
case REGERR_BADTYPE:
case REGERR_BADNAME:
rv = NS_ERROR_INVALID_ARG;
break;
case REGERR_NOFILE:
rv = NS_ERROR_REG_NOFILE;
break;
case REGERR_MEMORY:
rv = NS_ERROR_OUT_OF_MEMORY;
break;
case REGERR_BUFTOOSMALL:
rv = NS_ERROR_REG_BUFFER_TOO_SMALL;
break;
case REGERR_NAMETOOLONG:
rv = NS_ERROR_REG_NAME_TOO_LONG;
break;
case REGERR_NOPATH:
rv = NS_ERROR_REG_NO_PATH;
break;
case REGERR_READONLY:
rv = NS_ERROR_REG_READ_ONLY;
break;
case REGERR_BADUTF8:
rv = NS_ERROR_REG_BAD_UTF8;
break;
}
return rv;
}
/*----------------------------- reginfo2DataType -------------------------------
| This utility function converts the type field in the REGINFO structure to |
| the corresponding nsIRegistry::DataType value. |
------------------------------------------------------------------------------*/
static void reginfo2DataType( const REGINFO &in, PRUint32 &out ) {
// Transfer information, based on entry type.
switch( in.entryType ) {
case REGTYPE_ENTRY_STRING_UTF:
out = nsIRegistry::String;
//out.length = in.entryLength;
break;
case REGTYPE_ENTRY_INT32_ARRAY:
out = nsIRegistry::Int32;
// Convert length in bytes to array dimension.
//out.length = in.entryLength / sizeof(PRInt32);
break;
case REGTYPE_ENTRY_BYTES:
out = nsIRegistry::Bytes;
//out.length = in.entryLength;
break;
case REGTYPE_ENTRY_FILE:
out = nsIRegistry::File;
//out.length = in.entryLength;
break;
}
}
/*----------------------------- reginfo2DataType -------------------------------
| This utility function converts the length field in the REGINFO structure to |
| the proper units (if type==Int32 array, we divide by sizeof(PRInt32)). |
------------------------------------------------------------------------------*/
static void reginfo2Length( const REGINFO &in, PRUint32 &out ) {
// Transfer information, based on entry type.
switch( in.entryType ) {
case REGTYPE_ENTRY_STRING_UTF:
out = in.entryLength;
break;
case REGTYPE_ENTRY_INT32_ARRAY:
// Convert length in bytes to array dimension.
out = in.entryLength / sizeof(PRInt32);
break;
case REGTYPE_ENTRY_BYTES:
out = in.entryLength;
break;
case REGTYPE_ENTRY_FILE:
out = in.entryLength;
break;
}
}
/*------------------------ nsISupports Implementation --------------------------
| This code generates the implementation of the nsISupports member functions |
| for each class implemented in this file. |
------------------------------------------------------------------------------*/
NS_IMPL_THREADSAFE_ISUPPORTS2(nsRegistry, nsIRegistry, nsIRegistryGetter)
NS_IMPL_ISUPPORTS2( nsRegSubtreeEnumerator, nsIEnumerator,
nsIRegistryEnumerator)
NS_IMPL_ISUPPORTS1( nsRegistryNode, nsIRegistryNode )
NS_IMPL_ISUPPORTS1( nsRegistryValue, nsIRegistryValue )
/*-------------------------- nsRegistry::nsRegistry ----------------------------
| Vanilla nsRegistry constructor. |
------------------------------------------------------------------------------*/
nsRegistry::nsRegistry()
: mReg(0), mCurRegID(0) {
#ifdef EXTRA_THREADSAFE
mregLock = PR_NewLock();
#endif
NR_StartupRegistry();
return;
}
/*------------------------- nsRegistry::~nsRegistry ----------------------------
| The dtor closes the registry file(if open). |
------------------------------------------------------------------------------*/
nsRegistry::~nsRegistry() {
if( mReg ) {
Close();
}
#ifdef EXTRA_THREADSAFE
if (mregLock) {
PR_DestroyLock(mregLock);
}
#endif
NR_ShutdownRegistry();
return;
}
/*----------------------------- nsRegistry::Open -------------------------------
| If the argument is null, delegate to OpenDefault, else open the registry |
| file. We first check to see if a registry file is already open and close |
| it if so. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::Open( nsIFile *regFile ) {
REGERR err = REGERR_OK;
// Check for default.
if( !regFile ) {
return OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
}
nsCAutoString regPath;
nsresult rv = regFile->GetNativePath(regPath);
if (NS_FAILED(rv)) return rv;
#ifdef DEBUG_dp
printf("nsRegistry: Opening registry %s\n", regPath.get());
#endif /* DEBUG_dp */
if (mCurRegID != nsIRegistry::None && mCurRegID != nsIRegistry::ApplicationCustomRegistry)
{
// Cant open another registry without closing explictly.
return NS_ERROR_INVALID_ARG;
}
// Do we have an open registry ?
if (mCurRegID != nsIRegistry::None)
{
PRBool equals;
if (mCurRegFile && NS_SUCCEEDED(mCurRegFile->Equals(regFile, &equals)) && equals)
{
// The right one is already open
return NS_OK;
}
else
{
// Opening a new registry without closing an already open one.
// This is an error.
return NS_ERROR_FAILURE;
}
}
// Open specified registry.
PR_Lock(mregLock);
err = NR_RegOpen(NS_CONST_CAST(char*,regPath.get()), &mReg);
PR_Unlock(mregLock);
mCurRegID = nsIRegistry::ApplicationCustomRegistry;
// No error checking for no mem. Trust me.
if (NS_FAILED(regFile->Clone(getter_AddRefs(mCurRegFile))))
mCurRegFile = nsnull; // not fatal
// Convert the result.
return regerr2nsresult( err );
}
static void
EnsureDefaultRegistryDirectory() {
#if defined(XP_UNIX) && !defined(XP_MACOSX)
// Create ~/.mozilla as that is the default place for the registry file
/* The default registry on the unix system is $HOME/.mozilla/registry per
* vr_findGlobalRegName(). vr_findRegFile() will create the registry file
* if it doesn't exist. But it wont create directories.
*
* Hence we need to create the directory if it doesn't exist already.
*
* Why create it here as opposed to the app ?
* ------------------------------------------
* The app cannot create the directory in main() as most of the registry
* and initialization happens due to use of static variables.
* And we dont want to be dependent on the order in which
* these static stuff happen.
*
* Permission for the $HOME/.mozilla will be Read,Write,Execute
* for user only. Nothing to group and others.
*/
char *home = getenv("HOME");
if (home != NULL)
{
char dotMozillaDir[1024];
PR_snprintf(dotMozillaDir, sizeof(dotMozillaDir),
"%s/" MOZ_USER_DIR, home);
if (PR_Access(dotMozillaDir, PR_ACCESS_EXISTS) != PR_SUCCESS)
{
PR_MkDir(dotMozillaDir, NS_MOZILLA_DIR_PERMISSION);
PR_LOG(nsComponentManagerLog, PR_LOG_ALWAYS,
("nsComponentManager: Creating Directory %s", dotMozillaDir));
}
}
#endif /* XP_UNIX */
#ifdef XP_BEOS
BPath p;
const char *settings = "/boot/home/config/settings";
if(find_directory(B_USER_SETTINGS_DIRECTORY, &p) == B_OK)
settings = p.Path();
char settingsMozillaDir[1024];
PR_snprintf(settingsMozillaDir, sizeof(settingsMozillaDir),
"%s/" MOZ_USER_DIR, settings);
if (PR_Access(settingsMozillaDir, PR_ACCESS_EXISTS) != PR_SUCCESS) {
PR_MkDir(settingsMozillaDir, NS_MOZILLA_DIR_PERMISSION);
PR_LOG(nsComponentManagerLog, PR_LOG_ALWAYS,
("nsComponentManager: Creating Directory %s", settingsMozillaDir));
}
#endif
}
/*----------------------------- nsRegistry::OpenWellKnownRegistry --------------
| Takes a registry id and maps that to a file name for opening. We first check |
| to see if a registry file is already open and close it if so. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::OpenWellKnownRegistry( nsWellKnownRegistry regid )
{
REGERR err = REGERR_OK;
if (mCurRegID != nsIRegistry::None && mCurRegID != regid)
{
// Cant open another registry without closing explictly.
return NS_ERROR_INVALID_ARG;
}
if (mCurRegID == regid)
{
// Already opened.
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsIFile> registryLocation;
PRBool foundReg = PR_FALSE;
nsCAutoString regFile;
switch ( (nsWellKnownRegistry) regid ) {
case ApplicationComponentRegistry:
NS_WARNING("ApplicationComponentRegistry is unsupported!");
break;
case ApplicationRegistry:
{
EnsureDefaultRegistryDirectory();
nsCOMPtr<nsIProperties> directoryService = do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
directoryService->Get(NS_APP_APPLICATION_REGISTRY_FILE, NS_GET_IID(nsIFile),
getter_AddRefs(registryLocation));
if (registryLocation)
{
foundReg = PR_TRUE;
rv = registryLocation->GetNativePath(regFile); // dougt fix...
// dveditz needs to fix his registry so that I can pass an
// nsIFile interface and not hack
if (NS_FAILED(rv))
return rv;
}
}
break;
default:
break;
}
if (foundReg == PR_FALSE) {
return NS_ERROR_REG_BADTYPE;
}
#ifdef DEBUG_dp
printf("nsRegistry: Opening std registry %s\n", regFile.get());
#endif /* DEBUG_dp */
PR_Lock(mregLock);
err = NR_RegOpen(NS_CONST_CAST(char*, regFile.get()), &mReg );
PR_Unlock(mregLock);
// Store the registry that was opened for optimizing future opens.
mCurRegID = regid;
// Convert the result.
return regerr2nsresult( err );
}
#if 0
/*-------------------------- nsRegistry::OpenDefault ---------------------------
| Open the "default" registry; in the case of this libreg-based implementation |
| that is done by passing a null file name pointer to NR_RegOpen. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::OpenDefault() {
return OpenWellKnownRegistry(nsIRegistry::ApplicationRegistry);
}
#endif
/*----------------------------- nsRegistry::Close ------------------------------
| Tests the mReg handle and if non-null, closes the registry via NR_RegClose. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::Close() {
REGERR err = REGERR_OK;
if( mReg ) {
PR_Lock(mregLock);
err = NR_RegClose( mReg );
PR_Unlock(mregLock);
mReg = 0;
mCurRegFile = nsnull;
mCurRegID = 0;
}
return regerr2nsresult( err );
}
/*----------------------------- nsRegistry::Flush ------------------------------
| Flushes the registry via NR_RegFlush. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::Flush() {
REGERR err = REGERR_FAIL;
if( mReg ) {
PR_Lock(mregLock);
err = NR_RegFlush( mReg );
PR_Unlock(mregLock);
}
return regerr2nsresult( err );
}
/*----------------------------- nsRegistry::IsOpen -----------------------------
| Tests the mReg handle and returns whether the registry is open or not. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::IsOpen( PRBool *result ) {
*result = ( mReg != 0 );
return NS_OK;
}
/*--------------------------- nsRegistry::AddKey -------------------------------
| Add a key into the registry or find an existing one. This is generally used |
| instead of GetKey unless it's an error for the key not to exist already i
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::AddKey( nsRegistryKey baseKey, const PRUnichar *keyname, nsRegistryKey *_retval)
{
if ( !keyname )
return NS_ERROR_NULL_POINTER;
return AddSubtree( baseKey, NS_ConvertUCS2toUTF8(keyname).get(), _retval );
}
/*--------------------------- nsRegistry::GetKey -------------------------------
| returns the nsRegistryKey associated with a given node in the registry |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetKey(nsRegistryKey baseKey, const PRUnichar *keyname, nsRegistryKey *_retval)
{
if ( !keyname || !_retval )
return NS_ERROR_NULL_POINTER;
return GetSubtree( baseKey, NS_ConvertUCS2toUTF8(keyname).get(), _retval );
}
/*--------------------------- nsRegistry::RemoveKey ----------------------------
| Delete a key from the registry |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::RemoveKey(nsRegistryKey baseKey, const PRUnichar *keyname)
{
if ( !keyname )
return NS_ERROR_NULL_POINTER;
return RemoveSubtree( baseKey, NS_ConvertUCS2toUTF8(keyname).get() );
}
NS_IMETHODIMP nsRegistry::GetString(nsRegistryKey baseKey, const PRUnichar *valname, PRUnichar **_retval)
{
// Make sure caller gave us place for result.
if ( !valname || !_retval )
return NS_ERROR_NULL_POINTER;
// initialize the return value
*_retval = nsnull;
nsXPIDLCString tmpstr;
nsresult rv = GetStringUTF8( baseKey, NS_ConvertUCS2toUTF8(valname).get(), getter_Copies(tmpstr) );
if (NS_SUCCEEDED(rv))
{
*_retval = nsTextFormatter::smprintf( widestrFormat, tmpstr.get() );
if ( *_retval == nsnull )
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
NS_IMETHODIMP nsRegistry::SetString(nsRegistryKey baseKey, const PRUnichar *valname, const PRUnichar *value)
{
if ( !valname || ! value )
return NS_ERROR_NULL_POINTER;
return SetStringUTF8( baseKey,
NS_ConvertUCS2toUTF8(valname).get(),
NS_ConvertUCS2toUTF8(value).get() );
}
/*--------------------------- nsRegistry::GetString ----------------------------
| First, look for the entry using GetValueInfo. If found, and it's a string, |
| allocate space for it and fetch the value. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetStringUTF8( nsRegistryKey baseKey, const char *path, char **result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure caller gave us place for result.
if ( !result )
return NS_ERROR_NULL_POINTER;
char regStr[MAXREGPATHLEN];
// initialize the return value
*result = 0;
// Attempt to get string into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, regStr,
sizeof(regStr) );
PR_Unlock(mregLock);
if ( err == REGERR_OK )
{
*result = nsCRT::strdup(regStr);
if (!*result)
rv = NS_ERROR_OUT_OF_MEMORY;
}
else if ( err == REGERR_BUFTOOSMALL )
{
// find the real size and malloc it
PRUint32 length;
rv = GetValueLength( baseKey, path, &length );
// See if that worked.
if( rv == NS_OK )
{
*result =(char*)nsMemory::Alloc( length + 1 );
if( *result )
{
// Get string from registry into result buffer.
PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, *result, length+1 );
PR_Unlock(mregLock);
// Convert status.
rv = regerr2nsresult( err );
if ( rv != NS_OK )
{
// Didn't get result, free buffer
nsCRT::free( *result );
*result = 0;
}
}
else
{
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
}
else
{
// Convert status.
rv = regerr2nsresult( err );
NS_ASSERTION(NS_FAILED(rv), "returning success code on failure");
}
return rv;
}
NS_IMETHODIMP
nsRegistry::GetStringUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
char *buf, PRUint32 *length )
{
REGERR err = REGERR_OK;
// Attempt to get string into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, buf, *length );
PR_Unlock(mregLock);
// Convert status.
nsresult rv = regerr2nsresult( err );
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*--------------------------- nsRegistry::SetString ----------------------------
| Simply sets the registry contents using NR_RegSetEntryString. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::SetStringUTF8( nsRegistryKey baseKey, const char *path, const char *value ) {
REGERR err = REGERR_OK;
// Set the contents.
PR_Lock(mregLock);
err = NR_RegSetEntryString( mReg,(RKEY)baseKey,(char*)path,(char*)value );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*---------------------------- nsRegistry::GetBytesUTF8 ------------------------------
| This function is just shorthand for fetching a char array. We |
| implement it "manually" using NR_RegGetEntry |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetBytesUTF8( nsRegistryKey baseKey, const char *path, PRUint32* length, PRUint8** result) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
if ( !result )
return NS_ERROR_NULL_POINTER;
char regStr[MAXREGPATHLEN];
// initialize the return value
*length = 0;
*result = 0;
// Get info about the requested entry.
PRUint32 type;
rv = GetValueType( baseKey, path, &type );
// See if that worked.
if( rv == NS_OK )
{
// Make sure the entry is an PRInt8 array.
if( type == Bytes )
{
// Attempt to get string into our fixed buffer
PR_Lock(mregLock);
uint32 length2 = sizeof regStr;
err = NR_RegGetEntry( mReg,(RKEY)baseKey,NS_CONST_CAST(char*,path), regStr, &length2);
PR_Unlock(mregLock);
if ( err == REGERR_OK )
{
*length = length2;
*result = (PRUint8*)(nsCRT::strdup(regStr));
if (!*result)
{
rv = NS_ERROR_OUT_OF_MEMORY;
*length = 0;
}
else
{
*length = length2;
}
}
else if ( err == REGERR_BUFTOOSMALL )
{
// find the real size and malloc it
rv = GetValueLength( baseKey, path, length );
// See if that worked.
if( rv == NS_OK )
{
*result = NS_REINTERPRET_CAST(PRUint8*,nsMemory::Alloc( *length ));
if( *result )
{
// Get bytes from registry into result field.
PR_Lock(mregLock);
length2 = *length;
err = NR_RegGetEntry( mReg,(RKEY)baseKey,NS_CONST_CAST(char*,path), *result, &length2);
*length = length2;
PR_Unlock(mregLock);
// Convert status.
rv = regerr2nsresult( err );
if ( rv != NS_OK )
{
// Didn't get result, free buffer
nsCRT::free( NS_REINTERPRET_CAST(char*, *result) );
*result = 0;
*length = 0;
}
}
else
{
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
}
}
else
{
// They asked for the wrong type of value.
rv = NS_ERROR_REG_BADTYPE;
}
}
return rv;
}
NS_IMETHODIMP
nsRegistry::GetBytesUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
PRUint8 *buf, PRUint32* length )
{
REGERR err = REGERR_OK;
// Get info about the requested entry.
PRUint32 type;
nsresult rv = GetValueType( baseKey, path, &type );
// See if that worked.
if(NS_FAILED(rv))
return rv;
// Make sure we are dealing with bytes
if (type != Bytes)
return NS_ERROR_REG_BADTYPE;
// Attempt to get bytes into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntry( mReg,(RKEY)baseKey,NS_CONST_CAST(char*,path),
buf, (uint32 *)length );
PR_Unlock(mregLock);
rv = regerr2nsresult(rv);
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*---------------------------- nsRegistry::GetInt ------------------------------
| This function is just shorthand for fetching a 1-element PRInt32 array. We |
| implement it "manually" using NR_RegGetEntry |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetInt( nsRegistryKey baseKey, const char *path, PRInt32 *result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure caller gave us place for result.
if( result ) {
// Get info about the requested entry.
PRUint32 type;
rv = GetValueType( baseKey, path, &type );
// See if that worked.
if( rv == NS_OK ) {
// Make sure the entry is an PRInt32 array.
if( type == Int32 ) {
uint32 len = sizeof *result;
// Get int from registry into result field.
PR_Lock(mregLock);
err = NR_RegGetEntry( mReg,(RKEY)baseKey,(char*)path, result, &len );
PR_Unlock(mregLock);
// Convert status.
rv = regerr2nsresult( err );
} else {
// They asked for the wrong type of value.
rv = NS_ERROR_REG_BADTYPE;
}
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*---------------------------- nsRegistry::GetLongLong--------------------------
| This function is just shorthand for fetching a 1-element PRInt64 array. We |
| implement it "manually" using NR_RegGetEntry |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetLongLong( nsRegistryKey baseKey, const char *path, PRInt64 *result ) {
REGERR err = REGERR_OK;
PR_Lock(mregLock);
uint32 length = sizeof(PRInt64);
err = NR_RegGetEntry( mReg,(RKEY)baseKey,(char*)path,(void*)result,&length);
PR_Unlock(mregLock);
// Convert status.
return regerr2nsresult( err );
}
/*---------------------------- nsRegistry::SetBytesUTF8 ------------------------------
| Write out the value as a char array, using NR_RegSetEntry. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::SetBytesUTF8( nsRegistryKey baseKey, const char *path, PRUint32 length, PRUint8* value) {
REGERR err = REGERR_OK;
// Set the contents.
PR_Lock(mregLock);
err = NR_RegSetEntry( mReg,
(RKEY)baseKey,
(char*)path,
REGTYPE_ENTRY_BYTES,
(char*)value,
length);
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*---------------------------- nsRegistry::SetInt ------------------------------
| Write out the value as a one-element PRInt32 array, using NR_RegSetEntry. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::SetInt( nsRegistryKey baseKey, const char *path, PRInt32 value ) {
REGERR err = REGERR_OK;
// Set the contents.
PR_Lock(mregLock);
err = NR_RegSetEntry( mReg,
(RKEY)baseKey,
(char*)path,
REGTYPE_ENTRY_INT32_ARRAY,
&value,
sizeof value );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*---------------------------- nsRegistry::SetLongLong---------------------------
| Write out the value as a one-element PRInt64 array, using NR_RegSetEntry. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::SetLongLong( nsRegistryKey baseKey, const char *path, PRInt64* value ) {
REGERR err = REGERR_OK;
// Set the contents.
PR_Lock(mregLock);
err = NR_RegSetEntry( mReg,
(RKEY)baseKey,
(char*)path,
REGTYPE_ENTRY_BYTES,
(void*)value,
sizeof(PRInt64) );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*-------------------------- nsRegistry::AddSubtree ----------------------------
| Add a new registry subkey with the specified name, using NR_RegAddKey. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::AddSubtree( nsRegistryKey baseKey, const char *path, nsRegistryKey *result ) {
REGERR err = REGERR_OK;
// Add the subkey.
PR_Lock(mregLock);
err = NR_RegAddKey( mReg,(RKEY)baseKey,(char*)path,(RKEY*)result );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*-------------------------- nsRegistry::AddSubtreeRaw--------------------------
| Add a new registry subkey with the specified name, using NR_RegAddKeyRaw |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::AddSubtreeRaw( nsRegistryKey baseKey, const char *path, nsRegistryKey *result ) {
REGERR err = REGERR_OK;
// Add the subkey.
PR_Lock(mregLock);
err = NR_RegAddKeyRaw( mReg,(RKEY)baseKey,(char*)path,(RKEY*)result );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*------------------------- nsRegistry::RemoveSubtree --------------------------
| Deletes the subtree at a given location using NR_RegDeleteKey. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::RemoveSubtree( nsRegistryKey baseKey, const char *path ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// libreg doesn't delete keys if there are subkeys under the key
// Hence we have to recurse through to delete the subtree
RKEY key;
PR_Lock(mregLock);
err = NR_RegGetKey(mReg, baseKey, (char *)path, &key);
PR_Unlock(mregLock);
if (err != REGERR_OK)
{
rv = regerr2nsresult( err );
return rv;
}
// Now recurse through and delete all keys under hierarchy
char subkeyname[MAXREGPATHLEN+1];
REGENUM state = 0;
subkeyname[0] = '\0';
while (NR_RegEnumSubkeys(mReg, key, &state, subkeyname, sizeof(subkeyname),
REGENUM_NORMAL) == REGERR_OK)
{
#ifdef DEBUG_dp
printf("...recursing into %s\n", subkeyname);
#endif /* DEBUG_dp */
// Even though this is not a "Raw" API the subkeys may still, in fact,
// *be* raw. Since we're recursively deleting this will work either way.
// If we were guaranteed none would be raw then a depth-first enumeration
// would be much more efficient.
err = RemoveSubtreeRaw(key, subkeyname);
if (err != REGERR_OK) break;
}
// If success in deleting all subkeys, delete this key too
if (err == REGERR_OK)
{
#ifdef DEBUG_dp
printf("...deleting %s\n", path);
#endif /* DEBUG_dp */
PR_Lock(mregLock);
err = NR_RegDeleteKey(mReg, baseKey, (char *)path);
PR_Unlock(mregLock);
}
// Convert result.
rv = regerr2nsresult( err );
return rv;
}
/*------------------------- nsRegistry::RemoveSubtreeRaw -----------------------
| Deletes the subtree at a given location using NR_RegDeleteKeyRaw |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::RemoveSubtreeRaw( nsRegistryKey baseKey, const char *keyname ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// libreg doesn't delete keys if there are subkeys under the key
// Hence we have to recurse through to delete the subtree
RKEY key;
char subkeyname[MAXREGPATHLEN+1];
int n = sizeof(subkeyname);
REGENUM state = 0;
PR_Lock(mregLock);
err = NR_RegGetKeyRaw(mReg, baseKey, (char *)keyname, &key);
PR_Unlock(mregLock);
if (err != REGERR_OK)
{
rv = regerr2nsresult( err );
return rv;
}
// Now recurse through and delete all keys under hierarchy
subkeyname[0] = '\0';
while (NR_RegEnumSubkeys(mReg, key, &state, subkeyname, n, REGENUM_NORMAL) == REGERR_OK)
{
#ifdef DEBUG_dp
printf("...recursing into %s\n", subkeyname);
#endif /* DEBUG_dp */
err = RemoveSubtreeRaw(key, subkeyname);
if (err != REGERR_OK) break;
}
// If success in deleting all subkeys, delete this key too
if (err == REGERR_OK)
{
#ifdef DEBUG_dp
printf("...deleting %s\n", keyname);
#endif /* DEBUG_dp */
PR_Lock(mregLock);
err = NR_RegDeleteKeyRaw(mReg, baseKey, (char *)keyname);
PR_Unlock(mregLock);
}
// Convert result.
rv = regerr2nsresult( err );
return rv;
}
/*-------------------------- nsRegistry::GetSubtree ----------------------------
| Returns a nsRegistryKey(RKEY) for a given key/path. The key is |
| obtained using NR_RegGetKey. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetSubtree( nsRegistryKey baseKey, const char *path, nsRegistryKey *result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure we have a place for the result.
if( result ) {
// Get key.
PR_Lock(mregLock);
err = NR_RegGetKey( mReg,(RKEY)baseKey,(char*)path,(RKEY*)result );
PR_Unlock(mregLock);
// Convert result.
rv = regerr2nsresult( err );
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*-------------------------- nsRegistry::GetSubtreeRaw--------------------------
| Returns a nsRegistryKey(RKEY) for a given key/path. The key is |
| obtained using NR_RegGetKeyRaw. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetSubtreeRaw( nsRegistryKey baseKey, const char *path, nsRegistryKey *result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure we have a place for the result.
if( result ) {
// Get key.
PR_Lock(mregLock);
err = NR_RegGetKeyRaw( mReg,(RKEY)baseKey,(char*)path,(RKEY*)result );
PR_Unlock(mregLock);
// Convert result.
rv = regerr2nsresult( err );
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*----------------------- nsRegistry::EnumerateSubtrees ------------------------
| Allocate a nsRegSubtreeEnumerator object and return it to the caller. |
| We construct the enumerator using the registry handle from this registry |
| object, the user-specified registry key, and indicate that we don't want |
| to recurse down subtrees. No libreg functions are invoked at this point |
|(that will happen when the enumerator member functions are called). |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::EnumerateSubtrees( nsRegistryKey baseKey, nsIEnumerator **result ) {
nsresult rv = NS_OK;
// Make sure we have a place to put the result.
if( result ) {
*result = new nsRegSubtreeEnumerator( mReg,(RKEY)baseKey, PR_FALSE );
// Check for success.
if( *result ) {
// Bump refcnt on behalf of caller.
NS_ADDREF(*result);
} else {
// Unable to allocate space for the enumerator object.
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*--------------------- nsRegistry::EnumerateAllSubtrees -----------------------
| Same as EnumerateSubtrees but we pass PR_TRUE to request that the |
| enumerator object descend subtrees when it is used. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::EnumerateAllSubtrees( nsRegistryKey baseKey, nsIEnumerator **result ) {
nsresult rv = NS_OK;
// Make sure we have a place to put the result.
if( result ) {
*result = new nsRegSubtreeEnumerator( mReg,(RKEY)baseKey, PR_TRUE );
// Check for success.
if( *result ) {
// Bump refcnt on behalf of caller.
NS_ADDREF(*result);
} else {
// Unable to allocate space for the enumerator object.
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*------------------------- nsRegistry::GetValueType ---------------------------
| Gets the type from the registry using the NR_GetEntryInfo libreg API. |
| The result is transferred to the PRUint32 value passed in (with conversion |
| to the appropriate nsIRegistry::DataType value). |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetValueType( nsRegistryKey baseKey, const char *path, PRUint32 *result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure we have a place to put the result.
if( result ) {
// Get registry info into local structure.
REGINFO info = { sizeof info, 0, 0 };
PR_Lock(mregLock);
err = NR_RegGetEntryInfo( mReg,(RKEY)baseKey,(char*)path, &info );
PR_Unlock(mregLock);
if( err == REGERR_OK ) {
// Copy info to user's result value.
reginfo2DataType( info, *result );
} else {
rv = regerr2nsresult( err );
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*------------------------ nsRegistry::GetValueLength --------------------------
| Gets the registry value info via NR_RegGetEntryInfo. The length is |
| converted to the proper "units" via reginfo2Length. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetValueLength( nsRegistryKey baseKey, const char *path, PRUint32 *result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure we have a place to put the result.
if( result ) {
// Get registry info into local structure.
REGINFO info = { sizeof info, 0, 0 };
PR_Lock(mregLock);
err = NR_RegGetEntryInfo( mReg,(RKEY)baseKey,(char*)path, &info );
PR_Unlock(mregLock);
if( err == REGERR_OK ) {
// Copy info to user's result value.
reginfo2Length( info, *result );
} else {
rv = regerr2nsresult( err );
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*-------------------------- nsRegistry::DeleteValue ---------------------------
| Remove the registry value with the specified name |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::DeleteValue( nsRegistryKey baseKey, const char *path)
{
REGERR err = REGERR_OK;
// Delete the value
PR_Lock(mregLock);
err = NR_RegDeleteEntry( mReg,(RKEY)baseKey,(char*)path );
PR_Unlock(mregLock);
// Convert result.
return regerr2nsresult( err );
}
/*------------------------ nsRegistry::EnumerateValues -------------------------
| Allocates and returns an instance of nsRegValueEnumerator constructed in |
| a similar fashion as the nsRegSubtreeEnumerator is allocated/returned by |
| EnumerateSubtrees. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::EnumerateValues( nsRegistryKey baseKey, nsIEnumerator **result ) {
nsresult rv = NS_OK;
// Make sure we have a place to put the result.
if( result ) {
*result = new nsRegValueEnumerator( mReg,(RKEY)baseKey );
// Check for success.
if( *result ) {
// Bump refcnt on behalf of caller.
NS_ADDREF(*result);
} else {
// Unable to allocate space for the enumerator object.
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*---------------------- nsRegistry::GetCurrentUserName ------------------------
| Simple wrapper for NR_RegGetUsername. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::GetCurrentUserName( char **result ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Make sure we have a place to put the result.
if( result ) {
// Get the user name.
PR_Lock(mregLock);
err = NR_RegGetUsername( result );
PR_Unlock(mregLock);
// Convert the result.
rv = regerr2nsresult( err );
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*---------------------- nsRegistry::SetCurrentUserName ------------------------
| Simple wrapper for NR_RegSetUsername. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::SetCurrentUserName( const char *name ) {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Set the user name.
PR_Lock(mregLock);
err = NR_RegSetUsername( name );
PR_Unlock(mregLock);
// Convert result.
rv = regerr2nsresult( err );
return rv;
}
/*----------------------------- nsRegistry::Pack -------------------------------
| Simple wrapper for NR_RegPack. We don't set up any callback. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::Pack() {
nsresult rv = NS_OK;
REGERR err = REGERR_OK;
// Pack the registry.
PR_Lock(mregLock);
err = NR_RegPack( mReg, 0, 0 );
PR_Unlock(mregLock);
// Convert result.
rv = regerr2nsresult( err );
return rv;
}
/*----------------------------- nsRegistry::EscapeKey -------------------------------
| Escape a binary key so that the registry works OK, since it expects UTF8
| with no slashes or control characters. This is probably better than raw.
| If no escaping is required, then the method is successful and a null is
| returned, indicating that the caller should use the original string.
------------------------------------------------------------------------------*/
static const char sEscapeKeyHex[] = "0123456789abcdef0123456789ABCDEF";
NS_IMETHODIMP nsRegistry::EscapeKey(PRUint8* key, PRUint32 termination, PRUint32* length, PRUint8** escaped)
{
nsresult rv = NS_OK;
char* value = (char*)key;
char* b = value;
char* e = b + *length;
int escapees = 0;
while (b < e) // Count characters outside legal range or slash
{
int c = *b++;
if (c <= ' '
|| c > '~'
|| c == '/'
|| c == '%')
{
escapees++;
}
}
if (escapees == 0) // If no escapees, then no results
{
*length = 0;
*escaped = nsnull;
return NS_OK;
}
// New length includes two extra chars for escapees.
*length += escapees * 2;
*escaped = (PRUint8*)nsMemory::Alloc(*length + termination);
if (*escaped == nsnull)
{
*length = 0;
*escaped = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
char* n = (char*)*escaped;
b = value;
while (escapees && b < e)
{
char c = *b++;
if (c < ' '
|| c > '~'
|| c == '/'
|| c == '%')
{
*(n++) = '%';
*(n++) = sEscapeKeyHex[ 0xF & (c >> 4) ];
*(n++) = sEscapeKeyHex[ 0xF & c ];
escapees--;
}
else
{
*(n++) = c;
}
}
e += termination;
if (b < e)
{
strncpy(n, b, e - b);
}
return rv;
}
/*----------------------------- nsRegistry::UnescapeKey -------------------------------
| Unscape a binary key so that the registry works OK, since it expects UTF8
| with no slashes or control characters. This is probably better than raw.
| If no escaping is required, then the method is successful and a null is
| returned, indicating that the caller should use the original string.
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistry::UnescapeKey(PRUint8* escaped, PRUint32 termination, PRUint32* length, PRUint8** key)
{
nsresult rv = NS_OK;
char* value = (char*)escaped;
char* b = value;
char* e = b + *length;
int escapees = 0;
while (b < e) // Count characters outside legal range or slash
{
if (*b++ == '%')
{
escapees++;
}
}
if (escapees == 0) // If no escapees, then no results
{
*length = 0;
*key = nsnull;
return NS_OK;
}
// New length includes two extra chars for escapees.
*length -= escapees * 2;
*key = (PRUint8*)nsMemory::Alloc(*length + termination);
if (*key == nsnull)
{
*length = 0;
*key = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
char* n = (char*)*key;
b = value;
while (escapees && b < e)
{
char c = *(b++);
if (c == '%')
{
if (e - b >= 2)
{
const char* c1 = strchr(sEscapeKeyHex, *(b++));
const char* c2 = strchr(sEscapeKeyHex, *(b++));
if (c1 != nsnull
&& c2 != nsnull)
{
*(n++) = ((c2 - sEscapeKeyHex) & 0xF)
| (((c1 - sEscapeKeyHex) & 0xF) << 4);
}
else
{
escapees = -1;
}
}
else
{
escapees = -1;
}
escapees--;
}
else
{
*(n++) = c;
}
}
if (escapees < 0)
{
nsMemory::Free(*key);
*length = 0;
*key = nsnull;
return NS_ERROR_INVALID_ARG;
}
e += termination;
if (b < e)
{
strncpy(n, b, e - b);
}
return rv;
}
/*-------------- nsRegistry::SetBufferSize-------------------------------------
| Sets the size of the file used for the registry's buffer size. |
------------------------------------------------------------------------------*/
int nsRegistry::SetBufferSize( int bufsize )
{
int newSize;
// set the file buffer size
PR_Lock(mregLock);
newSize = NR_RegSetBufferSize( mReg, bufsize );
PR_Unlock(mregLock);
return newSize;
}
/*-------------- nsRegSubtreeEnumerator::nsRegSubtreeEnumerator ----------------
| The ctor simply stashes all the information that will be needed to enumerate |
| the subkeys. |
------------------------------------------------------------------------------*/
nsRegSubtreeEnumerator::nsRegSubtreeEnumerator( HREG hReg, RKEY rKey, PRBool all )
: mReg( hReg ), mKey( rKey ), mEnum( 0 ), mNext( 0 ),
mStyle( all ? REGENUM_DESCEND : REGENUM_CHILDREN ), mDone( PR_FALSE ) {
mName[0] = '\0';
#ifdef EXTRA_THREADSAFE
// Create a registry lock
mregLock = PR_NewLock();
#endif
return;
}
nsRegSubtreeEnumerator::~nsRegSubtreeEnumerator()
{
#ifdef EXTRA_THREADSAFE
if (mregLock) {
PR_DestroyLock(mregLock);
}
#endif
}
/*----------------------- nsRegSubtreeEnumerator::First ------------------------
| Set mEnum to 0; this will cause the next NR_RegEnum call to go to |
| the beginning. We then do a Next() call in order to do a "lookahead" to |
| properly detect an empty list (i.e., set the mDone flag). |
------------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegSubtreeEnumerator::First() {
nsresult rv = NS_OK;
// Reset "done" flag.
mDone = PR_FALSE;
// Clear Name
mName[0] = '\0';
// Go to beginning.
mEnum = mNext = 0;
// Lookahead so mDone flag gets set for empty list.
rv = Next();
return rv;
}
/*----------------------- nsRegSubtreeEnumerator::Next -------------------------
| First, we check if we've already advanced to the end by checking the mDone |
| flag. |
| |
| We advance mEnum to the next enumeration value which is in the mNext |
| lookahead buffer. We must then call advance to lookahead and properly set |
| the isDone flag. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegSubtreeEnumerator::Next() {
nsresult rv = NS_OK;
// Check for at end.
if ( !mDone ) {
// Advance to next spot.
mEnum = mNext;
// Lookahead so mDone is properly set (and to update mNext).
rv = advance();
} else {
// Set result accordingly.
rv = regerr2nsresult( REGERR_NOMORE );
}
return rv;
}
/*---------------------- nsRegSubtreeEnumerator::advance -----------------------
| Advance mNext to next subkey using NR_RegEnumSubkeys. We set mDone if |
| there are no more subkeys. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegSubtreeEnumerator::advance() {
REGERR err = REGERR_OK;
PR_Lock(mregLock);
err = NR_RegEnumSubkeys( mReg, mKey, &mNext, mName, sizeof mName, mStyle );
// See if we ran off end.
if( err == REGERR_NOMORE ) {
// Remember we've run off end.
mDone = PR_TRUE;
}
PR_Unlock(mregLock);
// Convert result.
nsresult rv = regerr2nsresult( err );
return rv;
}
/*-------------------- nsRegSubtreeEnumerator::CurrentItem ---------------------
| Allocates and returns a new instance of class nsRegistryNode. The node |
| object will hold the curent mEnum value so it can obtain its name from |
| the registry when asked. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegSubtreeEnumerator::CurrentItem( nsISupports **result) {
nsresult rv = NS_OK;
// Make sure there is a place to put the result.
if( result ) {
*result = new nsRegistryNode( mReg, mName, (RKEY) mNext );
if( *result ) {
NS_ADDREF(*result);
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*--------------nsRegSubtreeEnumerator::CurrentItemInPlaceUTF8-----------------
| An ugly name for an ugly function. Hands back a shared pointer to the |
| name (encoded as UTF-8), and the subkey identifier. |
-----------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegSubtreeEnumerator::CurrentItemInPlaceUTF8( nsRegistryKey *childKey ,
const char **name )
{
*childKey = mNext;
/* [shared] */
*name = mName;
return NS_OK;
}
/*---------------------- nsRegSubtreeEnumerator::IsDone ------------------------
| Simply return mDone. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegSubtreeEnumerator::IsDone() {
nsresult rv = mDone ? NS_OK : NS_ENUMERATOR_FALSE;
return rv;
}
/*---------------- nsRegValueEnumerator::nsRegValueEnumerator ------------------
| Delegates everything to the base class constructor. |
------------------------------------------------------------------------------*/
nsRegValueEnumerator::nsRegValueEnumerator( HREG hReg, RKEY rKey )
: nsRegSubtreeEnumerator( hReg, rKey, PR_FALSE ) {
return;
}
/*--------------------- nsRegValueEnumerator::CurrentItem ----------------------
| As the nsRegSubtreeEnumerator counterpart, but allocates an object of |
| class nsRegistryValue. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP
nsRegValueEnumerator::CurrentItem( nsISupports **result ) {
nsresult rv = NS_OK;
// Make sure there is a place to put the result.
if( result ) {
*result = new nsRegistryValue( mReg, mKey, mEnum );
if( *result ) {
NS_ADDREF(*result);
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*----------------------- nsRegValueEnumerator::advance ------------------------
| Advance mNext to next subkey using NR_RegEnumEntries. We set mDone if |
| there are no more entries. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegValueEnumerator::advance() {
REGERR err = REGERR_OK;
char name[MAXREGNAMELEN];
PRUint32 len = sizeof name;
REGINFO info = { sizeof info, 0, 0 };
PR_Lock(mregLock);
err = NR_RegEnumEntries( mReg, mKey, &mNext, name, len, &info );
// See if we ran off end.
if( err == REGERR_NOMORE ) {
// Remember we've run off end.
mDone = PR_TRUE;
}
PR_Unlock(mregLock);
// Convert result.
nsresult rv = regerr2nsresult( err );
return rv;
}
/*---------------------- nsRegistryNode::nsRegistryNode ------------------------
| Store the arguments in the corresponding data members and initialize |
| the other data members. We defer the libreg calls till we're asked for |
| our name. We use mErr==-1 to indicate we haven't fetched the name yet. |
------------------------------------------------------------------------------*/
nsRegistryNode::nsRegistryNode( HREG hReg, char *name, RKEY childKey )
: mReg( hReg ), mChildKey( childKey ) {
PR_ASSERT(name != nsnull);
strcpy(mName, name);
#ifdef EXTRA_THREADSAFE
mregLock = PR_NewLock();
#endif
return;
}
nsRegistryNode::~nsRegistryNode()
{
#ifdef EXTRA_THREADSAFE
if (mregLock) {
PR_DestroyLock(mregLock);
}
#endif
}
/*-------------------------- nsRegistryNode::GetName ---------------------------
| If we haven't fetched it yet, get the name of the corresponding subkey now, |
| using NR_RegEnumSubkeys. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryNode::GetName( PRUnichar **result ) {
if (result == nsnull) return NS_ERROR_NULL_POINTER;
// Make sure there is a place to put the result.
*result = nsTextFormatter::smprintf( widestrFormat, mName );
if ( !*result ) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
/*-------------------------- nsRegistryNode::GetNameUTF8 -----------------------
| If we haven't fetched it yet, get the name of the corresponding subkey now, |
| using NR_RegEnumSubkeys. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryNode::GetNameUTF8( char **result ) {
if (result == nsnull) return NS_ERROR_NULL_POINTER;
// Make sure there is a place to put the result.
*result = nsCRT::strdup( mName );
if ( !*result ) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
/*-------------------------- nsRegistryNode::GetKey ----------------------------
| Get the subkey corresponding to this node |
| using NR_RegEnumSubkeys. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryNode::GetKey( nsRegistryKey *r_key ) {
nsresult rv = NS_OK;
if (r_key == nsnull) return NS_ERROR_NULL_POINTER;
*r_key = mChildKey;
return rv;
}
/*--------------------- nsRegistryValue::nsRegistryValue -----------------------
| Implemented the same way as the nsRegistryNode ctor. |
------------------------------------------------------------------------------*/
nsRegistryValue::nsRegistryValue( HREG hReg, RKEY key, REGENUM slot )
: mReg( hReg ), mKey( key ), mEnum( slot ), mErr( -1 ) {
#ifdef EXTRA_THREADSAFE
mregLock = PR_NewLock();
#endif
mInfo.size = sizeof(REGINFO);
}
nsRegistryValue::~nsRegistryValue()
{
#ifdef EXTRA_THREADSAFE
if (mregLock) {
PR_DestroyLock(mregLock);
}
#endif
}
/*------------------------- nsRegistryValue::GetName ---------------------------
| See nsRegistryNode::GetName; we use NR_RegEnumEntries in this case. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryValue::GetName( PRUnichar **result ) {
nsresult rv = NS_OK;
// Make sure we have a place to put the result.
if( result ) {
// Ensure we've got the info we need.
rv = getInfo();
if( rv == NS_OK || rv == NS_ERROR_REG_NO_MORE ) {
// worked, return actual result.
*result = nsTextFormatter::smprintf( widestrFormat, mName );
if ( *result ) {
rv = NS_OK;
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*------------------------- nsRegistryValue::GetNameUTF8 -----------------------
| See nsRegistryNode::GetName; we use NR_RegEnumEntries in this case. |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryValue::GetNameUTF8( char **result ) {
nsresult rv = NS_OK;
// Make sure we have a place to put the result.
if( result ) {
// Ensure we've got the info we need.
rv = getInfo();
if( rv == NS_OK || rv == NS_ERROR_REG_NO_MORE ) {
// worked, return actual result.
*result = nsCRT::strdup( mName );
if ( *result ) {
rv = NS_OK;
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*----------------------- nsRegistryValue::GetType ------------------------
| We test if we've got the info already. If not, we git it by calling |
| getInfo. We calculate the result by converting the REGINFO type field to |
| a nsIRegistry::DataType value (using reginfo2DataType). |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryValue::GetType( PRUint32 *result ) {
nsresult rv = NS_OK;
// Make sure we have room for th result.
if( result ) {
// Make sure we've got the info we need.
rv = getInfo();
// Check if it worked.
if( rv == NS_OK ) {
// Convert result from REGINFO to nsIRegistry::ValueInfo.
reginfo2DataType( mInfo, *result );
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*---------------------- nsRegistryValue::GetLength -----------------------
| We test if we've got the info already. If not, we git it by calling |
| getInfo. We calculate the result by converting the REGINFO type field to |
| a nsIRegistry::DataType value (using reginfo2Length). |
------------------------------------------------------------------------------*/
NS_IMETHODIMP nsRegistryValue::GetLength( PRUint32 *result ) {
nsresult rv = NS_OK;
// Make sure we have room for th result.
if( result ) {
// Make sure we've got the info we need.
rv = getInfo();
// Check if it worked.
if( rv == NS_OK ) {
// Convert result from REGINFO to length.
reginfo2Length( mInfo, *result );
}
} else {
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
/*------------------------- nsRegistryValue::getInfo ---------------------------
| Call NR_RegEnumEntries to set the mInfo/mName data members. |
------------------------------------------------------------------------------*/
nsresult nsRegistryValue::getInfo() {
nsresult rv = NS_OK;
// Test whether we haven't tried to get it yet.
if( mErr == -1 ) {
REGENUM temp = mEnum;
// Get name and info.
PR_Lock(mregLock);
mErr = NR_RegEnumEntries( mReg, mKey, &temp, mName, sizeof mName, &mInfo );
// Convert result.
rv = regerr2nsresult( mErr );
PR_Unlock(mregLock);
}
return rv;
}
nsRegistryFactory::nsRegistryFactory() {
}
NS_IMPL_ISUPPORTS1(nsRegistryFactory, nsIFactory)
NS_IMETHODIMP
nsRegistryFactory::CreateInstance(nsISupports *aOuter,
const nsIID &aIID,
void **aResult) {
nsresult rv = NS_OK;
nsRegistry* newRegistry;
if(aResult == nsnull) {
return NS_ERROR_NULL_POINTER;
} else {
*aResult = nsnull;
}
if(0 != aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
NS_NEWXPCOM(newRegistry, nsRegistry);
if(newRegistry == nsnull) {
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(newRegistry);
rv = newRegistry->QueryInterface(aIID, aResult);
NS_RELEASE(newRegistry);
return rv;
}
nsresult
nsRegistryFactory::LockFactory(PRBool aLock)
{
// Not implemented in simplest case.
return NS_OK;
}
// This is a temporary hack; needs work to support dynamic binding
// via nsComponentManager and support for multiple factories per DLL.
extern "C" NS_EXPORT nsresult
NS_RegistryGetFactory(nsIFactory** aFactory ) {
nsresult rv = NS_OK;
if( aFactory == 0 ) {
return NS_ERROR_NULL_POINTER;
} else {
*aFactory = 0;
}
nsIFactory* inst = new nsRegistryFactory();
if(0 == inst) {
rv = NS_ERROR_OUT_OF_MEMORY;
} else {
NS_ADDREF(inst);
*aFactory = inst;
}
return rv;
}
|