toolkit/crashreporter/test/browser/head.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 function create_subdir(dir, subdirname) {
michael@0 2 let subdir = dir.clone();
michael@0 3 subdir.append(subdirname);
michael@0 4 if (subdir.exists()) {
michael@0 5 subdir.remove(true);
michael@0 6 }
michael@0 7 subdir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
michael@0 8 return subdir;
michael@0 9 }
michael@0 10
michael@0 11 // need to hold on to this to unregister for cleanup
michael@0 12 let _provider = null;
michael@0 13
michael@0 14 function make_fake_appdir() {
michael@0 15 // Create a directory inside the profile and register it as UAppData, so
michael@0 16 // we can stick fake crash reports inside there. We put it inside the profile
michael@0 17 // just because we know that will get cleaned up after the mochitest run.
michael@0 18 let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
michael@0 19 .getService(Ci.nsIProperties);
michael@0 20 let profD = dirSvc.get("ProfD", Ci.nsILocalFile);
michael@0 21 // create a subdir just to keep our files out of the way
michael@0 22 let appD = create_subdir(profD, "UAppData");
michael@0 23
michael@0 24 let crashesDir = create_subdir(appD, "Crash Reports");
michael@0 25 create_subdir(crashesDir, "pending");
michael@0 26 create_subdir(crashesDir, "submitted");
michael@0 27
michael@0 28 _provider = {
michael@0 29 getFile: function(prop, persistent) {
michael@0 30 persistent.value = true;
michael@0 31 if (prop == "UAppData") {
michael@0 32 return appD.clone();
michael@0 33 }
michael@0 34 throw Components.results.NS_ERROR_FAILURE;
michael@0 35 },
michael@0 36 QueryInterface: function(iid) {
michael@0 37 if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
michael@0 38 iid.equals(Ci.nsISupports)) {
michael@0 39 return this;
michael@0 40 }
michael@0 41 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 42 }
michael@0 43 };
michael@0 44 // register our new provider
michael@0 45 dirSvc.QueryInterface(Ci.nsIDirectoryService)
michael@0 46 .registerProvider(_provider);
michael@0 47 // and undefine the old value
michael@0 48 try {
michael@0 49 dirSvc.undefine("UAppData");
michael@0 50 } catch(ex) {} // it's ok if this fails, the value might not be cached yet
michael@0 51 return appD.clone();
michael@0 52 }
michael@0 53
michael@0 54 function cleanup_fake_appdir() {
michael@0 55 let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
michael@0 56 .getService(Ci.nsIProperties);
michael@0 57 dirSvc.QueryInterface(Ci.nsIDirectoryService)
michael@0 58 .unregisterProvider(_provider);
michael@0 59 // undefine our value so future calls get the real value
michael@0 60 try {
michael@0 61 dirSvc.undefine("UAppData");
michael@0 62 } catch(ex) {
michael@0 63 dump("cleanup_fake_appdir: dirSvc.undefine failed: " + ex.message +"\n");
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 function add_fake_crashes(crD, count) {
michael@0 68 let results = [];
michael@0 69 let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
michael@0 70 .getService(Ci.nsIUUIDGenerator);
michael@0 71 let submitdir = crD.clone();
michael@0 72 submitdir.append("submitted");
michael@0 73 // create them from oldest to newest, to ensure that about:crashes
michael@0 74 // displays them in the correct order
michael@0 75 let date = Date.now() - count * 60000;
michael@0 76 for (let i = 0; i < count; i++) {
michael@0 77 let uuid = uuidGenerator.generateUUID().toString();
michael@0 78 // ditch the {}
michael@0 79 uuid = "bp-" + uuid.substring(1, uuid.length - 2);
michael@0 80 let fn = uuid + ".txt";
michael@0 81 let file = submitdir.clone();
michael@0 82 file.append(fn);
michael@0 83 file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 84 file.lastModifiedTime = date;
michael@0 85 results.push({'id': uuid, 'date': date, 'pending': false});
michael@0 86
michael@0 87 date += 60000;
michael@0 88 }
michael@0 89 // we want them sorted newest to oldest, since that's the order
michael@0 90 // that about:crashes lists them in
michael@0 91 results.sort(function(a,b) b.date - a.date);
michael@0 92 return results;
michael@0 93 }
michael@0 94
michael@0 95 function writeDataToFile(file, data) {
michael@0 96 var fstream = Cc["@mozilla.org/network/file-output-stream;1"]
michael@0 97 .createInstance(Ci.nsIFileOutputStream);
michael@0 98 // open, write, truncate
michael@0 99 fstream.init(file, -1, -1, 0);
michael@0 100 var os = Cc["@mozilla.org/intl/converter-output-stream;1"]
michael@0 101 .createInstance(Ci.nsIConverterOutputStream);
michael@0 102 os.init(fstream, "UTF-8", 0, 0x0000);
michael@0 103 os.writeString(data);
michael@0 104 os.close();
michael@0 105 fstream.close();
michael@0 106 }
michael@0 107
michael@0 108 function addPendingCrashreport(crD, date, extra) {
michael@0 109 let pendingdir = crD.clone();
michael@0 110 pendingdir.append("pending");
michael@0 111 let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
michael@0 112 .getService(Ci.nsIUUIDGenerator);
michael@0 113 let uuid = uuidGenerator.generateUUID().toString();
michael@0 114 // ditch the {}
michael@0 115 uuid = uuid.substring(1, uuid.length - 1);
michael@0 116 let dumpfile = pendingdir.clone();
michael@0 117 dumpfile.append(uuid + ".dmp");
michael@0 118 writeDataToFile(dumpfile, "MDMP"); // that's the start of a valid minidump, anyway
michael@0 119 let extrafile = pendingdir.clone();
michael@0 120 extrafile.append(uuid + ".extra");
michael@0 121 let extradata = "";
michael@0 122 for (let x in extra) {
michael@0 123 extradata += x + "=" + extra[x] + "\n";
michael@0 124 }
michael@0 125 writeDataToFile(extrafile, extradata);
michael@0 126 dumpfile.lastModifiedTime = date;
michael@0 127 extrafile.lastModifiedTime = date;
michael@0 128 return {'id': uuid, 'date': date, 'pending': true, 'extra': extra};
michael@0 129 }

mercurial