2

I am using the 'chrome.storage.sync.get/set' to save my data. My question is, will the data saved using this API will get reset when I close chrome browser window and open it again. Is there any method to clear the data saved when we exit from chrome? I know there is a method 'chrome.storage.sync.clear()'. But this should happen only when the user exits from chrome.

abhilash
  • 827
  • 11
  • 20

2 Answers2

2

will the data saved using this API will get reset when I close chrome browser window and open it again

No, it's saved — not just locally, but also potentially synced to the user's Google account. That's the point of the sync storage area. The documentation says sync is like localStorage, but synced to the user's account.

If you're using it for transient data, don't. Use local instead (as there doesn't appear to be an extension equivalent of session storage) or perhaps just store the data in your extension's runtime environment (it's not clear what you're using this for).

I know there is a method chrome.storage.sync.clear(). But this should happen only when the user exits from chrome.

You'd have to trigger clear from an event. Unfortunately, there is not currently an official way for an extension to get notified when Chrome closes (there's an open issue on it). Hopefully there will be a way at some point, but there isn't currently.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • But it works even if I had signed out from the chrome account – abhilash Oct 08 '14 at 09:04
  • I found another way it is possible by listening to the port. – abhilash Oct 08 '14 at 09:45
  • @abhilash: *"But it works even if I had signed out from the chrome account"* Right, because it acts like local storage which **also** syncs to the account *if attached*. *"I found another way it is possible by listening to the port."* Cool! What port? – T.J. Crowder Oct 08 '14 at 10:19
0

You're right chrome.storage.sync.clear(), removes all storage from sync. However, if you want to use that function when the browser 'exists', you'll have to look at the Processes Api.

You would probably want to use this function (from the documentation) :

chrome.processes.onExited.addListener(function(){...});

It takes processId as one of the arguments. So you would just have to get hold of the processId for the browser process. I found a relevant answer for this on SO only. It suggests you do something like:

//Basically loop through all the processes to get the browser's Process Id
var browserId;
chrome.processes.getProcessInfo([], false, function(processes) {
processes.forEach(function(process) {
    if (process.type === 'browser') {
        browserId = process.id;
    }
   });
});

Once you have the browser's Process Id, you can add a listener to the exit event and do the needful. I hope it gets you started in the right direction.

Community
  • 1
  • 1
Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
  • If that works, I'm surprised no one's mentioned it [in this Chrome issue](https://code.google.com/p/chromium/issues/detail?id=30885). – T.J. Crowder Oct 08 '14 at 07:37