Skip to content

Commit

Permalink
ALP for A4A (ampproject#5430)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouyx authored and Vanessa Pasque committed Dec 22, 2016
1 parent b076625 commit 8d5ce77
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
13 changes: 10 additions & 3 deletions ads/alp/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ export function installAlpClickHandler(win) {
* Filter click event and then transform URL for direct AMP navigation
* with impression logging.
* @param {!Event} e
* @param {function(string)=} opt_viewerNavigate
* @visibleForTesting
*/
export function handleClick(e) {
export function handleClick(e, opt_viewerNavigate) {
if (e.defaultPrevented) {
return;
}
Expand Down Expand Up @@ -91,9 +92,15 @@ export function handleClick(e) {
destination = destination.replace(`${urls.cdn}/c/`,
'http://localhost:8000/max/');
}

e.preventDefault();
navigateTo(win, link.a, destination);
if (opt_viewerNavigate) {
// TODO: viewer navigate only support navigating top level window to
// destination. should we try to open a new window here with target=_blank
// here instead of using viewer navigation.
opt_viewerNavigate(destination);
} else {
navigateTo(win, link.a, destination);
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions build-system/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ exports.rules = [
'ads/google/a4a/**->src/timer.js',
'ads/google/a4a/**->src/viewer.js',
'ads/google/a4a/**->src/viewport.js',
// alp handler needs to depend on src files
'ads/alp/handler.js->src/dom.js',
'ads/alp/handler.js->src/config.js',
],
},
{
Expand Down
16 changes: 15 additions & 1 deletion extensions/amp-a4a/0.1/amp-a4a.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
verifySignature,
PublicKeyInfoDef,
} from './crypto-verifier';
import {isExperimentOn} from '../../../src/experiments';
import {handleClick} from '../../../ads/alp/handler';

/**
* Dev public key set. This will go away once the dev signing service goes live.
Expand Down Expand Up @@ -665,7 +667,8 @@ export class AmpA4A extends AMP.BaseElement {
html: modifiedCreative,
extensionIds: creativeMetaData.customElementExtensions || [],
fonts: fontsArray,
}).then(() => {
}).then(friendlyIframeEmbed => {
this.registerAlpHandler_(friendlyIframeEmbed.win);
this.rendered_ = true;
this.onAmpCreativeRender();
return true;
Expand Down Expand Up @@ -831,4 +834,15 @@ export class AmpA4A extends AMP.BaseElement {
metaData.cssUtf16CharOffsets[0],
metaData.cssUtf16CharOffsets[1]);
}

registerAlpHandler_(iframeWin) {
if (!isExperimentOn(this.win, 'alp-for-a4a')) {
return;
}
iframeWin.document.documentElement.addEventListener('click', event => {
handleClick(event, url => {
viewerForDoc(this.getAmpDoc()).navigateTo(url, 'a4a');
});
});
}
}
15 changes: 15 additions & 0 deletions test/functional/test-alp-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ describe('alp-handler', () => {
simpleSuccess();
});

it('should perform special navigation if specially asked for', () => {
const navigateSpy = sandbox.spy();
const opt_navigate = val => {
navigateSpy();
expect(val).to.equal(
'https://cdn.ampproject.org/c/www.example.com/amp.html#click=' +
'https%3A%2F%2Ftest.com%3Famp%3D1%26adurl%3Dhttps%253A%252F%252F' +
'cdn.ampproject.org%252Fc%252Fwww.example.com%252Famp.html');
};
handleClick(event, opt_navigate);
expect(event.preventDefault).to.be.calledOnce;
expect(open).to.not.be.called;
expect(navigateSpy).to.be.calledOnce;
});

it('should navigate if trusted is not set.', () => {
delete event.trusted;
simpleSuccess();
Expand Down
5 changes: 5 additions & 0 deletions tools/experiments/experiments.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ const EXPERIMENTS = [
spec: 'https://github.com/ampproject/amphtml/issues/4820',
cleanupIssue: 'https://github.com/ampproject/amphtml/issues/4894',
},
{
id: 'alp-for-a4a',
name: 'Enable redirect to landing page directly for A4A',
spec: 'https://github.com/ampproject/amphtml/issues/5212',
},
];

if (getMode().localDev) {
Expand Down

0 comments on commit 8d5ce77

Please sign in to comment.