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

feat: add 'dom-ready' event to WebFrameMain #29290

Merged
merged 5 commits into from Sep 1, 2021

Conversation

samuelmaddock
Copy link
Member

@samuelmaddock samuelmaddock commented May 22, 2021

Description of Change

resolves #27344

WebContents now emits a 'frame-dom-ready' event when either the top-level frame or a sub frame's document is ready.

WebFrameMain now emits a 'dom-ready' event when the frame's document is ready.

Checklist

Release Notes

Notes:

  • Added 'dom-ready' event to WebFrameMain which emits when the frame's document is ready.
  • Added 'frame-created' event to WebContents which emits when a frame is created in the page.

@MarshallOfSound
Copy link
Member

Should this be a dom-ready event on the WebFrameMain class instead? Seems frame specific things should go their now we have it

@samuelmaddock
Copy link
Member Author

Should this be a dom-ready event on the WebFrameMain class instead? Seems frame specific things should go their now we have it

I see frame discovery as a potential limitation of that approach. Currently we don't have enough events provided to know the full lifecycle of a frame. There's no frame-created or destroyed events yet which might be needed first.

Adding frame-dom-ready is admittedly a convenience event for frames not necessarily being tracked. Without lifecycle events for frames, this is the easiest approach. Do you see many downsides to this event? WebContents event bloat?

Also, as an aside, does dom-ready-in-frame read better? 🤔

Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API looks good to me.

@samuelmaddock
Copy link
Member Author

@MarshallOfSound Here's a proposal for how I could see providing equivalent functionality with events moved into the frame itself.

interface WebContents {
  /** Emitted when an iframe is created within the page. */
  on(event: 'frame-created', listener: (event: Event, frame: WebFrameMain) => void): this;
}

interface WebFrameMain {
  /** Emitted when the DOM has finished loading. */
  on(event: 'dom-ready', listener: (event: Event) => void): this;
}

Example usage:

const browserWindow = new BrowserWindow()

browserWindow.webContents.on('frame-created', (e, frame) => {
  frame.on('dom-ready', () => {})
})

I'd be fine making these changes if the proposal looks okay.

@MarshallOfSound
Copy link
Member

My concern is with on(event: 'frame-created', listener: (event: Event, frame: WebFrameMain) => void): this; is the slight overhead of creating a new WebFrameMain instance for every frame. Seems like a price that every app will pay even though 99% of apps won't use this API.

I've had a thing on the backlog for a while to not emit events that don't have listeners (to avoid paying gin converter costs) but without that work this would contain an annoying memory / performance hit for folks with lots of frames

@samuelmaddock
Copy link
Member Author

That's a fair point, and likely pushes the API into a better design overall. How would you feel about a details object with the IDs to lookup the frame?

interface FrameCreatedDetails {
  processId: number;
  routingId: number;
  /** When we eventually feel more comfortable, we can extend the details to include the actual frame. */
  // frame: Frame;
}

interface WebContents {
  /** Emitted when an iframe is created within the page. */
  on(event: 'frame-created', listener: (event: Event, details: FrameCreatedDetails) => void): this;
}

interface WebFrameMain {
  /** Emitted when the DOM has finished loading. */
  on(event: 'dom-ready', listener: (event: Event) => void): this;
}
const browserWindow = new BrowserWindow()

browserWindow.webContents.on('frame-created', (e, details) => {
  const frame = webFrameMain.fromId(details.processId, details.routingId)
  frame.on('dom-ready', () => {})
})

@MarshallOfSound
Copy link
Member

That seems more reasonable although slightly less easy to use for folks that do really want that frame 🤷 I think it's the better side of the trade off though

@samuelmaddock
Copy link
Member Author

That seems more reasonable although slightly less easy to use for folks that do really want that frame 🤷 I think it's the better side of the trade off though

Do we have precedence for events that use getters? e.g.

const details: FrameCreatedDetails = {
  get frame(): WebFrameMain {
    // lazily performs gin conversion
  }
}

I'm not familiar with how this could be done with v8, but I believe Chrome does this for a few of their globals such as chrome.*.

@MarshallOfSound
Copy link
Member

Do we have precedence for events that use getters? e.g.

No but doing this lazily sounds like a nice middle ground

@samuelmaddock samuelmaddock changed the title feat: add 'frame-dom-ready' event to WebContents [WIP] feat: add 'frame-dom-ready' event to WebContents May 26, 2021
@samuelmaddock samuelmaddock changed the title [WIP] feat: add 'frame-dom-ready' event to WebContents feat: add 'frame-dom-ready' event to WebContents May 27, 2021
@samuelmaddock samuelmaddock changed the title feat: add 'frame-dom-ready' event to WebContents feat: add 'dom-ready' event to WebFrameMain May 27, 2021
@samuelmaddock
Copy link
Member Author

@MarshallOfSound @zcbenz @miniak The contents of this PR has changed to reflect the discussion above.

Instead of emitting a 'frame-dom-ready' event from WebContents, 'dom-ready' is now emitted from WebFrameMain itself. To listen for newly created frames, 'frame-created' is now emitted from WebContents.

@miniak
Copy link
Contributor

miniak commented May 28, 2021

@samuelmaddock does it make sense to also have a destroyed event on the frame?

docs/api/web-frame-main.md Outdated Show resolved Hide resolved
shell/browser/api/electron_api_web_contents.cc Outdated Show resolved Hide resolved
@nornagon
Copy link
Member

What's the status of this PR @samuelmaddock ?

@samuelmaddock
Copy link
Member Author

What's the status of this PR @samuelmaddock ?

It's now unblocked by #30076 so I'm planning to rebase and retest sometime this week hopefully.

@samuelmaddock
Copy link
Member Author

This PR is ready for review again. WebContent's frame-created event is now emitted correctly as defined in the added docs.

docs/api/web-frame-main.md Outdated Show resolved Hide resolved
@nornagon
Copy link
Member

API LGTM.

fix: test failing possibly caused by lazy loaded iframes?

refactor: move frame-dom-ready event into WebFrameMain

test: new WebFrameMain dom-ready event

refactor: return bool

refactor: add gin_helper::AccessorValue for gin accessor conversion

refactor: call methods directly from electron:WebContents

Writing static method boilerplate isn't fun

feat: add WebFrameMain 'destroyed' event

Update shell/common/gin_converters/frame_converter.cc

Co-authored-by: Jeremy Rose <nornagon@nornagon.net>

refactor: use persistent handle for rfh obj template

docs: when the WebFrameMain 'destroyed' event can occur

refactor: use dcheck and 'As' instead of 'Cast'

chore: add type checking to internal fields

refactor: add conditional for internal field count

refactor: removed 'destroyed event and isDestroyed()

This may be revisited at a later time.

test: frame-created event
|old_host| may be nullptr if the previous RFH was shutdown.
@nornagon
Copy link
Member

nornagon commented Sep 1, 2021

Build failures are unrelated. This has two API LGTMs, one from @zcbenz and one from myself, so I'm going to go ahead and merge.

@nornagon nornagon merged commit 4d89174 into electron:main Sep 1, 2021
@release-clerk
Copy link

release-clerk bot commented Sep 1, 2021

Release Notes Persisted

  • Added 'dom-ready' event to WebFrameMain which emits when the frame's document is ready.
  • Added 'frame-created' event to WebContents which emits when a frame is created in the page.

@trop
Copy link
Contributor

trop bot commented Sep 1, 2021

I have automatically backported this PR to "15-x-y", please check out #30801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review/requested 🗳 semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Emit webContents 'dom-ready' event for sub frames
6 participants