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