Skip to content

Commit

Permalink
Fixes for logging from the UI process, for replaylinker mozilla#62
Browse files Browse the repository at this point in the history
  • Loading branch information
bhackett1024 committed Mar 3, 2020
1 parent 0ec72bb commit 06705f9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
2 changes: 2 additions & 0 deletions browser/base/content/tabbrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6526,6 +6526,8 @@ function EnsureRecordReplayWatcher() {
const { logId } = msg.data;
const description = { logId, date: Date.now() };

ChromeUtils.recordReplayLog(`AddRecordReplayLog ${logId}`);

// See also crashes.js
const dir = Services.dirsvc.get("UAppData", Ci.nsIFile);
dir.append("Recordings");
Expand Down
20 changes: 7 additions & 13 deletions devtools/server/actors/replay/connection-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ function openServerSocket() {
gServerSocket.onerror = onServerError;
}

// Status of the cloud server connection.
let gStatus = "cloudInitialize.label";
// Whether log messages can be sent to the cloud server.
let gConnected = false;

function updateStatus(status) {
gStatus = status;
postMessage({ kind: "updateStatus", status });
}

Expand All @@ -65,12 +64,14 @@ function onServerOpen(evt) {
}

function onServerClose() {
gConnected = false;
updateStatus("cloudReconnecting.label");
setTimeout(openServerSocket, 3000);
doLog(`CloudServer Connection Closed\n`);
}

function onServerError(evt) {
gConnected = false;
updateStatus("cloudError.label");
doLog(`CloudServer Connection Error\n`);
}
Expand All @@ -81,15 +82,8 @@ async function onServerMessage(evt) {
switch (data.kind) {
case "modules": {
const { sessionId, controlJS, replayJS, updateNeeded, updateWanted } = data;
if (updateNeeded) {
updateStatus("cloudUpdateNeeded.label");
} else {
postMessage({ kind: "loaded", sessionId, controlJS, replayJS });
updateStatus("");
}
if (updateNeeded || updateWanted) {
postMessage({ kind: "downloadUpdate", updateNeeded });
}
gConnected = true;
postMessage({ kind: "loaded", sessionId, controlJS, replayJS, updateNeeded, updateWanted });
break;
}
case "connectionAddress": {
Expand Down Expand Up @@ -227,7 +221,7 @@ function PostError(why, id) {
}

function doLog(text) {
if (gStatus) {
if (gConnected) {
sendMessageToCloudServer({ kind: "log", text });
} else {
postMessage({ kind: "logOffline", text });
Expand Down
36 changes: 23 additions & 13 deletions devtools/server/actors/replay/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
const { setTimeout } = Components.utils.import('resource://gre/modules/Timer.jsm');

XPCOMUtils.defineLazyModuleGetters(this, {
AppUpdater: "resource:///modules/AppUpdater.jsm",
Expand All @@ -32,7 +33,7 @@ function Initialize(address, callbacks) {
try {
onMessage(evt);
} catch (e) {
ChromeUtils.recordReplayLog(`RecordReplaySocketError ${evt.data.why}`);
ChromeUtils.recordReplayLog(`RecordReplaySocketError ${e}`);
}
});
gCallbacks = callbacks;
Expand All @@ -49,14 +50,23 @@ function onMessage(evt) {
switch (evt.data.kind) {
case "updateStatus":
gCallbacks.updateStatus(evt.data.status);
if (!evt.data.status.length) {
flushOfflineLog();
}
break;
case "loaded": {
const { sessionId, controlJS, replayJS } = evt.data;
let { sessionId, controlJS, replayJS, updateNeeded, updateWanted } = evt.data;

gSessionId = sessionId;
gCallbacks.loadedJS(sessionId, controlJS, replayJS);
flushOfflineLog();

if (updateNeeded) {
gCallbacks.updateStatus("cloudUpdateNeeded.label");
} else {
gCallbacks.updateStatus("");
gCallbacks.loadedJS(controlJS, replayJS);
}

if (updateNeeded || updateWanted) {
downloadUpdate(updateNeeded);
}
break;
}
case "message":
Expand All @@ -65,9 +75,6 @@ function onMessage(evt) {
case "connectionFailed":
Services.cpmm.sendAsyncMessage("RecordReplayCriticalError", { kind: "CloudSpawnError" });
break;
case "downloadUpdate":
downloadUpdate(evt.data.updateNeeded);
break;
case "connected":
ChromeUtils.recordReplayLog(`RecordReplayConnected ${gSessionId}`);
gCallbacks.onConnected(evt.data.id);
Expand Down Expand Up @@ -164,7 +171,7 @@ function offlineLogPath() {
OS.File.makeDir(dir.path);
}

dir.append("offlineLog.txt");
dir.append("offlineLog.log");
return dir.path;
}

Expand All @@ -190,12 +197,14 @@ async function waitForOfflineLogContents() {

async function addToOfflineLog(text) {
await waitForOfflineLogContents();
offlineLogContents += text;
offlineLogContents += `Offline ${gSessionId} ${text}`;

if (!hasOfflineLogFlushTimer) {
hasOfflineLogFlushTimer = true;
setTimeout(() => {
OS.File.writeAtomic(offlineLogPath(), offlineLogContents);
if (offlineLogContents.length) {
OS.File.writeAtomic(offlineLogPath(), offlineLogContents);
}
hasOfflineLogFlushTimer = false;
}, 500);
}
Expand All @@ -205,8 +214,9 @@ async function flushOfflineLog() {
await waitForOfflineLogContents();

if (offlineLogContents.length) {
OS.File.writeAtomic(offlineLogPath(), "");
AddToLog(offlineLogContents);
offlineLogContents = "";
OS.File.remove(offlineLogPath());
}
}

Expand Down
13 changes: 3 additions & 10 deletions toolkit/recordreplay/ipc/ParentIPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,17 @@ static void ExtractJSString(JSContext* aCx, JSString* aString,
aBuffer.append(dataChars, dataLength);
}

// ID which has been assigned to this browser session by the cloud server.
nsAutoCString gSessionId;

static bool LoadedCallback(JSContext* aCx, unsigned aArgc, JS::Value* aVp) {
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);

if (!args.get(0).isString() ||
!args.get(1).isString() ||
!args.get(2).isString()) {
if (!args.get(0).isString() || !args.get(1).isString()) {
JS_ReportErrorASCII(aCx, "Expected strings");
return false;
}

js::ConvertJSStringToCString(aCx, args.get(0).toString(), gSessionId);

if (!getenv("WEBREPLAY_SOURCES")) {
ExtractJSString(aCx, args.get(1).toString(), gControlJS);
ExtractJSString(aCx, args.get(2).toString(), gReplayJS);
ExtractJSString(aCx, args.get(0).toString(), gControlJS);
ExtractJSString(aCx, args.get(1).toString(), gReplayJS);
}

args.rval().setUndefined();
Expand Down

0 comments on commit 06705f9

Please sign in to comment.