Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_PRIVET_PRIVET_HANDLER_H_ |
| 6 | #define LIBWEAVE_SRC_PRIVET_PRIVET_HANDLER_H_ |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/macros.h> |
| 13 | #include <base/memory/weak_ptr.h> |
| 14 | #include <base/scoped_observer.h> |
| 15 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 16 | #include "src/privet/cloud_delegate.h" |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | class Value; |
| 20 | class DictionaryValue; |
| 21 | } // namespace base |
| 22 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 23 | namespace weave { |
| 24 | namespace privet { |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 25 | |
| 26 | class DeviceDelegate; |
| 27 | class IdentityDelegate; |
| 28 | class SecurityDelegate; |
| 29 | class WifiDelegate; |
| 30 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 31 | // Privet V3 HTTP/HTTPS requests handler. |
| 32 | // API details at https://developers.google.com/cloud-devices/ |
| 33 | class PrivetHandler : public CloudDelegate::Observer { |
| 34 | public: |
| 35 | // Callback to handle requests asynchronously. |
| 36 | // |status| is HTTP status code. |
| 37 | // |output| is result returned in HTTP response. Contains result of |
| 38 | // successfully request of information about error. |
| 39 | using RequestCallback = |
| 40 | base::Callback<void(int status, const base::DictionaryValue& output)>; |
| 41 | |
| 42 | PrivetHandler(CloudDelegate* cloud, |
| 43 | DeviceDelegate* device, |
| 44 | SecurityDelegate* pairing, |
Johan Euphrosine | 0b7bb9f | 2015-09-29 01:11:21 -0700 | [diff] [blame] | 45 | WifiDelegate* wifi); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 46 | ~PrivetHandler() override; |
| 47 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 48 | void OnTraitDefsChanged() override; |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame^] | 49 | void OnStateChanged() override; |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 50 | void OnComponentTreeChanged() override; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 51 | |
Vitaly Buka | 1a39c81 | 2015-10-08 21:20:58 -0700 | [diff] [blame] | 52 | std::vector<std::string> GetHttpPaths() const; |
| 53 | std::vector<std::string> GetHttpsPaths() const; |
| 54 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 55 | // Handles HTTP/HTTPS Privet request. |
| 56 | // |api| is the path from the HTTP request, e.g /privet/info. |
| 57 | // |auth_header| is the Authentication header from HTTP request. |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 58 | // |input| is the POST data from HTTP request. If nullptr, data format is |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 59 | // not valid JSON. |
| 60 | // |callback| will be called exactly once during or after |HandleRequest| |
| 61 | // call. |
| 62 | void HandleRequest(const std::string& api, |
| 63 | const std::string& auth_header, |
| 64 | const base::DictionaryValue* input, |
| 65 | const RequestCallback& callback); |
| 66 | |
| 67 | private: |
| 68 | using ApiHandler = void (PrivetHandler::*)(const base::DictionaryValue&, |
| 69 | const UserInfo&, |
| 70 | const RequestCallback&); |
| 71 | |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 72 | // Adds a handler for both HTTP and HTTPS interfaces. |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 73 | void AddHandler(const std::string& path, ApiHandler handler, AuthScope scope); |
| 74 | |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 75 | // Adds a handler for both HTTPS interface only. |
| 76 | void AddSecureHandler(const std::string& path, |
| 77 | ApiHandler handler, |
| 78 | AuthScope scope); |
| 79 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 80 | void HandleInfo(const base::DictionaryValue&, |
| 81 | const UserInfo& user_info, |
| 82 | const RequestCallback& callback); |
| 83 | void HandlePairingStart(const base::DictionaryValue& input, |
| 84 | const UserInfo& user_info, |
| 85 | const RequestCallback& callback); |
| 86 | void HandlePairingConfirm(const base::DictionaryValue& input, |
| 87 | const UserInfo& user_info, |
| 88 | const RequestCallback& callback); |
| 89 | void HandlePairingCancel(const base::DictionaryValue& input, |
| 90 | const UserInfo& user_info, |
| 91 | const RequestCallback& callback); |
| 92 | void HandleAuth(const base::DictionaryValue& input, |
| 93 | const UserInfo& user_info, |
| 94 | const RequestCallback& callback); |
| 95 | void HandleSetupStart(const base::DictionaryValue& input, |
| 96 | const UserInfo& user_info, |
| 97 | const RequestCallback& callback); |
| 98 | void HandleSetupStatus(const base::DictionaryValue&, |
| 99 | const UserInfo& user_info, |
| 100 | const RequestCallback& callback); |
| 101 | void HandleState(const base::DictionaryValue& input, |
| 102 | const UserInfo& user_info, |
| 103 | const RequestCallback& callback); |
| 104 | void HandleCommandDefs(const base::DictionaryValue& input, |
| 105 | const UserInfo& user_info, |
| 106 | const RequestCallback& callback); |
| 107 | void HandleCommandsExecute(const base::DictionaryValue& input, |
| 108 | const UserInfo& user_info, |
| 109 | const RequestCallback& callback); |
| 110 | void HandleCommandsStatus(const base::DictionaryValue& input, |
| 111 | const UserInfo& user_info, |
| 112 | const RequestCallback& callback); |
| 113 | void HandleCommandsList(const base::DictionaryValue& input, |
| 114 | const UserInfo& user_info, |
| 115 | const RequestCallback& callback); |
| 116 | void HandleCommandsCancel(const base::DictionaryValue& input, |
| 117 | const UserInfo& user_info, |
| 118 | const RequestCallback& callback); |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 119 | void HandleCheckForUpdates(const base::DictionaryValue& input, |
| 120 | const UserInfo& user_info, |
| 121 | const RequestCallback& callback); |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame^] | 122 | void HandleTraits(const base::DictionaryValue& input, |
| 123 | const UserInfo& user_info, |
| 124 | const RequestCallback& callback); |
| 125 | void HandleComponents(const base::DictionaryValue& input, |
| 126 | const UserInfo& user_info, |
| 127 | const RequestCallback& callback); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 128 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 129 | void ReplyWithSetupStatus(const RequestCallback& callback) const; |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 130 | void ReplyToUpdateRequest(const RequestCallback& callback) const; |
| 131 | void OnUpdateRequestTimeout(int update_request_id); |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 132 | |
| 133 | CloudDelegate* cloud_ = nullptr; |
| 134 | DeviceDelegate* device_ = nullptr; |
| 135 | SecurityDelegate* security_ = nullptr; |
| 136 | WifiDelegate* wifi_ = nullptr; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 137 | |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 138 | struct HandlerParameters { |
| 139 | ApiHandler handler; |
| 140 | AuthScope scope; |
| 141 | bool https_only = true; |
| 142 | }; |
| 143 | std::map<std::string, HandlerParameters> handlers_; |
| 144 | |
| 145 | struct UpdateRequestParameters { |
| 146 | RequestCallback callback; |
| 147 | int request_id = 0; |
| 148 | int state_fingerprint = -1; |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame^] | 149 | int traits_fingerprint = -1; |
| 150 | int components_fingerprint = -1; |
Alex Vakulenko | efee3a2 | 2015-11-17 15:08:38 -0800 | [diff] [blame] | 151 | }; |
| 152 | std::vector<UpdateRequestParameters> update_requests_; |
| 153 | int last_update_request_id_{0}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 154 | |
| 155 | uint64_t last_user_id_{0}; |
| 156 | int state_fingerprint_{0}; |
Alex Vakulenko | 551a82b | 2015-12-07 14:46:12 -0800 | [diff] [blame^] | 157 | int traits_fingerprint_{0}; |
| 158 | int components_fingerprint_{0}; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 159 | ScopedObserver<CloudDelegate, CloudDelegate::Observer> cloud_observer_{this}; |
| 160 | |
| 161 | base::WeakPtrFactory<PrivetHandler> weak_ptr_factory_{this}; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(PrivetHandler); |
| 164 | }; |
| 165 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 166 | } // namespace privet |
| 167 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 168 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 169 | #endif // LIBWEAVE_SRC_PRIVET_PRIVET_HANDLER_H_ |