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 | 0fef815 | 2015-09-25 08:45:22 -0700 | [diff] [blame^] | 26 | #include <weave/network_provider.h> |
| 27 | #include <weave/wifi_provider.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 | 0fef815 | 2015-09-25 08:45:22 -0700 | [diff] [blame^] | 34 | class NetworkClient : public weave::NetworkProvider, |
| 35 | public weave::WifiProvider { |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 36 | public: |
| 37 | explicit NetworkClient(const std::set<std::string>& device_whitelist) |
| 38 | : device_whitelist_{device_whitelist} { |
| 39 | } |
| 40 | |
| 41 | ~NetworkClient() override = default; |
| 42 | |
Alex Vakulenko | 0fef815 | 2015-09-25 08:45:22 -0700 | [diff] [blame^] | 43 | // NetworkProvider implementation. |
| 44 | void AddConnectionChangedCallback( |
| 45 | const ConnectionChangedCallback& listener) override {} |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 46 | |
| 47 | weave::NetworkState GetConnectionState() const override { |
| 48 | return weave::NetworkState::kOffline; |
| 49 | } |
| 50 | |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 51 | void OpenSslSocket( |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 52 | const std::string& host, |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 53 | uint16_t port, |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 54 | const base::Callback<void(std::unique_ptr<weave::Stream>)>& on_success, |
Alex Vakulenko | 0fef815 | 2015-09-25 08:45:22 -0700 | [diff] [blame^] | 55 | const weave::ErrorCallback& on_error) override { |
Alex Vakulenko | 35d119a | 2015-09-10 16:16:27 -0700 | [diff] [blame] | 56 | auto socket = SocketStream::ConnectBlocking(host, port); |
| 57 | if (socket) { |
| 58 | SocketStream::TlsConnect(std::move(socket), host, on_success, on_error); |
| 59 | return; |
| 60 | } |
| 61 | chromeos::ErrorPtr error; |
| 62 | chromeos::errors::system::AddSystemError(&error, FROM_HERE, errno); |
| 63 | weave::ErrorPtr weave_error; |
| 64 | ConvertError(*error.get(), &weave_error); |
| 65 | on_error.Run(weave_error.get()); |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Alex Vakulenko | 0fef815 | 2015-09-25 08:45:22 -0700 | [diff] [blame^] | 68 | // WifiProvider implementation. |
| 69 | void Connect(const std::string& ssid, |
| 70 | const std::string& passphrase, |
| 71 | const weave::SuccessCallback& on_success, |
| 72 | const weave::ErrorCallback& on_error) override {} |
| 73 | |
| 74 | void StartAccessPoint(const std::string& ssid) override {} |
| 75 | |
| 76 | void StopAccessPoint() override {} |
| 77 | |
Alex Vakulenko | f0f5534 | 2015-08-18 15:51:40 -0700 | [diff] [blame] | 78 | static std::unique_ptr<NetworkClient> CreateInstance( |
| 79 | const std::set<std::string>& device_whitelist); |
| 80 | |
| 81 | protected: |
| 82 | std::set<std::string> device_whitelist_; |
| 83 | |
| 84 | private: |
| 85 | DISALLOW_COPY_AND_ASSIGN(NetworkClient); |
| 86 | }; |
| 87 | |
| 88 | } // namespace buffet |
| 89 | |
| 90 | #endif // BUFFET_NETWORK_CLIENT_H_ |
| 91 | |