manager.h revision 63bdf085f9d68f86950f5b45c8c8ad0ba2088dc0
1// Copyright 2014 The Chromium OS 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 BUFFET_MANAGER_H_
6#define BUFFET_MANAGER_H_
7
8#include <memory>
9#include <set>
10#include <string>
11#include <vector>
12
13#include <base/files/file_path.h>
14#include <base/macros.h>
15#include <base/memory/weak_ptr.h>
16#include <base/values.h>
17#include <chromeos/dbus/data_serialization.h>
18#include <chromeos/dbus/dbus_object.h>
19#include <chromeos/dbus/exported_property_set.h>
20#include <chromeos/errors/error.h>
21#include <weave/device.h>
22
23#include "buffet/dbus_bindings/com.android.Weave.Manager.h"
24
25namespace chromeos {
26namespace dbus_utils {
27class ExportedObjectManager;
28}  // namespace dbus_utils
29}  // namespace chromeos
30
31namespace buffet {
32
33class BuffetConfig;
34class DBusCommandDispacher;
35class HttpTransportClient;
36class MdnsClient;
37class NetworkClient;
38class WebServClient;
39struct BuffetConfigPaths;
40
41template<typename... Types>
42using DBusMethodResponsePtr =
43    std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<Types...>>;
44
45template<typename... Types>
46using DBusMethodResponse =
47    chromeos::dbus_utils::DBusMethodResponse<Types...>;
48
49// The Manager is responsible for global state of Buffet.  It exposes
50// interfaces which affect the entire device such as device registration and
51// device state.
52class Manager final : public com::android::Weave::ManagerInterface {
53 public:
54  explicit Manager(
55      const base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager>&
56          object_manager);
57  ~Manager();
58
59  void Start(const weave::Device::Options& options,
60             const BuffetConfigPaths& paths,
61             const std::set<std::string>& device_whitelist,
62             chromeos::dbus_utils::AsyncEventSequencer* sequencer);
63
64  void Stop();
65
66 private:
67  // DBus methods:
68  void CheckDeviceRegistered(
69      DBusMethodResponsePtr<std::string> response) override;
70  void GetDeviceInfo(DBusMethodResponsePtr<std::string> response) override;
71  void RegisterDevice(DBusMethodResponsePtr<std::string> response,
72                      const std::string& ticket_id) override;
73  bool UpdateDeviceInfo(chromeos::ErrorPtr* error,
74                        const std::string& name,
75                        const std::string& description,
76                        const std::string& location) override;
77  bool UpdateServiceConfig(chromeos::ErrorPtr* error,
78                           const std::string& client_id,
79                           const std::string& client_secret,
80                           const std::string& api_key,
81                           const std::string& oauth_url,
82                           const std::string& service_url) override;
83  void UpdateState(DBusMethodResponsePtr<> response,
84                   const chromeos::VariantDictionary& property_set) override;
85  bool GetState(chromeos::ErrorPtr* error, std::string* state) override;
86  void AddCommand(DBusMethodResponsePtr<std::string> response,
87                  const std::string& json_command,
88                  const std::string& in_user_role) override;
89  void GetCommand(DBusMethodResponsePtr<std::string> response,
90                  const std::string& id) override;
91  std::string TestMethod(const std::string& message) override;
92  bool EnableWiFiBootstrapping(
93      chromeos::ErrorPtr* error,
94      const dbus::ObjectPath& in_listener_path,
95      const chromeos::VariantDictionary& in_options) override;
96  bool DisableWiFiBootstrapping(chromeos::ErrorPtr* error) override;
97  bool EnableGCDBootstrapping(
98      chromeos::ErrorPtr* error,
99      const dbus::ObjectPath& in_listener_path,
100      const chromeos::VariantDictionary& in_options) override;
101  bool DisableGCDBootstrapping(chromeos::ErrorPtr* error) override;
102
103  void OnGetDeviceInfoSuccess(
104      const std::shared_ptr<DBusMethodResponse<std::string>>& response,
105      const base::DictionaryValue& device_info);
106  void OnGetDeviceInfoError(
107      const std::shared_ptr<DBusMethodResponse<std::string>>& response,
108      const weave::Error* error);
109
110  void StartPrivet(const weave::Device::Options& options,
111                   chromeos::dbus_utils::AsyncEventSequencer* sequencer);
112
113  void OnStateChanged();
114  void OnRegistrationChanged(weave::RegistrationStatus status);
115  void OnConfigChanged(const weave::Settings& settings);
116  void UpdateWiFiBootstrapState(weave::WifiSetupState state);
117  void OnPairingStart(const std::string& session_id,
118                      weave::PairingType pairing_type,
119                      const std::vector<uint8_t>& code);
120  void OnPairingEnd(const std::string& session_id);
121
122  com::android::Weave::ManagerAdaptor dbus_adaptor_{this};
123  chromeos::dbus_utils::DBusObject dbus_object_;
124
125  class TaskRunner;
126  std::unique_ptr<TaskRunner> task_runner_;
127  std::unique_ptr<HttpTransportClient> http_client_;
128  std::unique_ptr<NetworkClient> network_client_;
129  std::unique_ptr<MdnsClient> mdns_client_;
130  std::unique_ptr<WebServClient> web_serv_client_;
131  std::unique_ptr<BuffetConfig> config_;
132  std::unique_ptr<weave::Device> device_;
133  std::unique_ptr<DBusCommandDispacher> command_dispatcher_;
134
135  base::WeakPtrFactory<Manager> weak_ptr_factory_{this};
136  DISALLOW_COPY_AND_ASSIGN(Manager);
137};
138
139}  // namespace buffet
140
141#endif  // BUFFET_MANAGER_H_
142