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