summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/application_runner.h
blob: 2fb28a2b24bff3a337b27bae941b7abfdc3abe51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_H_
#define MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_H_

#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "mojo/public/cpp/system/core.h"

namespace mojo {

class ShellClient;
class ShellConnection;

// A utility for running a chromium based mojo Application. The typical use
// case is to use when writing your MojoMain:
//
//  MojoResult MojoMain(MojoHandle shell_handle) {
//    mojo::ApplicationRunner runner(new MyDelegate());
//    return runner.Run(shell_handle);
//  }
//
// ApplicationRunner takes care of chromium environment initialization and
// shutdown, and starting a MessageLoop from which your application can run and
// ultimately Quit().
class ApplicationRunner {
 public:
  // Takes ownership of |client|.
  explicit ApplicationRunner(ShellClient* client);
  ~ApplicationRunner();

  static void InitBaseCommandLine();

  void set_message_loop_type(base::MessageLoop::Type type);

  // Once the various parameters have been set above, use Run to initialize an
  // ShellConnection wired to the provided delegate, and run a MessageLoop until
  // the application exits.
  //
  // Iff |init_base| is true, the runner will perform some initialization of
  // base globals (e.g. CommandLine and AtExitManager) before starting the
  // application.
  MojoResult Run(MojoHandle shell_handle, bool init_base);

  // Calls Run above with |init_base| set to |true|.
  MojoResult Run(MojoHandle shell_handle);

  // Allows the caller to shut down the connection with the shell. After the
  // shell notices the pipe has closed, it will no longer track an instance of
  // this application, though this application may continue to run and service
  // requests from others.
  void DestroyShellConnection();

  // Allows the caller to explicitly quit the application. Must be called from
  // the thread which created the ApplicationRunner.
  void Quit();

 private:
  scoped_ptr<ShellConnection> connection_;
  scoped_ptr<ShellClient> client_;

  // MessageLoop type. TYPE_CUSTOM is default (MessagePumpMojo will be used as
  // the underlying message pump).
  base::MessageLoop::Type message_loop_type_;
  // Whether Run() has been called.
  bool has_run_;

  DISALLOW_COPY_AND_ASSIGN(ApplicationRunner);
};

}  // namespace mojo

#endif  // MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_H_