Skip to content

Commit

Permalink
Fix broken system social share in Webviews and Opera UA detection (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
aghassemi authored Oct 4, 2017
1 parent a08b1a3 commit 8541477
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
23 changes: 19 additions & 4 deletions extensions/amp-social-share/0.1/amp-social-share.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class AmpSocialShare extends AMP.BaseElement {
/** @private {?../../../src/service/platform-impl.Platform} */
this.platform_ = null;

/** @private {?../../../src/service/viewer-impl.Viewer} */
this.viewer_ = null;

/** @private {?string} */
this.href_ = null;

Expand All @@ -60,15 +63,19 @@ class AmpSocialShare extends AMP.BaseElement {
user().assert(!/\s/.test(typeAttr),
'Space characters are not allowed in type attribute value. %s',
this.element);

this.platform_ = Services.platformFor(this.win);
this.viewer_ = Services.viewerForDoc(this.element);

if (typeAttr === 'system') {
// Hide/ignore system component if navigator.share unavailable
if (!('share' in navigator)) {
if (!this.systemShareSupported_()) {
setStyle(this.element, 'display', 'none');
return;
}
} else {
// Hide/ignore non-system component if system share wants to be unique
const systemOnly = ('share' in navigator) &&
const systemOnly = this.systemShareSupported_() &&
!!this.win.document.querySelectorAll(
'amp-social-share[type=system][data-mode=replace]').length;
if (systemOnly) {
Expand All @@ -83,7 +90,6 @@ class AmpSocialShare extends AMP.BaseElement {
'The data-share-endpoint attribute is required. %s', this.element);
Object.assign(this.params_, typeConfig['defaultParams'],
getDataParamsFromAttributes(this.element));
this.platform_ = Services.platformFor(this.win);

const hrefWithVars = addParamsToUrl(this.shareEndpoint_, this.params_);
const urlReplacements = Services.urlReplacementsForDoc(this.getAmpDoc());
Expand Down Expand Up @@ -158,8 +164,17 @@ class AmpSocialShare extends AMP.BaseElement {
openWindowDialog(this.win, href, target, windowFeatures);
}
}
}

/** @private */
systemShareSupported_() {
// Chrome exports navigator.share in WebView but does not implement it.
// See https://bugs.chromium.org/p/chromium/issues/detail?id=765923
const isChromeWebview = this.viewer_.isWebviewEmbedded() &&
this.platform_.isChrome();

return ('share' in navigator) && !isChromeWebview;
}
}

AMP.extension('amp-social-share', '0.1', AMP => {
AMP.registerElement('amp-social-share', AmpSocialShare, CSS);
Expand Down
5 changes: 4 additions & 1 deletion src/service/platform-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export class Platform {
* @return {boolean}
*/
isOpera() {
return /OPR|Opera|OPiOS/i.test(this.navigator_.userAgent);
// Chrome mobile UA includes OPR<v> stating with Chrome 60, however real
// Opera puts put a / after OPR and that's the only tell, so
// we check for OPR/ instead of OPR
return /OPR\/|Opera|OPiOS/i.test(this.navigator_.userAgent);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/service/viewer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@ export class Viewer {
return this.isEmbedded_;
}

/**
* Whether the document is embedded in a webview.
* @return {boolean}
*/
isWebviewEmbedded() {
return this.isWebviewEmbedded_;
}

/**
* @return {boolean}
*/
Expand Down
12 changes: 12 additions & 0 deletions test/functional/test-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ describe('Platform', () => {
testStandalone(userAgent, isStandalone);
});

it('Pixel Chrome 61', () => {
isAndroid = true;
isChrome = true;
isWebKit = true;
majorVersion = 61;
userAgent = 'Mozilla/5.0 (Linux; Android 8.0.0; Pixel XL Build/OPR6.' +
'170623.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163' +
'.98 Mobile Safari/537.36';
testUserAgent(userAgent);
testStandalone(userAgent, isStandalone);
});

it('Firefox', () => {
isFirefox = true;
majorVersion = 40;
Expand Down
26 changes: 26 additions & 0 deletions test/functional/test-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,32 @@ describe('Viewer', () => {
});
});

describe('isWebviewEmbedded', () => {
it('should be webview w/ "webview=1"', () => {
windowApi.parent = windowApi;
windowApi.location.hash = '#webview=1';
expect(new Viewer(ampdoc).isWebviewEmbedded()).to.be.true;
});

it('should NOT be webview w/o "webview=1"', () => {
windowApi.parent = windowApi;
windowApi.location.hash = '#foo=1';
expect(new Viewer(ampdoc).isWebviewEmbedded()).to.be.false;
});

it('should NOT be webview w/ "webview=0"', () => {
windowApi.parent = windowApi;
windowApi.location.hash = '#webview=0';
expect(new Viewer(ampdoc).isWebviewEmbedded()).to.be.false;
});

it('should NOT be webview if iframed regardless of "webview=1"', () => {
windowApi.parent = {};
windowApi.location.hash = '#webview=1';
expect(new Viewer(ampdoc).isEmbedded()).to.be.false;
});
});

describe('isTrustedViewer', () => {

it('should consider non-trusted when not iframed', () => {
Expand Down

0 comments on commit 8541477

Please sign in to comment.