Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 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_PRIVET_PRIVET_HANDLER_H_ |
| 6 | #define BUFFET_PRIVET_PRIVET_HANDLER_H_ |
| 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 | |
| 16 | #include "buffet/privet/cloud_delegate.h" |
| 17 | |
| 18 | namespace base { |
| 19 | class Value; |
| 20 | class DictionaryValue; |
| 21 | } // namespace base |
| 22 | |
| 23 | namespace privetd { |
| 24 | |
| 25 | class DeviceDelegate; |
| 26 | class IdentityDelegate; |
| 27 | class SecurityDelegate; |
| 28 | class WifiDelegate; |
| 29 | |
| 30 | enum class AuthScope; |
| 31 | |
| 32 | // Privet V3 HTTP/HTTPS requests handler. |
| 33 | // API details at https://developers.google.com/cloud-devices/ |
| 34 | class PrivetHandler : public CloudDelegate::Observer { |
| 35 | public: |
| 36 | // Callback to handle requests asynchronously. |
| 37 | // |status| is HTTP status code. |
| 38 | // |output| is result returned in HTTP response. Contains result of |
| 39 | // successfully request of information about error. |
| 40 | using RequestCallback = |
| 41 | base::Callback<void(int status, const base::DictionaryValue& output)>; |
| 42 | |
| 43 | PrivetHandler(CloudDelegate* cloud, |
| 44 | DeviceDelegate* device, |
| 45 | SecurityDelegate* pairing, |
| 46 | WifiDelegate* wifi, |
| 47 | IdentityDelegate* identity); |
| 48 | ~PrivetHandler() override; |
| 49 | |
| 50 | void OnCommandDefsChanged() override; |
| 51 | void OnStateChanged() override; |
| 52 | |
| 53 | // Handles HTTP/HTTPS Privet request. |
| 54 | // |api| is the path from the HTTP request, e.g /privet/info. |
| 55 | // |auth_header| is the Authentication header from HTTP request. |
| 56 | // |input| is the the POST data from HTTP request. If nullptr, data format is |
| 57 | // not valid JSON. |
| 58 | // |callback| will be called exactly once during or after |HandleRequest| |
| 59 | // call. |
| 60 | void HandleRequest(const std::string& api, |
| 61 | const std::string& auth_header, |
| 62 | const base::DictionaryValue* input, |
| 63 | const RequestCallback& callback); |
| 64 | |
| 65 | private: |
| 66 | using ApiHandler = void (PrivetHandler::*)(const base::DictionaryValue&, |
| 67 | const UserInfo&, |
| 68 | const RequestCallback&); |
| 69 | |
| 70 | void AddHandler(const std::string& path, ApiHandler handler, AuthScope scope); |
| 71 | |
| 72 | void HandleInfo(const base::DictionaryValue&, |
| 73 | const UserInfo& user_info, |
| 74 | const RequestCallback& callback); |
| 75 | void HandlePairingStart(const base::DictionaryValue& input, |
| 76 | const UserInfo& user_info, |
| 77 | const RequestCallback& callback); |
| 78 | void HandlePairingConfirm(const base::DictionaryValue& input, |
| 79 | const UserInfo& user_info, |
| 80 | const RequestCallback& callback); |
| 81 | void HandlePairingCancel(const base::DictionaryValue& input, |
| 82 | const UserInfo& user_info, |
| 83 | const RequestCallback& callback); |
| 84 | void HandleAuth(const base::DictionaryValue& input, |
| 85 | const UserInfo& user_info, |
| 86 | const RequestCallback& callback); |
| 87 | void HandleSetupStart(const base::DictionaryValue& input, |
| 88 | const UserInfo& user_info, |
| 89 | const RequestCallback& callback); |
| 90 | void HandleSetupStatus(const base::DictionaryValue&, |
| 91 | const UserInfo& user_info, |
| 92 | const RequestCallback& callback); |
| 93 | void HandleState(const base::DictionaryValue& input, |
| 94 | const UserInfo& user_info, |
| 95 | const RequestCallback& callback); |
| 96 | void HandleCommandDefs(const base::DictionaryValue& input, |
| 97 | const UserInfo& user_info, |
| 98 | const RequestCallback& callback); |
| 99 | void HandleCommandsExecute(const base::DictionaryValue& input, |
| 100 | const UserInfo& user_info, |
| 101 | const RequestCallback& callback); |
| 102 | void HandleCommandsStatus(const base::DictionaryValue& input, |
| 103 | const UserInfo& user_info, |
| 104 | const RequestCallback& callback); |
| 105 | void HandleCommandsList(const base::DictionaryValue& input, |
| 106 | const UserInfo& user_info, |
| 107 | const RequestCallback& callback); |
| 108 | void HandleCommandsCancel(const base::DictionaryValue& input, |
| 109 | const UserInfo& user_info, |
| 110 | const RequestCallback& callback); |
| 111 | |
| 112 | void OnUpdateDeviceInfoDone(const std::string& ssid, |
| 113 | const std::string& passphrase, |
| 114 | const std::string& ticket, |
| 115 | const std::string& user, |
| 116 | const RequestCallback& callback) const; |
| 117 | void ReplyWithSetupStatus(const RequestCallback& callback) const; |
| 118 | |
| 119 | CloudDelegate* cloud_ = nullptr; |
| 120 | DeviceDelegate* device_ = nullptr; |
| 121 | SecurityDelegate* security_ = nullptr; |
| 122 | WifiDelegate* wifi_ = nullptr; |
| 123 | IdentityDelegate* identity_ = nullptr; |
| 124 | |
| 125 | std::map<std::string, std::pair<AuthScope, ApiHandler>> handlers_; |
| 126 | |
| 127 | uint64_t last_user_id_{0}; |
| 128 | int state_fingerprint_{0}; |
| 129 | int command_defs_fingerprint_{0}; |
| 130 | ScopedObserver<CloudDelegate, CloudDelegate::Observer> cloud_observer_{this}; |
| 131 | |
| 132 | base::WeakPtrFactory<PrivetHandler> weak_ptr_factory_{this}; |
| 133 | |
| 134 | DISALLOW_COPY_AND_ASSIGN(PrivetHandler); |
| 135 | }; |
| 136 | |
| 137 | } // namespace privetd |
| 138 | |
| 139 | #endif // BUFFET_PRIVET_PRIVET_HANDLER_H_ |