Home | History | Annotate | Download | only in tests
      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/cpp/bindings/strong_binding.h"
     12 #include "mojo/public/interfaces/bindings/tests/versioning_test_service.mojom.h"
     13 #include "services/service_manager/public/c/main.h"
     14 #include "services/service_manager/public/cpp/binder_registry.h"
     15 #include "services/service_manager/public/cpp/service.h"
     16 #include "services/service_manager/public/cpp/service_runner.h"
     17 
     18 namespace mojo {
     19 namespace test {
     20 namespace versioning {
     21 
     22 struct EmployeeInfo {
     23  public:
     24   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:
     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 
     54   ~HumanResourceDatabaseImpl() override {
     55     for (auto iter = employees_.begin(); iter != employees_.end(); ++iter)
     56       delete iter->second;
     57   }
     58 
     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 
     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 
     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 : public service_manager::Service {
     98  public:
     99   HumanResourceSystemServer() {
    100     registry_.AddInterface<HumanResourceDatabase>(
    101         base::Bind(&HumanResourceSystemServer::Create, base::Unretained(this)));
    102   }
    103 
    104   // service_manager::Service implementation.
    105   void OnBindInterface(const service_manager::BindSourceInfo& source_info,
    106                        const std::string& interface_name,
    107                        mojo::ScopedMessagePipeHandle interface_pipe) override {
    108     registry_.BindInterface(interface_name, std::move(interface_pipe));
    109   }
    110 
    111   void Create(HumanResourceDatabaseRequest request) {
    112     // It will be deleted automatically when the underlying pipe encounters a
    113     // connection error.
    114     new HumanResourceDatabaseImpl(std::move(request));
    115   }
    116 
    117  private:
    118   service_manager::BinderRegistry registry_;
    119 };
    120 
    121 }  // namespace versioning
    122 }  // namespace test
    123 }  // namespace mojo
    124 
    125 MojoResult ServiceMain(MojoHandle request) {
    126   mojo::ServiceRunner runner(
    127       new mojo::test::versioning::HumanResourceSystemServer());
    128 
    129   return runner.Run(request);
    130 }
    131