Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef BUFFET_NETWORK_CLIENT_H_ |
| 18 | #define BUFFET_NETWORK_CLIENT_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <set> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <base/cancelable_callback.h> |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 25 | #include <chromeos/errors/error_codes.h> |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 26 | #include <weave/network.h> |
Alex Vakulenko | 059c30d | 2015-09-22 14:09:16 -0700 | [diff] [blame^] | 27 | #include <weave/wifi.h> |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 28 | |
| 29 | #include "buffet/socket_stream.h" |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 30 | #include "buffet/weave_error_conversion.h" |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 31 | |
| 32 | namespace buffet { |
| 33 | |
Alex Vakulenko | 059c30d | 2015-09-22 14:09:16 -0700 | [diff] [blame^] | 34 | class NetworkClient : public weave::Network, public weave::Wifi { |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 35 | public: |
| 36 | explicit NetworkClient(const std::set<std::string>& device_whitelist) |
| 37 | : device_whitelist_{device_whitelist} { |
| 38 | } |
| 39 | |
| 40 | ~NetworkClient() override = default; |
| 41 | |
| 42 | // Implements the Network interface. |
| 43 | void AddOnConnectionChangedCallback( |
| 44 | const OnConnectionChangedCallback& listener) override { |
| 45 | } |
| 46 | |
Alex Vakulenko | 059c30d | 2015-09-22 14:09:16 -0700 | [diff] [blame^] | 47 | void ConnectToService( |
| 48 | const std::string& ssid, |
| 49 | const std::string& passphrase, |
| 50 | const base::Closure& on_success, |
| 51 | const base::Callback<void(const weave::Error*)>& on_error) override { |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | weave::NetworkState GetConnectionState() const override { |
| 55 | return weave::NetworkState::kOffline; |
| 56 | } |
| 57 | |
| 58 | void EnableAccessPoint(const std::string& ssid) override { |
| 59 | } |
| 60 | |
| 61 | void DisableAccessPoint() override { |
| 62 | } |
| 63 | |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 64 | void OpenSslSocket( |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 65 | const std::string& host, |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 66 | uint16_t port, |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 67 | const base::Callback<void(std::unique_ptr<weave::Stream>)>& on_success, |
| 68 | const base::Callback<void(const weave::Error*)>& on_error) override { |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 69 | auto socket = SocketStream::ConnectBlocking(host, port); |
| 70 | if (socket) { |
| 71 | SocketStream::TlsConnect(std::move(socket), host, on_success, on_error); |
| 72 | return; |
| 73 | } |
| 74 | chromeos::ErrorPtr error; |
| 75 | chromeos::errors::system::AddSystemError(&error, FROM_HERE, errno); |
| 76 | weave::ErrorPtr weave_error; |
| 77 | ConvertError(*error.get(), &weave_error); |
| 78 | on_error.Run(weave_error.get()); |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static std::unique_ptr<NetworkClient> CreateInstance( |
| 82 | const std::set<std::string>& device_whitelist); |
| 83 | |
| 84 | protected: |
| 85 | std::set<std::string> device_whitelist_; |
| 86 | |
| 87 | private: |
| 88 | DISALLOW_COPY_AND_ASSIGN(NetworkClient); |
| 89 | }; |
| 90 | |
| 91 | } // namespace buffet |
| 92 | |
| 93 | #endif // BUFFET_NETWORK_CLIENT_H_ |
| 94 | |