OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "examples/bank_app/bank.mojom.h" | 7 #include "examples/bank_app/bank.mojom.h" |
8 #include "mojo/common/binding_set.h" | 8 #include "mojo/common/binding_set.h" |
9 #include "mojo/public/c/system/main.h" | 9 #include "mojo/public/c/system/main.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
13 #include "mojo/public/cpp/application/application_runner.h" | 13 #include "mojo/public/cpp/application/application_runner.h" |
14 #include "mojo/public/cpp/application/connect.h" | 14 #include "mojo/public/cpp/application/connect.h" |
15 #include "mojo/public/cpp/application/interface_factory.h" | |
16 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
17 #include "mojo/public/cpp/utility/run_loop.h" | 16 #include "mojo/public/cpp/utility/run_loop.h" |
18 #include "mojo/services/vanadium/security/interfaces/principal.mojom.h" | 17 #include "mojo/services/vanadium/security/interfaces/principal.mojom.h" |
19 | 18 |
20 namespace examples { | 19 namespace examples { |
21 | 20 |
22 using vanadium::PrincipalServicePtr; | 21 using vanadium::PrincipalServicePtr; |
23 | 22 |
24 class BankImpl : public Bank { | 23 class BankImpl : public Bank { |
25 public: | 24 public: |
(...skipping 13 matching lines...) Expand all Loading... |
39 void Run(const vanadium::UserPtr& user) const { | 38 void Run(const vanadium::UserPtr& user) const { |
40 user_->clear(); | 39 user_->clear(); |
41 if (user) { | 40 if (user) { |
42 *user_ = user->email; | 41 *user_ = user->email; |
43 } | 42 } |
44 } | 43 } |
45 private: | 44 private: |
46 std::string *user_; | 45 std::string *user_; |
47 }; | 46 }; |
48 | 47 |
49 class BankApp : public mojo::ApplicationDelegate, | 48 class BankApp : public mojo::ApplicationDelegate { |
50 public mojo::InterfaceFactory<Bank> { | |
51 public: | 49 public: |
52 BankApp() {} | 50 BankApp() {} |
53 | 51 |
54 void Initialize(mojo::ApplicationImpl* app) override { | 52 void Initialize(mojo::ApplicationImpl* app) override { |
55 mojo::ConnectToService(app->shell(), "mojo:principal_service", | 53 mojo::ConnectToService(app->shell(), "mojo:principal_service", |
56 GetProxy(&login_service_)); | 54 GetProxy(&login_service_)); |
57 } | 55 } |
58 | 56 |
59 // From ApplicationDelegate | 57 // From ApplicationDelegate |
60 bool ConfigureIncomingConnection( | 58 bool ConfigureIncomingConnection( |
(...skipping 11 matching lines...) Expand all Loading... |
72 return false; | 70 return false; |
73 } | 71 } |
74 // Record user access to the bank and reject customers that | 72 // Record user access to the bank and reject customers that |
75 // don't have a user identity. | 73 // don't have a user identity. |
76 if (user.empty()) { | 74 if (user.empty()) { |
77 MOJO_LOG(INFO) << "Rejecting customer without a user identity"; | 75 MOJO_LOG(INFO) << "Rejecting customer without a user identity"; |
78 return false; | 76 return false; |
79 } | 77 } |
80 MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; | 78 MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; |
81 } | 79 } |
82 connection->AddService(this); | 80 connection->GetServiceProviderImpl().AddService<Bank>( |
| 81 [this](const mojo::ConnectionContext& connection_context, |
| 82 mojo::InterfaceRequest<Bank> bank_request) { |
| 83 bindings_.AddBinding(&bank_impl_, bank_request.Pass()); |
| 84 }); |
83 return true; | 85 return true; |
84 } | 86 } |
85 | 87 |
86 void Create(const mojo::ConnectionContext& connection_context, | |
87 mojo::InterfaceRequest<Bank> request) override { | |
88 bindings_.AddBinding(&bank_impl_, request.Pass()); | |
89 } | |
90 | |
91 private: | 88 private: |
92 BankImpl bank_impl_; | 89 BankImpl bank_impl_; |
93 mojo::BindingSet<Bank> bindings_; | 90 mojo::BindingSet<Bank> bindings_; |
94 vanadium::PrincipalServicePtr login_service_; | 91 vanadium::PrincipalServicePtr login_service_; |
95 }; | 92 }; |
96 | 93 |
97 } // namespace examples | 94 } // namespace examples |
98 | 95 |
99 MojoResult MojoMain(MojoHandle application_request) { | 96 MojoResult MojoMain(MojoHandle application_request) { |
100 mojo::ApplicationRunner runner( | 97 mojo::ApplicationRunner runner( |
101 std::unique_ptr<examples::BankApp>(new examples::BankApp())); | 98 std::unique_ptr<examples::BankApp>(new examples::BankApp())); |
102 return runner.Run(application_request); | 99 return runner.Run(application_request); |
103 } | 100 } |
OLD | NEW |