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

Backup CID in Storage for non-proxy origin #30375

Merged
merged 8 commits into from
Oct 23, 2020

Conversation

micajuine-ho
Copy link
Contributor

@micajuine-ho micajuine-ho commented Sep 23, 2020

For #29484

Change the cid-impl's GetCidDef to include the optOutBackup property.

 * @typedef {{
 *   scope: string,
 *   createCookieIfNotPresent: (boolean|undefined),
 *   cookieName: (string|undefined),
 *   optOutBackup: (boolean|undefined),
 * }}

Adds the opt_optOutBackup parameter to the CLIENT_ID macro in url-replacements, (defaulting to false)

'CLIENT_ID', (scope, opt_userNotificationId, opt_cookieName, opt_optOutBackup) => ...

When the document is served from a non-proxy origin, and optOutBackup is false, we will store any AMP generated CIDs in storage as a backup, as well as search for backup CID value in storage if the cookie does not exist.

It will be stored in local storage in the following manner:

Key: amp-store:${origin}
Value: btoa('{vv: {amp-cid-backup-cookiename: {v: cid, t: timestamp}}}')

The cid-impl is used in other parts of the code base (extensions and src/ad-cid.js), and we have elected to not opt-out to use this new feature in any of these cases.

No changes to inabox as CIDs are not allowed in that environment.

This feature is guarded by an experiment flag: amp-cid-backup. Publishers and vendors will be able to opt into this experiment to test out the feature. After this PR is merged we will turn the flags on for canary and prod, which will give pubs/vendors 2 weeks to change their configs to opt out of this feature.

@google-cla google-cla bot added the cla: yes label Sep 23, 2020
@micajuine-ho micajuine-ho force-pushed the backup_cid branch 3 times, most recently from a837605 to 65ef96d Compare September 24, 2020 21:58
@micajuine-ho micajuine-ho changed the title [WIP] Backup CID in Storage Backup CID in Storage for non-proxy origin Sep 24, 2020
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Show resolved Hide resolved
@@ -51,6 +51,8 @@ const CID_OPTOUT_STORAGE_KEY = 'amp-cid-optout';

const CID_OPTOUT_VIEWER_MESSAGE = 'cidOptOut';

const AMP_CID_BACKUP_PREFIX = 'amp-cid-backup-';
Copy link
Contributor

@zhouyx zhouyx Sep 24, 2020

Choose a reason for hiding this comment

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

I realized that all AMP stored value in origin are stored under the same localstorage key already. amp-store:${origin}.
How important it is for vendors to access the storage backup value? Shall we reuse the namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the design doc, we stated that we wanted to give publishers the option to access (and potentially change) the backup value. I think reusing the name space is ok as long as it doesn't hinder this goal.

Copy link
Contributor

Choose a reason for hiding this comment

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

That was my concern: reusing the namespace might hinder the goal.
One need to access the data like this

ampStorage = JSON.parse(atob(localStorage.getItem('amp-store:myorigin')));
cidStorage = ampStorage['amp-cid-backup-scope']

Keeping all AMP stored value under the same key is neat but the tradeoff is that it may be confusing to developers who want to access it outside of AMP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would prefer to keep it neat and rely on good documentation and examples to explain how to retrieve and change the values. Do you have a preference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Discussed offline: We agreed that from the feedback from the original issue thread, AMP is not responsible for changes made to the local storage values in the non-AMP case. If vendors/pubs wish to change the values, it will be up to them to correctly decode the values and then encode and store them back in the proper location. B/c this backup CID is stored under the same namespace as other information, faulty encoding could lead to information being dropped (i.e. stored consent info would be lost).

@micajuine-ho micajuine-ho force-pushed the backup_cid branch 3 times, most recently from 861464e to 6f25d61 Compare September 28, 2020 21:06
spec/amp-var-substitutions.md Outdated Show resolved Hide resolved
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/storage-impl.js Outdated Show resolved Hide resolved
Copy link
Contributor

@zhouyx zhouyx left a comment

Choose a reason for hiding this comment

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

Looks good to me. Left a few nitpicks and test requests. Please also confirm with the owner of individual extensions based on the affected components list you had. Thanks!

src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Outdated Show resolved Hide resolved
src/service/cid-impl.js Show resolved Hide resolved
src/service/storage-impl.js Outdated Show resolved Hide resolved
src/service/url-replacements-impl.js Outdated Show resolved Hide resolved
test/unit/test-cid.js Outdated Show resolved Hide resolved
test/unit/test-storage.js Outdated Show resolved Hide resolved
test/unit/test-cid.js Outdated Show resolved Hide resolved
test/unit/test-cid.js Show resolved Hide resolved
return item ? item['v'] : undefined;
const timestamp = item ? item['t'] : undefined;
const isNotExpired =
opt_duration && timestamp != undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Is 0 or negative value a valid opt_duration value? We need to handle them specially if so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are not valid values, so I don't think we need to handle them specifically.

@CLAassistant
Copy link

CLAassistant commented Oct 8, 2020

CLA assistant check
All committers have signed the CLA.

@micajuine-ho
Copy link
Contributor Author

FYI, this will change the CID service to store the CID in local storage as a backup when the document is served from a non-proxy origin.

The following extensions currently use the CID service: amp-subscription, amp-access, amp-user-notification, and amp-story-interactive.

@dvoytenko @erwinmombay @ampproject/wg-stories

@gmajoulet
Copy link
Contributor

Thank you for cc'ing us here!

Just to clarify as I don't have so much knowledge on CID things: this change will make the CID a bit more persistent on non-proxy origins? That sounds like a good things for the way we use CIDs in amp-story-interactive.
Thanks!

@micajuine-ho
Copy link
Contributor Author

Just to clarify as I don't have so much knowledge on CID things: this change will make the CID a bit more persistent on non-proxy origins?

Yes, in this scenario, it will backup the scoped CID (amp-story) to local storage to be used if the scoped CID is not found in cookies.

Copy link
Collaborator

@estherkim estherkim left a comment

Choose a reason for hiding this comment

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

presubmit-check change lgtm 👍

Copy link
Contributor Author

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

OWNERS check

Copy link
Contributor Author

@micajuine-ho micajuine-ho left a comment

Choose a reason for hiding this comment

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

Owners bump again

Copy link
Contributor

@rcebulko rcebulko left a comment

Choose a reason for hiding this comment

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

owners bump

@micajuine-ho micajuine-ho requested review from lannka and jridgewell and removed request for alanorozco and dreamofabear October 19, 2020 18:41
@micajuine-ho
Copy link
Contributor Author

/cc @jridgewell and @lannka for bundle size check for v0 and ads.

I think it makes sense that the bundle size increases for these files as we have added functionality to the CLINET_ID macro. Please let me know if you don't think this is expected.

if (isBackupCidExpOn && !disableBackup) {
return Services.storageForDoc(ampdoc).then((storage) => {
const key = getStorageKey(cookieName);
return storage.get(key, BASE_CID_MAX_AGE_MILLIS).then((backupCid) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Unnest this promise chain.

Copy link
Member

@danielrozenberg danielrozenberg left a comment

Choose a reason for hiding this comment

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

Approval for bundle-size

@micajuine-ho micajuine-ho merged commit fb809b0 into ampproject:master Oct 23, 2020
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* init

* Added experiment flag

* Refactor

* Better tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants