AppData.jsm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = [
  6. "makeFakeAppDir",
  7. ];
  8. const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
  9. Cu.import("resource://gre/modules/osfile.jsm");
  10. Cu.import("resource://gre/modules/Promise.jsm");
  11. // Reference needed in order for fake app dir provider to be active.
  12. var gFakeAppDirectoryProvider;
  13. /**
  14. * Installs a fake UAppData directory.
  15. *
  16. * This is needed by tests because a UAppData directory typically isn't
  17. * present in the test environment.
  18. *
  19. * We create the new UAppData directory under the profile's directory
  20. * because the profile directory is automatically cleaned as part of
  21. * test shutdown.
  22. *
  23. * This returns a promise that will be resolved once the new directory
  24. * is created and installed.
  25. */
  26. this.makeFakeAppDir = function () {
  27. let dirMode = OS.Constants.libc.S_IRWXU;
  28. let dirService = Cc["@mozilla.org/file/directory_service;1"]
  29. .getService(Ci.nsIProperties);
  30. let baseFile = dirService.get("ProfD", Ci.nsIFile);
  31. let appD = baseFile.clone();
  32. appD.append("UAppData");
  33. if (gFakeAppDirectoryProvider) {
  34. return Promise.resolve(appD.path);
  35. }
  36. function makeDir(f) {
  37. if (f.exists()) {
  38. return;
  39. }
  40. dump("Creating directory: " + f.path + "\n");
  41. f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
  42. }
  43. makeDir(appD);
  44. let reportsD = appD.clone();
  45. reportsD.append("Crash Reports");
  46. let pendingD = reportsD.clone();
  47. pendingD.append("pending");
  48. let submittedD = reportsD.clone();
  49. submittedD.append("submitted");
  50. makeDir(reportsD);
  51. makeDir(pendingD);
  52. makeDir(submittedD);
  53. let provider = {
  54. getFile: function (prop, persistent) {
  55. persistent.value = true;
  56. if (prop == "UAppData") {
  57. return appD.clone();
  58. }
  59. throw Cr.NS_ERROR_FAILURE;
  60. },
  61. QueryInterace: function (iid) {
  62. if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
  63. iid.equals(Ci.nsISupports)) {
  64. return this;
  65. }
  66. throw Cr.NS_ERROR_NO_INTERFACE;
  67. },
  68. };
  69. // Register the new provider.
  70. dirService.QueryInterface(Ci.nsIDirectoryService)
  71. .registerProvider(provider);
  72. // And undefine the old one.
  73. try {
  74. dirService.undefine("UAppData");
  75. } catch (ex) {};
  76. gFakeAppDirectoryProvider = provider;
  77. dump("Successfully installed fake UAppDir\n");
  78. return Promise.resolve(appD.path);
  79. };