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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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 {
18 namespace examples {
19
20 class BankImpl : public Bank {
21 public:
22 BankImpl() : balance_(0) {
23 }
24 void Deposit(const int32_t usd) override { balance_ += usd; }
25 void Withdraw(const int32_t usd) override { balance_ -= usd; }
26 void GetBalance(const GetBalanceCallback& callback) override {
27 callback.Run(balance_);
28 }
29 void Run(const BlessingPtr& b) const {
30 user_.clear();
31 if (b.get()) {
32 for (size_t i = 0; i < b.get()->chain.size(); i++) {
33 user_ += "/";
34 user_ += b.get()->chain[i].get()->extension;
35 }
36 }
37 RunLoop::current()->Quit(); // All done!
38 }
39 private:
40 mutable std::string user_;
41 int32_t balance_;
42 };
43
44 class BankApp : public mojo::ApplicationDelegate,
45 public mojo::InterfaceFactory<Bank> {
46 public:
47 BankApp() : binding_(&bank_impl_) {}
48
49 void Initialize(ApplicationImpl* app) override {
50 app->ConnectToService("mojo:principal_service", &login_service_);
51 }
52
53 // From ApplicationDelegate
54 bool ConfigureIncomingConnection(
55 mojo::ApplicationConnection* connection) override {
56 AppInstanceNamePtr app(AppInstanceName::New());
57 app->url = connection->GetRemoteApplicationURL();
58 // Authorize user access to the bank account
59 login_service_->GetUserBlessing(app.Pass(), bank_impl_);
60 connection->AddService<Bank>(this);
61 return true;
62 }
63
64 void Create(mojo::ApplicationConnection* connection,
65 mojo::InterfaceRequest<Bank> request) override {
66 binding_.Bind(request.Pass());
67 }
68
69 private:
70 BankImpl bank_impl_;
71 mojo::Binding<Bank> binding_;
72 PrincipalServicePtr login_service_;
73 };
74
75 } // namespace examples
76 } // namespace mojo
77
78 MojoResult MojoMain(MojoHandle application_request) {
79 mojo::ApplicationRunner runner(new mojo::examples::BankApp());
80 return runner.Run(application_request);
81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698