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..d94bc1e8a2703975699986aac64e2a0f20698ae0 |
--- /dev/null |
+++ b/examples/bank_app/bank.cc |
@@ -0,0 +1,98 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// 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 { |
jamesr
2015/08/19 05:30:54
drop the 'mojo::' part
gautham
2015/08/19 17:45:51
Done.
|
+namespace examples { |
+ |
+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.
|
+ |
+class BankImpl : public Bank { |
+ public: |
+ BankImpl() : balance_(0) {} |
+ void Deposit(int32_t usd) override { balance_ += usd; } |
+ void Withdraw(int32_t usd) override { balance_ -= usd; } |
+ void GetBalance(const GetBalanceCallback& callback) override { |
+ callback.Run(balance_); |
+ } |
+ private: |
+ int32_t balance_; |
+}; |
+ |
+class BankUser { |
+ public: |
+ explicit BankUser(std::string* user) : user_(user) { } |
+ void Run(const BlessingPtr& b) const { |
+ user_->clear(); |
+ if (b) { |
+ for (size_t i = 0; i < b->chain.size(); i++) { |
+ 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.
|
+ user_->append(b->chain[i]->extension); |
+ } |
+ } |
+ } |
+ private: |
+ std::string *user_; |
+}; |
+ |
+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) { |
+ AppInstanceNamePtr app(AppInstanceName::New()); |
+ app->url = url; |
+ std::string user; |
+ login_service_->GetUserBlessing(app.Pass(), BankUser(&user)); |
+ 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
|
+ // Record user access to the bank and reject customers that |
+ // don't have a user identity. |
+ if (user.empty()) { |
+ MOJO_LOG(INFO) << "Rejecting customer without a user identity"; |
+ 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:
|
+ } |
+ MOJO_LOG(INFO) << "Customer " << user << " accessing bank"; |
+ } |
+ 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.
|
+ 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); |
+} |