summaryrefslogtreecommitdiffstats
path: root/mojo/shell/application_instance.h
blob: afe30e59f508517ba8d9a043c6cb781f1770c87a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2015 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_APPLICATION_INSTANCE_H_
#define MOJO_SHELL_APPLICATION_INSTANCE_H_

#include <stdint.h>

#include <set>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/process/process_handle.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/shell/capability_filter.h"
#include "mojo/shell/connect_params.h"
#include "mojo/shell/identity.h"
#include "mojo/shell/public/interfaces/application_manager.mojom.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"
#include "mojo/shell/public/interfaces/shell_client.mojom.h"
#include "url/gurl.h"

namespace mojo {
namespace shell {

class ApplicationManager;
class NativeRunner;

// Encapsulates a connection to an instance of an application, tracked by the
// shell's ApplicationManager.
class ApplicationInstance : public mojom::Shell,
                            public mojom::PIDReceiver {
 public:
  ApplicationInstance(
      mojom::ShellClientPtr shell_client,
      ApplicationManager* manager,
      const Identity& identity);
  ~ApplicationInstance() override;

  void InitializeApplication();

  void ConnectToClient(scoped_ptr<ConnectParams> params);

  // Required before GetProcessId can be called.
  void SetNativeRunner(NativeRunner* native_runner);

  void BindPIDReceiver(InterfaceRequest<mojom::PIDReceiver> pid_receiver);

  mojom::ShellClient* shell_client() { return shell_client_.get(); }
  const Identity& identity() const { return identity_; }
  uint32_t id() const { return id_; }
  base::ProcessId pid() const { return pid_; }
  void set_pid(base::ProcessId pid) { pid_ = pid; }

 private:
  // Shell implementation:
  void Connect(const String& app_url,
               shell::mojom::InterfaceProviderRequest remote_interfaces,
               shell::mojom::InterfaceProviderPtr local_interfaces,
               mojom::CapabilityFilterPtr filter,
               const ConnectCallback& callback) override;
  void QuitApplication() override;

  // PIDReceiver implementation:
  void SetPID(uint32_t pid) override;

  uint32_t GenerateUniqueID() const;

  void CallAcceptConnection(scoped_ptr<ConnectParams> params);

  void OnConnectionError();

  void OnQuitRequestedResult(bool can_quit);

  void DestroyRunner();

  ApplicationManager* const manager_;
  // An id that identifies this instance. Distinct from pid, as a single process
  // may vend multiple application instances, and this object may exist before a
  // process is launched.
  const uint32_t id_;
  const Identity identity_;
  const bool allow_any_application_;
  mojom::ShellClientPtr shell_client_;
  Binding<mojom::Shell> binding_;
  Binding<mojom::PIDReceiver> pid_receiver_binding_;
  bool queue_requests_;
  std::vector<ConnectParams*> queued_client_requests_;
  NativeRunner* native_runner_;
  base::ProcessId pid_;

  DISALLOW_COPY_AND_ASSIGN(ApplicationInstance);
};

}  // namespace shell
}  // namespace mojo

#endif  // MOJO_SHELL_APPLICATION_INSTANCE_H_