mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
put background in service worker
This commit is contained in:
parent
e602cb13c5
commit
9f3445252c
@ -1,3 +1,4 @@
|
||||
/*
|
||||
const urlUtil = require('url')
|
||||
const extend = require('xtend')
|
||||
const Dnode = require('dnode')
|
||||
@ -157,3 +158,24 @@ function initializeZeroClient() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
const SWcontroller = require('./sw-controller')
|
||||
console.log('outside:open')
|
||||
const background = new SWcontroller({
|
||||
fileName: 'sw-build.js',
|
||||
registerOpts: {
|
||||
scope: './',
|
||||
}
|
||||
})
|
||||
|
||||
background.startWorker()
|
||||
.then(registerdWorker => {
|
||||
return background.sendMessage('connect')
|
||||
})
|
||||
.then((port) => {
|
||||
debugger
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(`SW Controller: ${err}`)
|
||||
})
|
||||
|
96
library/controllers/index-db-controller.js
Normal file
96
library/controllers/index-db-controller.js
Normal file
@ -0,0 +1,96 @@
|
||||
const EventEmitter = require('events')
|
||||
module.exports = class IndexDbController extends EventEmitter {
|
||||
|
||||
constructor (opts) {
|
||||
super()
|
||||
this.migrations = opts.migrations
|
||||
this.key = opts.key
|
||||
this.dbObject = opts.global.indexedDB
|
||||
this.IDBTransaction = opts.global.IDBTransaction || opts.global.webkitIDBTransaction || opts.global.msIDBTransaction || {READ_WRITE: "readwrite"}; // This line should only be needed if it is needed to support the object's constants for older browsers
|
||||
this.IDBKeyRange = opts.global.IDBKeyRange || opts.global.webkitIDBKeyRange || opts.global.msIDBKeyRange;
|
||||
this.version = opts.version
|
||||
this.logging = opts.logging
|
||||
this.initialState = opts.initialState
|
||||
if (this.logging) this.on('log', logger)
|
||||
}
|
||||
|
||||
// Opens the database connection and returns a promise
|
||||
open (version = this.version) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dbOpenRequest = this.dbObject.open(this.key, version)
|
||||
dbOpenRequest.onerror = (event) => {
|
||||
return reject(event)
|
||||
}
|
||||
dbOpenRequest.onsuccess = (event) => {
|
||||
this.db = dbOpenRequest.result
|
||||
if (!this.db.objectStoreNames.length) {
|
||||
Object.keys(this.initialState).forEach((key) => {
|
||||
this._add(key, this.initialState[key])
|
||||
})
|
||||
}
|
||||
this.emit('success')
|
||||
resolve(this.db)
|
||||
}
|
||||
dbOpenRequest.onupgradeneeded = (event) => {
|
||||
// if (this.migrators)
|
||||
this.db = event.target.result
|
||||
this.migrate()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
requestObjectStore (key, type = 'readonly') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dbReadWrite = this.db.transaction(key, type)
|
||||
const dataStore = dbReadWrite.objectStore(key)
|
||||
resolve(dataStore)
|
||||
})
|
||||
}
|
||||
|
||||
get (key) {
|
||||
return this.requestObjectStore(key)
|
||||
.then((dataObject)=> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const getRequest = dataObject.get(key)
|
||||
getRequest.onsuccess = (event) => resolve(event.currentTarget.result)
|
||||
getRequest.onerror = (event) => reject(event)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
put (key, store) {
|
||||
return this.requestObjectStore(key, 'readwrite')
|
||||
.then((dataObject)=> {
|
||||
const putRequest = dataObject.put(store)
|
||||
putRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result)
|
||||
putRequest.onerror = (event) => Promise.reject(event)
|
||||
})
|
||||
}
|
||||
|
||||
update (key, value) {
|
||||
|
||||
}
|
||||
|
||||
migrate () {
|
||||
// this.migrations
|
||||
|
||||
// Place holder for future migrations eg:
|
||||
this.db.deleteObjectStore('meta')
|
||||
this.db.deleteObjectStore('data')
|
||||
this.db.createObjectStore('dataStore')
|
||||
}
|
||||
|
||||
_add (key, objStore, cb = logger) {
|
||||
return this.requestObjectStore(key, 'readwrite')
|
||||
.then((dataObject)=> {
|
||||
const addRequest = dataObject.add(objStore, key)
|
||||
addRequest.onsuccess = (event) => Promise.resolve(event.currentTarget.result)
|
||||
addRequest.onerror = (event) => Promise.reject(event)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function logger (err, ress) {
|
||||
err ? console.error(`Logger says: ${err}`) : console.dir(`Logger says: ${ress}`)
|
||||
}
|
@ -26,7 +26,7 @@ var shouldPop = false
|
||||
window.addEventListener('click', function(){
|
||||
if (!shouldPop) return
|
||||
shouldPop = false
|
||||
window.open('http://127.0.0.1:9001/popup/popup.html', '', 'width=360 height=500')
|
||||
window.open('http://localhost:9001/popup/popup.html', '', 'width=360 height=500')
|
||||
console.log('opening window...')
|
||||
})
|
||||
|
||||
|
@ -11,7 +11,7 @@ var name = 'popup'
|
||||
window.METAMASK_UI_TYPE = name
|
||||
|
||||
var iframeStream = setupIframe({
|
||||
zeroClientProvider: 'http://127.0.0.1:9001',
|
||||
zeroClientProvider: 'http://localhost:9001',
|
||||
sandboxAttributes: ['allow-scripts', 'allow-popups', 'allow-same-origin'],
|
||||
container: document.body,
|
||||
})
|
||||
|
@ -7,6 +7,7 @@ const zeroBundle = createBundle('./index.js')
|
||||
const controllerBundle = createBundle('./controller.js')
|
||||
const popupBundle = createBundle('./popup.js')
|
||||
const appBundle = createBundle('./example/index.js')
|
||||
const swBuild = createBundle('./sw-core.js')
|
||||
|
||||
//
|
||||
// Iframe Server
|
||||
@ -24,6 +25,11 @@ iframeServer.use('/popup', express.static('../dist/chrome'))
|
||||
iframeServer.get('/controller.js', function(req, res){
|
||||
res.send(controllerBundle.latest)
|
||||
})
|
||||
iframeServer.get('/sw-build.js', function(req, res){
|
||||
console.log('/sw-build.js')
|
||||
res.setHeader('Content-Type', 'application/javascript')
|
||||
res.send(swBuild.latest)
|
||||
})
|
||||
|
||||
// serve background controller
|
||||
iframeServer.use(express.static('./server'))
|
||||
|
49
library/sw-controller.js
Normal file
49
library/sw-controller.js
Normal file
@ -0,0 +1,49 @@
|
||||
const EventEmitter = require('events')
|
||||
|
||||
module.exports = class serviceWorkerController extends EventEmitter{
|
||||
constructor (opts) {
|
||||
super()
|
||||
this.fileName = opts.fileName
|
||||
this.registerOpts = opts.registerOpts
|
||||
this.serviceWorker = navigator.serviceWorker
|
||||
}
|
||||
|
||||
|
||||
startWorker () {
|
||||
// check to see if their is a preregistered service worker
|
||||
if (!this.serviceWorker.controller) {
|
||||
return Promise.resolve(this.registerWorker())
|
||||
} else {
|
||||
return Promise.resolve(this.serviceWorker.ready)
|
||||
}
|
||||
}
|
||||
|
||||
registerWorker () {
|
||||
return this.serviceWorker.register(this.fileName, this.registerOpts)
|
||||
.then(sw => {
|
||||
return sw
|
||||
})
|
||||
}
|
||||
|
||||
syncSW (registeredSW) {
|
||||
return registeredSW.sync.register('sync')
|
||||
.then(() => {
|
||||
console.log('sync')
|
||||
})
|
||||
}
|
||||
|
||||
sendMessage (message) {
|
||||
const self = this
|
||||
return new Promise((resolve, reject) => {
|
||||
var messageChannel = new MessageChannel()
|
||||
messageChannel.port1.onmessage = (event) => {
|
||||
if (event.data.err) {
|
||||
reject(event.data.error)
|
||||
} else {
|
||||
resolve(event.data.data)
|
||||
}
|
||||
}
|
||||
self.serviceWorker.controller.postMessage(message, [messageChannel.port2])
|
||||
})
|
||||
}
|
||||
}
|
203
library/sw-core.js
Normal file
203
library/sw-core.js
Normal file
@ -0,0 +1,203 @@
|
||||
global.window = global
|
||||
const SWGlobal = self
|
||||
const urlUtil = require('url')
|
||||
const endOfStream = require('end-of-stream')
|
||||
const asyncQ = require('async-q')
|
||||
const pipe = require('pump')
|
||||
// const ParentStream = require('iframe-stream').ParentStream
|
||||
const setupMultiplex = require('../app/scripts/lib/stream-utils.js').setupMultiplex
|
||||
const PortStream = require('../app/scripts/lib/port-stream.js')
|
||||
// const notification = require('../app/scripts/lib/notifications.js')
|
||||
|
||||
const DbController = require('./controllers/index-db-controller')
|
||||
|
||||
// // all this will go in service worker
|
||||
const MetamaskController = require('../app/scripts/metamask-controller')
|
||||
// const extension = require('../app/scripts/lib/extension')
|
||||
// const LocalStorageStore = require('obs-store/lib/localStorage')
|
||||
const storeTransform = require('obs-store/lib/transform')
|
||||
const Migrator = require('../app/scripts/lib/migrator/')
|
||||
const migrations = require('../app/scripts/migrations/')
|
||||
const firstTimeState = require('../app/scripts/first-time-state')
|
||||
|
||||
const STORAGE_KEY = 'metamask-config'
|
||||
const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG'
|
||||
let popupIsOpen = false
|
||||
|
||||
self.addEventListener('install', function(event) {
|
||||
event.waitUntil(self.skipWaiting())
|
||||
})
|
||||
self.addEventListener('activate', function(event) {
|
||||
event.waitUntil(self.clients.claim())
|
||||
})
|
||||
|
||||
self.onsync = function (syncEvent) {
|
||||
// What is done when a sync even is fired
|
||||
console.log('inside:sync')
|
||||
var focused
|
||||
self.clients.matchAll()
|
||||
.then(clients => {
|
||||
clients.forEach(function(client) {
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
console.log('inside:open')
|
||||
|
||||
|
||||
// // state persistence
|
||||
let diskStore
|
||||
const dbController = new DbController({
|
||||
key: STORAGE_KEY,
|
||||
global: self,
|
||||
version: 6,
|
||||
initialState: {
|
||||
dataStore: {
|
||||
meta: 2,
|
||||
data: firstTimeState,
|
||||
},
|
||||
},
|
||||
})
|
||||
asyncQ.waterfall([
|
||||
() => loadStateFromPersistence(),
|
||||
(initState) => setupController(initState),
|
||||
])
|
||||
.then(() => console.log('MetaMask initialization complete.'))
|
||||
.catch((err) => {
|
||||
console.log('WHILE SETTING UP:')
|
||||
console.error(err)
|
||||
})
|
||||
|
||||
// initialization flow
|
||||
|
||||
//
|
||||
// State and Persistence
|
||||
//
|
||||
function loadStateFromPersistence() {
|
||||
// migrations
|
||||
let migrator = new Migrator({ migrations })
|
||||
const initialState = migrator.generateInitialState(firstTimeState)
|
||||
dbController.initialState = initialState
|
||||
return dbController.open()
|
||||
.then((stuff) => {
|
||||
return dbController.get('dataStore')
|
||||
})
|
||||
.then((data) => {
|
||||
if (!data) {
|
||||
return dbController._add('dataStore', initialState)
|
||||
.then(() => dbController.get('dataStore'))
|
||||
.then((versionedData) => Promise.resolve(versionedData.data))
|
||||
}
|
||||
|
||||
return Promise.resolve(data.data)
|
||||
})
|
||||
.catch((err) => console.error(err))
|
||||
|
||||
// return asyncQ.waterfall([
|
||||
// // read from disk
|
||||
// () => Promise.resolve(diskStore || initialState),
|
||||
// // migrate data
|
||||
// (versionedData) => migrator.migrateData(versionedData),
|
||||
// // write to disk
|
||||
// (versionedData) => {
|
||||
// diskStore.put(versionedData)
|
||||
// return Promise.resolve(versionedData)
|
||||
// },
|
||||
// // resolve to just data
|
||||
// (versionedData) => Promise.resolve(versionedData.data),
|
||||
// ])
|
||||
}
|
||||
|
||||
function setupController (initState, client) {
|
||||
|
||||
//
|
||||
// MetaMask Controller
|
||||
//
|
||||
|
||||
const controller = new MetamaskController({
|
||||
// User confirmation callbacks:
|
||||
showUnconfirmedMessage: triggerUi,
|
||||
unlockAccountMessage: triggerUi,
|
||||
showUnapprovedTx: triggerUi,
|
||||
// initial state
|
||||
initState,
|
||||
})
|
||||
global.metamaskController = controller
|
||||
|
||||
// setup state persistence
|
||||
pipe(
|
||||
controller.store,
|
||||
storeTransform(versionifyData),
|
||||
diskStore
|
||||
)
|
||||
|
||||
function versionifyData(state) {
|
||||
let versionedData = diskStore.getState()
|
||||
versionedData.data = state
|
||||
return versionedData
|
||||
}
|
||||
|
||||
//
|
||||
// connect to other contexts
|
||||
//
|
||||
var connectionStream //= new ParentStream()
|
||||
SWGlobal.onmessage = (message) => {
|
||||
|
||||
debugger
|
||||
connectRemote(connectionStream, message.origin)
|
||||
}
|
||||
|
||||
connectRemote(connectionStream, client.origin)
|
||||
|
||||
function connectRemote (connectionStream, originDomain) {
|
||||
var isMetaMaskInternalProcess = (originDomain === 'http://localhost:9001')
|
||||
if (isMetaMaskInternalProcess) {
|
||||
// communication with popup
|
||||
controller.setupTrustedCommunication(connectionStream, 'MetaMask')
|
||||
} else {
|
||||
// communication with page
|
||||
setupUntrustedCommunication(connectionStream, originDomain)
|
||||
}
|
||||
}
|
||||
|
||||
function setupUntrustedCommunication (connectionStream, originDomain) {
|
||||
// setup multiplexing
|
||||
var mx = setupMultiplex(connectionStream)
|
||||
// connect features
|
||||
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
|
||||
controller.setupPublicConfig(mx.createStream('publicConfig'))
|
||||
}
|
||||
|
||||
function setupTrustedCommunication (connectionStream, originDomain) {
|
||||
// setup multiplexing
|
||||
var mx = setupMultiplex(connectionStream)
|
||||
// connect features
|
||||
controller.setupProviderConnection(mx.createStream('provider'), originDomain)
|
||||
}
|
||||
//
|
||||
// User Interface setup
|
||||
//
|
||||
return Promise.resolve()
|
||||
|
||||
}
|
||||
|
||||
// // //
|
||||
// // // Etc...
|
||||
// // //
|
||||
|
||||
// // // popup trigger
|
||||
function triggerUi () {
|
||||
if (!popupIsOpen) notification.show()
|
||||
}
|
||||
|
||||
// function getParentHref(){
|
||||
// try {
|
||||
// var parentLocation = window.parent.location
|
||||
// return parentLocation.hostname + ':' + parentLocation.port
|
||||
// } catch (err) {
|
||||
// return 'unknown'
|
||||
// }
|
||||
// }
|
Loading…
Reference in New Issue
Block a user