2

How do the runtime.onUpdateAvailable and runtime.onInstall events follow each other?
(1°Q) First, can they coexist in the same background script?
The documentation states that onUpdateAvailable can be useful for postpone an update for example, when you are in the middle of some important operation that we don't want it to be interrupted.
If the extension has registered an event handler for onUpdateAvailable, then the update will be applied the next time the extension is reloaded.

This happens when:

  • we restart the browser
  • the extension is disabled and then re-enabled
  • the extension reloads itself by calling the runtime.reload method

So I imagine that the onUpdateAvailable event handler that will be executed is the one referring to the old version.
(2nd Q) Am I right?

Now I ask myself:

(3nd Q) When the extension is reloaded later in one of the above ways, will the onInstall event be honored or ignored?
(4th Q) Is there a way to test this behavior by myself, specifically I am referring to the onUpdateAvailable event?

syncmate
  • 160
  • 9

1 Answers1

4

Regardless of onUpdateAvailable's presence, the extension will always be updated when the background script is inactive (i.e. it doesn't run), in particular when it's reloaded or re-enabled.

An onUpdateAvailable listener was necessary for a persistent background script in ManifestV2 because without this listener Chrome would automatically terminate the extension and update it. Chrome did it to prevent an outdated extension continuously running for days/months.

In ManifestV3 the background script can't be persistent by design, so Chrome won't restart an extension even if it doesn't have this event listener. However, you may want to use this event anyway if you connect to a native app via chrome.runtime.connectNative in Chrome 105 or newer because it makes the background run forever (i.e. it becomes persistent) or if you prolong its lifetime artificially.

The onUpdateAvailable and onInstalled listeners can co-exist and are triggered independently.

  • onUpdateAvailable (of the current version) runs before updating,
  • onInstalled (of the new version) runs afterwards.

To test the event in case a newer version is present in the web store, press the Update button in chrome://extensions page or call chrome.runtime.requestUpdateCheck() in your extension.

To test the event independently of the web store, configure your extension to use local updates.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thank you for your detailed answer. Outside the CWS (local extension) I've just tried the "Update" button but I notice the onUpdateAvailable event doesn't trigger. This also happpen with .crx file dragged on extensions page. The link you suggest me is valid only for Linus OS if I do understand well. Now I try to set a keyboard command in manifest.json which executes "chrome.runtime.requestUpdateCheck" to check if onUpdateAvailable is triggered. Anyway, as I understand, onUpdateAvailable is pretty much useless with MV3 (apart from the specific case you mentioned). – syncmate Sep 10 '22 at 15:33
  • Regarding local updates, I've never tried it myself, so I don't know. – wOxxOm Sep 10 '22 at 15:53
  • I just finished doing some tests. I run requestUpdateCheck throught a keyboard command. The callback function return always "no_update" or "throttled" (value that I have no idea what that means). So no way to test requestUpdateCheck with local extension. – syncmate Sep 10 '22 at 17:48
  • Yeah, it should be a crx with `update_url` pointing to your localhost update server. In Windows you may need to use *Chrome policies* and a [fake domain](https://superuser.com/a/1741774/). – wOxxOm Sep 11 '22 at 11:02