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
|
/* ***** 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 Java XPCOM Bindings.
*
* The Initial Developer of the Original Code is IBM Corporation.
* Portions created by the Initial Developer are Copyright (C) 2006
* IBM Corporation. All Rights Reserved.
*
* Contributor(s):
* Javier Pedemonte (jhpedemonte@gmail.com)
*
* Alternatively, the contents of this file may be used under the terms of
* either 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 ***** */
package org.mozilla.xpcom;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import org.mozilla.interfaces.nsIComponentManager;
import org.mozilla.interfaces.nsIComponentRegistrar;
import org.mozilla.interfaces.nsILocalFile;
import org.mozilla.interfaces.nsIServiceManager;
import org.mozilla.interfaces.nsISupports;
/**
* A singleton class which provides access to the Mozilla browser. Requires
* that XULRunner be installed on the user's system.
* <p>
* You would use to class to find a XULRunner installation, setup a profile (if
* necessary), and initialize embedding. A typical scenario would look like
* this:
* </p><pre>
* Mozilla mozilla = Mozilla.getInstance();
* GREVersionRange[] range = new GREVersionRange[1];
* range[0] = new GREVersionRange("1.8.0.*", false, "1.8.1.*", true);
* try {
* File grePath = Mozilla.getGREPathWithProperties(range, null);
* mozilla.initialize(grePath);
* profLock = mozilla.lockProfileDirectory(profileDir);
* // LocationProvider is a user class that implements IAppFileLocProvider
* LocationProvider locProvider = new LocationProvider(grePath, profileDir);
* mozilla.initEmbedding(grePath, grePath, locProvider);
* mozilla.notifyProfile();
* } catch (XPCOMInitializationException xie) {
* // handle exception
* } catch (XPCOMException xe) {
* // handle exception
* }
* </pre>
*
* @see http://www.mozilla.org/projects/embedding/GRE.html
*/
public class Mozilla implements IMozilla, IGRE, IXPCOM, IJavaXPCOMUtils,
IXPCOMError {
private static Mozilla mozillaInstance = new Mozilla();
private static final String JAVAXPCOM_JAR = "javaxpcom.jar";
private IMozilla mozilla = null;
private IGRE gre = null;
private IXPCOM xpcom = null;
private IJavaXPCOMUtils jxutils = null;
/**
* @return
*/
public static Mozilla getInstance() {
return mozillaInstance;
}
/**
*
*/
private Mozilla() {
}
/**
* Locates the path of a GRE with the specified properties. This method
* will only return GREs that support Java embedding (looks for the
* presence of "javaxpcom.jar").
* <p>
* Currently this uses a "first-fit" algorithm, it does not select
* the newest available GRE.
*
* @param aVersions An array of version ranges: if any version range
* matches, the GRE is considered acceptable.
* @param aProperties A list of GRE property/value pairs which must
* all be satisfied. This parameter is ignored on
* Macintosh, because of the manner in which the
* XUL frameworks are installed.
*
* @return A file object of the appropriate path. If
* the "local" GRE is specified (via the USE_LOCAL_GRE
* environment variable, for example), returns
* <code>null</code>.
*
* @throws FileNotFoundException if an appropriate GRE could not be found
*/
public static File getGREPathWithProperties(GREVersionRange[] aVersions,
Properties aProperties) throws FileNotFoundException {
File grePath = null;
// if GRE_HOME is in the environment, use that GRE
String env = System.getProperty("GRE_HOME");
if (env != null) {
try {
grePath = new File(env).getCanonicalFile();
} catch (IOException e) {
throw new FileNotFoundException("cannot access GRE_HOME");
}
if (!grePath.exists()) {
throw new FileNotFoundException("GRE_HOME doesn't exist");
}
return grePath;
}
// the Gecko bits that sit next to the application or in the PATH
env = System.getProperty("USE_LOCAL_GRE");
if (env != null) {
return null;
}
// Search for GRE in platform specific locations. We want a GRE that
// supports Java, so we look for the "javaxpcom" property by default.
if (aProperties == null) {
aProperties = new Properties();
}
aProperties.setProperty("javaxpcom", "1");
String osName = System.getProperty("os.name").toLowerCase();
if (osName.startsWith("mac os x")) {
grePath = getGREPathMacOSX(aVersions);
} else if (osName.startsWith("windows")) {
grePath = getGREPathWindows(aVersions, aProperties);
} else {
// assume everything else is Unix/Linux
grePath = getGREPathUnix(aVersions, aProperties);
}
if (grePath == null) {
throw new FileNotFoundException("GRE not found");
}
return grePath;
}
/**
* @param aVersions
* @return
*/
private static File getGREPathMacOSX(GREVersionRange[] aVersions) {
/*
* Check the application bundle first, for
* <bundle>/Contents/Frameworks/XUL.framework/libxpcom.dylib.
*/
File grePath = findGREBundleFramework();
if (grePath != null) {
return grePath;
}
// Check ~/Library/Frameworks/XUL.framework/Versions/<version>/libxpcom.dylib
String home = System.getProperty("user.home");
if (home != null) {
grePath = findGREFramework(home, aVersions);
if (grePath != null) {
return grePath;
}
}
// Check /Library/Frameworks/XUL.framework/Versions/<version>/libxpcom.dylib
return findGREFramework("", aVersions);
}
/**
* @return
*/
private static File findGREBundleFramework() {
/*
* Use reflection to get Apple's NSBundle class, which can be used
* to get the bundle's "Frameworks" directory.
*/
try {
URL[] urls = new URL[1];
urls[0] = new File("/System/Library/Java/").toURL();
ClassLoader loader = new URLClassLoader(urls);
Class bundleClass = Class.forName("com.apple.cocoa.foundation.NSBundle",
true, loader);
// Get the bundle for this app. If this is not executing from
// a bundle, this will return null.
Method mainBundleMethod = bundleClass.getMethod("mainBundle", null);
Object bundle = mainBundleMethod.invoke(null, null);
if (bundle != null) {
// Get the path to the bundle's "Frameworks" directory
Method fwPathMethod = bundleClass.getMethod("privateFrameworksPath",
null);
String path = (String) fwPathMethod.invoke(bundle, null);
// look for libxpcom.dylib
if (path.length() != 0) {
File xulDir = new File(path, "XUL.framework");
if (xulDir.isDirectory()) {
File xpcomLib = new File(xulDir, "libxpcom.dylib");
if (xpcomLib.canRead()) {
File grePath = xpcomLib.getCanonicalFile().getParentFile();
// Since GRE Properties aren't supported on Mac OS X, we check
// for the existence of the "javaxpcom.jar" file in the GRE.
File jar = new File(grePath, JAVAXPCOM_JAR);
if (jar.canRead()) {
// found GRE
return grePath;
}
}
}
}
}
} catch (Exception e) { }
return null;
}
/**
* @param aRootPath
* @param aVersions
* @return
*/
private static File findGREFramework(String aRootPath,
GREVersionRange[] aVersions) {
File frameworkDir = new File(aRootPath +
"/Library/Frameworks/XUL.framework/Versions");
if (!frameworkDir.exists())
return null;
File[] files = frameworkDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (checkVersion(files[i].getName(), aVersions)) {
File xpcomLib = new File(files[i], "libxpcom.dylib");
// Since GRE Properties aren't supported on Mac OS X, we check
// for the existence of the "javaxpcom.jar" file in the GRE.
File jar = new File(files[i], JAVAXPCOM_JAR);
if (xpcomLib.canRead() && jar.canRead()) {
return files[i];
}
}
}
return null;
}
/**
* @param aVersions
* @param aProperties
* @return
*/
private static File getGREPathWindows(GREVersionRange[] aVersions,
Properties aProperties) {
/*
* Note the usage of the "Software\\mozilla.org\\GRE" subkey - this allows
* us to have multiple versions of GREs on the same machine by having
* subkeys such as 1.0, 1.1, 2.0 etc. under it.
*
* Please see http://www.mozilla.org/projects/embedding/GRE.html for
* more info.
*/
final String greKey = "Software\\mozilla.org\\GRE";
// See if there is a GRE registered for the current user.
// If not, look for one on the system.
String key = "HKEY_CURRENT_USER" + "\\" + greKey;
File grePath = getGREPathFromRegKey(key, aVersions, aProperties);
if (grePath == null) {
key = "HKEY_LOCAL_MACHINE" + "\\" + greKey;
grePath = getGREPathFromRegKey(key, aVersions, aProperties);
}
return grePath;
}
/**
* @param aRegKey
* @param aVersions
* @param aProperties
* @return
*/
private static File getGREPathFromRegKey(String aRegKey,
GREVersionRange[] aVersions, Properties aProperties) {
// create a temp file for the registry export
File tempFile;
try {
tempFile = File.createTempFile("jx_registry", null);
} catch (IOException e) {
// failed to create temp file. ABORT
return null;
}
Process proc;
try {
proc = Runtime.getRuntime().exec("regedit /e " + "\"" + tempFile.getPath()
+ "\" \"" + aRegKey + "\"");
proc.waitFor();
} catch (Exception e) {
// Failed to run regedit.exe. Length of temp file is zero, and that's
// handled next.
}
// If there is a key by that name in the registry, then the file length
// will not be zero.
File grePath = null;
if (tempFile.length() != 0) {
grePath = getGREPathFromRegistryFile(tempFile.getPath(),
aRegKey, aVersions, aProperties);
}
tempFile.delete();
return grePath;
}
/**
* @param aFileName
* @param aCharset
* @param aKeyName
* @param aVersions
* @param aProperties
* @return
*/
private static File getGREPathFromRegistryFile(String aFileName,
String aKeyName, GREVersionRange[] aVersions,
Properties aProperties) {
INIParser parser;
try {
parser = new INIParser(aFileName, Charset.forName("UTF-16"));
} catch (Exception e) {
// Problem reading from file. Bail out.
return null;
}
Iterator sectionsIter = parser.getSections();
while (sectionsIter.hasNext()) {
// get 'section' name, which will be a registry key name
String section = (String) sectionsIter.next();
// Skip over GRE key ("<root>\Software\mozilla.org\GRE")
int gre_len = aKeyName.length();
if (section.length() <= gre_len) {
continue;
}
// Get the GRE subkey; that is, everything after
// "<root>\Software\mozilla.org\GRE\"
String subkeyName = section.substring(gre_len + 1);
// We are only interested in _immediate_ subkeys. We want
// "<root>\Software\mozilla.org\GRE\<version>" but not
// "<root>\Software\mozilla.org\GRE\<version>\<moretext>".
if (subkeyName.indexOf('\\') != -1) {
continue;
}
// See if this registry key has a "Version" value, and if so, compare
// it to our desired versions.
String version = parser.getString(section, "\"Version\"");
if (version == null) {
continue;
}
// remove quotes around string
version = version.substring(1, version.length() - 1);
if (!checkVersion(version, aVersions)) {
continue;
}
// All properties must match, keeping in mind that the propery/value
// pairs returned by regedit.exe have quotes around them.
if (aProperties != null) {
boolean ok = true;
Enumeration e = aProperties.propertyNames();
while (ok && e.hasMoreElements()) {
String prop = (String) e.nextElement();
String greValue = parser.getString(section, "\"" + prop + "\"");
if (greValue == null) {
// No such property is set for this GRE. Go on to next GRE.
ok = false;
} else {
// See if the value of the property for the GRE matches
// the given value.
String value = aProperties.getProperty(prop);
if (!greValue.equals("\"" + value + "\"")) {
ok = false;
}
}
}
if (!ok) {
continue;
}
}
String pathStr = parser.getString(section, "\"GreHome\"");
if (pathStr != null) {
// remove quotes around string
pathStr = pathStr.substring(1, pathStr.length() - 1);
File grePath = new File(pathStr);
if (grePath.exists()) {
File xpcomLib = new File(grePath, "xpcom.dll");
if (xpcomLib.canRead()) {
// found a good GRE
return grePath;
}
}
}
}
return null;
}
/**
* @param aVersions
* @param aProperties
* @return
*/
private static File getGREPathUnix(GREVersionRange[] aVersions,
Properties aProperties) {
File grePath = null;
String env = System.getProperty("MOZ_GRE_CONF");
if (env != null) {
grePath = getPathFromConfigFile(env, aVersions, aProperties);
if (grePath != null) {
return grePath;
}
}
final String greUserConfFile = ".gre.config";
final String greUserConfDir = ".gre.d";
final String greConfPath = "/etc/gre.conf";
final String greConfDir = "/etc/gre.d";
env = System.getProperty("user.home");
if (env != null) {
// Look in ~/.gre.config
grePath = getPathFromConfigFile(env + File.separator + greUserConfFile,
aVersions, aProperties);
if (grePath != null) {
return grePath;
}
// Look in ~/.gre.d/*.conf
grePath = getPathFromConfigDir(env + File.separator + greUserConfDir,
aVersions, aProperties);
if (grePath != null) {
return grePath;
}
}
// Look for a global /etc/gre.conf file
grePath = getPathFromConfigFile(greConfPath, aVersions, aProperties);
if (grePath != null) {
return grePath;
}
// Look for a group of config files in /etc/gre.d/
grePath = getPathFromConfigDir(greConfDir, aVersions, aProperties);
return grePath;
}
/**
* @param aFileName
* @param aVersions
* @param aProperties
* @return
*/
private static File getPathFromConfigFile(String aFileName,
GREVersionRange[] aVersions, Properties aProperties) {
INIParser parser;
try {
parser = new INIParser(aFileName);
} catch (Exception e) {
// Problem reading from file. Bail out.
return null;
}
Iterator sectionsIter = parser.getSections();
while (sectionsIter.hasNext()) {
// get 'section' name, which will be a version string
String section = (String) sectionsIter.next();
// if this isn't one of the versions we are looking for, move
// on to next section
if (!checkVersion(section, aVersions)) {
continue;
}
// all properties must match
if (aProperties != null) {
boolean ok = true;
Enumeration e = aProperties.propertyNames();
while (ok && e.hasMoreElements()) {
String prop = (String) e.nextElement();
String greValue = parser.getString(section, prop);
if (greValue == null) {
// No such property is set for this GRE. Go on to next GRE.
ok = false;
} else {
// See if the value of the property for the GRE matches
// the given value.
if (!greValue.equals(aProperties.getProperty(prop))) {
ok = false;
}
}
}
if (!ok) {
continue;
}
}
String pathStr = parser.getString(section, "GRE_PATH");
if (pathStr != null) {
File grePath = new File(pathStr);
if (grePath.exists()) {
File xpcomLib = new File(grePath, "libxpcom.so");
if (xpcomLib.canRead()) {
// found a good GRE
return grePath;
}
}
}
}
return null;
}
/**
* @param aDirName
* @param aVersions
* @param aProperties
* @return
*/
private static File getPathFromConfigDir(String aDirName,
GREVersionRange[] aVersions, Properties aProperties) {
/*
* Open the directory provided and try to read any files in that
* directory that end with .conf. We look for an entry that might
* point to the GRE that we're interested in.
*/
File dir = new File(aDirName);
if (!dir.isDirectory()) {
return null;
}
File grePath = null;
File[] files = dir.listFiles();
for (int i = 0; i < files.length && grePath == null; i++) {
// only look for files that end in '.conf'
if (!files[i].getName().endsWith(".conf")) {
continue;
}
grePath = getPathFromConfigFile(files[i].getPath(), aVersions,
aProperties);
}
return grePath;
}
/**
* @param aVersionToCheck
* @param aVersions
* @return
*/
private static boolean checkVersion(String aVersionToCheck,
GREVersionRange[] aVersions) {
for (int i = 0; i < aVersions.length; i++) {
if (aVersions[i].check(aVersionToCheck)) {
return true;
}
}
return false;
}
/**
* Initialize the Mozilla object with the given XULRunner path. All
* subsequent Mozilla method invocations be done against the given XULRunner
* version.
*
* @param aLibXULDirectory path of XULRunner build to use
*
* @throws XPCOMInitializationException if failure occurred during
* initialization
*/
public void initialize(File aLibXULDirectory)
throws XPCOMInitializationException {
File jar = new File(aLibXULDirectory, JAVAXPCOM_JAR);
if (!jar.exists()) {
throw new XPCOMInitializationException("Could not find " + JAVAXPCOM_JAR +
" in " + aLibXULDirectory);
}
URL[] urls = new URL[1];
try {
urls[0] = jar.toURL();
} catch (MalformedURLException e) {
throw new XPCOMInitializationException(e);
}
ClassLoader loader = new URLClassLoader(urls,
this.getClass().getClassLoader());
try {
Class mozillaClass = Class.forName("org.mozilla.xpcom.internal.MozillaImpl",
true, loader);
mozilla = (IMozilla) mozillaClass.newInstance();
Class greClass = Class.forName("org.mozilla.xpcom.internal.GREImpl",
true, loader);
gre = (IGRE) greClass.newInstance();
Class xpcomClass = Class.forName("org.mozilla.xpcom.internal.XPCOMImpl",
true, loader);
xpcom = (IXPCOM) xpcomClass.newInstance();
Class javaXPCOMClass =
Class.forName("org.mozilla.xpcom.internal.JavaXPCOMMethods",
true, loader);
jxutils = (IJavaXPCOMUtils) javaXPCOMClass.newInstance();
} catch (Exception e) {
throw new XPCOMInitializationException("Could not load " +
"org.mozilla.xpcom.internal.* classes", e);
}
mozilla.initialize(aLibXULDirectory);
}
/**
* Initializes libXUL for embedding purposes.
* <p>
* NOTE: This function must be called from the "main" thread.
* <p>
* NOTE: At the present time, this function may only be called once in
* a given process. Use <code>termEmbedding</code> to clean up and free
* resources allocated by <code>initEmbedding</code>.
*
* @param aLibXULDirectory The directory in which the libXUL shared library
* was found.
* @param aAppDirectory The directory in which the application components
* and resources can be found. This will map to
* the "resource:app" directory service key.
* @param aAppDirProvider A directory provider for the application. This
* provider will be aggregated by a libXUL provider
* which will provide the base required GRE keys.
*
* @throws XPCOMException if a failure occurred during initialization
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public void initEmbedding(File aLibXULDirectory, File aAppDirectory,
IAppFileLocProvider aAppDirProvider) throws XPCOMException {
try {
gre.initEmbedding(aLibXULDirectory, aAppDirectory, aAppDirProvider);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Terminates libXUL embedding.
* <p>
* NOTE: Release any references to XPCOM objects that you may be holding
* before calling this function.
*
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public void termEmbedding() {
try {
gre.termEmbedding();
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
} finally {
mozilla = null;
gre = null;
xpcom = null;
}
}
/**
* Lock a profile directory using platform-specific semantics.
*
* @param aDirectory The profile directory to lock.
*
* @return A lock object. The directory will remain locked until the lock is
* released by invoking the <code>release</code> method, or by the
* termination of the JVM, whichever comes first.
*
* @throws XPCOMException if profile is already locked (with
* <code>errorcode</code> == <code>NS_ERROR_FILE_ACCESS_DENIED</code>);
* or if a failure occurred
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public ProfileLock lockProfileDirectory(File aDirectory)
throws XPCOMException {
try {
return gre.lockProfileDirectory(aDirectory);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Fire notifications to inform the toolkit about a new profile. This
* method should be called after <code>initEmbedding</code> if the
* embedder wishes to run with a profile.
* <p>
* Normally the embedder should call <code>lockProfileDirectory</code>
* to lock the directory before calling this method.
* <p>
* NOTE: There are two possibilities for selecting a profile:
* <ul>
* <li>
* Select the profile before calling <code>initEmbedding</code>.
* The aAppDirProvider object passed to <code>initEmbedding</code>
* should provide the NS_APP_USER_PROFILE_50_DIR key, and
* may also provide the following keys:
* <ul>
* <li>NS_APP_USER_PROFILE_LOCAL_50_DIR
* <li>NS_APP_PROFILE_DIR_STARTUP
* <li>NS_APP_PROFILE_LOCAL_DIR_STARTUP
* </ul>
* In this scenario <code>notifyProfile</code> should be called
* immediately after <code>initEmbedding</code>. Component
* registration information will be stored in the profile and
* JS components may be stored in the fastload cache.
* </li>
* <li>
* Select a profile some time after calling <code>initEmbedding</code>.
* In this case the embedder must install a directory service
* provider which provides NS_APP_USER_PROFILE_50_DIR and optionally
* NS_APP_USER_PROFILE_LOCAL_50_DIR. Component registration information
* will be stored in the application directory and JS components will not
* fastload.
* </li>
* </ul>
*
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public void notifyProfile() {
try {
gre.notifyProfile();
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Initializes XPCOM. You must call this method before proceeding
* to use XPCOM.
*
* @param aMozBinDirectory The directory containing the component
* registry and runtime libraries;
* or use <code>null</code> to use the working
* directory.
*
* @param aAppFileLocProvider The object to be used by Gecko that specifies
* to Gecko where to find profiles, the component
* registry preferences and so on; or use
* <code>null</code> for the default behaviour.
*
* @return the service manager
*
* @throws XPCOMException <ul>
* <li> NS_ERROR_NOT_INITIALIZED - if static globals were not initialied,
* which can happen if XPCOM is reloaded, but did not completly
* shutdown. </li>
* <li> Other error codes indicate a failure during initialisation. </li>
* </ul>
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public nsIServiceManager initXPCOM(File aMozBinDirectory,
IAppFileLocProvider aAppFileLocProvider) throws XPCOMException {
try {
return xpcom.initXPCOM(aMozBinDirectory, aAppFileLocProvider);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Shutdown XPCOM. You must call this method after you are finished
* using xpcom.
*
* @param aServMgr The service manager which was returned by initXPCOM.
* This will release servMgr.
*
* @throws XPCOMException if a failure occurred during termination
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public void shutdownXPCOM(nsIServiceManager aServMgr) throws XPCOMException {
try {
xpcom.shutdownXPCOM(aServMgr);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
} finally {
mozilla = null;
gre = null;
xpcom = null;
}
}
/**
* Public Method to access to the service manager.
*
* @return the service manager
*
* @throws XPCOMException if a failure occurred
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public nsIServiceManager getServiceManager() throws XPCOMException {
try {
return xpcom.getServiceManager();
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Public Method to access to the component manager.
*
* @return the component manager
*
* @throws XPCOMException if a failure occurred
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public nsIComponentManager getComponentManager() throws XPCOMException {
try {
return xpcom.getComponentManager();
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Public Method to access to the component registration manager.
*
* @return the component registration manager
*
* @throws XPCOMException if a failure occurred
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public nsIComponentRegistrar getComponentRegistrar() throws XPCOMException {
try {
return xpcom.getComponentRegistrar();
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* Public Method to create an instance of a nsILocalFile.
*
* @param aPath A string which specifies a full file path to a
* location. Relative paths will be treated as an
* error (NS_ERROR_FILE_UNRECOGNIZED_PATH).
* @param aFollowLinks This attribute will determine if the nsLocalFile will
* auto resolve symbolic links. By default, this value
* will be false on all non unix systems. On unix, this
* attribute is effectively a noop.
*
* @return an instance of an nsILocalFile that points to given path
*
* @throws XPCOMException <ul>
* <li> NS_ERROR_FILE_UNRECOGNIZED_PATH - raised for unrecognized paths
* or relative paths (must supply full file path) </li>
* </ul>
* @throws XPCOMInitializationException if Mozilla was not properly
* initialized
*/
public nsILocalFile newLocalFile(String aPath, boolean aFollowLinks)
throws XPCOMException {
try {
return xpcom.newLocalFile(aPath, aFollowLinks);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
/**
* If you create a class that implements nsISupports, you will need to provide
* an implementation of the <code>queryInterface</code> method. This helper
* function provides a simple implementation. Therefore, if your class does
* not need to do anything special with <code>queryInterface</code>, your
* implementation would look like:
* <pre>
* public nsISupports queryInterface(String aIID) {
* return XPCOM.queryInterface(this, aIID);
* }
* </pre>
*
* @param aObject object to query
* @param aIID requested interface IID
*
* @return <code>aObject</code> if the given object supports that
* interface;
* <code>null</code> otherwise.
*/
public static nsISupports queryInterface(nsISupports aObject, String aIID) {
ArrayList classes = new ArrayList();
classes.add(aObject.getClass());
while (!classes.isEmpty()) {
Class clazz = (Class) classes.remove(0);
// Skip over any class/interface in the "java.*" and "javax.*" domains.
String className = clazz.getName();
if (className.startsWith("java.") || className.startsWith("javax.")) {
continue;
}
// If given IID matches that of the current interface, then we
// know that aObject implements the interface specified by the given IID.
if (clazz.isInterface() && className.startsWith("org.mozilla")) {
String iid = Mozilla.getInterfaceIID(clazz);
if (iid != null && aIID.equals(iid)) {
return aObject;
}
}
// clazz didn't match, so add the interfaces it implements
Class[] interfaces = clazz.getInterfaces();
for (int i = 0; i < interfaces.length; i++ ) {
classes.add(interfaces[i]);
}
// Also add its superclass
Class superclass = clazz.getSuperclass();
if (superclass != null) {
classes.add(superclass);
}
}
return null;
}
/**
* Gets the interface IID for a particular Java interface. This is similar
* to NS_GET_IID in the C++ Mozilla files.
*
* @param aInterface interface which has defined an IID
*
* @return IID for given interface
*/
public static String getInterfaceIID(Class aInterface) {
// Get short class name (i.e. "bar", not "org.blah.foo.bar")
StringBuffer iidName = new StringBuffer();
String fullClassName = aInterface.getName();
int index = fullClassName.lastIndexOf(".");
String className = index > 0 ? fullClassName.substring(index + 1)
: fullClassName;
// Create iid field name
if (className.startsWith("ns")) {
iidName.append("NS_");
iidName.append(className.substring(2).toUpperCase());
} else {
iidName.append(className.toUpperCase());
}
iidName.append("_IID");
String iid;
try {
Field iidField = aInterface.getDeclaredField(iidName.toString());
iid = (String) iidField.get(null);
} catch (NoSuchFieldException e) {
// Class may implement non-Mozilla interfaces, which would not have an
// IID method. In that case, just null.
iid = null;
} catch (IllegalAccessException e) {
// Not allowed to access that field for some reason. Write out an
// error message, but don't fail.
System.err.println("ERROR: Could not get field " + iidName.toString());
iid = null;
}
return iid;
}
public long getNativeHandleFromAWT(Object widget) {
try {
return mozilla.getNativeHandleFromAWT(widget);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
public long wrapJavaObject(Object aJavaObject, String aIID) {
try {
return jxutils.wrapJavaObject(aJavaObject, aIID);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
public Object wrapXPCOMObject(long aXPCOMObject, String aIID) {
try {
return jxutils.wrapXPCOMObject(aXPCOMObject, aIID);
} catch (NullPointerException e) {
throw new XPCOMInitializationException("Must call " +
"Mozilla.getInstance().initialize() before using this method", e);
}
}
}
|