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