1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BUFFET_BINDER_WEAVE_SERVICE_H_
16 #define BUFFET_BINDER_WEAVE_SERVICE_H_
17 
18 #include <memory>
19 #include <vector>
20 #include <string>
21 
22 #include <base/macros.h>
23 #include <base/memory/weak_ptr.h>
24 
25 #include "android/weave/IWeaveClient.h"
26 #include "android/weave/BnWeaveService.h"
27 
28 namespace weave {
29 class Command;
30 class Device;
31 }
32 
33 namespace buffet {
34 
35 // An implementation of android::weave::IWeaveService binder.
36 // This object is a proxy for weave::Device. A new instance of weave service is
37 // created for each connected client. As soon as the client disconnects, this
38 // object takes care of cleaning up that client's resources (e.g. it removes
39 // the components and their state added by the client).
40 class BinderWeaveService final : public android::weave::BnWeaveService {
41  public:
42   BinderWeaveService(weave::Device* device,
43                      android::sp<android::weave::IWeaveClient> client);
44   ~BinderWeaveService() override;
45 
46  private:
47   // Binder methods for android::weave::IWeaveService:
48   android::binder::Status addComponent(
49       const android::String16& name,
50       const std::vector<android::String16>& traits) override;
51   android::binder::Status registerCommandHandler(
52       const android::String16& component,
53       const android::String16& command) override;
54   android::binder::Status updateState(
55       const android::String16& component,
56       const android::String16& state) override;
57 
58   void OnCommand(const std::string& component_name,
59                  const std::string& command_name,
60                  const std::weak_ptr<weave::Command>& command);
61 
62   weave::Device* device_;
63   android::sp<android::weave::IWeaveClient> client_;
64   std::vector<std::string> components_;
65 
66   base::WeakPtrFactory<BinderWeaveService> weak_ptr_factory_{this};
67   DISALLOW_COPY_AND_ASSIGN(BinderWeaveService);
68 };
69 
70 }  // namespace buffet
71 
72 #endif  // BUFFET_BINDER_WEAVE_SERVICE_H_
73