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 | |
| 48 | void OnCommandDefsChanged() override; |
| 49 | void OnStateChanged() override; |
| 50 | |
Vitaly Buka | 1a39c81 | 2015-10-08 21:20:58 -0700 | [diff] [blame] | 51 | std::vector<std::string> GetHttpPaths() const; |
| 52 | std::vector<std::string> GetHttpsPaths() const; |
| 53 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 54 | // Handles HTTP/HTTPS Privet request. |
| 55 | // |api| is the path from the HTTP request, e.g /privet/info. |
| 56 | // |auth_header| is the Authentication header from HTTP request. |
| 57 | // |input| is the the POST data from HTTP request. If nullptr, data format is |
| 58 | // not valid JSON. |
| 59 | // |callback| will be called exactly once during or after |HandleRequest| |
| 60 | // call. |
| 61 | void HandleRequest(const std::string& api, |
| 62 | const std::string& auth_header, |
| 63 | const base::DictionaryValue* input, |
| 64 | const RequestCallback& callback); |
| 65 | |
| 66 | private: |
| 67 | using ApiHandler = void (PrivetHandler::*)(const base::DictionaryValue&, |
| 68 | const UserInfo&, |
| 69 | const RequestCallback&); |
| 70 | |
| 71 | void AddHandler(const std::string& path, ApiHandler handler, AuthScope scope); |
| 72 | |
| 73 | void HandleInfo(const base::DictionaryValue&, |
| 74 | const UserInfo& user_info, |
| 75 | const RequestCallback& callback); |
| 76 | void HandlePairingStart(const base::DictionaryValue& input, |
| 77 | const UserInfo& user_info, |
| 78 | const RequestCallback& callback); |
| 79 | void HandlePairingConfirm(const base::DictionaryValue& input, |
| 80 | const UserInfo& user_info, |
| 81 | const RequestCallback& callback); |
| 82 | void HandlePairingCancel(const base::DictionaryValue& input, |
| 83 | const UserInfo& user_info, |
| 84 | const RequestCallback& callback); |
| 85 | void HandleAuth(const base::DictionaryValue& input, |
| 86 | const UserInfo& user_info, |
| 87 | const RequestCallback& callback); |
| 88 | void HandleSetupStart(const base::DictionaryValue& input, |
| 89 | const UserInfo& user_info, |
| 90 | const RequestCallback& callback); |
| 91 | void HandleSetupStatus(const base::DictionaryValue&, |
| 92 | const UserInfo& user_info, |
| 93 | const RequestCallback& callback); |
| 94 | void HandleState(const base::DictionaryValue& input, |
| 95 | const UserInfo& user_info, |
| 96 | const RequestCallback& callback); |
| 97 | void HandleCommandDefs(const base::DictionaryValue& input, |
| 98 | const UserInfo& user_info, |
| 99 | const RequestCallback& callback); |
| 100 | void HandleCommandsExecute(const base::DictionaryValue& input, |
| 101 | const UserInfo& user_info, |
| 102 | const RequestCallback& callback); |
| 103 | void HandleCommandsStatus(const base::DictionaryValue& input, |
| 104 | const UserInfo& user_info, |
| 105 | const RequestCallback& callback); |
| 106 | void HandleCommandsList(const base::DictionaryValue& input, |
| 107 | const UserInfo& user_info, |
| 108 | const RequestCallback& callback); |
| 109 | void HandleCommandsCancel(const base::DictionaryValue& input, |
| 110 | const UserInfo& user_info, |
| 111 | const RequestCallback& callback); |
| 112 | |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 113 | void ReplyWithSetupStatus(const RequestCallback& callback) const; |
| 114 | |
| 115 | CloudDelegate* cloud_ = nullptr; |
| 116 | DeviceDelegate* device_ = nullptr; |
| 117 | SecurityDelegate* security_ = nullptr; |
| 118 | WifiDelegate* wifi_ = nullptr; |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 119 | |
| 120 | std::map<std::string, std::pair<AuthScope, ApiHandler>> handlers_; |
| 121 | |
| 122 | uint64_t last_user_id_{0}; |
| 123 | int state_fingerprint_{0}; |
| 124 | int command_defs_fingerprint_{0}; |
| 125 | ScopedObserver<CloudDelegate, CloudDelegate::Observer> cloud_observer_{this}; |
| 126 | |
| 127 | base::WeakPtrFactory<PrivetHandler> weak_ptr_factory_{this}; |
| 128 | |
| 129 | DISALLOW_COPY_AND_ASSIGN(PrivetHandler); |
| 130 | }; |
| 131 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 132 | } // namespace privet |
| 133 | } // namespace weave |
Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 134 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 135 | #endif // LIBWEAVE_SRC_PRIVET_PRIVET_HANDLER_H_ |