Home | History | Annotate | Download | only in embedder
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_EMBEDDER_EMBEDDER_H_
      6 #define MOJO_EMBEDDER_EMBEDDER_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/task_runner.h"
     11 #include "mojo/embedder/scoped_platform_handle.h"
     12 #include "mojo/public/cpp/system/core.h"
     13 #include "mojo/system/system_impl_export.h"
     14 
     15 namespace mojo {
     16 namespace embedder {
     17 
     18 // Must be called first to initialize the (global, singleton) system.
     19 MOJO_SYSTEM_IMPL_EXPORT void Init();
     20 
     21 // Creates a new "channel", returning a handle to the bootstrap message pipe on
     22 // that channel. |platform_handle| should be an OS-dependent handle to one side
     23 // of a suitable bidirectional OS "pipe" (e.g., a file descriptor to a socket on
     24 // POSIX, a handle to a named pipe on Windows); this "pipe" should be connected
     25 // and ready for operation (e.g., to be written to or read from).
     26 // |io_thread_task_runner| should be a |TaskRunner| for the thread on which the
     27 // "channel" will run (read data and demultiplex).
     28 //
     29 // On completion, it will run |callback| with a pointer to a |ChannelInfo|
     30 // (which is meant to be opaque to the embedder). If
     31 // |callback_thread_task_runner| is non-null, it the callback will be posted to
     32 // that task runner. Otherwise, it will be run on the I/O thread directly.
     33 //
     34 // Returns an invalid |MOJO_HANDLE_INVALID| on error. Note that this will happen
     35 // only if, e.g., the handle table is full (operation of the channel begins
     36 // asynchronously and if, e.g., the other end of the "pipe" is closed, this will
     37 // report an error to the returned handle in the usual way).
     38 //
     39 // Notes: The handle returned is ready for use immediately, with messages
     40 // written to it queued. E.g., it would be perfectly valid for a message to be
     41 // immediately written to the returned handle and the handle closed, all before
     42 // the channel has begun operation on the IO thread. In this case, the channel
     43 // is expected to connect as usual, send the queued message, and report that the
     44 // handle was closed to the other side. (This message may well contain another
     45 // handle, so there may well still be message pipes "on" this channel.)
     46 //
     47 // TODO(vtl): Figure out channel teardown.
     48 struct ChannelInfo;
     49 typedef base::Callback<void(ChannelInfo*)> DidCreateChannelCallback;
     50 MOJO_SYSTEM_IMPL_EXPORT ScopedMessagePipeHandle CreateChannel(
     51     ScopedPlatformHandle platform_handle,
     52     scoped_refptr<base::TaskRunner> io_thread_task_runner,
     53     DidCreateChannelCallback callback,
     54     scoped_refptr<base::TaskRunner> callback_thread_task_runner);
     55 
     56 MOJO_SYSTEM_IMPL_EXPORT void DestroyChannelOnIOThread(
     57     ChannelInfo* channel_info);
     58 
     59 // Creates a |MojoHandle| that wraps the given |PlatformHandle| (taking
     60 // ownership of it). This |MojoHandle| can then, e.g., be passed through message
     61 // pipes. Note: This takes ownership (and thus closes) |platform_handle| even on
     62 // failure, which is different from what you'd expect from a Mojo API, but it
     63 // makes for a more convenient embedder API.
     64 MOJO_SYSTEM_IMPL_EXPORT MojoResult CreatePlatformHandleWrapper(
     65     ScopedPlatformHandle platform_handle,
     66     MojoHandle* platform_handle_wrapper_handle);
     67 // Retrieves the |PlatformHandle| that was wrapped into a |MojoHandle| (using
     68 // |CreatePlatformHandleWrapper()| above). Note that the |MojoHandle| must still
     69 // be closed separately.
     70 MOJO_SYSTEM_IMPL_EXPORT MojoResult PassWrappedPlatformHandle(
     71     MojoHandle platform_handle_wrapper_handle,
     72     ScopedPlatformHandle* platform_handle);
     73 
     74 }  // namespace embedder
     75 }  // namespace mojo
     76 
     77 #endif  // MOJO_EMBEDDER_EMBEDDER_H_
     78