OLD | NEW |
---|---|
(Empty) | |
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 "examples/bank_app/bank.mojom.h" | |
6 #include "mojo/common/binding_set.h" | |
7 #include "mojo/public/c/system/main.h" | |
8 #include "mojo/public/cpp/application/application_connection.h" | |
9 #include "mojo/public/cpp/application/application_delegate.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/interface_factory.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" | |
16 | |
17 namespace mojo { | |
jamesr
2015/08/19 05:30:54
drop the 'mojo::' part
gautham
2015/08/19 17:45:51
Done.
| |
18 namespace examples { | |
19 | |
20 using namespace vanadium; | |
jamesr
2015/08/19 05:30:54
google's C++ style guide (which we use) bans using
gautham
2015/08/19 17:45:51
Done.
| |
21 | |
22 class BankImpl : public Bank { | |
23 public: | |
24 BankImpl() : balance_(0) {} | |
25 void Deposit(int32_t usd) override { balance_ += usd; } | |
26 void Withdraw(int32_t usd) override { balance_ -= usd; } | |
27 void GetBalance(const GetBalanceCallback& callback) override { | |
28 callback.Run(balance_); | |
29 } | |
30 private: | |
31 int32_t balance_; | |
32 }; | |
33 | |
34 class BankUser { | |
35 public: | |
36 explicit BankUser(std::string* user) : user_(user) { } | |
37 void Run(const BlessingPtr& b) const { | |
38 user_->clear(); | |
39 if (b) { | |
40 for (size_t i = 0; i < b->chain.size(); i++) { | |
41 user_->append("/"); | |
ashankar
2015/08/19 05:50:38
Should this separator "/" be defined as a mojom co
gautham
2015/08/19 17:45:50
Done.
| |
42 user_->append(b->chain[i]->extension); | |
43 } | |
44 } | |
45 } | |
46 private: | |
47 std::string *user_; | |
48 }; | |
49 | |
50 class BankApp : public mojo::ApplicationDelegate, | |
51 public mojo::InterfaceFactory<Bank> { | |
52 public: | |
53 BankApp() { | |
54 } | |
55 void Initialize(ApplicationImpl* app) override { | |
56 app->ConnectToService("mojo:principal_service", &login_service_); | |
57 } | |
58 | |
59 // From ApplicationDelegate | |
60 bool ConfigureIncomingConnection( | |
61 mojo::ApplicationConnection* connection) override { | |
62 std::string url = connection->GetRemoteApplicationURL(); | |
63 if (url.length() > 0) { | |
64 AppInstanceNamePtr app(AppInstanceName::New()); | |
65 app->url = url; | |
66 std::string user; | |
67 login_service_->GetUserBlessing(app.Pass(), BankUser(&user)); | |
68 login_service_.WaitForIncomingResponse(); | |
jamesr
2015/08/19 05:30:54
hmm - our c++ bindings let you choose whether to r
gautham
2015/08/19 17:45:50
Checked the return value and logged a failure for
| |
69 // Record user access to the bank and reject customers that | |
70 // don't have a user identity. | |
71 if (user.empty()) { | |
72 MOJO_LOG(INFO) << "Rejecting customer without a user identity"; | |
73 return false; | |
ashankar
2015/08/19 05:50:38
When would url.length be 0, and in that case why i
gautham
2015/08/19 17:45:50
This happens I launch both mojo:customer and mojo:
| |
74 } | |
75 MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; | |
76 } | |
77 connection->AddService<Bank>(this); | |
jamesr
2015/08/19 05:30:54
nit: i don't think you need the <Bank> part of thi
gautham
2015/08/19 17:45:51
Done.
| |
78 return true; | |
79 } | |
80 | |
81 void Create(mojo::ApplicationConnection* connection, | |
82 mojo::InterfaceRequest<Bank> request) override { | |
83 bindings_.AddBinding(&bank_impl_, request.Pass()); | |
84 } | |
85 | |
86 private: | |
87 BankImpl bank_impl_; | |
88 mojo::BindingSet<Bank> bindings_; | |
89 PrincipalServicePtr login_service_; | |
90 }; | |
91 | |
92 } // namespace examples | |
93 } // namespace mojo | |
94 | |
95 MojoResult MojoMain(MojoHandle application_request) { | |
96 mojo::ApplicationRunner runner(new mojo::examples::BankApp()); | |
97 return runner.Run(application_request); | |
98 } | |
OLD | NEW |