Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "examples/echo/echo.mojom.h" | 5 #include "examples/echo/echo.mojom.h" |
| 6 #include "mojo/common/binding_set.h" | 6 #include "mojo/common/binding_set.h" |
| 7 #include "mojo/public/c/system/main.h" | 7 #include "mojo/public/c/system/main.h" |
| 8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | |
| 10 #include "mojo/public/cpp/application/application_runner.h" | 11 #include "mojo/public/cpp/application/application_runner.h" |
| 11 #include "mojo/public/cpp/application/interface_factory.h" | 12 #include "mojo/public/cpp/application/interface_factory.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 14 #include "mojo/public/cpp/utility/run_loop.h" | |
| 15 #include "mojo/services/vanadium/security/public/interfaces/principal.mojom.h" | |
| 13 | 16 |
| 14 namespace mojo { | 17 namespace mojo { |
| 15 namespace examples { | 18 namespace examples { |
| 16 | 19 |
| 17 // This file demonstrates three ways to implement a simple Mojo server. Because | 20 // This file demonstrates three ways to implement a simple Mojo server. Because |
| 18 // there's no state that's saved per-pipe, all three servers appear to the | 21 // there's no state that's saved per-pipe, all three servers appear to the |
| 19 // client to do exactly the same thing. | 22 // client to do exactly the same thing. |
| 20 // | 23 // |
| 21 // To choose one of the them, update MojoMain() at the end of this file. | 24 // To choose one of the them, update MojoMain() at the end of this file. |
| 22 // 1. MultiServer - creates a new object for each connection. Cleans up by using | 25 // 1. MultiServer - creates a new object for each connection. Cleans up by using |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 41 // This simplifies lifetime management. This class is only used by MultiServer. | 44 // This simplifies lifetime management. This class is only used by MultiServer. |
| 42 class StrongBindingEchoImpl : public EchoImpl { | 45 class StrongBindingEchoImpl : public EchoImpl { |
| 43 public: | 46 public: |
| 44 explicit StrongBindingEchoImpl(InterfaceRequest<Echo> handle) | 47 explicit StrongBindingEchoImpl(InterfaceRequest<Echo> handle) |
| 45 : strong_binding_(this, handle.Pass()) {} | 48 : strong_binding_(this, handle.Pass()) {} |
| 46 | 49 |
| 47 private: | 50 private: |
| 48 mojo::StrongBinding<Echo> strong_binding_; | 51 mojo::StrongBinding<Echo> strong_binding_; |
| 49 }; | 52 }; |
| 50 | 53 |
| 54 class GetBlessingResponse { | |
| 55 public: | |
| 56 void Run(const BlessingPtr& b) const { | |
| 57 // EchoClient user available in b.get()->chain[0]->extension | |
|
ataly
2015/07/31 19:04:04
MInor: Should we just say "user identity available
gauthamt
2015/07/31 20:24:16
Done.
| |
| 58 RunLoop::current()->Quit(); | |
| 59 } | |
| 60 }; | |
| 61 | |
| 51 // MultiServer creates a new object to handle each message pipe. | 62 // MultiServer creates a new object to handle each message pipe. |
| 52 class MultiServer : public mojo::ApplicationDelegate, | 63 class MultiServer : public mojo::ApplicationDelegate, |
| 53 public mojo::InterfaceFactory<Echo> { | 64 public mojo::InterfaceFactory<Echo> { |
| 54 public: | 65 public: |
| 55 MultiServer() {} | 66 MultiServer() {} |
| 56 | 67 |
| 68 void Initialize(ApplicationImpl* app) override { | |
| 69 app->ConnectToService("mojo:go_principal_service", &login_service_); | |
| 70 } | |
| 71 | |
| 57 // From ApplicationDelegate | 72 // From ApplicationDelegate |
| 58 bool ConfigureIncomingConnection( | 73 bool ConfigureIncomingConnection( |
| 59 mojo::ApplicationConnection* connection) override { | 74 mojo::ApplicationConnection* connection) override { |
| 75 AppNamePtr app(AppName::New()); | |
| 76 app->url = connection->GetRemoteApplicationURL(); | |
| 77 login_service_->GetUserBlessing(app.Pass(), GetBlessingResponse()); | |
| 60 connection->AddService<Echo>(this); | 78 connection->AddService<Echo>(this); |
| 61 return true; | 79 return true; |
| 62 } | 80 } |
| 63 | 81 |
| 64 // From InterfaceFactory<Echo> | 82 // From InterfaceFactory<Echo> |
| 65 void Create(mojo::ApplicationConnection* connection, | 83 void Create(mojo::ApplicationConnection* connection, |
| 66 mojo::InterfaceRequest<Echo> request) override { | 84 mojo::InterfaceRequest<Echo> request) override { |
| 67 // This object will be deleted automatically because of the use of | 85 // This object will be deleted automatically because of the use of |
| 68 // StrongBinding<> for the declaration of |strong_binding_|. | 86 // StrongBinding<> for the declaration of |strong_binding_|. |
| 69 new StrongBindingEchoImpl(request.Pass()); | 87 new StrongBindingEchoImpl(request.Pass()); |
| 70 } | 88 } |
| 89 | |
| 90 private: | |
| 91 PrincipalServicePtr login_service_; | |
| 71 }; | 92 }; |
| 72 | 93 |
| 73 // SingletonServer uses the same object to handle all message pipes. Useful | 94 // SingletonServer uses the same object to handle all message pipes. Useful |
| 74 // for stateless operation. | 95 // for stateless operation. |
| 75 class SingletonServer : public mojo::ApplicationDelegate, | 96 class SingletonServer : public mojo::ApplicationDelegate, |
| 76 public mojo::InterfaceFactory<Echo> { | 97 public mojo::InterfaceFactory<Echo> { |
| 77 public: | 98 public: |
| 78 SingletonServer() {} | 99 SingletonServer() {} |
| 79 | 100 |
| 80 // From ApplicationDelegate | 101 // From ApplicationDelegate |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 } // namespace mojo | 153 } // namespace mojo |
| 133 | 154 |
| 134 MojoResult MojoMain(MojoHandle application_request) { | 155 MojoResult MojoMain(MojoHandle application_request) { |
| 135 // Uncomment one of the three servers at a time to see it work: | 156 // Uncomment one of the three servers at a time to see it work: |
| 136 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); | 157 mojo::ApplicationRunner runner(new mojo::examples::MultiServer()); |
| 137 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); | 158 // mojo::ApplicationRunner runner(new mojo::examples::SingletonServer()); |
| 138 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); | 159 // mojo::ApplicationRunner runner(new mojo::examples::OneAtATimeServer()); |
| 139 | 160 |
| 140 return runner.Run(application_request); | 161 return runner.Run(application_request); |
| 141 } | 162 } |
| OLD | NEW |