Home | History | Annotate | Download | only in shell
      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_SHELL_CHILD_PROCESS_HOST_H_
      6 #define MOJO_SHELL_CHILD_PROCESS_HOST_H_
      7 
      8 #include "base/macros.h"
      9 #include "base/process/process_handle.h"
     10 #include "mojo/embedder/platform_channel_pair.h"
     11 #include "mojo/embedder/scoped_platform_handle.h"
     12 #include "mojo/shell/child_process.h"  // For |ChildProcess::Type|.
     13 
     14 namespace mojo {
     15 namespace shell {
     16 
     17 class Context;
     18 
     19 // (Base) class for a "child process host". Handles launching and connecting a
     20 // platform-specific "pipe" to the child, and supports joining the child
     21 // process. Intended for use as a base class, but may be used on its own in
     22 // simple cases.
     23 //
     24 // This class is not thread-safe. It should be created/used/destroyed on a
     25 // single thread.
     26 //
     27 // Note: Does not currently work on Windows before Vista.
     28 class ChildProcessHost {
     29  public:
     30   class Delegate {
     31    public:
     32     virtual void WillStart() = 0;
     33     virtual void DidStart(bool success) = 0;
     34   };
     35 
     36   ChildProcessHost(Context* context,
     37                    Delegate* delegate,
     38                    ChildProcess::Type type);
     39   virtual ~ChildProcessHost();
     40 
     41   // |Start()|s the child process; calls the delegate's |DidStart()| (on the
     42   // thread on which |Start()| was called) when the child has been started (or
     43   // failed to start). After calling |Start()|, this object must not be
     44   // destroyed until |DidStart()| has been called.
     45   // TODO(vtl): Consider using weak pointers and removing this requirement.
     46   void Start();
     47 
     48   // Waits for the child process to terminate, and returns its exit code.
     49   // Note: If |Start()| has been called, this must not be called until the
     50   // callback has been called.
     51   int Join();
     52 
     53   embedder::ScopedPlatformHandle* platform_channel() {
     54     return &platform_channel_;
     55   }
     56 
     57  protected:
     58   Context* context() const {
     59     return context_;
     60   }
     61 
     62  private:
     63   bool DoLaunch();
     64   void DidLaunch(bool success);
     65 
     66   Context* const context_;
     67   Delegate* const delegate_;
     68   const ChildProcess::Type type_;
     69 
     70   base::ProcessHandle child_process_handle_;
     71 
     72   embedder::PlatformChannelPair platform_channel_pair_;
     73 
     74   // Platform-specific "pipe" to the child process. Valid immediately after
     75   // creation.
     76   embedder::ScopedPlatformHandle platform_channel_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(ChildProcessHost);
     79 };
     80 
     81 }  // namespace shell
     82 }  // namespace mojo
     83 
     84 #endif  // MOJO_SHELL_CHILD_PROCESS_HOST_H_
     85