4

On the manifest for Chrome OS apps one must declare background js scripts.

{
  "name": "Hello World!",
  "description": "My first Chrome App.",
  "version": "0.1",
  "manifest_version": 2,
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": { "16": "calculator-16.png", "128": "calculator-128.png" }
}

Are each of these running in their own thread?

Do they block each other or the JS threads running on the app windows?

Pier
  • 10,298
  • 17
  • 67
  • 113
  • This is simply a shorthand for multiple script tags in html. There is only one background page. Also, JavaScript is single threaded. – wOxxOm Jul 23 '17 at 04:24
  • JS is single threaded yes (unless using workers) but browser windows have their own thread. In any case I'd like to see a source to confirm. – Pier Jul 23 '17 at 16:40
  • Internal implementation of browser windows is irrelevant. The JS engine that runs a page script is single threaded. It's just the way it always was, and is. – wOxxOm Jul 23 '17 at 18:21
  • As for confirmation, you can see it yourself by inspecting the extension's background page in devtools or in the [documentation](https://developer.chrome.com/extensions/background_pages): "A background page will be generated by the extension system that includes each of the files listed in the scripts property." – wOxxOm Jul 23 '17 at 18:22
  • Ok, and what about the other windows of the application? Do these run on the same thread as the background page? – Pier Jul 23 '17 at 18:36
  • All extension's own pages run in the same *process*. You can see it in Chrome Task Manager. Content scripts run in the page *process*, as well any code code injected into the web page from a content script, of course. – wOxxOm Jul 23 '17 at 18:37
  • As for threading, it is not used for normal (non-worker) javascript at all. I don't have a "proof" at hand, but that's just the way it has always been. – wOxxOm Jul 23 '17 at 18:43
  • By default Chrome assigns a new process to each tab/window unless these are from the same site which is why I'm asking. https://blog.chromium.org/2009/12/links-that-open-in-new-processes.html – Pier Jul 23 '17 at 19:27
  • 1
    Yes, and since the extension's own pages all have the same origin of `chrome-extension://***id***` they run in the same process as you can see in Chrome Task Manager. The background page is just another page with that same origin. – wOxxOm Jul 23 '17 at 19:32
  • Thanks for your input. If you put this information in an answer I will accept it. – Pier Jul 23 '17 at 21:23

1 Answers1

2

Chrome App windows are only "views", and are not living in separate processes. Here's the proof. Each app view page can run chrome.runtime.getBackgroundPage to get a direct reference to the background page's javascript context (all variables, functions, etc). Also the background page can get references to the javascript contexts of the app windows, using chrome.app.window.getAll to reference contentWindow on the appWindow objects returned.

As another commenter points out, the situation is a bit different for chrome extensions, but since the question is about apps, we only concern ourselves with that.

kzahel
  • 2,765
  • 1
  • 22
  • 31