blob: 8d3cbf6087f93e9f8f2ae7a150d4996707052489 [file] [log] [blame]
Viet-Trung Luu0a68a8d2016-05-17 16:43:16 -07001// Copyright 2016 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_PUBLIC_CPP_APPLICATION_APPLICATION_IMPL_BASE_H_
6#define MOJO_PUBLIC_CPP_APPLICATION_APPLICATION_IMPL_BASE_H_
7
8#include <memory>
9#include <string>
10#include <vector>
11
12#include "mojo/public/cpp/bindings/binding.h"
13#include "mojo/public/cpp/bindings/interface_request.h"
14#include "mojo/public/cpp/system/macros.h"
15#include "mojo/public/interfaces/application/application.mojom.h"
16#include "mojo/public/interfaces/application/shell.mojom.h"
17
18namespace mojo {
19
20class ServiceProviderImpl;
21
22// Base helper class for implementing the |Application| interface, which the
23// shell uses for basic communication with an application (e.g., to connect
24// clients to services provided by an application).
25//
26// To use this class, subclass it and implement/override the required methods
27// (see below).
28//
29// TODO(vtl): ApplicationRunners should take this instead of an
30// ApplicationDelegate. Write more here when that's true (it's pretty hard to
31// use this class in the current setup).
32class ApplicationImplBase : public Application {
33 public:
Viet-Trung Luu21a4f272016-05-19 09:53:20 -070034 ApplicationImplBase();
Viet-Trung Luu0a68a8d2016-05-17 16:43:16 -070035 ~ApplicationImplBase() override;
36
Viet-Trung Luu21a4f272016-05-19 09:53:20 -070037 // Binds the given |Application| request to this object. This must be done
38 // with the message (run) loop available/running, and this will cause this
39 // object to start serving requests (via that message loop).
40 void Bind(InterfaceRequest<Application> application_request);
41
Viet-Trung Luu0a68a8d2016-05-17 16:43:16 -070042 // Quits the main run loop for this application.
43 // TODO(vtl): This is implemented in application_runner.cc (for example). Its
44 // presence here is pretty dubious.
45 static void Terminate();
46
47 // This will be valid after |Initialize()| has been received and remain valid
48 // until this object is destroyed.
49 Shell* shell() const { return shell_.get(); }
50
51 // Returns any initial configuration arguments, passed by the shell.
52 const std::vector<std::string>& args() const { return args_; }
53 bool HasArg(const std::string& arg) const;
54
55 const std::string& url() const { return url_; }
56
57 // Methods to be implemented/overridden by subclasses:
58
59 // Called after |Initialize()| has been received (|shell()|, |args()|, and
60 // |url()| will be valid when this is called. The default implementation does
61 // nothing.
62 virtual void OnInitialize() {}
63
64 // Called when another application connects to this application (i.e., we
65 // receive |AcceptConnection()|). This should either configure what services
66 // are "provided" (made available via a |ServiceProvider|) to that application
67 // and return true, or this may return false to reject the connection
68 // entirely.
69 virtual bool OnAcceptConnection(
70 ServiceProviderImpl* service_provider_impl) = 0;
71
72 // Called before quitting the main message (run) loop, i.e., before
73 // |Terminate()|. The default implementation does nothing.
74 virtual void OnQuit() {}
75
76 private:
77 // |Application| implementation. In general, you probably shouldn't call these
78 // directly (but I can't really stop you).
79 void Initialize(InterfaceHandle<Shell> shell,
80 Array<String> args,
81 const mojo::String& url) final;
82 void AcceptConnection(const String& requestor_url,
83 InterfaceRequest<ServiceProvider> services,
84 InterfaceHandle<ServiceProvider> exposed_services,
85 const String& url) final;
86 void RequestQuit() final;
87
88 Binding<Application> application_binding_;
89
90 // Set by |Initialize()|.
91 ShellPtr shell_;
92 std::vector<std::string> args_;
93 std::string url_;
94
95 std::vector<std::unique_ptr<ServiceProviderImpl>> service_provider_impls_;
96
97 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImplBase);
98};
99
100} // namespace mojo
101
102#endif // MOJO_PUBLIC_CPP_APPLICATION_APPLICATION_IMPL_BASE_H_