{"id":695,"date":"2010-12-03T16:13:40","date_gmt":"2010-12-03T20:13:40","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/?p=695"},"modified":"2010-12-03T16:13:40","modified_gmt":"2010-12-03T20:13:40","slug":"running-extension-code-in-another-process","status":"publish","type":"post","link":"https:\/\/benjamin.smedbergs.us\/blog\/2010-12-03\/running-extension-code-in-another-process\/","title":{"rendered":"Running Extension Code In Another Process"},"content":{"rendered":"<p>In order to support running Jetpack-style extensions in another process, Firefox 4 has support for <a href=\"https:\/\/developer.mozilla.org\/en\/Jetpack_Processes\">running arbitrary JavaScript code in a separate process<\/a>. Although this code was designed primarily to support the Jetpack SDK, Firefox and extensions can use this support to run arbitrary code in a separate process.<\/p>\n<p>Running code in a separate process has advantages similar to running code in a separate thread. The running code will not block the main Firefox user interface. An added advantage is crash protection: if the code causes a crash, it will not take down the entire browser. There may also be some performance benefits from separating the garbage collection heaps and avoiding XPCOM overhead.<\/p>\n<p>The basic steps to start a subprocess and run code in it are as follows:<\/p>\n<pre class=\"code\">var process = Components.classes[\"@mozilla.org\/jetpack\/service;1\"].\r\n  getService(Components.interfaces.nsIJetpackService).createJetpack();\r\nprocess.evalScript(\"Put your JS here\");\r\n\/\/ When you are done with the process, you should explicitly destroy it.\r\nprocess.destroy();\r\n<\/pre>\n<p>Of course, running a script in another process isn&#8217;t that useful unless you can communicate with it. This is accomplished by passing messages back and forth. To send a message to the remote process, use process.sendMessage:<\/p>\n<pre class=\"code\">process.sendMessage(\"messageName\", param...)<\/pre>\n<p>To receive messages from the remote process, register a receiver function:<\/p>\n<pre class=\"code\">process.registerReceiver(\"messageName, function(messageName, argument...) { ... });<\/pre>\n<p>The remote process has access to a similar set of global functions, as well as the ability to create sandboxes and use ctypes. For more information about the full capabilities, see the <a href=\"https:\/\/developer.mozilla.org\/en\/Jetpack_Processes\">Mozilla Developer Center documentation<\/a>. Note that code running in a jetpack-style process does not have access to XPCOM, because XPCOM is not started in the jetpack process; it runs code using only the JavaScript engine.<\/p>\n<p>If an extension is using ctypes to work with third-party code or OS libraries, I strongly encourage that extension to consider running the code in a separate process for crash protection. If an extension has long-running or computationally expensive tasks, it might make sense to move those into a separate process as well. If nothing else, it will make it much easier to measure the CPU and memory usage of that code separate from the rest of Firefox.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to support running Jetpack-style extensions in another process, Firefox 4 has support for running arbitrary JavaScript code in a separate process. Although this code was designed primarily to support the Jetpack SDK, Firefox and extensions can use this support to run arbitrary code in a separate process. Running code in a separate process [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[247,249,39,248],"class_list":["post-695","post","type-post","status-publish","format-standard","hentry","category-mozilla","tag-ctypes","tag-extensions","tag-javascript","tag-jetpack"],"_links":{"self":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":7,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":702,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/695\/revisions\/702"}],"wp:attachment":[{"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}