2012-12-11 15:13:00 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
#ifndef MERGED_COMPARTMENT
|
|
|
|
|
2012-12-11 15:13:00 -08:00
|
|
|
this.EXPORTED_SYMBOLS = [
|
|
|
|
"ProfileCreationTimeAccessor",
|
|
|
|
"ProfileMetadataProvider",
|
|
|
|
];
|
|
|
|
|
|
|
|
const {utils: Cu, classes: Cc, interfaces: Ci} = Components;
|
|
|
|
|
|
|
|
const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
2013-01-27 11:26:48 -08:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Metrics.jsm");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const DEFAULT_PROFILE_MEASUREMENT_NAME = "age";
|
2012-12-11 15:13:00 -08:00
|
|
|
const REQUIRED_UINT32_TYPE = {type: "TYPE_UINT32"};
|
|
|
|
|
2013-06-13 18:36:21 -07:00
|
|
|
Cu.import("resource://gre/modules/Promise.jsm");
|
2012-12-11 15:13:00 -08:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm")
|
2013-01-06 12:13:27 -08:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2013-08-26 11:55:58 -07:00
|
|
|
Cu.import("resource://gre/modules/Log.jsm");
|
2012-12-11 15:13:00 -08:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
|
|
|
|
|
|
|
// Profile creation time access.
|
|
|
|
// This is separate from the provider to simplify testing and enable extraction
|
|
|
|
// to a shared location in the future.
|
2013-06-26 17:01:45 -07:00
|
|
|
this.ProfileCreationTimeAccessor = function(profile, log) {
|
2012-12-11 15:13:00 -08:00
|
|
|
this.profilePath = profile || OS.Constants.Path.profileDir;
|
|
|
|
if (!this.profilePath) {
|
|
|
|
throw new Error("No profile directory.");
|
|
|
|
}
|
2013-01-06 12:13:27 -08:00
|
|
|
this._log = log || {"debug": function (s) { dump(s + "\n"); }};
|
2012-12-11 15:13:00 -08:00
|
|
|
}
|
2013-06-26 17:01:45 -07:00
|
|
|
this.ProfileCreationTimeAccessor.prototype = {
|
2012-12-11 15:13:00 -08:00
|
|
|
/**
|
|
|
|
* There are three ways we can get our creation time:
|
|
|
|
*
|
|
|
|
* 1. From our own saved value (to avoid redundant work).
|
|
|
|
* 2. From the on-disk JSON file.
|
|
|
|
* 3. By calculating it from the filesystem.
|
|
|
|
*
|
|
|
|
* If we have to calculate, we write out the file; if we have
|
|
|
|
* to touch the file, we persist in-memory.
|
|
|
|
*
|
|
|
|
* @return a promise that resolves to the profile's creation time.
|
|
|
|
*/
|
|
|
|
get created() {
|
|
|
|
if (this._created) {
|
|
|
|
return Promise.resolve(this._created);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onSuccess(times) {
|
|
|
|
if (times && times.created) {
|
|
|
|
return this._created = times.created;
|
|
|
|
}
|
|
|
|
return onFailure.call(this, null, times);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFailure(err, times) {
|
|
|
|
return this.computeAndPersistTimes(times)
|
|
|
|
.then(function onSuccess(created) {
|
|
|
|
return this._created = created;
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.readTimes()
|
|
|
|
.then(onSuccess.bind(this),
|
|
|
|
onFailure.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Explicitly make `file`, a filename, a full path
|
|
|
|
* relative to our profile path.
|
|
|
|
*/
|
|
|
|
getPath: function (file) {
|
|
|
|
return OS.Path.join(this.profilePath, file);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a promise which resolves to the JSON contents
|
|
|
|
* of the time file in this accessor's profile.
|
|
|
|
*/
|
|
|
|
readTimes: function (file="times.json") {
|
|
|
|
return CommonUtils.readJSON(this.getPath(file));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a promise representing the writing of `contents`
|
|
|
|
* to `file` in the specified profile.
|
|
|
|
*/
|
|
|
|
writeTimes: function (contents, file="times.json") {
|
|
|
|
return CommonUtils.writeJSON(contents, this.getPath(file));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge existing contents with a 'created' field, writing them
|
|
|
|
* to the specified file. Promise, naturally.
|
|
|
|
*/
|
|
|
|
computeAndPersistTimes: function (existingContents, file="times.json") {
|
|
|
|
let path = this.getPath(file);
|
|
|
|
function onOldest(oldest) {
|
|
|
|
let contents = existingContents || {};
|
|
|
|
contents.created = oldest;
|
|
|
|
return this.writeTimes(contents, path)
|
|
|
|
.then(function onSuccess() {
|
|
|
|
return oldest;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getOldestProfileTimestamp()
|
|
|
|
.then(onOldest.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse the contents of the profile directory, finding the oldest file
|
|
|
|
* and returning its creation timestamp.
|
|
|
|
*/
|
|
|
|
getOldestProfileTimestamp: function () {
|
2013-01-06 12:13:27 -08:00
|
|
|
let self = this;
|
2012-12-11 15:13:00 -08:00
|
|
|
let oldest = Date.now() + 1000;
|
|
|
|
let iterator = new OS.File.DirectoryIterator(this.profilePath);
|
2013-01-06 12:13:27 -08:00
|
|
|
self._log.debug("Iterating over profile " + this.profilePath);
|
2012-12-11 15:13:00 -08:00
|
|
|
if (!iterator) {
|
|
|
|
throw new Error("Unable to fetch oldest profile entry: no profile iterator.");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onEntry(entry) {
|
2013-01-06 12:13:27 -08:00
|
|
|
function onStatSuccess(info) {
|
|
|
|
// OS.File doesn't seem to be behaving. See Bug 827148.
|
|
|
|
// Let's do the best we can. This whole function is defensive.
|
2013-01-06 14:07:34 -08:00
|
|
|
let date = info.winBirthDate || info.macBirthDate;
|
2013-01-06 12:13:27 -08:00
|
|
|
if (!date || !date.getTime()) {
|
2013-01-06 14:07:34 -08:00
|
|
|
// OS.File will only return file creation times of any kind on Mac
|
|
|
|
// and Windows, where birthTime is defined.
|
|
|
|
// That means we're unable to function on Linux, so we use mtime
|
|
|
|
// instead.
|
|
|
|
self._log.debug("No birth date. Using mtime.");
|
|
|
|
date = info.lastModificationDate;
|
2013-01-06 12:13:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
let timestamp = date.getTime();
|
|
|
|
self._log.debug("Using date: " + entry.path + " = " + date);
|
|
|
|
if (timestamp < oldest) {
|
|
|
|
oldest = timestamp;
|
|
|
|
}
|
2012-12-11 15:13:00 -08:00
|
|
|
}
|
|
|
|
}
|
2013-02-18 13:34:51 -08:00
|
|
|
|
|
|
|
function onStatFailure(e) {
|
|
|
|
// Never mind.
|
|
|
|
self._log.debug("Stat failure: " + CommonUtils.exceptionStr(e));
|
|
|
|
}
|
|
|
|
|
2012-12-11 15:13:00 -08:00
|
|
|
return OS.File.stat(entry.path)
|
2013-02-18 13:34:51 -08:00
|
|
|
.then(onStatSuccess, onStatFailure);
|
2012-12-11 15:13:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let promise = iterator.forEach(onEntry);
|
|
|
|
|
|
|
|
function onSuccess() {
|
|
|
|
iterator.close();
|
|
|
|
return oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFailure(reason) {
|
|
|
|
iterator.close();
|
|
|
|
throw new Error("Unable to fetch oldest profile entry: " + reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.then(onSuccess, onFailure);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Measurements pertaining to the user's profile.
|
|
|
|
*/
|
2013-01-06 12:13:27 -08:00
|
|
|
function ProfileMetadataMeasurement() {
|
|
|
|
Metrics.Measurement.call(this);
|
2012-12-11 15:13:00 -08:00
|
|
|
}
|
|
|
|
ProfileMetadataMeasurement.prototype = {
|
2013-01-06 12:13:27 -08:00
|
|
|
__proto__: Metrics.Measurement.prototype,
|
|
|
|
|
|
|
|
name: DEFAULT_PROFILE_MEASUREMENT_NAME,
|
|
|
|
version: 1,
|
2012-12-11 15:13:00 -08:00
|
|
|
|
2013-02-21 14:11:54 -08:00
|
|
|
fields: {
|
2012-12-11 15:13:00 -08:00
|
|
|
// Profile creation date. Number of days since Unix epoch.
|
2013-02-21 14:11:54 -08:00
|
|
|
profileCreation: {type: Metrics.Storage.FIELD_LAST_NUMERIC},
|
2012-12-11 15:13:00 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a millisecond timestamp into a day timestamp.
|
|
|
|
*
|
|
|
|
* @param msec a number of milliseconds since epoch.
|
|
|
|
* @return the number of whole days denoted by the input.
|
|
|
|
*/
|
|
|
|
function truncate(msec) {
|
|
|
|
return Math.floor(msec / MILLISECONDS_PER_DAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-06 12:13:27 -08:00
|
|
|
* A Metrics.Provider for profile metadata, such as profile creation time.
|
2012-12-11 15:13:00 -08:00
|
|
|
*/
|
2013-06-26 17:01:45 -07:00
|
|
|
this.ProfileMetadataProvider = function() {
|
2013-01-06 12:13:27 -08:00
|
|
|
Metrics.Provider.call(this);
|
2012-12-11 15:13:00 -08:00
|
|
|
}
|
2013-06-26 17:01:45 -07:00
|
|
|
this.ProfileMetadataProvider.prototype = {
|
2013-01-06 12:13:27 -08:00
|
|
|
__proto__: Metrics.Provider.prototype,
|
|
|
|
|
|
|
|
name: "org.mozilla.profile",
|
|
|
|
|
|
|
|
measurementTypes: [ProfileMetadataMeasurement],
|
2012-12-11 15:13:00 -08:00
|
|
|
|
2013-02-18 13:05:07 -08:00
|
|
|
pullOnly: true,
|
2013-01-31 08:58:19 -08:00
|
|
|
|
2012-12-11 15:13:00 -08:00
|
|
|
getProfileCreationDays: function () {
|
2013-01-06 12:13:27 -08:00
|
|
|
let accessor = new ProfileCreationTimeAccessor(null, this._log);
|
2012-12-11 15:13:00 -08:00
|
|
|
|
|
|
|
return accessor.created
|
|
|
|
.then(truncate);
|
|
|
|
},
|
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
collectConstantData: function () {
|
|
|
|
let m = this.getMeasurement(DEFAULT_PROFILE_MEASUREMENT_NAME, 1);
|
2012-12-11 15:13:00 -08:00
|
|
|
|
2013-01-06 12:13:27 -08:00
|
|
|
return Task.spawn(function collectConstant() {
|
|
|
|
let createdDays = yield this.getProfileCreationDays();
|
|
|
|
|
|
|
|
yield this.enqueueStorageOperation(function storeDays() {
|
|
|
|
return m.setLastNumeric("profileCreation", createdDays);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2012-12-11 15:13:00 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|