IPDL Tutorial

  • Revision slug: IPDL/Tutorial
  • Revision title: IPDL Tutorial
  • Revision id: 69595
  • Created:
  • Creator: jdm
  • Is current revision? No
  • Comment 16 words added, 3 words removed

Revision Content

IPDL, short for "Inter-process-communication Protocol Definition Language", is a Mozilla-specific language allowing C++ code to pass messages between processes or threads in an organized and secure way. All messages for multi-process plugins and tabs in Firefox are declared in the IPDL language.

To experiment with adding a new IPDL protocol, see Creating a New Protocol.

All IPDL messages are sent between parent and a child end points, called actors. An IPDL protocol declares how actors communicate: it declares the possible messages that may be sent between actors, as well as a state machine describing when messages are allowed to be sent.

The parent actor is typically the more permanent side of the conversation:

  Parent Child
Parent/Child Actors
IPC Tabs Chrome process Content process
IPC Plugins Content process Plugin Process

Each protocol is declared in a separate file. The IPDL compiler generates several C++ headers from each IPDL protocol. This generated code manages the details of the underlying communication layer (sockets and pipes), constructing and sending messages, ensuring that all actors adhere to their specifications, and handling many error conditions. The following IPDL code defines a very basic interaction of browser and plugin actors:

async protocol PPlugin
{
child:
  Init(nsCString pluginPath);
  Shutdown();

parent:
  Ready()
};

This code declares the PPlugin protocol. Two messages can be sent from the parent to the child, Init() and Shutdown(). One message can be sent from the child to the parent, Ready().

IPDL protocols start with the letter P. The file in which the protocol is declared must have a matching name, PPlugin.ipdl.

Generated C++ Code

When PPlugin.ipdl is compiled, the headers PPluginParent.h, and PPluginChild.h will be generated in ipc/ipdl/_ipdlgen of the build tree. The PPluginParent and PPluginChild classes are abstract classes that must be subclassed. Each outgoing message is a C++ method which can be called. Each incoming message is a pure-virtual C++ method which must be implemented:

class PPluginParent
{
public:
  bool SendInit(const nsCString& pluginPath) {
    // generated code to send an Init() message
  }

  bool SendShutdown() {
    // generated code to send a Shutdown() message
  }

protected:
  /**
   * A subclass of PPluginParent must implement this method to handle the Ready() message.
   */
  bool RecvReady() = 0;
};

class PPluginChild
{
protected:
  bool RecvInit(const nsCString& pluginPath) = 0;
  bool RecvShutdown() = 0;

public:
  bool SendReady() {
    // generated code to send a Ready() message
  }
};

These Parent and Child abstract classes take care of all the "protocol layer" concerns: serializing data, sending and receiving messages, and checking protocol safety. It is the responsibility of the implementor to create subclasses to perform the actual work involved in each message. Here is a dirt-simple example of how a browser implementor might use PPluginParent.

class PluginParent : public PPluginParent
{
public:
  PluginParent(const nsCString& pluginPath) {
    // launch child plugin process
    SendInit(pluginPath);
  }

  ~PluginParent() {
    SendShutdown();
  }

protected:
  bool RecvReady() {
    mObservers.Notify("ready for action");
  }
};

Here's how the PPluginChild might be used by a C++ implementor in the plugin process:

class PluginChild : public PPluginChild 
{
protected:
  void RecvInit(const nsCString& pluginPath) {
    mPluginLibrary = PR_LoadLibrary(pluginPath.get());
    SendReady();
  }
  void RecvShutdown() {
    PR_UnloadLibrary(mPluginLibrary);
  }

private:
  PRLibrary* mPluginLibrary;
};

Launching the subprocess and hooking these protocol actors into our IPC "transport layer" is beyond the scope of this document. See IPDL Processes and Threads for more details.

Because protocol messages are represented as C++ methods, it's easy to forget that they are in fact asynchronous messages: by default the C++ method will return immediately, before the message has been delivered.

Direction

Each message type includes a "direction." The message direction specifies whether the message can be sent from-parent-to-child, from-child-to-parent, or both ways. Three keywords serve as direction specifiers; child was introduced above. The second is parent, which means that the messages declared under the parent label can only be sent from-child-to-parent. The third is both, which means that the declared messages can be sent in both directions. The following artificial example shows how these specifiers are used and how these specifiers change the generated abstract actor classes.

// PDirection.ipdl
async protocol PDirection
{
child:
  Foo();  // can be sent from-parent-to-child
parent:
  Bar();  // can be sent from-child-to-parent
both:
  Baz();  // can be sent both ways
};  
// PDirectionParent.h
class PDirectionParent
{
protected:
  virtual void RecvBar() = 0;
  virtual void RecvBaz() = 0;

public:
  void SendFoo() { /* boilerplate */ }
  void SendBaz() { /* boilerplate */ }
};
// PDirectionChild.h
class PDirectionChild
{
protected:
  virtual void RecvFoo() = 0;
  virtual void RecvBaz() = 0;

public:
  void SendBar() { /* boilerplate */ }
  void SendBaz() { /* boilerplate */ }
};

You can use the child, parent, and both labels multiple times in a protocol specification. They behave like public, protected, and private labels in C++.

Parameters

Message declarations allow any number of parameters. Parameters specify data that are sent with the message. Their values are serialized by the sender and deserialized by the receiver. IPDL supports built-in and custom primitive types, as well as unions and arrays.

The built-in simple types include the C++ integer types (bool, char, int, double) and XPCOM string types (nsString, nsCString). IPDL imports these automatically because they are common, and because the base IPC library knows how to serialize and deserialize these types. See ipc/ipdl/ipdl/builtin.py for the most up-to-date list of automatically imported types.

Actors may be passed as parameters. The C++ signature will accept a PProtocolParent* on one side and convert it to a PProtocolChild* on the other.

Custom primitive types. When you need to send data of a type other than one built into IPDL, you can add a using declaration in an IPDL specification.
A custom serializer and deserializer must be provided by your C++ code.

using mozilla::plugins::NPRemoteEvent;

sync protocol PPluginInstance
{
child:
  HandleEvent(NPRemoteEvent);
};

Unions: IPDL has built-in support for declaring discriminated unions.

using mozilla::ipc::void_t;

union Variant
{
  void_t;
  bool;
  int;
  double;
  nsCString;
  PPluginScriptableObject;
};

Arrays: IPDL has simple syntax for arrays:

InvokeMethod(nsCString[] args);

 In C++ this is translated into a nsTArray reference:

virtual bool RecvInvokeMethod(nsTArray<nsCString>& args);

Synchronous and RPC Messaging

Up until now, all the messages have been asynchronous. The message is sent, and the C++ method returns immediately. But what if we wanted to wait until the message was handled, or get return values from a message?

In IPDL, there are three different semantics:

  1. asynchronous semantics; the sender is not blocked.
  2. Wait until the receiver acknowledges that it received the message. We call this synchronous semantics, as the sender blocks until the receiver receives the message and sends back a reply. The message may have return values.
  3. rpc semantics are a variation on synchronous semantics, see below.

Note that the parent can send messages to the child, and vice versa, so 'sender' and 'receiver' in the above three cases can be either the parent or the child. The messaging semantics applies in the same way to both directions. So, for example, in synchronous semantics from the child to the parent, the child will block until the parent receives the message and the response arrives, and in asynchronous semantics from the parent to the child the parent will not block.

When creating a plugin instance, the browser should block until instance creation is finished, and needs some information returned from the plugin:

sync protocol PPluginInstance
{
child:
    sync Init() returns (bool windowless, bool ok);
};

We added two new keywords to the Plugin protocol, sync and returns. sync marks a message as being sent synchronously (async is the default if no semantic is specified). The returns keyword marks the beginning of the list of values that are returned in the reply to the message. It is an error to add a returns block to an asynchronous message.

To make the blocking nature more noticeable to programmers, the C++ method names for synchronous and RPC messages are different:

  sender receiver
async/sync SendMessageName RecvMessageName
rpc CallMessageName AnswerMessageName

Message Semantics Strength

The above protocol will fail the IPDL type checker. IPDL protocols also have "semantics specifiers", just like messages. A protocol must be declared to have semantics at least as "strong" as its strongest message semantics. Synchronous semantics is called "stronger than" asynchronous. Like message declarations, the default protocol semantics is asynchronous; however, since the Plugin protocol declares a synchronous message, this type rule is violated. The fixed up Plugin protocol is shown below.

sync protocol PPluginInstance
{
child:
   
sync Init() returns (bool windowless, bool ok); };

The generated C++ code for this method uses outparam pointers for the returned values:

class PPluginInstanceParent
{
  ...
  bool CallInit(bool* windowless, bool* ok) { ... };
};

class PPluginInstnaceChild
{
  ...
  virtual bool RecvInit(bool* windowless, bool* ok) = 0;
}

RPC semantics

"RPC" stands for "remote procedure call," and this third semantics models procedure call semantics. A quick summary of the difference between RPC and sync semantics is that RPC allows "re-entrant" message handlers: while an actor is blocked waiting for an "answer" to an RPC "call", it can be unblocked to handle a new, incoming RPC call.

In the example protocol below, the child actor offers a "CallMeCallYou()" RPC interface, and the parent offers a "CallYou()" RPC interface. The rpc qualifiers mean that if the parent calls "CallMeCallYou()" on the child actor, then the child actor, while servicing this call, is allowed to call back into the parent actor's "CallYou()" message.

rpc protocol Example {
child:
    rpc CallMeCallYou() returns (int rv);

parent:
    rpc CallYou() returns (int rv);
};

If this were instead a sync protocol, the child actor would not be allowed to call the parent actor's "CallYou()" method while servicing the "CallMeCallYou()" message. (The child actor would be terminated with extreme prejudice.)

Preferred semantics

Use async semantics whenever possible.

Blocking on replies to messages is discouraged. If you absolutely need to block on a reply, use sync semantics very carefully. It is possible to get into trouble with careless uses of synchronous messages; while IPDL can check and/or guarantee that your code does not deadlock, it is easy to cause nasty performance problems by blocking.

Please don't use RPC semantics. RPC semantics exists mainly to support remoting plugins (NPAPI), where we have no choice.

Chrome to content calls (for IPC tabs) must only use async semantics. In order to preserve responsiveness, the chrome process may never block on a content process which may be busy or hung.

Message Delivery Order

Delivery is "in-order", that is, messages are delivered to the receiver in the order that they are sent, regardless of the messages' semantics.  If an actor A sends messages M1 then M2 to actor B, B will be awoken to process M1 then M2.

Subprotocols and Protocol Management

So far we've seen a single protocol, but no real-world situation would have a single protocol in isolation. Instead, protocols are arranged in a managed hierarchy of subprotocols. A sub-protocol is bound to a "manager" which tracks its lifetime and acts as a factory. A protocol hierarchy begins with a single top-level protocol from which all subprotocol actors are eventually created. In Mozilla there are two main top-level protocols: PPluginModule for remote plugins, and PContent for remote tabs.

The following example extends the toplevel plugin protocol to manage plugin instances.

// ----- file PPlugin.ipdl

include protocol PPluginInstance;
rpc protocol PPlugin
{
    manages PPluginInstance;
child:
    rpc Init(nsCString pluginPath) returns (bool ok);
    rpc PPluginInstance(nsCString type, nsCString[] args) returns (int rv);
};
// ----- file PPluginInstance.ipdl

include protocol PPlugin;

rpc protocol PPluginInstance
{
    manager PPlugin;
child:
    rpc __delete__();
    SetSize(int width, int height);
};

This example has several new elements: `include protocol` imports another protocol declaration into this file. Note that this is not a preprocessor directive, but a part of the IPDL language. The generated C++ code will have proper #include preprocessor directives for the imported protocols.

The `manages` statement declares that this protocol manages PPluginInstance. The PPlugin protocol must declare constructor and destructor messages for PPluginInstance actors. The `manages` statement also means that PPluginInstance actors are tied to the lifetime of the Plugin actor that creates them: if this PPlugin instance is destroyed, all the PPluginInstances associated with it become invalid or are destroyed as well.

The mandatory constructor and destructor messages (PPluginInstance and __delete__ respectively) exist, confusingly, in separate locations.  The constructor must be located in the managing protocol, while the destructor belongs to the managed subprotocol.  These messages have syntax similar to C++ constructors, but the behavior is different. Constructors and destructors have parameters, direction, semantics, and return values like other IPDL messages. A constructor and destructor message must be declared for each managed protocol.

Each subprotocol must include a `manager` statement.

At the C++ layer, the subclasses in both the child and the parent must implement methods for allocating and deallocating the subprotocol actor. The constructor and destructor are translated into standard C++ methods for messages.

Note: __delete__ is a built-in construct, and is the only IPDL message which does not require an overridden implementation (ie. Recv/Answer__delete__).  However, overridden implementations are encouraged when some action should happen on protocol destruction in lieu of using the DeallocPProtocol function.

class PPluginParent
{
  /* Allocate a PPluginInstanceParent when the first form of CallPluginInstanceConstructor is called */
  virtual PPluginInstanceParent* AllocPPluginInstance(const nsCString& type, const nsTArray<nsCString>& args, int* rv) = 0;

  /* Deallocate the PPluginInstanceParent after PPluginInstanceDestructor is done with it */
  virtual bool DeallocPPluginInstance(PPluginInstanceParent* actor) = 0;

  /* constructor message */
  virtual CallPPluginInstanceConstructor(const nsCString& type, const nsTArray<nsCString>& args, int* rv) { /* generated code */ }

  /* alternate form of constructor message: supply your own PPluginInstanceParent* to bypass AllocPPluginInstance */
  virtual bool CallPPluginInstanceConstructor(PPluginInstanceParent* actor, const nsCString& type, const nsTArray<nsCString>& args, int* rv)
  { /* generated code */ }

  /* destructor message */
  virtual bool Call__delete__(PPluginInstanceParent* actor) { /* generated code */ }

  /* Notification that actor deallocation is imminent, IPDL mechanisms are now unusable */
  virtual void ActorDestroy(ActorDestroyReason why);

  ...
};

class PPluginChild
{
  /* Allocate a PPluginInstanceChild when we receive the PPluginInstance constructor */
  virtual PPluginInstanceChild* AllocPPluginInstance(const nsCString& type, const nsTArray<nsCString>& args, int* rv) = 0;

  /* Deallocate a PPluginInstanceChild after we handle the PPluginInstance destructor */
  virtual bool DeallocPPluginInstance(PPluginInstanceChild* actor) = 0;

  /* Answer the constructor message. Implementing this method is optional: it may be possible to answer the message directly in AllocPPluginInstance. */
  virtual bool AnswerPPluginInstanceConstructor(PPluginInstanceChild* actor, const nsCString& type, const nsTArray<nsCString>& args, int* rv) { }

  /* Answer the desctructor message. */
  virtual bool Answer__delete__(PPluginInstanceChild* actor) = 0;

  /* Notification that actor deallocation is imminent, IPDL mechanisms are now unusable */
  virtual void ActorDestroy(ActorDestroyReason why);

  ...
};

Subprotocol Actor Lifetime

AllocPProtocol and DeallocPProtocol are a matched pair of functions. The typical implementation of these functions uses `new` and `delete`:

class PluginChild : PPluginChild
{
 virtual PPluginInstanceChild* AllocPPluginInstance(const nsCString& type, const nsTArray<nsCString>& args, int* rv)
  {
    return new PluginInstanceChild(type, args, rv);
  }

  virtual bool DeallocPPluginInstanceChild(PPluginInstanceChild* actor)
  {
    delete actor; // actor destructors are always virtual, so it's safe to call delete on them!
    return true;
  }

  ...
};

In some cases, however, external code may hold references to actor implementations which require refcounting or other lifetime strategies. In this case, the alloc/dealloc pairs can perform different actions. Here is an example of refcounting:

class ExampleChild : public nsIObserver, public PExampleChild { ... };

virtual PExampleChild* TopLevelChild::AllocPExample()
{
  nsRefPtr<ExampleChild*> actor = new ExampleChild();
  return actor.forget();
}

virtual bool TopLevelChild::DeallocPExample(PExampleChild* actor)
{
  NS_RELEASE(static_cast<ExampleChild*>(actor));
  return true;
}

If an object that implements a protocol can't be constructed inside AllocPFoo, or has been previously constructed and doesn't require an IPDL connection throughout its lifetime, there is a second form of SendPFooConstructor which can be used:

class ExampleChild
{
public:
    void DoSomething() {
        aManagerChild->SendPExampleConstructor(this, ...);
    }
};

Internally, the first constructor form simply calls

PExample(Parent|Child)* actor = AllocPExample(...);
SendPExampleConstructor(actor, ...);
return actor;

with the same effect.

Subprotocol Deletion

It is worth understanding the protocol deletion process.  Given the simple protocols:

// --- PExample.ipdl
include protocol PSubExample;

async protocol PExample
{
    manages PSubExample;

parent:
   
PChild(); }; // --- PSubExample.ipdl include protocol PExample; async protocol PSubExample {     manager PExample; child:
    __delete__(); };

We assume that there is a PSubExampleParent/Child pair in existence, such that some element now wishes to trigger the protocol's deletion from the parent side.

aPSubExampleParent->Send__delete__();

will trigger the following ordered function calls:

PSubExampleParent::ActorDestroy(Deletion) /* Deletion is an enumerated value indicating that the destruction was intentional */
PExampleParent::DeallocPSubExample()
PSubExampleChild::Recv__delete__()
PSubExampleChild::ActorDestroy(Deletion)
PExampleChild::DeallocPSubExample()

ActorDestroy is a generated function that allows code to run with the knowledge that actor deallocation is imminent.  This is useful for actors with lifetimes outside of IPDL - for instance, a flag could be set indicating that IPDL-related functions are no longer safe to use.

Shutdown and Error Handling

The C++ methods which implement IPDL messages return bool: true for success, and false for catastrophic failure. Message implementations should return false from a message implementation if the data is corrupted or otherwise malformed. Any time a message implementation returns false, IPDL will immediately begin catastrophic error handling: the communication channels for the child process (tab or plugin) will be disconnected, and the process will be terminated. Do not return false from message handlers for "normal" error conditions such as inability to load a network request! Normal errors should be signaled with a message or return value.

Note: the following paragraphs are not yet implemented. IPDL tracks all active protocols between two endpoints. If if the child side crashes or becomes hung:

  • any synchronous or RPC messages currently active will return false
  • no further messages will be accepted (C++ methods will return false)
  • each IPDL actor will receive an OnError message
  • DeallocPSubprotocol will be called on each manager protocol to deallocate any active subprotocols.

When a manager protocol is destroyed, any subprotocols will be notified:

  • no further messages will be accepted
  • DeallocPSubprotocol will be called on the manager protocol to deallocate any active subprotocols

When the toplevel protocol is destroyed, this is equivalent to shutting down the entire IPDL machinery for that connection, because no more messages can be sent and all subprotocols are destroyed.

Protocol state machines

The astute reader might question why IPDL includes the word "protocol" when all that has been introduced so far are unstructured grab-bags of messages. IPDL allows protocol authors to define the order and structure of how messages may be sent/received by defining protocol state machines (finite state machines).

[Note that the state machine portion of the IPDL compiler is not complete as of this writing, 22 October 2009. IPDL code for state machines is accepted by the compiler, but it does not affect the generated C++, yet.]

IPDL parent and child actors communicating via a protocol are paired. Each actor in the pair follows the same state machine. The pair attempts to keep their single collective state synchronized. Though, it is possible that the parent and child actors may be momentarily out of sync while messages are transmitted.

IPDL (arbitrarily) requires state machines to be defined from the perspective of the parent side of the protocol. For example, when you see the send Msg syntax, it means "when the parent actor sends Msg".

The following example shows one such state machine for the Plugin protocol.

Note: The following example uses the old ~Destructor syntax, and needs significant reworking to make use of the new __delete__ syntax instead.  This is no longer a good example.
include protocol PPluginInstance;

sync protocol PPlugin {
  manages PPluginInstance;

child:
  sync Init() returns (int rv);
  Deinit();

  sync PPluginInstance(String type, StringArray args) returns (int rv); 

// NOTE: state machine follows
state START:
  send Init goto IDLE;

state IDLE:
  send PPluginInstance goto ACTIVE;

state ACTIVE:
  send PPluginInstance goto ACTIVE;
  send ~PPluginInstance goto ACTIVE;
  send Deinit goto DYING;

state DYING:
  send ~PPluginInstance goto DYING;
};

There are three new syntactic elements, above. First are "state declarations": the code state FOO: declares a state "FOO". (States are capitalized by convention, not because of syntactic rules.) The first state to be declared is the protocol's "start state"; when an actor is created, its initial state is the "start state."

The second new syntactic element is the trigger. The syntax send MsgDecl defines a trigger for a state transition; in this case, the trigger is sending the async or sync message "MsgDecl." The triggers are:

  1. sending an async or sync message
  2. recving an async or sync message
  3. calling an RPC
  4. answering an RPC

Aside: this is why actor ctors/dtors act like normal messages, with directions etc.: this allows them to be checked against the protocol state machine like any other message.

The third new syntactic element is a state transition. The syntax is: goto NEXT_STATE. When the trigger preceding this transition occurs, the protocol actor's internal state is changed to, in this case, "NEXT_STATE."

Another example state machine, for PluginInstance, follows.

sync protocol PluginInstance {
  manager Plugin;

child:
  SetWindow(PluginWindow window);
  Paint();

parent:
  sync GetBrowserValue(String key) returns (String value);

state START:
  send SetWindow goto SENT_WINDOW;
  recv GetBrowserValue goto START;

state SENT_WINDOW:
  send SetWindow goto SENT_WINDOW;
  send Paint goto SENT_WINDOW;
  recv GetBrowserValue goto SENT_WINDOW;
};


Note:

  • The following points will apply when the IPDL compiler fully supports states. It is incomplete as of this writing, on 22 October 2009.
  • Protocol state machines are optional, but strongly encouraged. Even simple state machines are useful.
  • All actor states, trigger matching, and transitions are managed by IPDL-generated code. Your C++ code need not manage these things.
  • All messages sent and received are checked against the protocol's state machine. If a message violates the state machine semantics defined in the IPDL code, the child process containing the child actor will be terminated with extreme prejudice, and all parent actors will be made invalid! It is up to the developer to make sure that this never happens.
  • Lots of syntactic sugar is possible for state machine definitions. Ping the Electrolysis team if you have a good proposal

Revision Source

<p>IPDL, short for "Inter-process-communication Protocol Definition Language", is a Mozilla-specific language allowing C++ code to pass messages between processes or threads in an organized and secure way. All messages for multi-process plugins and tabs in Firefox are declared in the IPDL language.</p>
<div class="note">To experiment with adding a new IPDL protocol, see <a href="/en/IPDL/Creating_a_New_Protocol" title="en/IPDL/Creating a New Protocol"><span>Creating a New Protocol</span></a>.</div>
<p>All IPDL messages are sent between <strong>parent</strong> and a <strong>child</strong> end points, called <strong>actors</strong>. An IPDL <strong>protocol</strong> declares how actors communicate: it declares the possible <strong>messages</strong> that may be sent between actors, as well as a <strong>state machine</strong> describing when messages are allowed to be sent.</p>
<p>The parent actor is typically the more permanent side of the conversation:</p>
<table border="1" cellpadding="2" cellspacing="0" style="table-layout: fixed;"> <thead> <tr> <th scope="col"> </th> <th scope="col">Parent</th> <th scope="col">Child</th> </tr> </thead> <caption>Parent/Child Actors</caption> <tbody> <tr> <th scope="row">IPC Tabs</th> <td>Chrome process</td> <td>Content process</td> </tr> <tr> <th scope="row">IPC Plugins</th> <td>Content process</td> <td>Plugin Process</td> </tr> </tbody>
</table>
<p>Each protocol is declared in a separate file. The IPDL compiler generates several C++ headers from each IPDL protocol. This generated code manages the details of the underlying communication layer (sockets and pipes), constructing and sending messages, ensuring that all actors adhere to their specifications, and handling many error conditions. The following IPDL code defines a very basic interaction of browser and plugin actors:</p>
<pre><strong>async protocol</strong> PPlugin
{
<strong>child:</strong>
  Init(nsCString pluginPath);
  Shutdown();

<strong>parent:</strong>
  Ready()
};
</pre>
<p>This code declares the <code>PPlugin</code> protocol. Two messages can be sent from the parent to the child, <code>Init()</code> and <code>Shutdown()</code>. One message can be sent from the child to the parent, <code>Ready()</code>.</p>
<div class="note">IPDL protocols start with the letter P. The file in which the protocol is declared must have a matching name, PPlugin.ipdl.</div>
<h3><span class="mw-headline">Generated C++ Code<br>
</span></h3>
<p>When PPlugin.ipdl is compiled, the headers <code>PPluginParent.h</code>, and <code>PPluginChild.h</code> will be generated in ipc/ipdl/_ipdlgen of the build tree. The PPluginParent and PPluginChild classes are abstract classes that must be subclassed. Each outgoing message is a C++ method which can be called. Each incoming message is a pure-virtual C++ method which must be implemented:</p>
<pre>class PPluginParent
{
public:
  bool SendInit(const nsCString&amp; pluginPath) {
    // generated code to send an Init() message
  }

  bool SendShutdown() {
    // generated code to send a Shutdown() message
  }

protected:
  /**
   * A subclass of PPluginParent must implement this method to handle the Ready() message.
   */
  bool RecvReady() = 0;
};

class PPluginChild
{
protected:
  bool RecvInit(const nsCString&amp; pluginPath) = 0;
  bool RecvShutdown() = 0;

public:
  bool SendReady() {
    // generated code to send a Ready() message
  }
};
</pre>
<p>These Parent and Child abstract classes take care of all the "protocol layer" concerns: serializing data, sending and receiving messages, and checking protocol safety. It is the responsibility of the implementor to create subclasses to perform the actual work involved in each message. Here is a dirt-simple example of how a browser implementor might use PPluginParent.</p>
<pre>class PluginParent : public PPluginParent
{
public:
  PluginParent(const nsCString&amp; pluginPath) {
    // launch child plugin process
    SendInit(pluginPath);
  }

  ~PluginParent() {
    SendShutdown();
  }

protected:
  bool RecvReady() {
    mObservers.Notify("ready for action");
  }
};
</pre>
<p>Here's how the PPluginChild might be used by a C++ implementor in the plugin process:</p>
<pre>class PluginChild : public PPluginChild 
{
protected:
  void RecvInit(const nsCString&amp; pluginPath) {
    mPluginLibrary = PR_LoadLibrary(pluginPath.get());
    SendReady();
  }
  void RecvShutdown() {
    PR_UnloadLibrary(mPluginLibrary);
  }

private:
  PRLibrary* mPluginLibrary;
};
</pre>
<p>Launching the subprocess and hooking these protocol actors into our IPC "transport layer" is beyond the scope of this document. See <a href="/en/IPDL/Processes_and_Threads" title="en/IPDL/Processes and Threads">IPDL Processes and Threads</a> for more details.</p>
<p>Because protocol messages are represented as C++ methods, it's easy to forget that they are in fact asynchronous messages: by default the C++ method will return immediately, before the message has been delivered.</p>
<h3><span class="mw-headline"> Direction </span></h3>
<p>Each message type includes a "direction." The message direction specifies whether the message can be sent from-parent-to-child, from-child-to-parent, or both ways. Three keywords serve as direction specifiers; <strong>child</strong> was introduced above. The second is <strong>parent</strong>, which means that the messages declared under the <strong>parent</strong> label can only be sent from-child-to-parent. The third is <strong>both</strong>, which means that the declared messages can be sent in both directions. The following artificial example shows how these specifiers are used and how these specifiers change the generated abstract actor classes.</p>
<pre>// PDirection.ipdl
<strong>async protocol</strong> PDirection
{
<strong>child:</strong>
  Foo();  // can be sent from-parent-to-child
<strong>parent:</strong>
  Bar();  // can be sent from-child-to-parent
<strong>both:</strong>
  Baz();  // can be sent both ways
};  
</pre>
<pre>// PDirectionParent.h
class PDirectionParent
{
protected:
  virtual void RecvBar() = 0;
  virtual void RecvBaz() = 0;

public:
  void SendFoo() { /* boilerplate */ }
  void SendBaz() { /* boilerplate */ }
};
</pre>
<pre>// PDirectionChild.h
class PDirectionChild
{
protected:
  virtual void RecvFoo() = 0;
  virtual void RecvBaz() = 0;

public:
  void SendBar() { /* boilerplate */ }
  void SendBaz() { /* boilerplate */ }
};
</pre>
<p>You can use the <code>child</code>, <code>parent</code>, and <code>both <span style="font-family: Verdana,Tahoma,sans-serif;">labels </span></code>multiple times in a protocol specification. They behave like <code>public</code>, <code>protected</code>, and <code>private </code>labels in C++.</p>
<h3><span class="mw-headline"> Parameters </span></h3>
<p>Message declarations allow any number of <strong>parameters</strong>. Parameters specify data that are sent with the message. Their values are serialized by the sender and deserialized by the receiver. IPDL supports built-in and custom primitive types, as well as unions and arrays.</p>
<div>
<div>
<div class="f">
<p>The built-in simple types include the C++ integer types (bool, char, int, double) and XPCOM string types (<code>nsString</code>, <code>nsCString</code>). IPDL imports these automatically because they are common, and because the base IPC library knows how to serialize and deserialize these types. See <code>ipc/ipdl/ipdl/builtin.py</code> for the most up-to-date list of automatically imported types.</p>
<p>Actors may be passed as parameters. The C++ signature will accept a PProtocolParent* on one side and convert it to a PProtocolChild* on the other.</p>
<p>Custom primitive types. When you need to send data of a type other than one built into IPDL, you can add a <code>using</code> declaration in an IPDL specification.<br>
A <a href="/en/IPDL/Type_Serialization" title="en/IPDL/Type Serialization">custom serializer and deserializer</a> must be provided by your C++ code.</p>
<pre><strong>using</strong> mozilla::plugins::NPRemoteEvent;

<strong>sync protocol</strong> PPluginInstance
{
<strong>child:</strong>
  HandleEvent(NPRemoteEvent);
};
</pre>
<p>Unions: IPDL has built-in support for declaring discriminated unions.</p>
<pre>using mozilla::ipc::void_t;

union Variant
{
  void_t;
  bool;
  int;
  double;
  nsCString;
  PPluginScriptableObject;
};</pre>
<p>Arrays: IPDL has simple syntax for arrays:</p>
<pre>InvokeMethod(nsCString[] args);</pre>
<p> In C++ this is translated into a <a href="/en/XPCOM_array_guide#nsTArray.3cT.3e" title="en/XPCOM array guide#nsTArray.3cT.3e">nsTArray</a> reference:</p>
<pre>virtual bool RecvInvokeMethod(nsTArray&lt;nsCString&gt;&amp; args);
</pre>
</div>
</div>
</div>
<h3><span class="mw-headline"> Synchronous and RPC Messaging<br>
</span></h3>
<p>Up until now, all the messages have been <strong>asynchronous</strong>. The message is sent, and the C++ method returns immediately. But what if we wanted to wait until the message was handled, or get return values from a message?</p>
<p>In IPDL, there are three different semantics:</p>
<ol> <li><strong>asynchronous</strong> semantics; the sender is not blocked.</li> <li>Wait until the receiver acknowledges that it received the message. We call this <strong>synchronous</strong> semantics, as the sender blocks until the receiver receives the message and sends back a reply. The message may have return values.</li> <li><strong>rpc</strong> semantics are a variation on synchronous semantics, see below.</li>
</ol>
<p>Note that the parent can send messages to the child, and vice versa, so 'sender' and 'receiver' in the above three cases can be either the parent or the child. The messaging semantics applies in the same way to both directions. So, for example, in synchronous semantics from the child to the parent, the child will block until the parent receives the message and the response arrives, and in <strong>a</strong>synchronous semantics from the parent to the child the parent will not block.</p>
<p>When creating a plugin instance, the browser should block until instance creation is finished, and needs some information returned from the plugin:</p>
<pre><strong>sync</strong> <strong>protocol</strong> PPluginInstance
{
<strong>child:</strong>
    <strong>sync</strong> Init() <strong>returns</strong> (bool windowless, bool ok);
};
</pre>
<p>We added two new keywords to the Plugin protocol, <strong>sync</strong> and <strong>returns</strong>. <strong>sync</strong> marks a message as being sent synchronously (async is the default if no semantic is specified). The <strong>returns</strong> keyword marks the beginning of the list of values that are returned in the reply to the message. It is an error to add a <strong>returns</strong> block to an asynchronous message.</p>
<p>To make the blocking nature more noticeable to programmers, the C++ method names for synchronous and RPC messages are different:</p>
<table border="1" cellpadding="2" cellspacing="0" style="table-layout: fixed;"> <thead> <tr> <th scope="col"> </th> <th scope="col">sender</th> <th scope="col">receiver</th> </tr> </thead> <tbody> <tr> <th scope="row">async/sync</th> <td>Send<em>MessageName</em></td> <td>Recv<em>MessageName</em></td> </tr> <tr> <th scope="row">rpc</th> <td>Call<em>MessageName</em></td> <td>Answer<em>MessageName</em></td> </tr> </tbody>
</table>
<h4><span class="mw-headline"> Message Semantics Strength </span></h4>
<p>The above protocol will fail the IPDL type checker. IPDL protocols also have "semantics specifiers", just like messages. A protocol must be declared to have semantics at least as "strong" as its strongest message semantics. Synchronous semantics is called "stronger than" asynchronous. Like message declarations, the default protocol semantics is asynchronous; however, since the Plugin protocol declares a synchronous message, this type rule is violated. The fixed up Plugin protocol is shown below.</p>
<pre><strong>sync protocol</strong> PPluginInstance
{
<strong>child:<br>    </strong><strong>sync</strong> Init() <strong>returns</strong> (bool windowless, bool ok);
};</pre>
<p>The generated C++ code for this method uses outparam pointers for the returned values:</p>
<pre>class PPluginInstanceParent
{
  ...
  bool CallInit(bool* windowless, bool* ok) { ... };
};

class PPluginInstnaceChild
{
  ...
  virtual bool RecvInit(bool* windowless, bool* ok) = 0;
}</pre>
<h4><span class="mw-headline">RPC semantics </span></h4>
<p>"RPC" stands for "remote procedure call," and this third semantics models procedure call semantics. A quick summary of the difference between RPC and sync semantics is that RPC allows "re-entrant" message handlers: while an actor is blocked waiting for an "answer" to an RPC "call", it can be <em>unblocked</em> to handle a new, incoming RPC <em>call</em>.</p>
<p>In the example protocol below, the child actor offers a "CallMeCallYou()" RPC interface, and the parent offers a "CallYou()" RPC interface. The <code><strong>rpc</strong></code> qualifiers mean that if the parent calls "CallMeCallYou()" on the child actor, then the child actor, while servicing this call, is allowed to call back into the parent actor's "CallYou()" message.</p>
<pre><strong>rpc</strong> <strong>protocol</strong> Example {
<strong>child</strong>:
    <strong>rpc</strong> CallMeCallYou() <strong>returns</strong> (int rv);

<strong>parent</strong>:
    <strong>rpc</strong> CallYou() <strong>returns</strong> (int rv);
};
</pre>
<p>If this were instead a sync protocol, the child actor would not be allowed to call the parent actor's "CallYou()" method while servicing the "CallMeCallYou()" message. (The child actor would be terminated with extreme prejudice.)</p>
<h4><span class="mw-headline">Preferred semantics </span></h4>
<p>Use <strong>async</strong> semantics whenever possible.</p>
<p>Blocking on replies to messages is discouraged. If you absolutely need to block on a reply, use <strong>sync</strong> semantics <em>very carefully</em>. It is possible to get into trouble with careless uses of synchronous messages; while IPDL can check and/or guarantee that your code does not deadlock, it is easy to cause nasty performance problems by blocking.</p>
<p>Please don't use RPC semantics. RPC semantics exists mainly to support remoting plugins (NPAPI), where we have no choice.</p>
<div class="note">Chrome to content calls (for IPC tabs) must only use async semantics. In order to preserve responsiveness, the chrome process may never block on a content process which may be busy or hung.</div>
<h3>Message Delivery Order</h3>
<p>Delivery is "in-order", that is, messages are delivered to the receiver in the order that they are sent, regardless of the messages' semantics.  If an actor A sends messages M1 then M2 to actor B, B will be awoken to process M1 <em>then</em> M2.</p>
<h2><span class="mw-headline"> Subprotocols and Protocol Management </span></h2>
<p>So far we've seen a single protocol, but no real-world situation would have a single protocol in isolation. Instead, protocols are arranged in a managed hierarchy of <strong>subprotocols</strong>. A sub-protocol is bound to a "manager" which tracks its lifetime and acts as a factory. A protocol hierarchy begins with a single top-level protocol from which all subprotocol actors are eventually created. In Mozilla there are two main top-level protocols: <a class=" external" href="http://mxr.mozilla.org/projects-central/source/electrolysis/dom/plugins/PPluginModule.ipdl" title="http://mxr.mozilla.org/projects-central/source/electrolysis/dom/plugins/PPluginModule.ipdl">PPluginModule</a> for remote plugins, and <a class=" external" href="http://mxr.mozilla.org/mozilla-central/source/dom/ipc/PContent.ipdl" title="http://mxr.mozilla.org/mozilla-central/source/dom/ipc/PContent.ipdl">PContent</a> for remote tabs.</p>
<p>The following example extends the toplevel plugin protocol to manage plugin instances.</p>
<pre><code>// ----- file PPlugin.ipdl<br><br><strong>include protocol</strong> PPluginInstance;</code>

<code><strong>rpc protocol</strong> PPlugin<br>{<br><strong>    manages</strong> PPluginInstance;<br><strong>child:</strong><br>    <strong>rpc</strong> Init(nsCString pluginPath) <strong>returns</strong> (bool ok);<br>    <span style="font-weight: bold;">rpc </span>PPluginInstance(nsCString type, nsCString[] args) <strong>returns</strong> (int rv);<br></code><code>};</code>
</pre>
<pre><code>// ----- file PPluginInstance.ipdl<br><br><strong>include protocol</strong> PPlugin;<br><br><strong>rpc protocol</strong> PPluginInstance<br>{<br><strong>    manager</strong> PPlugin;<br><strong>child:</strong><br>    <strong>rpc</strong> __delete__();<br>    SetSize(int width, int height);<br>};</code></pre>
<p>This example has several new elements: `include protocol` imports another protocol declaration into this file. Note that this is not a preprocessor directive, but a part of the IPDL language. The generated C++ code will have proper #include preprocessor directives for the imported protocols.</p>
<p>The `manages` statement declares that this protocol manages PPluginInstance. The PPlugin protocol must declare constructor and destructor messages for PPluginInstance actors. The `manages` statement also means that PPluginInstance actors are tied to the lifetime of the Plugin actor that creates them: if this PPlugin instance is destroyed, all the PPluginInstances associated with it become invalid or are destroyed as well.</p>
<p>The mandatory constructor and destructor messages (PPluginInstance and __delete__ respectively) exist, confusingly, in separate locations.  The constructor must be located in the managing protocol, while the destructor belongs to the managed subprotocol.  These messages have syntax similar to C++ constructors, but the behavior is different. Constructors and destructors have parameters, direction, semantics, and return values like other IPDL messages. A constructor and destructor message must be declared for each managed protocol.</p>
<p>Each subprotocol must include a `manager` statement.</p>
<p>At the C++ layer, the subclasses in both the child and the parent must implement methods for allocating and deallocating the subprotocol actor. The constructor and destructor are translated into standard C++ methods for messages.</p>
<p>Note: __delete__ is a built-in construct, and is the only IPDL message which does not require an overridden implementation (ie. Recv/Answer__delete__).  However, overridden implementations are encouraged when some action should happen on protocol destruction in lieu of using the DeallocPProtocol function.</p>
<pre>class PPluginParent
{
  /* Allocate a PPluginInstanceParent when the first form of CallPluginInstanceConstructor is called */
  virtual PPluginInstanceParent* AllocPPluginInstance(const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv) = 0;

  /* Deallocate the PPluginInstanceParent after PPluginInstanceDestructor is done with it */
  virtual bool DeallocPPluginInstance(PPluginInstanceParent* actor) = 0;

  /* constructor message */
  virtual CallPPluginInstanceConstructor(const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv) { /* generated code */ }

  /* alternate form of constructor message: supply your own PPluginInstanceParent* to bypass AllocPPluginInstance */
  virtual bool CallPPluginInstanceConstructor(PPluginInstanceParent* actor, const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv)
  { /* generated code */ }

  /* destructor message */
  virtual bool Call__delete__(PPluginInstanceParent* actor) { /* generated code */ }

  /* Notification that actor deallocation is imminent, IPDL mechanisms are now unusable */
  virtual void ActorDestroy(ActorDestroyReason why);

  ...
};

class PPluginChild
{
  /* Allocate a PPluginInstanceChild when we receive the PPluginInstance constructor */
  virtual PPluginInstanceChild* AllocPPluginInstance(const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv) = 0;

  /* Deallocate a PPluginInstanceChild after we handle the PPluginInstance destructor */
  virtual bool DeallocPPluginInstance(PPluginInstanceChild* actor) = 0;

  /* Answer the constructor message. Implementing this method is optional: it may be possible to answer the message directly in AllocPPluginInstance. */
  virtual bool AnswerPPluginInstanceConstructor(PPluginInstanceChild* actor, const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv) { }

  /* Answer the desctructor message. */
  virtual bool Answer__delete__(PPluginInstanceChild* actor) = 0;

  /* Notification that actor deallocation is imminent, IPDL mechanisms are now unusable */
  virtual void ActorDestroy(ActorDestroyReason why);

  ...
};</pre>
<h4>Subprotocol Actor Lifetime</h4>
<p>AllocPProtocol and DeallocPProtocol are a matched pair of functions. The typical implementation of these functions uses `new` and `delete`:</p>
<pre>class PluginChild : PPluginChild
{
 virtual PPluginInstanceChild* AllocPPluginInstance(const nsCString&amp; type, const nsTArray&lt;nsCString&gt;&amp; args, int* rv)
  {
    return new PluginInstanceChild(type, args, rv);
  }

  virtual bool DeallocPPluginInstanceChild(PPluginInstanceChild* actor)
  {
    delete actor; // actor destructors are always virtual, so it's safe to call delete on them!
    return true;
  }

  ...
};</pre>
<p>In some cases, however, external code may hold references to actor implementations which require refcounting or other lifetime strategies. In this case, the alloc/dealloc pairs can perform different actions. Here is an example of refcounting:</p>
<pre>class ExampleChild : public nsIObserver, public PExampleChild { ... };

virtual PExampleChild* TopLevelChild::AllocPExample()
{
  nsRefPtr&lt;ExampleChild*&gt; actor = new ExampleChild();
  return actor.forget();
}

virtual bool TopLevelChild::DeallocPExample(PExampleChild* actor)
{
  NS_RELEASE(static_cast&lt;ExampleChild*&gt;(actor));
  return true;
}
</pre>
<p>If an object that implements a protocol can't be constructed inside AllocPFoo, or has been previously constructed and doesn't require an IPDL connection throughout its lifetime, there is a second form of SendPFooConstructor which can be used:</p>
<pre>class ExampleChild
{
public:
    void DoSomething() {
        aManagerChild-&gt;SendPExampleConstructor(this, ...);
    }
};
</pre>
<p>Internally, the first constructor form simply calls</p>
<pre>PExample(Parent|Child)* actor = AllocPExample(...);
SendPExampleConstructor(actor, ...);
return actor;
</pre>
<p>with the same effect.</p>
<h4>Subprotocol Deletion</h4>
<p>It is worth understanding the protocol deletion process.  Given the simple protocols:</p>
<pre>// --- PExample.ipdl
<strong>include protocol</strong> PSubExample;

<strong>async protocol</strong> PExample
{
    <strong>manages</strong> PSubExample;

<strong>p</strong><strong>arent:<br>    </strong>PChild();
};

// --- PSubExample.ipdl
<strong>include protocol</strong> PExample;

<strong>async protocol</strong> PSubExample
{
    <strong>manager </strong>PExample;

<strong>child:<br></strong>    __delete__();
};
</pre>
<p>We assume that there is a PSubExampleParent/Child pair in existence, such that some element now wishes to trigger the protocol's deletion from the parent side.</p>
<pre><code><code>aPSubExampleParent-&gt;Send__delete__();</code></code></pre>
<p>will trigger the following ordered function calls:</p>
<pre>PSubExampleParent::ActorDestroy(Deletion) /* Deletion is an enumerated value indicating that the destruction was intentional */
PExampleParent::DeallocPSubExample()</pre>
<pre>PSubExampleChild::Recv__delete__()
PSubExampleChild::ActorDestroy(Deletion)
PExampleChild::DeallocPSubExample()</pre>
<p>ActorDestroy is a generated function that allows code to run with the knowledge that actor deallocation is imminent.  This is useful for actors with lifetimes outside of IPDL - for instance, a flag could be set indicating that IPDL-related functions are no longer safe to use.</p>
<h2>Shutdown and Error Handling</h2>
<p>The C++ methods which implement IPDL messages return <code>bool</code>: true for success, and false for catastrophic failure. Message implementations should return false from a message implementation if the data is corrupted or otherwise malformed. Any time a message implementation returns false, IPDL will immediately begin catastrophic error handling: the communication channels for the child process (tab or plugin) will be disconnected, and the process will be terminated. Do not return false from message handlers for "normal" error conditions such as inability to load a network request! Normal errors should be signaled with a message or return value.</p>
<p><em>Note: the following paragraphs are not yet implemented.</em> IPDL tracks all active protocols between two endpoints. If if the child side crashes or becomes hung:</p>
<ul> <li>any synchronous or RPC messages currently active will return false</li> <li>no further messages will be accepted (C++ methods will return false)</li> <li>each IPDL actor will receive an OnError message</li> <li>DeallocPSubprotocol will be called on each manager protocol to deallocate any active subprotocols.</li>
</ul>
<p>When a manager protocol is destroyed, any subprotocols will be notified:</p>
<ul> <li>no further messages will be accepted</li> <li>DeallocPSubprotocol will be called on the manager protocol to deallocate any active subprotocols</li>
</ul>
<p>When the toplevel protocol is destroyed, this is equivalent to shutting down the entire IPDL machinery for that connection, because no more messages can be sent and all subprotocols are destroyed.</p>
<h2><span class="mw-headline">Protocol state machines </span></h2>
<p>The astute reader might question why IPDL includes the word "protocol" when all that has been introduced so far are unstructured grab-bags of messages. IPDL allows protocol authors to define the order and structure of how messages may be sent/received by defining protocol <em>state machines</em> (finite state machines).</p>
<p>[Note that the state machine portion of the IPDL compiler is not complete as of this writing, 22 October 2009. IPDL code for state machines is accepted by the compiler, but it does not affect the generated C++, yet.]</p>
<p>IPDL parent and child actors communicating via a protocol are paired. Each actor in the pair follows the same state machine. The pair attempts to keep their single collective state synchronized. Though, it is possible that the parent and child actors may be momentarily out of sync while messages are transmitted.</p>
<p>IPDL (arbitrarily) requires state machines to be defined from the perspective of the <strong>parent</strong> side of the protocol. For example, when you see the <code><strong>send</strong> Msg</code> syntax, it means "when the parent actor sends Msg".</p>
<p>The following example shows one such state machine for the Plugin protocol.</p>
<div class="warning">Note: The following example uses the old ~Destructor syntax, and needs significant reworking to make use of the new __delete__ syntax instead.  This is no longer a good example.</div>
<pre><strong>include</strong> <strong>protocol</strong> PPluginInstance;

<strong>sync</strong> <strong>protocol</strong> PPlugin {
  <strong>manages</strong> PPluginInstance;

<strong>child</strong>:
  <strong>sync</strong> Init() <strong>returns</strong> (int rv);
  Deinit();

  <strong>sync</strong> PPluginInstance(String type, StringArray args) <strong>returns</strong> (int rv); 

// NOTE: state machine follows
<strong>state</strong> START:
  <strong>send</strong> Init <strong>goto</strong> IDLE;

<strong>state</strong> IDLE:
  <strong>send</strong> PPluginInstance <strong>goto</strong> ACTIVE;

<strong>state</strong> ACTIVE:
  <strong>send</strong> PPluginInstance <strong>goto</strong> ACTIVE;
  <strong>send</strong> ~PPluginInstance <strong>goto</strong> ACTIVE;
  <strong>send</strong> Deinit <strong>goto</strong> DYING;

<strong>state</strong> DYING:
  <strong>send</strong> ~PPluginInstance <strong>goto</strong> DYING;
};
</pre>
<p>There are three new syntactic elements, above. First are "state declarations": the code <code><strong>state</strong> FOO:</code> declares a state "FOO". (States are capitalized by convention, not because of syntactic rules.) The first state to be declared is the protocol's "start state"; when an actor is created, its initial state is the "start state."</p>
<p>The second new syntactic element is the <em>trigger</em>. The syntax <code><strong>send</strong> MsgDecl</code> defines a trigger for a state <em>transition</em>; in this case, the trigger is <code><strong>send</strong></code>ing the async or sync message "MsgDecl." The triggers are:</p>
<ol> <li><code><strong>send</strong></code>ing an async or sync message</li> <li><code><strong>recv</strong></code>ing an async or sync message</li> <li><code><strong>call</strong></code>ing an RPC</li> <li><code><strong>answer</strong></code>ing an RPC</li>
</ol>
<p><strong>Aside</strong>: this is why actor ctors/dtors act like normal messages, with directions etc.: this allows them to be checked against the protocol state machine like any other message.</p>
<p>The third new syntactic element is a state <em>transition</em>. The syntax is: <code><strong>goto</strong> NEXT_STATE</code>. When the trigger preceding this transition occurs, the protocol actor's internal state is changed to, in this case, "NEXT_STATE."</p>
<p>Another example state machine, for PluginInstance, follows.</p>
<pre><strong>sync</strong> <strong>protocol</strong> PluginInstance {
  <strong>manager</strong> Plugin;

<strong>child</strong>:
  SetWindow(PluginWindow window);
  Paint();

<strong>parent</strong>:
  <strong>sync</strong> GetBrowserValue(String key) <strong>returns</strong> (String value);

<strong>state</strong> START:
  <strong>send</strong> SetWindow <strong>goto</strong> SENT_WINDOW;
  <strong>recv</strong> GetBrowserValue <strong>goto</strong> START;

<strong>state</strong> SENT_WINDOW:
  <strong>send</strong> SetWindow <strong>goto</strong> SENT_WINDOW;
  <strong>send</strong> Paint <strong>goto</strong> SENT_WINDOW;
  <strong>recv</strong> GetBrowserValue <strong>goto</strong> SENT_WINDOW;
};
</pre>
<p><br>
Note:</p>
<ul> <li>The following points will apply when the IPDL compiler fully supports states. It is incomplete as of this writing, on 22 October 2009.</li> <li>Protocol state machines are optional, but strongly encouraged. Even simple state machines are useful.</li> <li>All actor states, trigger matching, and transitions are managed by IPDL-generated code. Your C++ code need not manage these things.</li> <li>All messages sent and received are checked against the protocol's state machine. <em>If a message violates the state machine semantics defined in the IPDL code, the child process containing the child actor will be terminated with extreme prejudice, and all parent actors will be made invalid!</em> It is up to the developer to make sure that this never happens.</li> <li>Lots of syntactic sugar is possible for state machine definitions. Ping the Electrolysis team if you have a good proposal</li>
</ul>
Revert to this revision