Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created November 16, 2020 20:26
Show Gist options
  • Select an option

  • Save jasonLaster/153b2fd76b9fe2ba031377be82b92c5b to your computer and use it in GitHub Desktop.

Select an option

Save jasonLaster/153b2fd76b9fe2ba031377be82b92c5b to your computer and use it in GitHub Desktop.
diff --git a/devtools/server/actors/replay/connection.js b/devtools/server/actors/replay/connection.js
index 10653c994a11..397e8071e766 100644
--- a/devtools/server/actors/replay/connection.js
+++ b/devtools/server/actors/replay/connection.js
@@ -10,7 +10,9 @@ 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');
+const { setTimeout } = Components.utils.import(
+ "resource://gre/modules/Timer.jsm"
+);
XPCOMUtils.defineLazyModuleGetters(this, {
AppUpdater: "resource:///modules/AppUpdater.jsm",
@@ -170,6 +172,60 @@ function getResourceInfo(url, text) {
};
}
+function isAuthenticationEnabled() {
+ // Authentication is controlled by a preference but can be disabled by an
+ // environment variable.
+ return (
+ Services.prefs.getBoolPref(
+ "devtools.recordreplay.authentication-enabled"
+ ) && !getenv("RECORD_REPLAY_DISABLE_AUTHENTICATION")
+ );
+}
+
+function isRunningTest() {
+ return !!getenv("RECORD_REPLAY_TEST_SCRIPT");
+}
+
+function saveRecordingInDB(description) {
+ let user;
+
+ if (isRunningTest()) {
+ user = { id: "77c6dc81-280d-4f28-971b-9915ab00f0f7" };
+ } else {
+ user = getLoggedInUser();
+ }
+
+ if (!user) {
+ return;
+ }
+
+ const pageUrl = Services.prefs.getStringPref(
+ "devtools.recordreplay.saveRecordingsUrl"
+ );
+
+ const body = {
+ user_id: user.id,
+ recording_id: description.recordingId,
+ id: description.recordingId,
+ url: description.url,
+ title: description.title,
+ duration: description.duration,
+ last_screen_data: description.lastScreenData || "",
+ last_screen_mime_type: description.lastScreenMimeType || "image/jpeg",
+ };
+
+ console.log(`>>> saving recording`, description.recordingId);
+ return fetch(`${pageUrl}/api/create-recording`, {
+ method: "post",
+ body: JSON.stringify(body),
+ headers: { "Content-Type": "application/json" },
+ })
+ .then(async (r) =>
+ console.log(`succeeded in creating recording`, await r.json())
+ )
+ .catch((err) => console.error(err));
+}
+
async function addRecordingResource(recordingId, url) {
try {
const response = await fetch(url);
@@ -180,11 +236,10 @@ async function addRecordingResource(recordingId, url) {
const text = await response.text();
const resource = getResourceInfo(url, text);
- await sendCommand(
- gMainChannelId,
- "Internal.addRecordingResource",
- { recordingId, resource }
- );
+ await sendCommand(gMainChannelId, "Internal.addRecordingResource", {
+ recordingId,
+ resource,
+ });
const { known } = await sendCommand(
gMainChannelId,
@@ -192,11 +247,10 @@ async function addRecordingResource(recordingId, url) {
{ resource }
);
if (!known) {
- await sendCommand(
- gMainChannelId,
- "Internal.addResource",
- { resource, contents: text }
- );
+ await sendCommand(gMainChannelId, "Internal.addResource", {
+ resource,
+ contents: text,
+ });
}
return text;
@@ -233,6 +287,14 @@ Services.ppmm.addMessageListener("RecordReplayGeneratedSourceWithSourceMap", {
},
});
+Services.ppmm.addMessageListener("RecordingFinished", {
+ async receiveMessage(msg) {
+ if (isAuthenticationEnabled()) {
+ await saveRecordingInDB(msg.data);
+ }
+ },
+});
+
function computeSourceURL(url, root, path) {
if (root != "") {
path = root + (root.endsWith("/") ? "" : "/") + path;
diff --git a/devtools/startup/DevToolsStartup.jsm b/devtools/startup/DevToolsStartup.jsm
index 6c61a235c716..ea1866102dcb 100644
--- a/devtools/startup/DevToolsStartup.jsm
+++ b/devtools/startup/DevToolsStartup.jsm
@@ -1432,46 +1432,6 @@ function getLoggedInUser() {
return user == "" ? null : user;
}
-function saveRecordingInDB(description) {
- let user;
-
- if (isRunningTest()) {
- user = {id: "77c6dc81-280d-4f28-971b-9915ab00f0f7"}
- } else {
- user = getLoggedInUser();
- }
-
- if (!user) {
- return;
- }
-
- const pageUrl = Services.prefs.getStringPref(
- "devtools.recordreplay.saveRecordingsUrl"
- );
-
- const body = {
- user_id: user.id,
- recording_id: description.recordingId,
- id: description.recordingId,
- url: description.url,
- title: description.title,
- duration: description.duration,
- last_screen_data: description.lastScreenData || "",
- last_screen_mime_type: description.lastScreenMimeType || "image/jpeg",
- };
-
- console.log(`>>> saving recording`, description.recordingId);
- return fetch(`${pageUrl}/api/create-recording`, {
- method: "post",
- body: JSON.stringify(body),
- headers: { "Content-Type": "application/json" },
- })
- .then(async (r) =>
- console.log(`succeeded in creating recording`, await r.json())
- )
- .catch((err) => console.error(err));
-}
-
async function saveRecordingUser(user) {
if (!user) {
Services.prefs.setStringPref("devtools.recordreplay.user", "");
@@ -1784,7 +1744,9 @@ function reloadAndRecordTab(gBrowser) {
}
// Don't preprocess recordings if we will be submitting them for testing.
- if (Services.prefs.getBoolPref("devtools.recordreplay.submitTestRecordings")) {
+ if (
+ Services.prefs.getBoolPref("devtools.recordreplay.submitTestRecordings")
+ ) {
env.set("RECORD_REPLAY_DONT_PROCESS_RECORDINGS", "1");
}
@@ -1867,7 +1829,9 @@ async function reloadAndStopRecordingTab(gBrowser) {
// When the submitTestRecordings pref is set we don't load the viewer,
// but show a simple page that the recording was submitted, to make things
// simpler for QA and provide feedback that the pref was set correctly.
- if (Services.prefs.getBoolPref("devtools.recordreplay.submitTestRecordings")) {
+ if (
+ Services.prefs.getBoolPref("devtools.recordreplay.submitTestRecordings")
+ ) {
const url = gBrowser.currentURI.spec;
fetch(`https://test-inbox.replay.io/${recordingId}:${url}`);
const why = `Test recording added: ${recordingId}`;
@@ -1882,9 +1846,6 @@ async function reloadAndStopRecordingTab(gBrowser) {
}
recordReplayLog(`FinishedRecording ${recordingId}`);
- if (isAuthenticationEnabled()) {
- await saveRecordingInDB(data);
- }
let viewHost = "https://replay.io";
@@ -1968,6 +1929,9 @@ function viewRecordings() {
function isAuthenticationEnabled() {
// Authentication is controlled by a preference but can be disabled by an
// environment variable.
- return Services.prefs.getBoolPref("devtools.recordreplay.authentication-enabled")
- && !env.get("RECORD_REPLAY_DISABLE_AUTHENTICATION");
+ return (
+ Services.prefs.getBoolPref(
+ "devtools.recordreplay.authentication-enabled"
+ ) && !env.get("RECORD_REPLAY_DISABLE_AUTHENTICATION")
+ );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment