OLD | NEW |
| (Empty) |
1 // 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 | |
18 namespace mojo { | |
19 | |
20 class 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 // Note that by default |ApplicationImplBase|s are not "strongly bound" to their | |
30 // |Application| requests (so, e.g., they can thus be constructed on the stack). | |
31 // A subclass may make itself strongly bound by setting a suitable connection | |
32 // error handler on the binding (available via |application_binding()|). | |
33 // | |
34 // The |RunApplication()| helper function (declared in run_application.h) takes | |
35 // a pointer to an instance of (a subclass of) |ApplicationImplBase| and runs it | |
36 // using a suitable message loop; see run_application.h for more details. | |
37 class ApplicationImplBase : public Application { | |
38 public: | |
39 ~ApplicationImplBase() override; | |
40 | |
41 // Binds the given |Application| request to this object. This must be done | |
42 // with the message (run) loop available/running, and this will cause this | |
43 // object to start serving requests (via that message loop). | |
44 void Bind(InterfaceRequest<Application> application_request); | |
45 | |
46 // This will be valid after |Initialize()| has been received and remain valid | |
47 // until this object is destroyed. | |
48 Shell* shell() const { return shell_.get(); } | |
49 | |
50 // Returns this object's |Application| binding (set up by |Bind()|; of course, | |
51 // you can always manipulate it directly). | |
52 const Binding<Application>& application_binding() const { | |
53 return application_binding_; | |
54 } | |
55 Binding<Application>& application_binding() { return application_binding_; } | |
56 | |
57 // Returns any initial configuration arguments, passed by the shell. | |
58 const std::vector<std::string>& args() const { return args_; } | |
59 bool HasArg(const std::string& arg) const; | |
60 | |
61 const std::string& url() const { return url_; } | |
62 | |
63 // Methods to be overridden (if desired) by subclasses: | |
64 | |
65 // Called after |Initialize()| has been received (|shell()|, |args()|, and | |
66 // |url()| will be valid when this is called. The default implementation does | |
67 // nothing. | |
68 virtual void OnInitialize(); | |
69 | |
70 // Called when another application connects to this application (i.e., we | |
71 // receive |AcceptConnection()|). This should either configure what services | |
72 // are "provided" (made available via a |ServiceProvider|) to that application | |
73 // and return true, or this may return false to reject the connection | |
74 // entirely. The default implementation rejects all connections (by just | |
75 // returning false). | |
76 virtual bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl); | |
77 | |
78 // Called before quitting the main message (run) loop, i.e., before | |
79 // |Terminate()|. The default implementation does nothing. | |
80 virtual void OnQuit(); | |
81 | |
82 // Terminates this application with result |result|. The default | |
83 // implementation quits the main run loop for this application by calling | |
84 // |mojo::TerminateApplication()| (and assumes that the application was run | |
85 // using |mojo::RunApplication()|). | |
86 virtual void Terminate(MojoResult result); | |
87 | |
88 protected: | |
89 // This class is meant to be subclassed. | |
90 ApplicationImplBase(); | |
91 | |
92 private: | |
93 // |Application| implementation. In general, you probably shouldn't call these | |
94 // directly (but I can't really stop you). | |
95 void Initialize(InterfaceHandle<Shell> shell, | |
96 Array<String> args, | |
97 const mojo::String& url) final; | |
98 void AcceptConnection(const String& requestor_url, | |
99 const String& url, | |
100 InterfaceRequest<ServiceProvider> services) final; | |
101 void RequestQuit() final; | |
102 | |
103 Binding<Application> application_binding_; | |
104 | |
105 // Set by |Initialize()|. | |
106 ShellPtr shell_; | |
107 std::vector<std::string> args_; | |
108 std::string url_; | |
109 | |
110 std::vector<std::unique_ptr<ServiceProviderImpl>> service_provider_impls_; | |
111 | |
112 MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImplBase); | |
113 }; | |
114 | |
115 } // namespace mojo | |
116 | |
117 #endif // MOJO_PUBLIC_CPP_APPLICATION_APPLICATION_IMPL_BASE_H_ | |
OLD | NEW |