Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: Attempt local execution on mobile #3085

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import type { ExecutionServiceArgs } from '../AbstractExecutionService';
import { ProxyExecutionService } from '../proxy/ProxyExecutionService';
import type { TerminateJobArgs } from '../AbstractExecutionService';
import {
AbstractExecutionService,
type ExecutionServiceArgs,
} from '../AbstractExecutionService';
import type { WebViewInterface } from './WebViewMessageStream';
import { WebViewMessageStream } from './WebViewMessageStream';

export type WebViewExecutionServiceArgs = ExecutionServiceArgs & {
getWebView: () => Promise<WebViewInterface>;
createWebView: (jobId: string) => Promise<WebViewInterface>;
removeWebView: (jobId: string) => void;
};

export class WebViewExecutionService extends ProxyExecutionService {
#getWebView;
export class WebViewExecutionService extends AbstractExecutionService<string> {
#createWebView;

#removeWebView;

constructor({
messenger,
setupSnapProvider,
getWebView,
createWebView,
removeWebView,
}: WebViewExecutionServiceArgs) {
super({
messenger,
setupSnapProvider,
stream: new WebViewMessageStream({
name: 'parent',
target: 'child',
getWebView,
}),
});
this.#getWebView = getWebView;
this.#createWebView = createWebView;
this.#removeWebView = removeWebView;
}

/**
Expand All @@ -35,16 +38,18 @@ export class WebViewExecutionService extends ProxyExecutionService {
* @returns An object with the worker ID and stream.
*/
protected async initEnvStream(jobId: string) {
// Ensure that the WebView has been loaded before we proceed.
await this.#ensureWebViewLoaded();
const webView = await this.#createWebView(jobId);

const stream = new WebViewMessageStream({
name: 'parent',
target: 'child',
getWebView: async () => webView,
});

return super.initEnvStream(jobId);
return { worker: jobId, stream };
}

/**
* Ensure that the WebView has been loaded by awaiting the getWebView promise.
*/
async #ensureWebViewLoaded() {
await this.#getWebView();
protected terminateJob(jobWrapper: TerminateJobArgs<string>): void {
this.#removeWebView(jobWrapper.id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"resources": {
"@metamask/post-message-stream": {
"globals": {
"MessageEvent.prototype": true,
"WorkerGlobalScope": true,
"addEventListener": true,
"browser": true,
"chrome": true,
"location.origin": true,
"postMessage": true,
"removeEventListener": true
},
"packages": {
"@metamask/utils": true,
"readable-stream": true
}
},
"@metamask/rpc-errors": {
"packages": {
"@metamask/rpc-errors>fast-safe-stringify": true,
"@metamask/utils": true
}
},
"@metamask/snaps-sdk": {
"globals": {
"URL": true,
"fetch": true
},
"packages": {
"@metamask/rpc-errors": true,
"@metamask/superstruct": true,
"@metamask/utils": true
}
},
"@metamask/snaps-utils": {
"globals": {
"URL": true,
"console.error": true,
"console.log": true,
"console.warn": true,
"document.body.appendChild": true,
"document.createElement": true
},
"packages": {
"@metamask/rpc-errors": true,
"@metamask/snaps-sdk": true,
"@metamask/superstruct": true,
"@metamask/utils": true
}
},
"@metamask/utils": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
},
"packages": {
"@metamask/superstruct": true,
"@metamask/utils>@noble/hashes": true,
"@metamask/utils>@scure/base": true,
"@metamask/utils>pony-cause": true,
"browserify>buffer": true,
"depcheck>semver": true,
"eslint>debug": true
}
},
"@metamask/utils>@noble/hashes": {
"globals": {
"TextEncoder": true,
"crypto": true
}
},
"@metamask/utils>@scure/base": {
"globals": {
"TextDecoder": true,
"TextEncoder": true
}
},
"browserify>browser-pack>safe-buffer": {
"packages": {
"browserify>buffer": true
}
},
"browserify>buffer": {
"globals": {
"console": true
},
"packages": {
"browserify>buffer>base64-js": true,
"browserify>buffer>ieee754": true
}
},
"browserify>events": {
"globals": {
"console": true
}
},
"browserify>process": {
"globals": {
"clearTimeout": true,
"setTimeout": true
}
},
"browserify>string_decoder": {
"packages": {
"browserify>browser-pack>safe-buffer": true
}
},
"depcheck>semver": {
"globals": {
"console.error": true
},
"packages": {
"browserify>process": true
}
},
"eslint>debug": {
"globals": {
"console": true,
"document": true,
"localStorage": true,
"navigator": true,
"process": true
},
"packages": {
"browserify>process": true,
"eslint>debug>ms": true
}
},
"readable-stream": {
"packages": {
"browserify>browser-resolve": true,
"browserify>buffer": true,
"browserify>events": true,
"browserify>inherits": true,
"browserify>process": true,
"browserify>string_decoder": true,
"readable-stream>util-deprecate": true
}
},
"readable-stream>util-deprecate": {
"globals": {
"console.trace": true,
"console.warn": true,
"localStorage": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
{
"resources": {
"@metamask/json-rpc-engine": {
"packages": {
"@metamask/providers>@metamask/safe-event-emitter": true,
"@metamask/rpc-errors": true,
"@metamask/utils": true
}
},
"@metamask/object-multiplex": {
"globals": {
"console.warn": true
},
"packages": {
"@metamask/object-multiplex>once": true,
"readable-stream": true
}
},
"@metamask/object-multiplex>once": {
"packages": {
"@metamask/object-multiplex>once>wrappy": true
}
},
"@metamask/post-message-stream": {
"globals": {
"MessageEvent.prototype": true,
Expand All @@ -16,6 +37,39 @@
"readable-stream": true
}
},
"@metamask/providers": {
"globals": {
"console": true
},
"packages": {
"@metamask/json-rpc-engine": true,
"@metamask/providers>@metamask/json-rpc-middleware-stream": true,
"@metamask/providers>@metamask/safe-event-emitter": true,
"@metamask/providers>is-stream": true,
"@metamask/rpc-errors": true,
"eslint>fast-deep-equal": true,
"readable-stream": true
}
},
"@metamask/providers>@metamask/json-rpc-middleware-stream": {
"globals": {
"console.warn": true,
"setTimeout": true
},
"packages": {
"@metamask/providers>@metamask/safe-event-emitter": true,
"@metamask/utils": true,
"readable-stream": true
}
},
"@metamask/providers>@metamask/safe-event-emitter": {
"globals": {
"setTimeout": true
},
"packages": {
"browserify>events": true
}
},
"@metamask/rpc-errors": {
"packages": {
"@metamask/rpc-errors>fast-safe-stringify": true,
Expand Down
5 changes: 5 additions & 0 deletions packages/snaps-execution-environments/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const ENTRY_POINTS = {
entryPoint: './src/webworker/pool/index.ts',
html: true,
},
'webview-proxy': {
entryPoint: './src/webview/proxy.ts',
html: true,
inlineBundle: true,
},
webview: {
entryPoint: './src/webview/index.ts',
html: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-execution-environments/src/webview/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { executeLockdownEvents } from '../common/lockdown/lockdown-events';
import { executeLockdownMore } from '../common/lockdown/lockdown-more';
import { ProxySnapExecutor } from '../proxy/ProxySnapExecutor';
import { IFrameSnapExecutor } from '../iframe/IFrameSnapExecutor';
import { WebViewExecutorStream } from './WebViewExecutorStream';

// Lockdown is already applied in LavaMoat
Expand All @@ -13,4 +13,4 @@ const parentStream = new WebViewExecutorStream({
targetWindow: window.ReactNativeWebView,
});

ProxySnapExecutor.initialize(parentStream);
IFrameSnapExecutor.initialize(parentStream);
16 changes: 16 additions & 0 deletions packages/snaps-execution-environments/src/webview/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { executeLockdownEvents } from '../common/lockdown/lockdown-events';
import { executeLockdownMore } from '../common/lockdown/lockdown-more';
import { ProxySnapExecutor } from '../proxy/ProxySnapExecutor';
import { WebViewExecutorStream } from './WebViewExecutorStream';

// Lockdown is already applied in LavaMoat
executeLockdownMore();
executeLockdownEvents();

const parentStream = new WebViewExecutorStream({
name: 'child', // webview
target: 'parent', // rnside
targetWindow: window.ReactNativeWebView,
});

ProxySnapExecutor.initialize(parentStream);
Loading