| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 <mojo/system/main.h> | |
| 6 | |
| 7 #include <map> | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "mojo/public/cpp/application/application_impl_base.h" | |
| 11 #include "mojo/public/cpp/application/run_application.h" | |
| 12 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 14 #include "mojo/public/cpp/system/macros.h" | |
| 15 #include "mojo/public/interfaces/bindings/tests/versioning_test_service.mojom.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 namespace test { | |
| 19 namespace versioning { | |
| 20 | |
| 21 struct EmployeeInfo { | |
| 22 public: | |
| 23 EmployeeInfo() {} | |
| 24 | |
| 25 EmployeePtr employee; | |
| 26 Array<uint8_t> finger_print; | |
| 27 | |
| 28 private: | |
| 29 MOJO_DISALLOW_COPY_AND_ASSIGN(EmployeeInfo); | |
| 30 }; | |
| 31 | |
| 32 class HumanResourceDatabaseImpl : public HumanResourceDatabase { | |
| 33 public: | |
| 34 explicit HumanResourceDatabaseImpl( | |
| 35 InterfaceRequest<HumanResourceDatabase> request) | |
| 36 : strong_binding_(this, request.Pass()) { | |
| 37 // Pretend that there is already some data in the system. | |
| 38 EmployeeInfo* info = new EmployeeInfo(); | |
| 39 employees_[1] = info; | |
| 40 info->employee = Employee::New(); | |
| 41 info->employee->employee_id = 1; | |
| 42 info->employee->name = "Homer Simpson"; | |
| 43 info->employee->department = Department::DEV; | |
| 44 info->employee->birthday = Date::New(); | |
| 45 info->employee->birthday->year = 1955; | |
| 46 info->employee->birthday->month = 5; | |
| 47 info->employee->birthday->day = 12; | |
| 48 info->finger_print.resize(1024); | |
| 49 for (uint32_t i = 0; i < 1024; ++i) | |
| 50 info->finger_print[i] = i; | |
| 51 } | |
| 52 | |
| 53 ~HumanResourceDatabaseImpl() override { | |
| 54 for (auto iter = employees_.begin(); iter != employees_.end(); ++iter) | |
| 55 delete iter->second; | |
| 56 } | |
| 57 | |
| 58 void AddEmployee(EmployeePtr employee, | |
| 59 const AddEmployeeCallback& callback) override { | |
| 60 uint64_t id = employee->employee_id; | |
| 61 if (employees_.find(id) == employees_.end()) | |
| 62 employees_[id] = new EmployeeInfo(); | |
| 63 employees_[id]->employee = employee.Pass(); | |
| 64 callback.Run(true); | |
| 65 } | |
| 66 | |
| 67 void QueryEmployee(uint64_t id, | |
| 68 bool retrieve_finger_print, | |
| 69 const QueryEmployeeCallback& callback) override { | |
| 70 if (employees_.find(id) == employees_.end()) { | |
| 71 callback.Run(nullptr, nullptr); | |
| 72 return; | |
| 73 } | |
| 74 callback.Run( | |
| 75 employees_[id]->employee.Clone(), | |
| 76 retrieve_finger_print ? employees_[id]->finger_print.Clone() : nullptr); | |
| 77 } | |
| 78 | |
| 79 void AttachFingerPrint(uint64_t id, | |
| 80 Array<uint8_t> finger_print, | |
| 81 const AttachFingerPrintCallback& callback) override { | |
| 82 if (employees_.find(id) == employees_.end()) { | |
| 83 callback.Run(false); | |
| 84 return; | |
| 85 } | |
| 86 employees_[id]->finger_print = finger_print.Pass(); | |
| 87 callback.Run(true); | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 std::map<uint64_t, EmployeeInfo*> employees_; | |
| 92 | |
| 93 StrongBinding<HumanResourceDatabase> strong_binding_; | |
| 94 | |
| 95 MOJO_DISALLOW_COPY_AND_ASSIGN(HumanResourceDatabaseImpl); | |
| 96 }; | |
| 97 | |
| 98 class HumanResourceSystemServer : public ApplicationImplBase { | |
| 99 public: | |
| 100 HumanResourceSystemServer() {} | |
| 101 ~HumanResourceSystemServer() override {} | |
| 102 | |
| 103 // |ApplicationImplBase| overrides: | |
| 104 bool OnAcceptConnection(ServiceProviderImpl* service_provider_impl) override { | |
| 105 service_provider_impl->AddService<HumanResourceDatabase>( | |
| 106 [](const ConnectionContext& connection_context, | |
| 107 InterfaceRequest<HumanResourceDatabase> hr_db_request) { | |
| 108 // It will be deleted automatically when the underlying pipe | |
| 109 // encounters a connection error. | |
| 110 new HumanResourceDatabaseImpl(hr_db_request.Pass()); | |
| 111 }); | |
| 112 return true; | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 MOJO_DISALLOW_COPY_AND_ASSIGN(HumanResourceSystemServer); | |
| 117 }; | |
| 118 | |
| 119 } // namespace versioning | |
| 120 } // namespace test | |
| 121 } // namespace mojo | |
| 122 | |
| 123 MojoResult MojoMain(MojoHandle application_request) { | |
| 124 mojo::test::versioning::HumanResourceSystemServer hr_system_server; | |
| 125 return mojo::RunApplication(application_request, &hr_system_server); | |
| 126 } | |
| OLD | NEW |