简体   繁体   English

使用Firefox扩展程序中的Web Workers的XPCOM

[英]XPCOM using Web Workers from a Firefox Extension

My Firefox extension is parsing big chunks of data. 我的Firefox扩展程序正在解析大量数据。 I would usually use WebWorkers to do this, however in XPCOM I seems that this is not an option. 我通常会使用WebWorkers来执行此操作,但是在XPCOM中,这似乎不是一个选择。 The ChromeWorker, https://developer.mozilla.org/en-US/docs/Web/API/ChromeWorker , seems to be obsolete and "discouraged in new projects". https://developer.mozilla.org/zh-CN/docs/Web/API/ChromeWorker,ChromeWorker似乎已过时,并且“不适合在新项目中使用”。

Are there any options to use workers in a Firefox extension? 在Firefox扩展中可以使用工作程序吗?

Short version: no, you cannot access XPCOM from a different thread. 短版:不,您不能从其他线程访问XPCOM。 But that doesn't mean that you cannot use chrome workers. 但这并不意味着您不能使用chrome工人。

Long version: Firefox used to allow accessing XPCOM from other threads, eg via ChromeWorker . 长版:Firefox曾经允许通过其他线程(例如,通过ChromeWorker访问XPCOM。 This led to all kinds of issues like weird crashes or just plain inconsistent behavior. 这导致了各种各样的问题,例如奇怪的崩溃或只是简单的不一致行为。 In the end Mozilla decided that supporting multithreaded XPCOM access was too complicated and error-prone, as was documenting its limitations and stopping people from shooting themselves in the feet. 最终,Mozilla认为支持多线程XPCOM访问过于复杂且容易出错,正如记录其局限性并阻止人们脚踏实地一样。

With current Firefox versions, accessing XPCOM from ChromeWorker is no longer possible. 在当前的Firefox版本中,不再可以从ChromeWorker访问XPCOM。 ChromeWorker itself however isn't deprecated however even though the MDN comment can be easily misread as a general deprecation statement. 但是,即使MDN注释很容易被误认为是一般的弃用声明,但ChromeWorker本身并没有被弃用。 The idea is that you would use ChromeWorker in combination with js-ctypes which will allow you to use native libraries (the ones provided by the operating system, libraries included in Firefox like NSS and libraries distributed with your extension) on a different thread. 这个想法是,您可以将ChromeWorkerjs-ctypes结合使用,从而允许您在不同的线程上使用本机库(操作系统提供的库,Firefox中包含的库(如NSS)和随扩展名分发的库)。

Depending on what you are trying to achieve, this might work for you. 根据您要实现的目标,这可能适合您。 Eg, if you need XPCOM for file access then you don't even need to use js-ctypes directly - OS.File API will do that for you. 例如,如果您需要XPCOM进行文件访问,那么您甚至不需要直接使用js-ctypes- OS.File API会为您完成。 However, XPCOM access is limited to the main thread. 但是,XPCOM访问仅限于主线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM