1// Copyright 2015 The Weave 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#ifndef LIBWEAVE_SRC_PRIVET_CLOUD_DELEGATE_H_
6#define LIBWEAVE_SRC_PRIVET_CLOUD_DELEGATE_H_
7
8#include <memory>
9#include <set>
10#include <string>
11
12#include <base/callback.h>
13#include <base/memory/ref_counted.h>
14#include <base/observer_list.h>
15
16#include "src/privet/privet_types.h"
17#include "src/privet/security_delegate.h"
18
19namespace base {
20class DictionaryValue;
21}  // namespace base
22
23namespace weave {
24
25class ComponentManager;
26class DeviceRegistrationInfo;
27
28namespace provider {
29class TaskRunner;
30}
31
32namespace privet {
33
34// Interface to provide GCD functionality for PrivetHandler.
35// TODO(vitalybuka): Rename to BuffetDelegate.
36class CloudDelegate {
37 public:
38  CloudDelegate();
39  virtual ~CloudDelegate();
40
41  using CommandDoneCallback =
42      base::Callback<void(const base::DictionaryValue& commands,
43                          ErrorPtr error)>;
44
45  class Observer {
46   public:
47    virtual ~Observer() {}
48
49    virtual void OnDeviceInfoChanged() {}
50    virtual void OnTraitDefsChanged() {}
51    virtual void OnStateChanged() {}
52    virtual void OnComponentTreeChanged() {}
53  };
54
55  // Returns the ID of the device.
56  virtual std::string GetDeviceId() const = 0;
57
58  // Returns the model ID of the device.
59  virtual std::string GetModelId() const = 0;
60
61  // Returns the name of device.
62  virtual std::string GetName() const = 0;
63
64  // Returns the description of the device.
65  virtual std::string GetDescription() const = 0;
66
67  // Returns the location of the device.
68  virtual std::string GetLocation() const = 0;
69
70  // Update basic device information.
71  virtual void UpdateDeviceInfo(const std::string& name,
72                                const std::string& description,
73                                const std::string& location) = 0;
74
75  // Returns the name of the maker.
76  virtual std::string GetOemName() const = 0;
77
78  // Returns the model name of the device.
79  virtual std::string GetModelName() const = 0;
80
81  // Returns max scope available for anonymous user.
82  virtual AuthScope GetAnonymousMaxScope() const = 0;
83
84  // Returns status of the GCD connection.
85  virtual const ConnectionState& GetConnectionState() const = 0;
86
87  // Returns status of the last setup.
88  virtual const SetupState& GetSetupState() const = 0;
89
90  // Starts GCD setup.
91  virtual bool Setup(const std::string& ticket_id,
92                     const std::string& user,
93                     ErrorPtr* error) = 0;
94
95  // Returns cloud id if the registered device or empty string if unregistered.
96  virtual std::string GetCloudId() const = 0;
97
98  // Returns dictionary with device state (for legacy APIs).
99  virtual const base::DictionaryValue& GetLegacyState() const = 0;
100
101  // Returns dictionary with commands definitions (for legacy APIs).
102  virtual const base::DictionaryValue& GetLegacyCommandDef() const = 0;
103
104  // Returns dictionary with component tree.
105  virtual const base::DictionaryValue& GetComponents() const = 0;
106
107  // Finds a component at the given path. Return nullptr in case of an error.
108  virtual const base::DictionaryValue* FindComponent(const std::string& path,
109                                                     ErrorPtr* error) const = 0;
110
111  // Returns dictionary with trait definitions.
112  virtual const base::DictionaryValue& GetTraits() const = 0;
113
114  // Adds command created from the given JSON representation.
115  virtual void AddCommand(const base::DictionaryValue& command,
116                          const UserInfo& user_info,
117                          const CommandDoneCallback& callback) = 0;
118
119  // Returns command with the given ID.
120  virtual void GetCommand(const std::string& id,
121                          const UserInfo& user_info,
122                          const CommandDoneCallback& callback) = 0;
123
124  // Cancels command with the given ID.
125  virtual void CancelCommand(const std::string& id,
126                             const UserInfo& user_info,
127                             const CommandDoneCallback& callback) = 0;
128
129  // Lists commands.
130  virtual void ListCommands(const UserInfo& user_info,
131                            const CommandDoneCallback& callback) = 0;
132
133  void AddObserver(Observer* observer) { observer_list_.AddObserver(observer); }
134  void RemoveObserver(Observer* observer) {
135    observer_list_.RemoveObserver(observer);
136  }
137
138  void NotifyOnDeviceInfoChanged();
139  void NotifyOnTraitDefsChanged();
140  void NotifyOnStateChanged();
141  void NotifyOnComponentTreeChanged();
142
143  // Create default instance.
144  static std::unique_ptr<CloudDelegate> CreateDefault(
145      provider::TaskRunner* task_runner,
146      DeviceRegistrationInfo* device,
147      ComponentManager* component_manager);
148
149 private:
150  base::ObserverList<Observer> observer_list_;
151};
152
153}  // namespace privet
154}  // namespace weave
155
156#endif  // LIBWEAVE_SRC_PRIVET_CLOUD_DELEGATE_H_
157