-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7943 from vector-im/dbkr/electron_custom_protocol
Electron: Load app from custom protocol
- Loading branch information
Showing
12 changed files
with
544 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2018 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
const { BrowserWindow, ipcMain } = require('electron'); | ||
const path = require('path'); | ||
|
||
async function migrateFromOldOrigin() { | ||
console.log("Attempting to migrate data between origins"); | ||
|
||
// We can use the same preload script: we just need ipcRenderer exposed | ||
const preloadScript = path.normalize(`${__dirname}/preload.js`); | ||
await new Promise(resolve => { | ||
const migrateWindow = new BrowserWindow({ | ||
show: false, | ||
webPreferences: { | ||
preload: preloadScript, | ||
nodeIntegration: false, | ||
sandbox: true, | ||
enableRemoteModule: false, | ||
webgl: false, | ||
}, | ||
}); | ||
ipcMain.on('origin_migration_complete', (e, success, sentSummary, storedSummary) => { | ||
if (success) { | ||
console.log("Origin migration completed successfully!"); | ||
} else { | ||
console.error("Origin migration failed!"); | ||
} | ||
console.error("Data sent", sentSummary); | ||
console.error("Data stored", storedSummary); | ||
migrateWindow.close(); | ||
resolve(); | ||
}); | ||
ipcMain.on('origin_migration_nodata', (e) => { | ||
console.log("No session to migrate from old origin"); | ||
migrateWindow.close(); | ||
resolve(); | ||
}); | ||
// Normalise the path because in the distribution, __dirname will be inside the | ||
// electron asar. | ||
const sourcePagePath = path.normalize(__dirname + '/../../origin_migrator/source.html'); | ||
console.log("Loading path: " + sourcePagePath); | ||
migrateWindow.loadURL('file://' + sourcePagePath); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
migrateFromOldOrigin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<body> | ||
<script src="browser-matrix.min.js"></script> | ||
<script src="dest.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
Copyright 2018 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
const SOURCE_ORIGIN = 'file://'; | ||
|
||
const IndexedDBCryptoStore = window.matrixcs.IndexedDBCryptoStore; | ||
const cryptoStore = new IndexedDBCryptoStore(window.indexedDB, 'matrix-js-sdk:crypto'); | ||
|
||
let accountStored = 0; | ||
let sessionsStored = 0; | ||
let inboundGroupSessionsStored = 0; | ||
let deviceDataStored = 0; | ||
let roomsStored = 0; | ||
let localStorageKeysStored = 0; | ||
|
||
const promises = []; | ||
|
||
async function onMessage(e) { | ||
if (e.origin !== SOURCE_ORIGIN) return; | ||
|
||
const data = e.data.data; // bleh, naming clash | ||
switch (e.data.cmd) { | ||
case 'init': | ||
// start with clean stores before we migrate data in | ||
window.localStorage.clear(); | ||
await cryptoStore.deleteAllData(); | ||
|
||
e.source.postMessage({ | ||
cmd: 'initOK', | ||
}, SOURCE_ORIGIN); | ||
break; | ||
case 'storeAccount': | ||
promises.push(cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_ACCOUNT], | ||
(txn) => { | ||
cryptoStore.storeAccount(txn, data); | ||
}, | ||
).then(() => { | ||
++accountStored; | ||
})); | ||
break; | ||
case 'storeSessions': | ||
promises.push(cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_SESSIONS], | ||
(txn) => { | ||
for (const sess of data) { | ||
cryptoStore.storeEndToEndSession(sess.deviceKey, sess.sessionId, sess, txn); | ||
} | ||
}, | ||
).then(() => { | ||
sessionsStored += data.length; | ||
})); | ||
break; | ||
case 'storeInboundGroupSessions': | ||
promises.push(cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_INBOUND_GROUP_SESSIONS], | ||
(txn) => { | ||
for (const sess of data) { | ||
cryptoStore.addEndToEndInboundGroupSession( | ||
sess.senderKey, sess.sessionId, sess.sessionData, txn, | ||
); | ||
} | ||
}, | ||
).then(() => { | ||
inboundGroupSessionsStored += data.length; | ||
})); | ||
break; | ||
case 'storeDeviceData': | ||
promises.push(cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_DEVICE_DATA], | ||
(txn) => { | ||
cryptoStore.storeEndToEndDeviceData(data, txn); | ||
}, | ||
).then(() => { | ||
++deviceDataStored; | ||
})); | ||
break; | ||
case 'storeRooms': | ||
promises.push(cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_ROOMS], | ||
(txn) => { | ||
for (const [roomId, roomInfo] of Object.entries(data)) { | ||
cryptoStore.storeEndToEndRoom(roomId, roomInfo, txn); | ||
} | ||
}, | ||
).then(() => { | ||
++roomsStored; | ||
})); | ||
break; | ||
case 'storeLocalStorage': | ||
window.localStorage.setItem(data.key, data.val); | ||
++localStorageKeysStored; | ||
break; | ||
case 'getSummary': | ||
await Promise.all(promises); | ||
e.source.postMessage({ | ||
cmd: 'summary', | ||
data: { | ||
accountStored, | ||
sessionsStored, | ||
inboundGroupSessionsStored, | ||
deviceDataStored, | ||
roomsStored, | ||
localStorageKeysStored, | ||
}, | ||
}, SOURCE_ORIGIN); | ||
break; | ||
} | ||
} | ||
|
||
window.addEventListener('message', onMessage); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<html> | ||
<body> | ||
<script src="dest/browser-matrix.min.js"></script> | ||
<script src="source.js"></script> | ||
<iframe name="dest" src="vector://vector/origin_migrator_dest/dest.html" onload="doMigrate()"></iframe> | ||
</body> | ||
</html> |
Oops, something went wrong.