Index: examples/bank_app/bank.cc |
diff --git a/examples/bank_app/bank.cc b/examples/bank_app/bank.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a051cfcc6de51f03b840eb3d0f2747431918abfa |
--- /dev/null |
+++ b/examples/bank_app/bank.cc |
@@ -0,0 +1,96 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
jamesr
2015/08/17 21:38:21
2015
gautham
2015/08/18 01:54:21
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "examples/bank_app/bank.mojom.h" |
+#include "mojo/common/binding_set.h" |
+#include "mojo/public/c/system/main.h" |
+#include "mojo/public/cpp/application/application_connection.h" |
+#include "mojo/public/cpp/application/application_delegate.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_runner.h" |
+#include "mojo/public/cpp/application/interface_factory.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+#include "mojo/public/cpp/utility/run_loop.h" |
+#include "mojo/services/vanadium/security/public/interfaces/principal.mojom.h" |
+ |
+namespace mojo { |
+namespace examples { |
+ |
+class BankImpl : public Bank { |
+ public: |
+ BankImpl() : balance_(0) {} |
+ 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.
|
+ void Withdraw(const int32_t usd) override { balance_ -= usd; } |
+ void GetBalance(const GetBalanceCallback& callback) override { |
+ callback.Run(balance_); |
+ } |
+ private: |
+ int32_t balance_; |
+}; |
+ |
+class BankUser { |
+ public: |
+ 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.
|
+ void Run(const BlessingPtr& b) const { |
+ user_->clear(); |
+ if (b.get()) { |
+ for (size_t i = 0; i < b.get()->chain.size(); i++) { |
+ user_->append("/"); |
+ user_->append(b.get()->chain[i].get()->extension); |
+ } |
+ } |
+ RunLoop::current()->Quit(); // All done! |
+ } |
+ private: |
+ 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.
|
+}; |
+ |
+class BankApp : public mojo::ApplicationDelegate, |
+ public mojo::InterfaceFactory<Bank> { |
+ public: |
+ BankApp() { |
+ } |
+ void Initialize(ApplicationImpl* app) override { |
+ app->ConnectToService("mojo:principal_service", &login_service_); |
+ } |
+ |
+ // From ApplicationDelegate |
+ bool ConfigureIncomingConnection( |
+ mojo::ApplicationConnection* connection) override { |
+ std::string url = connection->GetRemoteApplicationURL(); |
+ 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
|
+ AppInstanceNamePtr app(AppInstanceName::New()); |
+ app->url = url; |
+ std::string user; |
+ login_service_->GetUserBlessing(app.Pass(), BankUser(&user)); |
+ 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
|
+ // Record user access to the bank and reject customers that |
+ // don't have a user identity. |
+ 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.
|
+ return false; |
+ } |
+ MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; |
+ } |
+ connection->AddService<Bank>(this); |
+ return true; |
+ } |
+ |
+ void Create(mojo::ApplicationConnection* connection, |
+ mojo::InterfaceRequest<Bank> request) override { |
+ bindings_.AddBinding(&bank_impl_, request.Pass()); |
+ } |
+ |
+ private: |
+ BankImpl bank_impl_; |
+ mojo::BindingSet<Bank> bindings_; |
+ PrincipalServicePtr login_service_; |
+}; |
+ |
+} // namespace examples |
+} // namespace mojo |
+ |
+MojoResult MojoMain(MojoHandle application_request) { |
+ mojo::ApplicationRunner runner(new mojo::examples::BankApp()); |
+ return runner.Run(application_request); |
+} |