\subsection[IPCs, Binder and AIDLs]{Inter-Process Communications, Binder and AIDLs}
\begin{frame}
  \frametitle{IPCs}
  \begin{itemize}
  \item On modern systems, each process has its own address space,
    allowing to isolate data
  \item This allows for better stability and security: only a given
    process can access its address space. If another process tries to
    access it, the kernel will detect it and kill this process.
  \item However, interactions between processes are sometimes needed,
    that's what IPCs are for.
  \item On classic Linux systems, we have several IPC mechanisms used:
    \begin{itemize}
    \item Signals
    \item Semaphores
    \item Sockets
    \item Message queues
    \item Pipes
    \item Shared memory
    \end{itemize}
  \item Android, however, uses mostly:
    \begin{itemize}
    \item Binder
    \item Ashmem and Sockets
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Binder 1/2}
  \begin{itemize}
  \item Uses shared memory for high performance
  \item Uses reference counting to garbage collect objects no longer
    in use
  \item Data are sent through \emph{parcels}, which is some kind of
    serialization
  \item Used across the whole system, e.g., clients connect to the
    window manager through Binder, which in turn connects to
    SurfaceFlinger using Binder
  \item Each object has an \emph{identity}, which does not change,
    even if you pass it to other processes.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Binder 2/2}
  \begin{itemize}
  \item This is useful if you want to separate components in distinct
    processes, or to manage several components of a single process (i.e.
    Activity's Windows).
  \item Object identity is also used for security. Some
    token passed correspond to specific permissions. Another
    security model to enforce permissions is for every transaction to
    check on the calling UID.
  \item Binder also supports one-way and two-way messages 
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Binder terminology}
  \begin{itemize}
  \item \emph{The Binder}
    \begin{itemize}
    \item The overall Binder Architecture
    \end{itemize}
  \item \emph{Binder Interface}
    \begin{itemize}
    \item A well-defined set of methods and properties other can call,
      and that should be implemented by \emph{a} binder
    \end{itemize}
  \item \emph{A Binder}
    \begin{itemize}
    \item A particular implementation of a Binder interface
    \end{itemize}
  \item \emph{Binder Object}
    \begin{itemize}
    \item An instance of a class that implements a Binder interface
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Binder Mechanism}
  \begin{center}
    \includegraphics[height=0.8\textheight]{slides/android-framework-ipc/binder-call-stack.pdf}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Binder Implementation 1/2}
  \begin{center}
    \includegraphics[height=0.8\textheight]{slides/android-framework-ipc/binder-calling.pdf}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Binder Implementation 2/2}
  \begin{center}
    \includegraphics[height=0.8\textheight]{slides/android-framework-ipc/binder-calling-aidl.pdf}
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Android Interface Definition Language (AIDL)}
  \begin{itemize}
  \item Very similar to any other Interface Definition Language you
    might have encountered
  \item Describes a programming interface for the client and the
    server to communicate using IPCs
  \item Looks a lot like Java interfaces. Several types are already
    defined, however, and you can't extend this like what you can do in
    Java:
    \begin{itemize}
    \item All Java primitive types (\code{int}, \code{long},
      \code{boolean}, etc.)
    \item \code{String}
    \item \code{CharSequence}
    \item \code{Parcelable}
    \item \code{List} of one of the previous types
    \item \code{Map}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{AIDLs HelloWorld}
\begin{minted}{java}
package com.example.android;

interface IRemoteService {
    void HelloPrint(String aString);
}
\end{minted}
\end{frame}

\begin{frame}
  \frametitle{Parcelable Objects}
  \begin{itemize}
  \item If you want to add extra objects to the AIDLs, you need to
    make them implement the \code{Parcelable} interface
  \item Most of the relevant Android objects already implement this interface.
  \item This is required to let Binder know how to serialize and
    deserialize these objects
  \item However, this is not a general purpose serialization
    mechanism. Underlying data structures may evolve, so you should
    not store parcelled objects to persistent storage
  \item Has primitives to store basic types, arrays, etc.
  \item You can even serialize file descriptors!
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Implement Parcelable Classes}
  \begin{itemize}
  \item To make an object parcelable, you need to:
    \begin{itemize}
    \item Make the object implement the \code{Parcelable} interface
    \item Implement the \code{writeToParcel} function, which stores
      the current state of the object to a \code{Parcel} object
    \item Add a static field called \code{CREATOR}, which implements
      the \code{Parcelable.Creator} interface, and takes a
      \code{Parcel}, deserializes the values and returns the object
    \item Create an AIDL file that declares your new parcelable class
    \end{itemize}
  \item You should also consider \code{Bundles}, that are type-safe
    key-value containers, and are optimized for reading and writing
    values
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Intents}
  \begin{itemize}
  \item Intents are a high-level use of Binder
  \item They describe the intention to do something
  \item They are used extensively across Android
    \begin{itemize}
    \item Activities, Services and BroadcastReceivers are started
      using intents
    \end{itemize}
  \item Two types of intents:
    \begin{description}
    \item[explicit] The developer designates the target by its name 
    \item[implicit] There is no explicit target for the Intent. The
      system will find best target for the Intent by itself, possibly
      asking the user what to do if there are several matches
    \end{description}
  \end{itemize}
\end{frame}
