Commit 232ba71f authored by smckay's avatar smckay Committed by Commit bot

Include a machine id in log files.

This is the first step in making more reliable cross-machine history synching. A subsequent step will support reading data from the history files from other machines.

Also, updated the debug log to employ machine id.

BUG=420680
TEST=browser_test: FileManagerJsTest.*

Review URL: https://codereview.chromium.org/886483004

Cr-Commit-Position: refs/heads/master@{#313799}
parent 319ba630
......@@ -1052,9 +1052,6 @@ importer.DriveSyncWatcher.prototype.getSyncStatus_ =
}.bind(this));
};
/** @const {string} */
importer.HISTORY_FILE_NAME = 'import-history.r1.log';
/**
* History loader that provides an ImportHistorty appropriate
* to user settings (if import history is enabled/disabled).
......@@ -1086,13 +1083,16 @@ importer.RuntimeHistoryLoader.prototype.getHistory = function() {
* @this {importer.RuntimeHistoryLoader}
*/
function(enabled) {
var loader = enabled ?
new importer.SynchronizedHistoryLoader(
new importer.ChromeSyncFileEntryProvider(
importer.HISTORY_FILE_NAME)) :
new importer.DummyImportHistory(false);
this.historyResolver_.resolve(loader.getHistory());
importer.getHistoryFilename().then(
function(filename) {
var loader = enabled ?
new importer.SynchronizedHistoryLoader(
new importer.ChromeSyncFileEntryProvider(
filename)) :
new importer.DummyImportHistory(false);
this.historyResolver_.resolve(loader.getHistory());
}.bind(this));
}.bind(this));
}
......
......@@ -133,6 +133,80 @@ importer.importEnabled = function() {
});
};
/**
* Local storage key for machine id.
* @const {string}
*/
importer.MACHINE_ID_STORAGE_KEY_ = 'importer-machine-id';
/**
* @return {!Promise.<number>} Resolves with an integer that is probably
* relatively unique to this machine (among a users machines).
*/
importer.getMachineId = function() {
return new Promise(
function(resolve, reject) {
chrome.storage.local.get(
importer.MACHINE_ID_STORAGE_KEY_,
/** @param {Object.<string, ?>} values */
function(values) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
return;
}
var machineId = values[importer.MACHINE_ID_STORAGE_KEY_];
if (!!machineId) {
resolve(machineId);
} else {
var machineId = importer.generateMachineId_();
var newValues = {};
newValues[importer.MACHINE_ID_STORAGE_KEY_] = machineId;
chrome.storage.local.set(
newValues,
function() {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(machineId);
}
});
}
});
});
};
/**
* @return {!Promise.<string>} Resolves with the filename of this
* machines history file.
*/
importer.getHistoryFilename = function() {
return importer.getMachineId().then(
function(machineId) {
return 'import-history.' + machineId + '.log';
});
};
/**
* @return {!Promise.<string>} Resolves with the filename of this
* machines debug log file.
*/
importer.getDebugLogFilename = function() {
return importer.getMachineId().then(
function(machineId) {
return 'import-debug.' + machineId + '.log';
});
};
/**
* @return {number} A relatively unique six digit integer that is most likely
* unique to this machine among a user's machines. Used only to segregate
* log files on sync storage.
*/
importer.generateMachineId_ = function() {
return Math.floor(Math.random() * 899999) + 100000;
};
/**
* A Promise wrapper that provides public access to resolve and reject methods.
*
......@@ -310,12 +384,16 @@ importer.ChromeSyncFileEntryProvider = function(fileName) {
/**
* Returns a sync FileEntry. Convenience method for class that just want
* a file, but don't need to monitor changes.
* @param {string} fileName
* @param {!Promise.<string>} fileNamePromise
* @return {!Promise.<!FileEntry>}
*/
importer.ChromeSyncFileEntryProvider.getFileEntry = function(fileName) {
return new importer.ChromeSyncFileEntryProvider(fileName)
.getSyncFileEntry();
importer.ChromeSyncFileEntryProvider.getFileEntry =
function(fileNamePromise) {
return fileNamePromise.then(
function(fileName) {
return new importer.ChromeSyncFileEntryProvider(fileName)
.getSyncFileEntry();
});
};
/**
......@@ -590,7 +668,7 @@ importer.getLogger = function() {
if (!importer.logger_) {
importer.logger_ = new importer.RuntimeLogger(
importer.ChromeSyncFileEntryProvider.getFileEntry(
'importer_debug.log'));
importer.getDebugLogFilename()));
}
return importer.logger_;
};
......@@ -23,13 +23,14 @@ var sdFileEntry;
/** @type {!MockFileEntry} */
var driveFileEntry;
// Sadly, boilerplate setup necessary to include test support classes.
loadTimeData.data = {
DRIVE_DIRECTORY_LABEL: 'My Drive',
DOWNLOADS_DIRECTORY_LABEL: 'Downloads'
};
// Set up the test components.
function setUp() {
// Sadly, boilerplate setup necessary to include test support classes.
loadTimeData.data = {
DRIVE_DIRECTORY_LABEL: 'My Drive',
DOWNLOADS_DIRECTORY_LABEL: 'Downloads'
};
var cameraFileSystem = new MockFileSystem(
'camera-fs', 'filesystem:camera-123');
var sdFileSystem = new MockFileSystem(
......@@ -103,6 +104,35 @@ function testResolver_Reject(callback) {
});
}
function testGetMachineId(callback) {
var storage = new MockChromeStorageAPI();
var promise = importer.getMachineId().then(
function(firstMachineId) {
assertTrue(100000 <= firstMachineId <= 9999999);
importer.getMachineId().then(
function(secondMachineId) {
assertEquals(firstMachineId, secondMachineId);
});
});
reportPromise(promise, callback);
}
function testHistoryFilename(callback) {
var storage = new MockChromeStorageAPI();
var promise = importer.getHistoryFilename().then(
function(firstName) {
assertTrue(!!firstName && firstName.length > 10);
importer.getHistoryFilename().then(
function(secondName) {
assertEquals(firstName, secondName);
});
});
reportPromise(promise, callback);
}
/** @param {string} path */
function assertIsMediaDir(path) {
var dir = createDirectoryEntry(sdVolume, path);
......
......@@ -200,12 +200,13 @@ function MockChromeStorageAPI() {
* @private
*/
MockChromeStorageAPI.prototype.get_ = function(keys, callback) {
var keyArray = keys instanceof Array ? keys : [keys];
var keys = keys instanceof Array ? keys : [keys];
var result = {};
for (var key in keys) {
if (key in this.state)
result[key] = this.state[key];
}
keys.forEach(
function(key) {
if (key in this.state)
result[key] = this.state[key];
}.bind(this));
callback(result);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment