OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
jamesr
2015/08/17 21:38:21
2015
gautham
2015/08/18 01:54:21
Done.
| |
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 { | |
18 namespace examples { | |
19 | |
20 class BankImpl : public Bank { | |
21 public: | |
22 BankImpl() : balance_(0) {} | |
23 void Deposit(const int32_t usd) override { balance_ += usd; } | |
jamesr
2015/08/17 21:38:21
drop the 'const' - the body is trivial enough that
gautham
2015/08/18 01:54:21
Done.
| |
24 void Withdraw(const int32_t usd) override { balance_ -= usd; } | |
25 void GetBalance(const GetBalanceCallback& callback) override { | |
26 callback.Run(balance_); | |
27 } | |
28 private: | |
29 int32_t balance_; | |
30 }; | |
31 | |
32 class BankUser { | |
33 public: | |
34 BankUser(std::string* user) : user_(user) { } | |
jamesr
2015/08/17 21:38:21
add explicit keyword to one-argument constructor
gautham
2015/08/18 01:54:21
Done.
| |
35 void Run(const BlessingPtr& b) const { | |
36 user_->clear(); | |
37 if (b.get()) { | |
38 for (size_t i = 0; i < b.get()->chain.size(); i++) { | |
39 user_->append("/"); | |
40 user_->append(b.get()->chain[i].get()->extension); | |
41 } | |
42 } | |
43 RunLoop::current()->Quit(); // All done! | |
44 } | |
45 private: | |
46 mutable std::string *user_; | |
jamesr
2015/08/17 21:38:21
you aren't modifying the pointer value, just the p
gautham
2015/08/18 01:54:21
Done.
| |
47 }; | |
48 | |
49 class BankApp : public mojo::ApplicationDelegate, | |
50 public mojo::InterfaceFactory<Bank> { | |
51 public: | |
52 BankApp() { | |
53 } | |
54 void Initialize(ApplicationImpl* app) override { | |
55 app->ConnectToService("mojo:principal_service", &login_service_); | |
56 } | |
57 | |
58 // From ApplicationDelegate | |
59 bool ConfigureIncomingConnection( | |
60 mojo::ApplicationConnection* connection) override { | |
61 std::string url = connection->GetRemoteApplicationURL(); | |
62 if (url.length() > 0) { | |
jamesr
2015/08/17 21:38:21
what is this branch for?
gautham
2015/08/18 01:54:21
I reject connection requests from customers that h
| |
63 AppInstanceNamePtr app(AppInstanceName::New()); | |
64 app->url = url; | |
65 std::string user; | |
66 login_service_->GetUserBlessing(app.Pass(), BankUser(&user)); | |
67 login_service_.WaitForIncomingResponse(); | |
jamesr
2015/08/17 21:38:21
why is this blocking? you should probably check th
gautham
2015/08/18 01:54:21
I wanted to reject connection requests from custom
| |
68 // Record user access to the bank and reject customers that | |
69 // don't have a user identity. | |
70 if (user.length() <= 0) { | |
jamesr
2015/08/17 21:38:21
std::string::length() returns a size_t, which is u
gautham
2015/08/18 01:54:21
Done.
| |
71 return false; | |
72 } | |
73 MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; | |
74 } | |
75 connection->AddService<Bank>(this); | |
76 return true; | |
77 } | |
78 | |
79 void Create(mojo::ApplicationConnection* connection, | |
80 mojo::InterfaceRequest<Bank> request) override { | |
81 bindings_.AddBinding(&bank_impl_, request.Pass()); | |
82 } | |
83 | |
84 private: | |
85 BankImpl bank_impl_; | |
86 mojo::BindingSet<Bank> bindings_; | |
87 PrincipalServicePtr login_service_; | |
88 }; | |
89 | |
90 } // namespace examples | |
91 } // namespace mojo | |
92 | |
93 MojoResult MojoMain(MojoHandle application_request) { | |
94 mojo::ApplicationRunner runner(new mojo::examples::BankApp()); | |
95 return runner.Run(application_request); | |
96 } | |
OLD | NEW |