11

all. I've started developing small extensions using Chrome's various API's, and although things are working great, I'm still curious about a few things.

Two questions, if you all wouldn't mind helping me out:

1. Could someone tell me what the limits are to chrome.storage.local's persistence? I've tried various tests on my own, such as storing a few things with chrome.storage.local.set(), clearing all of my browser history, cookies, etc. and then seeing if everything is still there. Often it will still be there, but sometimes I'll check back later and it will be gone. Overall, I have been unable to definitively label what I'm doing that is occasionally clearing the .local data.

2. I've been working mostly with chrome.storage.sync so far, all while not being signed in using Chrome's "Sign in to Chrome" feature. I read on the API page that in the case that .sync is used while Chrome is offline (as well as not logged in, I'm assuming, which is my case), data is stored locally and then sync'ed later on. My main concern is does this mean the data I'm storing using chrome.storage.sync could potentially get erased as the data I'm storing using chrome.storage.local has in the past? One of the main reasons I've been using .sync anyway is because I've never had an experience of data getting erased with it, while I have with .local (as I've described in #1).

Thanks so much! Help me out, please!

Edit: I'm pretty sure the .local clear isn't happening because of me mistakenly removing the extension and then adding it back in. I know that that will clear the .local data (but preserve the .sync).

Matt White
  • 145
  • 1
  • 2
  • 7

2 Answers2

28

I'm the author of that API.

chrome.storage.local shouldn't be disappearing except on uninstallation (which sounds like is your case) or, very rarely, on database corruption (and we've seen this particularly happening on System Restore).

chrome.storage.sync works the same way, except that the merge algorithm it uses may cause data loss if 2 machines make conflicting changes. In your case, this might happen if you sign into the machine which is using chrome.storage.sync. More commonly it will be because one machine is offline while making the change, or perhaps the user managed to simultaneously change data on 2 machines (which is why it's recommended to only change data on user action -- we should document that).

For what it's worth -- and we should document this too -- the merge algorithm is last-change-wins and sync-is-source-of-truth -- but any local key/value pairs added won't be deleted. If you have:

{a:1, b:2} on computer A (signed in and syncing), {b:3, c:4} on computer B (not signed in),

and computer B signs in, after doing a full sync the state of storage on both A and B will end up at {a:1, b:2, c:4} because A's data was already part of sync, this the source of truth, but 'c' didn't exist yet so was added.

In this scenario A will have gotten an onChange event adding 'c', and B will have gotten an onChange event adding 'a' and updating 'b' from 3 to 2.

kalman
  • 654
  • 5
  • 3
  • 1
    Thanks so much! Very informative. I guess I have just been reinstalling the app completely and forgetting about it, then assuming that the .local clear was because of something else. Does this mean that storage in .local is safe for relatively long periods of time? – Matt White Apr 23 '14 at 15:58
  • "sync-is-source-of-truth" definitely needs to be documented. And what happens if `c` was explicitly deleted by A before B started syncing? Say, it was first added, then deleted by A. – Xan May 08 '14 at 13:46
  • @kalman, Is there any way to use this google.storage API for session storage rather than local storage? – sbru Sep 15 '16 at 00:34
  • Can the storage be preserverd if the extension that has created it is uninstalled? – CristianC Feb 28 '17 at 18:44
  • 1
    Why would the data for that extension need to reasonably persist if it isn't installed anymore? @CristianC – Stephen Tetreault Jul 03 '17 at 21:47
  • @StephenTetreault maybe to stop people from uninstall/reinstall to gain additional free trial and avoid paying for a license – r14n Oct 27 '18 at 19:18
  • fair enough, but i figure that's easy to spoof @r14n – Stephen Tetreault Oct 28 '18 at 22:37
  • What would be the implications of storing large image files (100GB) in chrome.storage.local (with unlimited param for chrome extension)? – Ouwen Huang Oct 08 '21 at 16:50
0

I spent some time looking at this today for work. My results are at https://github.com/mozilla/application-services/issues/2900#issuecomment-612251230. The behavior of chrome.storage.sync seems to have changed since the accepted answer. In particular, there doesn't appear to be any merging behavior any more. Instead it's all-or-nothing, with whatever object is on the sync server "winning". Deletes are transmitted to the server but not from the server to the other clients.

Ethan
  • 111
  • 2
  • 6