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

refactor: 'focus-change' does not need guestInstanceId #29001

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 6 additions & 17 deletions lib/browser/guest-view-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,8 @@ handleMessageSync(IPC_MESSAGES.GUEST_VIEW_MANAGER_DETACH_GUEST, function (event,
});

// this message is sent by the actual <webview>
ipcMainInternal.on(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, function (event: ElectronInternal.IpcMainInternalEvent, focus: boolean, guestInstanceId: number) {
const guest = getGuest(guestInstanceId);
if (guest === event.sender) {
event.sender.emit('focus-change', {}, focus, guestInstanceId);
} else {
console.error(`focus-change for guestInstanceId: ${guestInstanceId}`);
}
ipcMainInternal.on(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, function (event: ElectronInternal.IpcMainInternalEvent, focus: boolean) {
event.sender.emit('-focus-change', {}, focus);
});

handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CALL, function (event, guestInstanceId: number, method: string, args: any[]) {
Expand Down Expand Up @@ -372,18 +367,12 @@ handleMessage(IPC_MESSAGES.GUEST_VIEW_MANAGER_CAPTURE_PAGE, async function (even

// Returns WebContents from its guest id hosted in given webContents.
const getGuestForWebContents = function (guestInstanceId: number, contents: Electron.WebContents) {
const guest = getGuest(guestInstanceId);
if (!guest) {
const guestInstance = guestInstances.get(guestInstanceId);
if (!guestInstance) {
throw new Error(`Invalid guestInstanceId: ${guestInstanceId}`);
}
if (guest.hostWebContents !== contents) {
if (guestInstance.guest.hostWebContents !== contents) {
throw new Error(`Access denied to guestInstanceId: ${guestInstanceId}`);
}
return guest;
};

// Returns WebContents from its guest id.
const getGuest = function (guestInstanceId: number) {
const guestInstance = guestInstances.get(guestInstanceId);
if (guestInstance != null) return guestInstance.guest;
return guestInstance.guest;
};
2 changes: 1 addition & 1 deletion lib/common/web-view-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const webViewEvents: Record<string, string[]> = {
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
'did-frame-navigate': ['url', 'httpResponseCode', 'httpStatusText', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'focus-change': ['focus', 'guestInstanceId'],
'-focus-change': ['focus'],
close: [],
crashed: [],
'render-process-gone': ['details'],
Expand Down
2 changes: 1 addition & 1 deletion lib/renderer/web-view/guest-view-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const dispatchEvent = function (

if (eventName === 'load-commit') {
webView.onLoadCommit(props);
} else if (eventName === 'focus-change') {
} else if (eventName === '-focus-change') {
webView.onFocusChange();
}
};
Expand Down
8 changes: 4 additions & 4 deletions lib/renderer/web-view/web-view-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';

const v8Util = process._linkedBinding('electron_common_v8_util');

function handleFocusBlur (guestInstanceId: number) {
function handleFocusBlur () {
// Note that while Chromium content APIs have observer for focus/blur, they
// unfortunately do not work for webview.

window.addEventListener('focus', () => {
ipcRendererInternal.send(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, true, guestInstanceId);
ipcRendererInternal.send(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, true);
});

window.addEventListener('blur', () => {
ipcRendererInternal.send(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, false, guestInstanceId);
ipcRendererInternal.send(IPC_MESSAGES.GUEST_VIEW_MANAGER_FOCUS_CHANGE, false);
});
}

Expand All @@ -32,6 +32,6 @@ export function webViewInit (

if (guestInstanceId) {
// Report focus/blur events of webview to browser.
handleFocusBlur(guestInstanceId);
handleFocusBlur();
}
}