summaryrefslogtreecommitdiffstats
path: root/mojo/shell/public/cpp/shell.h
blob: 1610693d6951ea74be2af8a9f3bb87890fe9e4be (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
// Copyright 2016 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_SHELL_H_
#define MOJO_SHELL_PUBLIC_CPP_SHELL_H_

#include "mojo/shell/public/cpp/app_lifetime_helper.h"
#include "mojo/shell/public/cpp/connection.h"
#include "mojo/shell/public/interfaces/application.mojom.h"
#include "mojo/shell/public/interfaces/shell.mojom.h"

namespace mojo {

shell::mojom::CapabilityFilterPtr CreatePermissiveCapabilityFilter();

using ApplicationRequest = InterfaceRequest<shell::mojom::Application>;

class Shell {
 public:
  class ConnectParams {
   public:
    explicit ConnectParams(const std::string& url);
    explicit ConnectParams(URLRequestPtr request);
    ~ConnectParams();

    URLRequestPtr TakeRequest() { return std::move(request_); }
    shell::mojom::CapabilityFilterPtr TakeFilter() {
      return std::move(filter_);
    }
    void set_filter(shell::mojom::CapabilityFilterPtr filter) {
      filter_ = std::move(filter);
    }

   private:
    URLRequestPtr request_;
    shell::mojom::CapabilityFilterPtr filter_;

    DISALLOW_COPY_AND_ASSIGN(ConnectParams);
  };

  // Requests a new connection to an application. Returns a pointer to the
  // connection if the connection is permitted by this application's delegate,
  // or nullptr otherwise. Caller takes ownership.
  virtual scoped_ptr<Connection> ConnectToApplication(
      const std::string& url) = 0;
  virtual scoped_ptr<Connection> ConnectToApplication(
      ConnectParams* params) = 0;

  // Connect to application identified by |request->url| and connect to the
  // service implementation of the interface identified by |Interface|.
  template <typename Interface>
  void ConnectToService(ConnectParams* params, InterfacePtr<Interface>* ptr) {
    scoped_ptr<Connection> connection = ConnectToApplication(params);
    if (!connection.get())
      return;
    connection->ConnectToService(ptr);
  }
  template <typename Interface>
  void ConnectToService(const std::string& url, InterfacePtr<Interface>* ptr) {
    ConnectParams params(url);
    params.set_filter(CreatePermissiveCapabilityFilter());
    return ConnectToService(&params, ptr);
  }

  // Initiate shutdown of this application. This may involve a round trip to the
  // Shell to ensure there are no inbound service requests.
  virtual void Quit() = 0;

  // Create an object that can be used to refcount the lifetime of the
  // application. The returned object may be cloned, and when the refcount falls
  // to zero Quit() is called.
  virtual scoped_ptr<AppRefCount> CreateAppRefCount() = 0;
};

}  // namespace mojo

#endif  // MOJO_SHELL_PUBLIC_CPP_SHELL_H_