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

create and navigate.browsingContext #55

Merged
merged 9 commits into from
Nov 2, 2021
Merged

create and navigate.browsingContext #55

merged 9 commits into from
Nov 2, 2021

Conversation

sadym-chromium
Copy link
Collaborator

@sadym-chromium sadym-chromium commented Oct 5, 2021

  • Implement browsingContext.navigate command.
  • Add browsingContext.create -> params.type.
  • Add unit test for different event orders in browsingContext.create.
  • Made EVALUATOR_SCRIPT a property to allow unit testing.
  • BrowsingContextProcessor is responsible for handling events for testability.

@sadym-chromium sadym-chromium changed the title Navigate browsingContext.navigate Oct 5, 2021
@sadym-chromium sadym-chromium marked this pull request as draft October 6, 2021 20:12
@sadym-chromium sadym-chromium force-pushed the navigate branch 3 times, most recently from f7700df to 89609d2 Compare October 7, 2021 14:40
@sadym-chromium sadym-chromium changed the base branch from main to typification October 7, 2021 14:40
@sadym-chromium sadym-chromium changed the title browsingContext.navigate browsingContext creation and navigation Oct 7, 2021
@sadym-chromium sadym-chromium marked this pull request as ready for review October 7, 2021 21:39
@sadym-chromium sadym-chromium changed the title browsingContext creation and navigation create and navigate.browsingContext Oct 8, 2021
@sadym-chromium sadym-chromium force-pushed the navigate branch 2 times, most recently from ba79ff1 to 4c0cf3d Compare October 8, 2021 09:56
@sadym-chromium sadym-chromium force-pushed the typification branch 3 times, most recently from 896600f to 1c08cbe Compare October 11, 2021 10:54
@sadym-chromium sadym-chromium force-pushed the navigate branch 2 times, most recently from 1be66dd to c1c3e1c Compare October 11, 2021 11:05
Base automatically changed from typification to main October 13, 2021 11:09
Copy link
Member

@foolip foolip left a comment

Choose a reason for hiding this comment

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

I've reviewed about half way through, sending the comments I have before I head into a meeting.

src/bidiMapper/bidiProtocolTypes.ts Outdated Show resolved Hide resolved
EVALUATOR_SCRIPT
);

// Actual `Context.create` logic involves several CDP calls, so mock it to avoid all the simulations.
Copy link
Member

Choose a reason for hiding this comment

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

This is a helpful comment, thanks!

src/bidiMapper/domains/context/browsingContextProcessor.ts Outdated Show resolved Hide resolved
browsingContextProcessor = new BrowsingContextProcessor(
cdpConnection,
'SELF_TARGET_ID',
sinon.fake as unknown as (t: Context) => Promise<void>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you wrap the sinon.fake as unknown as (t: Context) => Promise<void> code into a typedFake function similar to typedSpy?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, shouldn't this call fake (e.g. fake())?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed


const existingContext = await this._tryGetContext(targetId);
if (existingContext) {
resolve(existingContext.toBidi());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think awaiting here might introduce a potential race condition. Once Target.createTarget returns, we need to register the onAttachToTarget handler synchronously, or there's a chance the event will come and we'll miss it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agree. Added synchronous check, if the context is already known, in which case attachedToTarget is not needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I understand how the latest change will solve the race condition. I see that line 172 is checking for a known contextId. But, we're creating a new blank CDP target on line 165, so won't we always have a new unknown contextId?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If the event came at the moment of line 172, it means it was handled by _handleAttachedToTargetEvent in line 54. If not, the code will go synchronously till line 195, where event handler is attached. Having the second handler in line 195 is needed just to resolve the command promise.

It looks confusing. Another option is to keep a single event handler in this class BrowsingContextProcessor in line 54. But in this case, we have to notify the process_PROTO_browsingContext_create command when the context is actually attached and initialized.

The first (current) approach allows to use built-in events queue like browserCdpClient.Target.on. The second approach requires introducing our internal event queue and subscription mechanism.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you, that makes more sense. I think this is safe then. Even though await this._tryGetContext(targetId) will yield to the runtime, any attachedToTarget event should be handled by _handleAttachedToTargetEvent and the context will be put in the map before _tryGetContext actually executes.

@@ -80,6 +87,23 @@ export class Context {
};
}

public async navigate(
url: string,
wait: BrowsingContext.ReadinessState = 'none'
Copy link
Collaborator

Choose a reason for hiding this comment

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

A default parameter makes sense at the API level, but for clarity, I'd prefer to avoid them in internal/implementation classes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

* Avoid race condition in waiting for a new target created;
* Minor changes.

const existingContext = await this._tryGetContext(targetId);
if (existingContext) {
resolve(existingContext.toBidi());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I understand how the latest change will solve the race condition. I see that line 172 is checking for a known contextId. But, we're creating a new blank CDP target on line 165, so won't we always have a new unknown contextId?

@sadym-chromium
Copy link
Collaborator Author

I replied @bwalderman in the thread #55 (comment)


const existingContext = await this._tryGetContext(targetId);
if (existingContext) {
resolve(existingContext.toBidi());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you, that makes more sense. I think this is safe then. Even though await this._tryGetContext(targetId) will yield to the runtime, any attachedToTarget event should be handled by _handleAttachedToTargetEvent and the context will be put in the map before _tryGetContext actually executes.

@sadym-chromium sadym-chromium merged commit 4b2e246 into main Nov 2, 2021
@sadym-chromium sadym-chromium deleted the navigate branch November 2, 2021 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants