From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../message_manager/index.html | 71 ++++ .../message_manager_overview/index.html | 444 +++++++++++++++++++++ 2 files changed, 515 insertions(+) create mode 100644 files/ja/mozilla/firefox/multiprocess_firefox/message_manager/index.html create mode 100644 files/ja/mozilla/firefox/multiprocess_firefox/message_manager/message_manager_overview/index.html (limited to 'files/ja/mozilla/firefox/multiprocess_firefox/message_manager') diff --git a/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/index.html b/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/index.html new file mode 100644 index 0000000000..869474d44f --- /dev/null +++ b/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/index.html @@ -0,0 +1,71 @@ +--- +title: Message manager +slug: Mozilla/Firefox/Multiprocess_Firefox/Message_Manager +tags: + - NeedsTranslation + - TopicStub +translation_of: Mozilla/Firefox/Multiprocess_Firefox/Message_Manager +--- +

Message managers provide a way for chrome-privileged JavaScript code to communicate across process boundaries. They are particularly useful for allowing chrome code, including the browser's own code and extension code, to access web content when the browser is running web content in a separate process.

+ +

These guides explain how to use message managers in multiprocess Firefox.

+ +

Note that none of this requires multiprocess Firefox: everything described here will work with single-process Firefox, so the same code will work in both variants.

+ +
+

Guides

+ +
+ + + +
+ +
+

API reference

+ +
+ + + +
diff --git a/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/message_manager_overview/index.html b/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/message_manager_overview/index.html new file mode 100644 index 0000000000..5e00266603 --- /dev/null +++ b/files/ja/mozilla/firefox/multiprocess_firefox/message_manager/message_manager_overview/index.html @@ -0,0 +1,444 @@ +--- +title: Message manager overview +slug: Mozilla/Firefox/Multiprocess_Firefox/Message_Manager/Message_manager_overview +translation_of: Mozilla/Firefox/Multiprocess_Firefox/Message_Manager/Message_manager_overview +--- +
+

マルチプロセスの Firefo では 2 つのプロセスが存在します。

+ + + +

Message manager はあるプロセス上の chrome 権限 JavaScript がほかのプロセス上の Chrome 権限 JavaScript と通信することを可能にします。

+ +

この記事では種類の違う message manager の説明、アクセス方法、使いどころの説明をします。

+
+ +

トップレベルにおいて、2つの違う種類の message manager が存在します。

+ + + +

Frame message managers

+ +

マルチプロセスの Firefo では、chrome コードが Web コンテンツにアクセスする必要が出た時に、以下のように使います。

+ + + +
+

Some older articles on multiprocess Firefox and the message manager might refer to "content scripts" instead of "frame scripts", but this usage is deprecated because the Add-on SDK uses "content script" to refer to a similar but different kind of script.

+
+ +

So fundamentally, frame message managers enable chrome code to:

+ + + +

There are various types of frame message managers, as depicted in this diagram:

+ +

+ +

This diagram shows the setup when there are 2 browser windows open, one with 2 tabs open and one with 1 tab open.

+ +

Chrome process

+ +

In the chrome process, there's a hierarchy of frame message managers: the global frame message manager, window message managers, and browser message managers.

+ +

Global frame message manager

+ + + + + + + + + + + + + + + + +
Description +

There's a single global frame message manager in the chrome process.

+ +

This operates on all frames, in all content tabs. If you load a frame script using the global frame message manager, the script gets loaded separately into every open tab: three times, in the diagram above. Similarly, if you send a message using the global frame message manager, it's received by all content tabs, and is then delivered to any frame scripts that are listening for it.

+ +

Its most important functions and attributes are:

+ +

childCount : contains the number of children (typically, browser windows)

+ +

getChildAt() : get the child at the given index

+ +

loadFrameScript() : load a frame script into every tab in the browser

+ +

broadcastAsyncMessage() : send a message to frame scripts

+ +

addMessageListener() : start listening to a specific message from all frame scripts

+ +

removeMessageListener() : stop listening to a specific message

+
Interfaces +

nsIFrameScriptLoader

+ +

nsIMessageListenerManager

+ +

nsIMessageBroadcaster

+
How to access +

Access it using Components.classes:

+ +
+// chrome script
+let globalMM = Cc["@mozilla.org/globalmessagemanager;1"]
+  .getService(Ci.nsIMessageListenerManager);
+ +

You can also access it as the mm property of Services.jsm, if you are in the parent process.

+
+ +

Window message manager

+ + + + + + + + + + + + + + + + +
Description +

There's a window message manager for every browser window: two, in the diagram above.

+ +

It operates on all content tabs in a given window. If you load a frame script using the window message manager it gets loaded separately into each tab open in that particular window. If you send a message using the window message manager, it gets sent to all content tabs in that window.

+ +

Its most important functions and attributes are:

+ +

childCount : contains the number of children (typically, browser tabs)

+ +

getChildAt() : get the child at the given index

+ +

loadFrameScript() : load a frame script into every tab in this window

+ +

broadcastAsyncMessage() : send a message to all frame scripts in this window

+ +

addMessageListener() : start listening to a specific message from frame scripts

+ +

removeMessageListener() : stop listening to a specific message

+
Interfaces +

nsIFrameScriptLoader

+ +

nsIMessageListenerManager

+ +

nsIMessageBroadcaster

+
How to access +

You can access it as a property of the browser window:

+ +
+// chrome script
+let windowMM = window.messageManager;
+
+ +

Browser message manager

+ +
+

Note that in this context, "browser" refers to the XUL <browser> object, which is a frame that hosts a single Web document. It does not refer to the more general sense of a Web browser.

+
+ + + + + + + + + + + + + + + + +
Description +

Finally, there's a browser message manager for every open content tab: three, in the diagram above.

+ +

This corresponds one-to-one with a content tab. Scripts you load using a browser message manager are loaded only into that content tab, and messages you send are delivered only to that content tab.

+ +

You can mix and match: so for example, you could load a script into every tab using the global message manager, but then send a message to the script instance loaded into a specific tab by using the browser message manager.

+ +

Its most important functions are:

+ +

loadFrameScript() : load a frame script into this browser frame (tab)

+ +

sendAsyncMessage() : send a message to all frame scripts in this browser frame

+ +

addMessageListener() : start listening to a specific message from frame scripts

+ +

removeMessageListener() : stop listening to a specific message

+
Interfaces +

nsIProcessChecker

+ +

nsIFrameScriptLoader

+ +

nsIMessageListenerManager

+ +

nsIMessageSender

+
How to access +

The browser message manager can be accessed as a property of the XUL <browser> element:

+ +
+// chrome script
+let browserMM = gBrowser.selectedBrowser.messageManager;
+
+ +

Content process

+ +

Content frame message manager

+ + + + + + + + + + + + + + + + +
Description +

There's a content frame message manager for every open tab. It's the content-side end of frame message manager conversations.

+ +

Frame scripts are loaded into the content frame message manager scope, and messages from chrome message managers end up here.

+ +

The content frame message manager provides the global object for frame scripts (but note that there is trickery to ensure that top-level variables defined by frame scripts are not shared).

+ +

Frame scripts can use this object to send messages to the chrome process, and to receive messages from the chrome process.

+ +

Its most important attributes and functions are:

+ +

content : access the DOM window hosted by the tab

+ +

docShell : access the top-level docshell

+ +

Components : access privileged objects and APIs

+ +

addEventListener() : listen to DOM events

+ +

addMessageListener() : receive messages from the chrome process

+ +

sendAsyncMessage() : send asynchronous messages to the chrome process

+ +

sendSyncMessage() : send synchronous messages to the chrome process

+
Interfaces +

nsIDOMEventTarget

+ +

nsIMessageListenerManager

+ +

nsIMessageSender

+ +

nsISyncMessageSender

+ +

nsIContentFrameMessageManager

+
How to accessThe content frame message manager is the global object in frame scripts.
+ +

Process message managers

+ +

Process message managers はプロセス境界を越え、異なるプロセスと通信することを可能にします。マルチプロセスの Firefo のコンセプトは次の通り。

+ + + +

実用的な目的で、マルチプロセスの Firefo の親プロセスは chrome プロセスで、子プロセスは コンテンツプロセスです。

+ +

各子プロセスは、single child process message manager (CPMM) を持ちます。それに加え、親プロエスでは child-in-process message manager (CIPMM) をもっています。

+ +

各子プロセスの message manager は、親プロセスに対応する parent process message manager (PPMM) を持っています。

+ +

親プロセスには 1つの global parent process message manager (GPPMM) をもっており、それがすべての親プロセスの message manager に対するアクセスを提供します。2 つの子プロセスを持つと以下の図のように構築されます。

+ +

+ +

GPPMM を使って、CIPMM とすべての CPMM にブロードキャストすることができます。PPMM は対応する CPMM にだけメッセージを送信できます。CPMM では親プロセスにメッセージを送信できます。まず初めに対応する PPMM が受信でき、次に GPPMM が受信します。
+
+ From Firefox 38 onwards, you can also use a parent process message manager to load a script into a child process. This is the recommended way to load a script that executes just once per child process, which is something you might want to do if you are interacting with some global service (for example, adding listeners to observer notifications or registering a content policy).

+ +

Parent process

+ +

Global parent process message manager

+ + + + + + + + + + + + + + + + +
Description +

The global parent process message manager (GPPMM) is global to the parent process.

+ +
    +
  • Messages sent using the GPPMM get sent to all CPMMs in all child processes.
  • +
  • Process scripts loaded using the GPPMM get loaded in all child processes.
  • +
+ +

Its most important functions and attributes are:

+ +

childCount : contains the number of children (child processes, plus the in-content child)

+ +

getChildAt() : get the child at the given index

+ +

loadProcessScript() : load a process script into every content process

+ +

broadcastAsyncMessage() : send a message to all process scripts

+ +

addMessageListener() : start listening to a specific message from process scripts

+ +

removeMessageListener() : stop listening to a specific message

+
Interfaces +

nsIProcessScriptLoader

+ +

nsIMessageListenerManager

+ +

nsIMessageBroadcaster

+
How to access +

You can access the GPPMM with code like this:

+ +
+// parent process
+let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
+           .getService(Ci.nsIMessageBroadcaster);
+ +

You can also access it as the ppmm property of Services.jsm, if you are in the parent process.

+
+ +

Parent process message manager

+ + + + + + + + + + + + + + + + +
Description +

There's one parent process message manager (PPMM) in the parent process for every child process, and its API is oriented to that one child process.

+ +
    +
  • Messages sent using the PPMM are received only by the corresponding CPMM
  • +
  • Scripts loaded using the PPMM are loaded only into the corresponding child process.
  • +
+ +

Its most important functions are:

+ +

loadProcessScript() : load a process script into the content process

+ +

broadcastAsyncMessage() : send a message to process scripts

+ +

addMessageListener() : start listening to a specific message from process scripts

+ +

removeMessageListener() : stop listening to a specific message

+
Interfaces +

nsIProcessChecker

+ +

nsIProcessScriptLoader

+ +

nsIMessageListenerManager

+ +

nsIMessageSender

+
How to access +

You can access a PPMM using the getChildAt() function in the GPPMM:

+ +
+// parent process
+let ppmm = Services.ppmm.getChildAt(1);
+
+ +

Child process

+ +

Child process message manager

+ + + + + + + + + + + + + + + + +
Description +

There's one child process message manager (CPMM) in each child process. Messages sent using the CPMM are sent to the corresponding PPMM and are also relayed to the GPPMM.

+ +

Its most important attributes and functions are:

+ +

Components : access privileged objects and APIs

+ +

addMessageListener() : receive messages from the parent process

+ +

sendAsyncMessage() : send asynchronous messages to the parent process

+ +

sendSyncMessage() : send synchronous messages to the parent process

+
Interfaces +

nsIMessageListenerManager

+ +

nsIMessageSender

+ +

nsISyncMessageSender

+ +

nsIContentProcessMessageManager

+
How to access +

Code running in a child process can access the CPMM with code like this:

+ +
+// child process script
+let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
+           .getService(Ci.nsISyncMessageSender);
+ +

You can also access it as the cpmm property of Services.jsm, if you are in the child process.

+
-- cgit v1.2.3-54-g00ecf