Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | #include "examples/bank_app/bank.mojom.h" |
| 8 | #include "mojo/public/c/system/main.h" |
| 9 | #include "mojo/public/cpp/application/application_impl.h" |
| 10 | #include "mojo/public/cpp/application/application_runner.h" |
| 11 | #include "mojo/public/cpp/utility/run_loop.h" |
Viet-Trung Luu | 15a59a8 | 2015-10-10 01:11:00 -0700 | [diff] [blame] | 12 | #include "mojo/services/vanadium/security/interfaces/principal.mojom.h" |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 13 | |
| 14 | namespace examples { |
| 15 | |
| 16 | class LoginHandler { |
| 17 | public: |
Ankur | 9c1db64 | 2015-11-04 17:15:15 -0800 | [diff] [blame] | 18 | void Run(const vanadium::UserPtr& user) const { |
| 19 | if (user) { |
| 20 | MOJO_LOG(INFO) << "User logged-in as " << user->email; |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | class BankCustomer : public mojo::ApplicationDelegate { |
| 26 | public: |
| 27 | void Initialize(mojo::ApplicationImpl* app) override { |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 28 | app->ConnectToService("mojo:principal_service", &login_service_); |
Ankur | 9c1db64 | 2015-11-04 17:15:15 -0800 | [diff] [blame] | 29 | |
| 30 | // Login to the principal service to get a user identity. |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 31 | login_service_->Login(LoginHandler()); |
Ankur | 9c1db64 | 2015-11-04 17:15:15 -0800 | [diff] [blame] | 32 | // Check and see whether we got a valid user id. |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 33 | if (!login_service_.WaitForIncomingResponse()) { |
Ankur | 9c1db64 | 2015-11-04 17:15:15 -0800 | [diff] [blame] | 34 | MOJO_LOG(INFO) << "Login() to the principal service failed"; |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 35 | } |
Ankur | 9c1db64 | 2015-11-04 17:15:15 -0800 | [diff] [blame] | 36 | |
Gautham Thambidorai | 658bb42 | 2015-08-19 12:40:00 -0700 | [diff] [blame] | 37 | BankPtr bank; |
| 38 | app->ConnectToService("mojo:bank", &bank); |
| 39 | bank->Deposit(500/*usd*/); |
| 40 | bank->Withdraw(100/*usd*/); |
| 41 | auto gb_callback = [](const int32_t& balance) { |
| 42 | MOJO_LOG(INFO) << "Bank balance: " << balance; |
| 43 | }; |
| 44 | bank->GetBalance(mojo::Callback<void(const int32_t&)>(gb_callback)); |
| 45 | bank.WaitForIncomingResponse(); |
| 46 | } |
| 47 | void Quit() override { |
| 48 | login_service_->Logout(); |
| 49 | } |
| 50 | private: |
| 51 | vanadium::PrincipalServicePtr login_service_; |
| 52 | }; |
| 53 | |
| 54 | } // namespace examples |
| 55 | |
| 56 | MojoResult MojoMain(MojoHandle application_request) { |
| 57 | mojo::ApplicationRunner runner(new examples::BankCustomer); |
| 58 | return runner.Run(application_request); |
| 59 | } |