Skip to content

Refactored sentinel to be passed as parameter#6825

Closed
bradfrizzell wants to merge 38 commits intoampproject:masterfrom
google:frizz-pm-sentinel-refactor
Closed

Refactored sentinel to be passed as parameter#6825
bradfrizzell wants to merge 38 commits intoampproject:masterfrom
google:frizz-pm-sentinel-refactor

Conversation

@bradfrizzell
Copy link
Copy Markdown
Contributor

Previously, sentinel was stored as a data attribute on the iframe. There is no reason it can't be passed as a parameter, however. Passing the sentinel would be preferable to adding a redundant data attribute.

This change broke many tests which expected the data-amp3pSentinel attribute, so they all had to be changed.

This commit may break functionality.
Previously, the sentinel had been passed both on the hash of the
iframe src, and as a data attribute. The data attribute was how
the sentinel was referenced on the runtime side, even though there
was no reason it could not simply be passed.

Now, the sentinel is only passed to the iframe as a member of an
object that is stringified and attached to the iframe name.

On the runtime side, the sentinel is now passed and expected as
a parameter to multiple functions that used to ultimately rely
on it being available as a data attribute of the iframe.
Previously, the sentinel had been passed both on the hash of the
iframe src, and as a data attribute. The data attribute was how
the sentinel was referenced on the runtime side, even though there
was no reason it could not simply be passed.

Now, the sentinel is only passed to the iframe as a member of an
object that is stringified and attached to the iframe name.

On the runtime side, the sentinel is now passed and expected as
a parameter to multiple functions that used to ultimately rely
on it being available as a data attribute of the iframe.
@bradfrizzell
Copy link
Copy Markdown
Contributor Author

bradfrizzell commented Dec 28, 2016

Apologies for how long this is... this change hit a lot of files. I made a new PR and am closing the WIP previous one

const object = opt_object || {};
object.type = type;
object.sentinel = window.context.amp3pSentinel;
object.sentinel = window.context.sentinel;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we split this change into a standalone PR? for sake of easy rollback in case

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure, I assume I should make a new experiment flag for it as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ideally. less hassle for our release eng in case of issues.
a lesson we just learned: #6864

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

return this.xOriginIframeHandler_.init(iframe, /* opt_isA4A */ true);
this.rendered_ = true;
return this.xOriginIframeHandler_.init(iframe, sentinel,
/* opt_isA4A */ true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: can we break after .init(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

return this.iframeRenderHelper_(iframe);
const sentinel = generateSentinel(window);
const metadata = getContextMetadata(window, iframe, sentinel);
iframe.setAttribute('name', encodeURI(JSON.stringify(metadata)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you will want encodeURIComponent. but is that necessary? setAttribute should take care of char escape?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just tested it in js console and setAttribute did not escape anything. will change to encodeURIComponent

expect(child).to.be.ok;
expect(child.src).to.have.string(srcUrl);
expect(child.getAttribute('name')).not.to.be.ok;
expect(child.getAttribute('name')).to.be.ok;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do you want to verify the content?

const iframe = getIframe(this.element.ownerDocument.defaultView,
this.element, undefined, opt_context);
let sentinel;
try {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

read flag instead of try-catch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

iframe,
opt_is3P
);
parentWin,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are we able to fit in one line if we break after =?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

export function listenFor(
iframe, typeOfMessage, callback, opt_is3P, opt_includingNestedWindows) {
export function listenFor(iframe, sentinel, typeOfMessage, callback,
opt_is3P, opt_includingNestedWindows) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

indentation.
please try to config your IDE to match our code style.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

}
object.type = type;
object.sentinel = getSentinel_(iframe, opt_is3P);
object.sentinel = sentinel;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

shall we use serializeMessage in 3p-iframe.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a todo, will do in later pr as we discussed

*/
export function postMessageToWindows(iframe, targets, type, object, opt_is3P) {
export function postMessageToWindows(
iframe, sentinel, targets, type, object, opt_is3P) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems param iframe is not needed anymore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

while I think you are right, getting rid of that parameter might be better suited to another PR. I suspect getting rid of it in this one will lead to many additional changes

*/
export function postMessage(iframe, type, object, targetOrigin, opt_is3P) {
postMessageToWindows(iframe,
export function postMessage(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this method used elsewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes

* @param {boolean=} opt_is3p
*/
constructor(baseElement, iframe, opt_is3p) {
constructor(baseElement, iframe, sentinel, opt_is3p) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you might need to change intersection-observer.js as well now. it's unfortunately reverted from trash.

iframe.setAttribute('data-amp-3p-sentinel', 'amp3ptest' + testIndex);
iframe.name = 'test_nomaster';
sentinel = 'amp3ptest' + testIndex;
iframe.setAttribute('data-amp-3p-sentinel', sentinel);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why are we still setting this attribute?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

* @private
*/
iframeRenderHelper_(iframe) {
iframeRenderHelper_(iframe, sentinel) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: for better readability can we move this method after renderViaNameAttrOfXOriginIframe_.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// TODO(keithwrightbos): noContentCallback?
this.xOriginIframeHandler_ = new AMP.AmpAdXOriginIframeHandler(this);
return this.xOriginIframeHandler_.init(iframe, /* opt_isA4A */ true);
this.rendered_ = true;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why was this added?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hm good question. I don't recall ever writing that line, I wonder if that is a holdover from some merge-conflict-resolution at some point. will remove.

const iframe = getIframe(this.win, this.element, 'facebook');
this.applyFillContent(iframe);
const sentinel = generateSentinel(window);
iframe.name = encodeURI(JSON.stringify({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't use such a predictable name, or we'll run into iframe caching.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry, I'm confused at what needs to be done to fix this. The name attribute is totally random, because it contains the sentinel (which has a random string as part of it).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, missed the random sentinel part. Never mind.

const iframe = getIframe(this.win, this.element, 'facebook');
this.applyFillContent(iframe);
const sentinel = generateSentinel(window);
iframe.name = encodeURI(JSON.stringify({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be a part of getIframe.

setSandbox(this.element, iframe, this.sandbox_);
iframe.src = this.iframeSrc;
const sentinel = generateSentinel(window);
iframe.name = encodeURI(JSON.stringify({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto.

.getUnconfirmedReferrerUrl();

attributes._context = {
ampcontextVersion: (getMode().localDev ? 'LOCAL' :
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this ternary necessary? In localdev mode, the $internalRuntimeVersion$ magic string will be a literal '$internalRuntimeVersion$'.

@lannka
Copy link
Copy Markdown
Contributor

lannka commented Feb 15, 2017

@bradfrizzell are you still on this? or it is already covered by the other PRs?

@bradfrizzell
Copy link
Copy Markdown
Contributor Author

Still on top of this. It is not a top priority, because the refactor is not blocking any functionality. Focusing on top blockers at the moment. I will close this PR for now though, as when I get back to this I will just do it from scratch. The merge conflict with this right now is a huge pain to deal with, tried to push it through quickly friday but it was not happening at all. I broke all the key functionality out of this PR already and it is all merged. All that is left is the refactoring elements.

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