Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(914)

Unified Diff: examples/bank_app/bank.cc

Issue 1261403003: Initial skeletal implementation of the PrincipalService. Also, use the Login()/GetUserBlessing() (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Add a new example for user authorization Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..93832911f81df2bd16925bfd5ad02cd9ed2f7b9f
--- /dev/null
+++ b/examples/bank_app/bank.cc
@@ -0,0 +1,81 @@
+// Copyright 2014 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 {
+namespace examples {
+
+class BankImpl : public Bank {
+ public:
+ BankImpl() : balance_(0) {
+ }
+ void Deposit(const int32_t usd) override { balance_ += usd; }
+ void Withdraw(const int32_t usd) override { balance_ -= usd; }
+ void GetBalance(const GetBalanceCallback& callback) override {
+ callback.Run(balance_);
+ }
+ void Run(const BlessingPtr& b) const {
+ user_.clear();
+ if (b.get()) {
+ for (size_t i = 0; i < b.get()->chain.size(); i++) {
+ user_ += "/";
+ user_ += b.get()->chain[i].get()->extension;
+ }
+ }
+ RunLoop::current()->Quit(); // All done!
+ }
+ private:
+ mutable std::string user_;
+ int32_t balance_;
+};
+
+class BankApp : public mojo::ApplicationDelegate,
+ public mojo::InterfaceFactory<Bank> {
+ public:
+ BankApp() : binding_(&bank_impl_) {}
+
+ void Initialize(ApplicationImpl* app) override {
+ app->ConnectToService("mojo:principal_service", &login_service_);
+ }
+
+ // From ApplicationDelegate
+ bool ConfigureIncomingConnection(
+ mojo::ApplicationConnection* connection) override {
+ AppInstanceNamePtr app(AppInstanceName::New());
+ app->url = connection->GetRemoteApplicationURL();
+ // Authorize user access to the bank account
+ login_service_->GetUserBlessing(app.Pass(), bank_impl_);
+ connection->AddService<Bank>(this);
+ return true;
+ }
+
+ void Create(mojo::ApplicationConnection* connection,
+ mojo::InterfaceRequest<Bank> request) override {
+ binding_.Bind(request.Pass());
+ }
+
+ private:
+ BankImpl bank_impl_;
+ mojo::Binding<Bank> binding_;
+ PrincipalServicePtr login_service_;
+};
+
+} // namespace examples
+} // namespace mojo
+
+MojoResult MojoMain(MojoHandle application_request) {
+ mojo::ApplicationRunner runner(new mojo::examples::BankApp());
+ return runner.Run(application_request);
+}

Powered by Google App Engine
This is Rietveld 408576698