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> | |
6 | |
7 #include "examples/bank_app/bank.mojom.h" | 5 #include "examples/bank_app/bank.mojom.h" |
8 #include "mojo/public/c/system/main.h" | 6 #include "mojo/public/c/system/main.h" |
9 #include "mojo/public/cpp/application/application_delegate.h" | 7 #include "mojo/public/cpp/application/application_impl_base.h" |
10 #include "mojo/public/cpp/application/application_impl.h" | |
11 #include "mojo/public/cpp/application/application_runner.h" | |
12 #include "mojo/public/cpp/application/connect.h" | 8 #include "mojo/public/cpp/application/connect.h" |
| 9 #include "mojo/public/cpp/application/run_application.h" |
13 #include "mojo/public/cpp/utility/run_loop.h" | 10 #include "mojo/public/cpp/utility/run_loop.h" |
14 #include "mojo/services/vanadium/security/interfaces/principal.mojom.h" | 11 #include "mojo/services/vanadium/security/interfaces/principal.mojom.h" |
15 | 12 |
16 namespace examples { | 13 namespace examples { |
17 | 14 |
18 class LoginHandler { | 15 class LoginHandler { |
19 public: | 16 public: |
20 void Run(const vanadium::UserPtr& user) const { | 17 void Run(const vanadium::UserPtr& user) const { |
21 if (user) { | 18 if (user) { |
22 MOJO_LOG(INFO) << "User logged-in as " << user->email; | 19 MOJO_LOG(INFO) << "User logged-in as " << user->email; |
23 } | 20 } |
24 } | 21 } |
25 }; | 22 }; |
26 | 23 |
27 class BankCustomer : public mojo::ApplicationDelegate { | 24 class BankCustomer : public mojo::ApplicationImplBase { |
28 public: | 25 public: |
29 void Initialize(mojo::ApplicationImpl* app) override { | 26 void OnInitialize() override { |
30 mojo::ConnectToService(app->shell(), "mojo:principal_service", | 27 mojo::ConnectToService(shell(), "mojo:principal_service", |
31 GetProxy(&login_service_)); | 28 GetProxy(&login_service_)); |
32 | 29 |
33 // Login to the principal service to get a user identity. | 30 // Login to the principal service to get a user identity. |
34 login_service_->Login(LoginHandler()); | 31 login_service_->Login(LoginHandler()); |
35 // Check and see whether we got a valid user id. | 32 // Check and see whether we got a valid user id. |
36 if (!login_service_.WaitForIncomingResponse()) { | 33 if (!login_service_.WaitForIncomingResponse()) { |
37 MOJO_LOG(INFO) << "Login() to the principal service failed"; | 34 MOJO_LOG(INFO) << "Login() to the principal service failed"; |
38 } | 35 } |
39 | 36 |
40 BankPtr bank; | 37 BankPtr bank; |
41 mojo::ConnectToService(app->shell(), "mojo:bank", GetProxy(&bank)); | 38 mojo::ConnectToService(shell(), "mojo:bank", GetProxy(&bank)); |
42 bank->Deposit(500/*usd*/); | 39 bank->Deposit(500/*usd*/); |
43 bank->Withdraw(100/*usd*/); | 40 bank->Withdraw(100/*usd*/); |
44 auto gb_callback = [](const int32_t& balance) { | 41 auto gb_callback = [](const int32_t& balance) { |
45 MOJO_LOG(INFO) << "Bank balance: " << balance; | 42 MOJO_LOG(INFO) << "Bank balance: " << balance; |
46 }; | 43 }; |
47 bank->GetBalance(mojo::Callback<void(const int32_t&)>(gb_callback)); | 44 bank->GetBalance(mojo::Callback<void(const int32_t&)>(gb_callback)); |
48 bank.WaitForIncomingResponse(); | 45 bank.WaitForIncomingResponse(); |
49 } | 46 } |
50 void Quit() override { | 47 void OnQuit() override { login_service_->Logout(); } |
51 login_service_->Logout(); | 48 |
52 } | |
53 private: | 49 private: |
54 vanadium::PrincipalServicePtr login_service_; | 50 vanadium::PrincipalServicePtr login_service_; |
55 }; | 51 }; |
56 | 52 |
57 } // namespace examples | 53 } // namespace examples |
58 | 54 |
59 MojoResult MojoMain(MojoHandle application_request) { | 55 MojoResult MojoMain(MojoHandle application_request) { |
60 mojo::ApplicationRunner runner( | 56 examples::BankCustomer bank_customer; |
61 std::unique_ptr<examples::BankCustomer>(new examples::BankCustomer())); | 57 return mojo::RunMainApplication(application_request, &bank_customer); |
62 return runner.Run(application_request); | |
63 } | 58 } |
OLD | NEW |