Refactored sentinel to be passed as parameter#6825
Refactored sentinel to be passed as parameter#6825bradfrizzell wants to merge 38 commits intoampproject:masterfrom google:frizz-pm-sentinel-refactor
Conversation
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.
|
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; |
There was a problem hiding this comment.
can we split this change into a standalone PR? for sake of easy rollback in case
There was a problem hiding this comment.
sure, I assume I should make a new experiment flag for it as well?
There was a problem hiding this comment.
ideally. less hassle for our release eng in case of issues.
a lesson we just learned: #6864
| return this.xOriginIframeHandler_.init(iframe, /* opt_isA4A */ true); | ||
| this.rendered_ = true; | ||
| return this.xOriginIframeHandler_.init(iframe, sentinel, | ||
| /* opt_isA4A */ true); |
There was a problem hiding this comment.
nit: can we break after .init(
| return this.iframeRenderHelper_(iframe); | ||
| const sentinel = generateSentinel(window); | ||
| const metadata = getContextMetadata(window, iframe, sentinel); | ||
| iframe.setAttribute('name', encodeURI(JSON.stringify(metadata))); |
There was a problem hiding this comment.
you will want encodeURIComponent. but is that necessary? setAttribute should take care of char escape?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
do you want to verify the content?
| const iframe = getIframe(this.element.ownerDocument.defaultView, | ||
| this.element, undefined, opt_context); | ||
| let sentinel; | ||
| try { |
There was a problem hiding this comment.
read flag instead of try-catch?
| iframe, | ||
| opt_is3P | ||
| ); | ||
| parentWin, |
There was a problem hiding this comment.
are we able to fit in one line if we break after =?
| export function listenFor( | ||
| iframe, typeOfMessage, callback, opt_is3P, opt_includingNestedWindows) { | ||
| export function listenFor(iframe, sentinel, typeOfMessage, callback, | ||
| opt_is3P, opt_includingNestedWindows) { |
There was a problem hiding this comment.
indentation.
please try to config your IDE to match our code style.
| } | ||
| object.type = type; | ||
| object.sentinel = getSentinel_(iframe, opt_is3P); | ||
| object.sentinel = sentinel; |
There was a problem hiding this comment.
shall we use serializeMessage in 3p-iframe.js
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
seems param iframe is not needed anymore?
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
is this method used elsewhere?
| * @param {boolean=} opt_is3p | ||
| */ | ||
| constructor(baseElement, iframe, opt_is3p) { | ||
| constructor(baseElement, iframe, sentinel, opt_is3p) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
why are we still setting this attribute?
| * @private | ||
| */ | ||
| iframeRenderHelper_(iframe) { | ||
| iframeRenderHelper_(iframe, sentinel) { |
There was a problem hiding this comment.
nit: for better readability can we move this method after renderViaNameAttrOfXOriginIframe_.
| // TODO(keithwrightbos): noContentCallback? | ||
| this.xOriginIframeHandler_ = new AMP.AmpAdXOriginIframeHandler(this); | ||
| return this.xOriginIframeHandler_.init(iframe, /* opt_isA4A */ true); | ||
| this.rendered_ = true; |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
Can't use such a predictable name, or we'll run into iframe caching.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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({ |
| .getUnconfirmedReferrerUrl(); | ||
|
|
||
| attributes._context = { | ||
| ampcontextVersion: (getMode().localDev ? 'LOCAL' : |
There was a problem hiding this comment.
Is this ternary necessary? In localdev mode, the $internalRuntimeVersion$ magic string will be a literal '$internalRuntimeVersion$'.
|
@bradfrizzell are you still on this? or it is already covered by the other PRs? |
|
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. |
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.