aboutsummaryrefslogtreecommitdiff
path: root/buffet/binder_weave_service.h
blob: c03336689301e43c75e127ce4cdc505fa9a10fac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef BUFFET_BINDER_WEAVE_SERVICE_H_
#define BUFFET_BINDER_WEAVE_SERVICE_H_

#include <memory>
#include <vector>
#include <string>

#include <base/macros.h>
#include <base/memory/weak_ptr.h>

#include "android/weave/IWeaveClient.h"
#include "android/weave/BnWeaveService.h"

namespace weave {
class Command;
class Device;
}

namespace buffet {

// An implementation of android::weave::IWeaveService binder.
// This object is a proxy for weave::Device. A new instance of weave service is
// created for each connected client. As soon as the client disconnects, this
// object takes care of cleaning up that client's resources (e.g. it removes
// the components and their state added by the client).
class BinderWeaveService final : public android::weave::BnWeaveService {
 public:
  BinderWeaveService(weave::Device* device,
                     android::sp<android::weave::IWeaveClient> client);
  ~BinderWeaveService() override;

 private:
  // Binder methods for android::weave::IWeaveService:
  android::binder::Status addComponent(
      const android::String16& name,
      const std::vector<android::String16>& traits) override;
  android::binder::Status registerCommandHandler(
      const android::String16& component,
      const android::String16& command) override;
  android::binder::Status updateState(
      const android::String16& component,
      const android::String16& state) override;

  void OnCommand(const std::string& component_name,
                 const std::string& command_name,
                 const std::weak_ptr<weave::Command>& command);

  weave::Device* device_;
  android::sp<android::weave::IWeaveClient> client_;
  std::vector<std::string> components_;

  base::WeakPtrFactory<BinderWeaveService> weak_ptr_factory_{this};
  DISALLOW_COPY_AND_ASSIGN(BinderWeaveService);
};

}  // namespace buffet

#endif  // BUFFET_BINDER_WEAVE_SERVICE_H_