diff options
-rw-r--r-- | Android.mk | 71 | ||||
-rw-r--r-- | buffet/avahi_mdns_client.cc | 44 | ||||
-rw-r--r-- | buffet/avahi_mdns_client.h | 47 | ||||
-rw-r--r-- | buffet/brillo_network_client.cc | 126 | ||||
-rw-r--r-- | buffet/brillo_network_client.h | 73 | ||||
-rw-r--r-- | buffet/main.cc | 2 | ||||
-rw-r--r-- | buffet/manager.cc | 24 | ||||
-rw-r--r-- | buffet/manager.h | 10 | ||||
-rw-r--r-- | buffet/mdns_client.h | 53 | ||||
-rw-r--r-- | buffet/network_client.h | 87 | ||||
-rw-r--r-- | buffet/stub_mdns_client.cc | 25 | ||||
-rw-r--r-- | buffet/stub_network_client.cc | 26 | ||||
-rw-r--r-- | dbus-proxies/peerd/dbus-proxies.h | 854 |
13 files changed, 557 insertions, 885 deletions
@@ -22,9 +22,9 @@ ifeq ($(HOST_OS),linux) # ======================================================== buffetCommonCppExtension := .cc -buffetCommonCFlags := -D__BRILLO__ -Wall -Werror \ +buffetCommonCFlags := -DBUFFET_USE_WIFI_BOOTSTRAPPING -Wall -Werror \ -Wno-char-subscripts -Wno-missing-field-initializers \ - -Wno-unused-function -Wno-unused-parameter + -Wno-unused-function -Wno-unused-parameter \ buffetCommonCppFlags := \ -Wno-deprecated-register \ @@ -47,6 +47,11 @@ buffetSharedLibraries := \ libdbus \ libweave \ +ifdef BRILLO +buffetCommonCFlags += -D__BRILLO__ +buffetSharedLibraries += libconnectivity +endif + # buffet-common # ======================================================== include $(CLEAR_VARS) @@ -71,12 +76,19 @@ LOCAL_SRC_FILES := \ buffet/manager.cc \ buffet/socket_stream.cc \ -# buffet/dbus_bindings/org.chromium.Buffet.Command.xml \ -# buffet/dbus_bindings/org.chromium.Buffet.Manager.xml \ -# buffet/ap_manager_client.cc \ -# buffet/peerd_client.cc \ -# buffet/shill_client.cc \ -# buffet/webserv_client.cc \ +ifdef BRILLO + +LOCAL_SRC_FILES += \ + buffet/avahi_mdns_client.cc \ + buffet/brillo_network_client.cc \ + +else # BRILLO + +LOCAL_SRC_FILES += \ + buffet/stub_mdns_client.cc \ + buffet/stub_network_client.cc \ + +endif # BRILLO include $(BUILD_STATIC_LIBRARY) @@ -84,7 +96,16 @@ include $(BUILD_STATIC_LIBRARY) # ======================================================== include $(CLEAR_VARS) LOCAL_MODULE := weaved -LOCAL_REQUIRED_MODULES := init.weaved.rc +LOCAL_REQUIRED_MODULES := \ + base_state.defaults.json \ + base_state.schema.json \ + gcd.json \ + org.chromium.Buffet.conf \ + +ifdef INITRC_TEMPLATE +LOCAL_REQUIRED_MODULES += init.weaved.rc +endif + LOCAL_CPP_EXTENSION := $(buffetCommonCppExtension) LOCAL_CFLAGS := $(buffetCommonCFlags) LOCAL_CPPFLAGS := $(buffetCommonCppFlags) @@ -144,4 +165,36 @@ LOCAL_SRC_FILES := \ include $(BUILD_NATIVE_TEST) +# Config files for /etc/buffet +# ======================================================== +include $(CLEAR_VARS) +LOCAL_MODULE := base_state.defaults.json +LOCAL_MODULE_CLASS := ETC +LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/buffet +LOCAL_SRC_FILES := buffet/etc/buffet/base_state.defaults.json +include $(BUILD_PREBUILT) + +include $(CLEAR_VARS) +LOCAL_MODULE := base_state.schema.json +LOCAL_MODULE_CLASS := ETC +LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/buffet +LOCAL_SRC_FILES := buffet/etc/buffet/base_state.schema.json +include $(BUILD_PREBUILT) + +include $(CLEAR_VARS) +LOCAL_MODULE := gcd.json +LOCAL_MODULE_CLASS := ETC +LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/buffet +LOCAL_SRC_FILES := buffet/etc/buffet/gcd.json +include $(BUILD_PREBUILT) + +# DBus config files for /etc/dbus-1 +# ======================================================== +include $(CLEAR_VARS) +LOCAL_MODULE := org.chromium.Buffet.conf +LOCAL_MODULE_CLASS := ETC +LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/dbus-1 +LOCAL_SRC_FILES := buffet/etc/dbus-1/org.chromium.Buffet.conf +include $(BUILD_PREBUILT) + endif # HOST_OS == linux diff --git a/buffet/avahi_mdns_client.cc b/buffet/avahi_mdns_client.cc new file mode 100644 index 0000000..fde28d5 --- /dev/null +++ b/buffet/avahi_mdns_client.cc @@ -0,0 +1,44 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "buffet/avahi_mdns_client.h" + +#include <base/guid.h> + +namespace buffet { + +AvahiMdnsClient::AvahiMdnsClient() { +} + +AvahiMdnsClient::~AvahiMdnsClient() { +} + +void AvahiMdnsClient::PublishService( + const std::string& service_name, + uint16_t port, + const std::map<std::string, std::string>& txt) { + // TODO(avakulenko) +} + +void AvahiMdnsClient::StopPublishing(const std::string& service_name) { + // TODO(avakulenko) +} + +std::unique_ptr<MdnsClient> MdnsClient::CreateInstance() { + return std::unique_ptr<MdnsClient>{new AvahiMdnsClient}; +} + +} // namespace buffet diff --git a/buffet/avahi_mdns_client.h b/buffet/avahi_mdns_client.h new file mode 100644 index 0000000..a8fd86e --- /dev/null +++ b/buffet/avahi_mdns_client.h @@ -0,0 +1,47 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUFFET_AVAHI_MDNS_CLIENT_H_ +#define BUFFET_AVAHI_MDNS_CLIENT_H_ + +#include <map> +#include <string> + +#include <weave/mdns.h> + +#include "buffet/mdns_client.h" + +namespace buffet { + +// Publishes privet service on mDns using Avahi. +class AvahiMdnsClient : public MdnsClient { + public: + AvahiMdnsClient(); + ~AvahiMdnsClient() override; + + // weave::Mdns implementation. + void PublishService(const std::string& service_name, + uint16_t port, + const std::map<std::string, std::string>& txt) override; + void StopPublishing(const std::string& service_name) override; + + private: + DISALLOW_COPY_AND_ASSIGN(AvahiMdnsClient); +}; + +} // namespace buffet + +#endif // BUFFET_AVAHI_MDNS_CLIENT_H_ diff --git a/buffet/brillo_network_client.cc b/buffet/brillo_network_client.cc new file mode 100644 index 0000000..c064d2a --- /dev/null +++ b/buffet/brillo_network_client.cc @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "buffet/brillo_network_client.h" + +#include <base/message_loop/message_loop.h> + +namespace buffet { + +namespace { +const char kErrorCommandFailed[] = "commandFailed"; +const int kConnectionTimeoutSeconds = 30; +const int kConnectionActivePollSeconds = 3; +const int kConnectionInactivePollSeconds = 10; +} // namespace + +BrilloNetworkClient::BrilloNetworkClient( + const std::set<std::string>& device_whitelist) + : NetworkClient{device_whitelist}, state_(weave::NetworkState::kOffline) { +} + +BrilloNetworkClient::~BrilloNetworkClient() { +} + +void BrilloNetworkClient::AddOnConnectionChangedCallback( + const OnConnectionChangedCallback& listener) { + connection_listeners_.push_back(listener); +} + +bool BrilloNetworkClient::ConnectToService(const std::string& ssid, + const std::string& passphrase, + const base::Closure& on_success, + weave::ErrorPtr* error) { + if (!connectivity_client_.ConnectToAccessPoint(ssid, passphrase)) { + weave::Error::AddTo(error, FROM_HERE, kErrorCommandFailed, "", ""); + return false; + } + + connection_success_closure_ = on_success; + state_ = weave::NetworkState::kConnecting; + + connection_timeout_closure_.Reset( + base::Bind(&BrilloNetworkClient::OnConnectionTimeout, + base::Unretained(this))); + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + connection_timeout_closure_.callback(), + base::TimeDelta::FromSeconds(kConnectionTimeoutSeconds)); + + ScheduleNextStatePoll(); + + return true; +} + +weave::NetworkState BrilloNetworkClient::GetConnectionState() const { + return state_; +} + +void BrilloNetworkClient::EnableAccessPoint(const std::string& ssid) { + connectivity_client_.EnableAccessPoint(ssid); + state_ = weave::NetworkState::kOffline; +} + +void BrilloNetworkClient::DisableAccessPoint() { + connectivity_client_.DisableAccessPoint(); +} + +void BrilloNetworkClient::OnConnectionTimeout() { + state_ = weave::NetworkState::kFailure; +} + +void BrilloNetworkClient::ScheduleNextStatePoll() { + periodic_connection_state_closure_.Reset( + base::Bind(&BrilloNetworkClient::UpdateConnectionState, + base::Unretained(this))); + int poll_period_seconds; + if (state_ == weave::NetworkState::kConnecting) { + poll_period_seconds = kConnectionActivePollSeconds; + } else { + poll_period_seconds = kConnectionInactivePollSeconds; + } + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + periodic_connection_state_closure_.callback(), + base::TimeDelta::FromSeconds(poll_period_seconds)); +} + +void BrilloNetworkClient::UpdateConnectionState() { + bool was_connected = state_ == weave::NetworkState::kConnected; + bool is_connected = connectivity_client_.IsConnected(); + + if (is_connected) { + if (state_ == weave::NetworkState::kConnecting) + connection_success_closure_.Run(); + state_ = weave::NetworkState::kConnected; + } else if (state_ == weave::NetworkState::kConnected) { + state_ = weave::NetworkState::kOffline; + } + if (is_connected != was_connected) { + for (const auto& listener : connection_listeners_) { + listener.Run(is_connected); + } + } + ScheduleNextStatePoll(); +} + +std::unique_ptr<NetworkClient> NetworkClient::CreateInstance( + const std::set<std::string>& device_whitelist) { + return std::unique_ptr<NetworkClient>{ + new BrilloNetworkClient{device_whitelist}}; +} + +} // namespace buffet diff --git a/buffet/brillo_network_client.h b/buffet/brillo_network_client.h new file mode 100644 index 0000000..6a00ac8 --- /dev/null +++ b/buffet/brillo_network_client.h @@ -0,0 +1,73 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUFFET_BRILLO_NETWORK_CLIENT_H_ +#define BUFFET_BRILLO_NETWORK_CLIENT_H_ + +#include <memory> +#include <vector> + +#include <base/cancelable_callback.h> +#include <weave/network.h> + +#include "connectivity_client.h" + +#include "buffet/network_client.h" + +namespace buffet { + +class BrilloNetworkClient : public NetworkClient { + public: + explicit BrilloNetworkClient(const std::set<std::string>& device_whitelist); + ~BrilloNetworkClient() override; + + // Implements the Network interface. + void AddOnConnectionChangedCallback( + const OnConnectionChangedCallback& listener) override; + bool ConnectToService(const std::string& ssid, + const std::string& passphrase, + const base::Closure& on_success, + weave::ErrorPtr* error) override; + weave::NetworkState GetConnectionState() const override; + void EnableAccessPoint(const std::string& ssid) override; + void DisableAccessPoint() override; + + private: + enum class ConnectionState { + kIdle, + kInProgress, + kConnected, + kTimedOut + }; + void OnConnectionTimeout(); + void ScheduleNextStatePoll(); + void UpdateConnectionState(); + + ConnectivityClient connectivity_client_; + std::vector<OnConnectionChangedCallback> connection_listeners_; + base::CancelableClosure connection_timeout_closure_; + base::CancelableClosure periodic_connection_state_closure_; + base::Closure connection_success_closure_; + weave::NetworkState state_; + bool is_connected_; + + DISALLOW_COPY_AND_ASSIGN(BrilloNetworkClient); +}; + +} // namespace buffet + +#endif // BUFFET_BRILLO_NETWORK_CLIENT_H_ + diff --git a/buffet/main.cc b/buffet/main.cc index 350a905..7e1c2e4 100644 --- a/buffet/main.cc +++ b/buffet/main.cc @@ -67,7 +67,7 @@ int main(int argc, char* argv[]) { "Path to file containing state information."); DEFINE_bool(enable_xmpp, true, "Connect to GCD via a persistent XMPP connection."); - DEFINE_bool(disable_privet, false, "disable Privet protocol"); + DEFINE_bool(disable_privet, true, "disable Privet protocol"); DEFINE_bool(enable_ping, false, "enable test HTTP handler at /privet/ping"); DEFINE_string(device_whitelist, "", "Comma separated list of network interfaces to monitor for " diff --git a/buffet/manager.cc b/buffet/manager.cc index bad0d8c..0180612 100644 --- a/buffet/manager.cc +++ b/buffet/manager.cc @@ -29,13 +29,10 @@ #include "buffet/dbus_command_dispatcher.h" #include "buffet/dbus_conversion.h" #include "buffet/http_transport_client.h" -//#include "buffet/shill_client.h" +#include "buffet/mdns_client.h" +#include "buffet/network_client.h" #include "buffet/weave_error_conversion.h" - -#ifdef BUFFET_USE_WIFI_BOOTSTRAPPING -//#include "buffet/peerd_client.h" //#include "buffet/webserv_client.h" -#endif // BUFFET_USE_WIFI_BOOTSTRAPPING using chromeos::dbus_utils::AsyncEventSequencer; using chromeos::dbus_utils::ExportedObjectManager; @@ -77,16 +74,12 @@ void Manager::Start(const weave::Device::Options& options, AsyncEventSequencer* sequencer) { task_runner_.reset(new TaskRunner{}); http_client_.reset(new HttpTransportClient); -// shill_client_.reset(new ShillClient{dbus_object_.GetBus(), device_whitelist}); - weave::Mdns* mdns{nullptr}; - weave::HttpServer* http_server{nullptr}; + network_client_ = NetworkClient::CreateInstance(device_whitelist); #ifdef BUFFET_USE_WIFI_BOOTSTRAPPING -// if (!options.disable_privet) { -// peerd_client_.reset(new PeerdClient{dbus_object_.GetBus()}); -// web_serv_client_.reset(new WebServClient{dbus_object_.GetBus(), sequencer}); -// mdns = peerd_client_.get(); -// http_server = web_serv_client_.get(); -// } + if (!options.disable_privet) { + mdns_client_ = MdnsClient::CreateInstance(); + //web_serv_client_.reset(new WebServClient{dbus_object_.GetBus(), sequencer}); + } #endif // BUFFET_USE_WIFI_BOOTSTRAPPING device_ = weave::Device::Create(); @@ -95,7 +88,8 @@ void Manager::Start(const weave::Device::Options& options, base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); device_->Start(options, config_.get(), task_runner_.get(), http_client_.get(), - nullptr /*shill_client_.get()*/, mdns, http_server); + network_client_.get(), mdns_client_.get(), + nullptr /*web_serv_client_.get()*/); command_dispatcher_.reset(new DBusCommandDispacher{ dbus_object_.GetObjectManager(), device_->GetCommands()}); diff --git a/buffet/manager.h b/buffet/manager.h index 43ba202..a72b1af 100644 --- a/buffet/manager.h +++ b/buffet/manager.h @@ -33,8 +33,8 @@ namespace buffet { class BuffetConfig; class DBusCommandDispacher; class HttpTransportClient; -//class PeerdClient; -//class ShillClient; +class MdnsClient; +class NetworkClient; //class WebServClient; struct BuffetConfigPaths; @@ -125,11 +125,9 @@ class Manager final : public org::chromium::Buffet::ManagerInterface { class TaskRunner; std::unique_ptr<TaskRunner> task_runner_; std::unique_ptr<HttpTransportClient> http_client_; -// std::unique_ptr<ShillClient> shill_client_; -#ifdef BUFFET_USE_WIFI_BOOTSTRAPPING -// std::unique_ptr<PeerdClient> peerd_client_; + std::unique_ptr<NetworkClient> network_client_; + std::unique_ptr<MdnsClient> mdns_client_; // std::unique_ptr<WebServClient> web_serv_client_; -#endif // BUFFET_USE_WIFI_BOOTSTRAPPING std::unique_ptr<BuffetConfig> config_; std::unique_ptr<weave::Device> device_; std::unique_ptr<DBusCommandDispacher> command_dispatcher_; diff --git a/buffet/mdns_client.h b/buffet/mdns_client.h new file mode 100644 index 0000000..f486d05 --- /dev/null +++ b/buffet/mdns_client.h @@ -0,0 +1,53 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUFFET_MDNS_CLIENT_H_ +#define BUFFET_MDNS_CLIENT_H_ + +#include <map> +#include <memory> +#include <string> + +#include <base/guid.h> +#include <weave/mdns.h> + +namespace buffet { + +// Stub MDNS implementation that does nothing on platform without MDNS support. +class MdnsClient : public weave::Mdns { + public: + MdnsClient() : device_id_{base::GenerateGUID()} {} + ~MdnsClient() override = default; + + // weave::Mdns implementation. + void PublishService(const std::string& service_name, + uint16_t port, + const std::map<std::string, std::string>& txt) override {} + void StopPublishing(const std::string& service_name) override {} + std::string GetId() const override { return device_id_; } + + static std::unique_ptr<MdnsClient> CreateInstance(); + + protected: + // Cached value of the device ID that we got from peerd. + std::string device_id_; + + DISALLOW_COPY_AND_ASSIGN(MdnsClient); +}; + +} // namespace buffet + +#endif // BUFFET_MDNS_CLIENT_H_ diff --git a/buffet/network_client.h b/buffet/network_client.h new file mode 100644 index 0000000..a97f05f --- /dev/null +++ b/buffet/network_client.h @@ -0,0 +1,87 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUFFET_NETWORK_CLIENT_H_ +#define BUFFET_NETWORK_CLIENT_H_ + +#include <memory> +#include <set> +#include <string> + +#include <base/cancelable_callback.h> +#include <weave/network.h> + +#include "buffet/socket_stream.h" + +namespace buffet { + +class NetworkClient : public weave::Network { + public: + explicit NetworkClient(const std::set<std::string>& device_whitelist) + : device_whitelist_{device_whitelist} { + } + + ~NetworkClient() override = default; + + // Implements the Network interface. + void AddOnConnectionChangedCallback( + const OnConnectionChangedCallback& listener) override { + } + + bool ConnectToService(const std::string& ssid, + const std::string& passphrase, + const base::Closure& on_success, + weave::ErrorPtr* error) override { + return true; + } + + weave::NetworkState GetConnectionState() const override { + return weave::NetworkState::kOffline; + } + + void EnableAccessPoint(const std::string& ssid) override { + } + + void DisableAccessPoint() override { + } + + std::unique_ptr<weave::Stream> OpenSocketBlocking(const std::string& host, + uint16_t port) override { + return SocketStream::ConnectBlocking(host, port); + } + + void CreateTlsStream( + std::unique_ptr<weave::Stream> socket, + const std::string& host, + const base::Callback<void(std::unique_ptr<weave::Stream>)>& on_success, + const base::Callback<void(const weave::Error*)>& on_error) override { + SocketStream::TlsConnect(std::move(socket), host, on_success, on_error); + } + + static std::unique_ptr<NetworkClient> CreateInstance( + const std::set<std::string>& device_whitelist); + + protected: + std::set<std::string> device_whitelist_; + + private: + DISALLOW_COPY_AND_ASSIGN(NetworkClient); +}; + +} // namespace buffet + +#endif // BUFFET_NETWORK_CLIENT_H_ + diff --git a/buffet/stub_mdns_client.cc b/buffet/stub_mdns_client.cc new file mode 100644 index 0000000..e535014 --- /dev/null +++ b/buffet/stub_mdns_client.cc @@ -0,0 +1,25 @@ +/* + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "buffet/mdns_client.h" + +namespace buffet { + +std::unique_ptr<MdnsClient> MdnsClient::CreateInstance() { + return std::unique_ptr<MdnsClient>{new MdnsClient}; +} + +} // namespace buffet diff --git a/buffet/stub_network_client.cc b/buffet/stub_network_client.cc new file mode 100644 index 0000000..c0671af --- /dev/null +++ b/buffet/stub_network_client.cc @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "buffet/network_client.h" + +namespace buffet { + +std::unique_ptr<NetworkClient> NetworkClient::CreateInstance( + const std::set<std::string>& device_whitelist) { + return std::unique_ptr<NetworkClient>{new NetworkClient{device_whitelist}}; +} + +} // namespace buffet diff --git a/dbus-proxies/peerd/dbus-proxies.h b/dbus-proxies/peerd/dbus-proxies.h deleted file mode 100644 index 3cf302d..0000000 --- a/dbus-proxies/peerd/dbus-proxies.h +++ /dev/null @@ -1,854 +0,0 @@ -// Automatic generation of D-Bus interfaces: -// - org.chromium.peerd.Manager -// - org.chromium.peerd.Peer -// - org.chromium.peerd.Service -#ifndef ____CHROMEOS_DBUS_BINDING___BUILD_LINK_VAR_CACHE_PORTAGE_CHROMEOS_BASE_BUFFET_OUT_DEFAULT_GEN_INCLUDE_PEERD_DBUS_PROXIES_H -#define ____CHROMEOS_DBUS_BINDING___BUILD_LINK_VAR_CACHE_PORTAGE_CHROMEOS_BASE_BUFFET_OUT_DEFAULT_GEN_INCLUDE_PEERD_DBUS_PROXIES_H -#include <memory> -#include <string> -#include <vector> - -#include <base/bind.h> -#include <base/callback.h> -#include <base/logging.h> -#include <base/macros.h> -#include <base/memory/ref_counted.h> -#include <chromeos/any.h> -#include <chromeos/dbus/dbus_method_invoker.h> -#include <chromeos/dbus/dbus_property.h> -#include <chromeos/dbus/dbus_signal_handler.h> -#include <chromeos/errors/error.h> -#include <chromeos/variant_dictionary.h> -#include <dbus/bus.h> -#include <dbus/message.h> -#include <dbus/object_manager.h> -#include <dbus/object_path.h> -#include <dbus/object_proxy.h> - -namespace org { -namespace chromium { -namespace peerd { -class ObjectManagerProxy; -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Abstract interface proxy for org::chromium::peerd::Manager. -// The Manager is responsible for global state of peerd. It exposes -// functionality affecting the entire device such as monitoring and -// local service advertisements. -class ManagerProxyInterface { - public: - virtual ~ManagerProxyInterface() = default; - - virtual bool StartMonitoring( - const std::vector<std::string>& in_requested_technologies, - const chromeos::VariantDictionary& in_options, - std::string* out_monitoring_token, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual void StartMonitoringAsync( - const std::vector<std::string>& in_requested_technologies, - const chromeos::VariantDictionary& in_options, - const base::Callback<void(const std::string& /*monitoring_token*/)>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual bool StopMonitoring( - const std::string& in_monitoring_token, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual void StopMonitoringAsync( - const std::string& in_monitoring_token, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual bool ExposeService( - const std::string& in_service_id, - const std::map<std::string, std::string>& in_service_info, - const chromeos::VariantDictionary& in_options, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual void ExposeServiceAsync( - const std::string& in_service_id, - const std::map<std::string, std::string>& in_service_info, - const chromeos::VariantDictionary& in_options, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual bool RemoveExposedService( - const std::string& in_service_id, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual void RemoveExposedServiceAsync( - const std::string& in_service_id, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual bool Ping( - std::string* out_message, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - virtual void PingAsync( - const base::Callback<void(const std::string& /*message*/)>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0; - - static const char* MonitoredTechnologiesName() { return "MonitoredTechnologies"; } - virtual const std::vector<std::string>& monitored_technologies() const = 0; -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Interface proxy for org::chromium::peerd::Manager. -// The Manager is responsible for global state of peerd. It exposes -// functionality affecting the entire device such as monitoring and -// local service advertisements. -class ManagerProxy final : public ManagerProxyInterface { - public: - class PropertySet : public dbus::PropertySet { - public: - PropertySet(dbus::ObjectProxy* object_proxy, - const PropertyChangedCallback& callback) - : dbus::PropertySet{object_proxy, - "org.chromium.peerd.Manager", - callback} { - RegisterProperty(MonitoredTechnologiesName(), &monitored_technologies); - } - - chromeos::dbus_utils::Property<std::vector<std::string>> monitored_technologies; - - private: - DISALLOW_COPY_AND_ASSIGN(PropertySet); - }; - - ManagerProxy( - const scoped_refptr<dbus::Bus>& bus, - PropertySet* property_set) : - bus_{bus}, - property_set_{property_set}, - dbus_object_proxy_{ - bus_->GetObjectProxy(service_name_, object_path_)} { - } - - ~ManagerProxy() override { - } - - void ReleaseObjectProxy(const base::Closure& callback) { - bus_->RemoveObjectProxy(service_name_, object_path_, callback); - } - - const dbus::ObjectPath& GetObjectPath() const { - return object_path_; - } - - dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } - - void SetPropertyChangedCallback( - const base::Callback<void(ManagerProxy*, const std::string&)>& callback) { - on_property_changed_ = callback; - } - - const PropertySet* GetProperties() const { return property_set_; } - PropertySet* GetProperties() { return property_set_; } - - bool StartMonitoring( - const std::vector<std::string>& in_requested_technologies, - const chromeos::VariantDictionary& in_options, - std::string* out_monitoring_token, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "StartMonitoring", - error, - in_requested_technologies, - in_options); - return response && chromeos::dbus_utils::ExtractMethodCallResults( - response.get(), error, out_monitoring_token); - } - - void StartMonitoringAsync( - const std::vector<std::string>& in_requested_technologies, - const chromeos::VariantDictionary& in_options, - const base::Callback<void(const std::string& /*monitoring_token*/)>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - chromeos::dbus_utils::CallMethodWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "StartMonitoring", - success_callback, - error_callback, - in_requested_technologies, - in_options); - } - - bool StopMonitoring( - const std::string& in_monitoring_token, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "StopMonitoring", - error, - in_monitoring_token); - return response && chromeos::dbus_utils::ExtractMethodCallResults( - response.get(), error); - } - - void StopMonitoringAsync( - const std::string& in_monitoring_token, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - chromeos::dbus_utils::CallMethodWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "StopMonitoring", - success_callback, - error_callback, - in_monitoring_token); - } - - bool ExposeService( - const std::string& in_service_id, - const std::map<std::string, std::string>& in_service_info, - const chromeos::VariantDictionary& in_options, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "ExposeService", - error, - in_service_id, - in_service_info, - in_options); - return response && chromeos::dbus_utils::ExtractMethodCallResults( - response.get(), error); - } - - void ExposeServiceAsync( - const std::string& in_service_id, - const std::map<std::string, std::string>& in_service_info, - const chromeos::VariantDictionary& in_options, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - chromeos::dbus_utils::CallMethodWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "ExposeService", - success_callback, - error_callback, - in_service_id, - in_service_info, - in_options); - } - - bool RemoveExposedService( - const std::string& in_service_id, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "RemoveExposedService", - error, - in_service_id); - return response && chromeos::dbus_utils::ExtractMethodCallResults( - response.get(), error); - } - - void RemoveExposedServiceAsync( - const std::string& in_service_id, - const base::Callback<void()>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - chromeos::dbus_utils::CallMethodWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "RemoveExposedService", - success_callback, - error_callback, - in_service_id); - } - - bool Ping( - std::string* out_message, - chromeos::ErrorPtr* error, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "Ping", - error); - return response && chromeos::dbus_utils::ExtractMethodCallResults( - response.get(), error, out_message); - } - - void PingAsync( - const base::Callback<void(const std::string& /*message*/)>& success_callback, - const base::Callback<void(chromeos::Error*)>& error_callback, - int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override { - chromeos::dbus_utils::CallMethodWithTimeout( - timeout_ms, - dbus_object_proxy_, - "org.chromium.peerd.Manager", - "Ping", - success_callback, - error_callback); - } - - const std::vector<std::string>& monitored_technologies() const override { - return property_set_->monitored_technologies.value(); - } - - private: - void OnPropertyChanged(const std::string& property_name) { - if (!on_property_changed_.is_null()) - on_property_changed_.Run(this, property_name); - } - - scoped_refptr<dbus::Bus> bus_; - const std::string service_name_{"org.chromium.peerd"}; - const dbus::ObjectPath object_path_{"/org/chromium/peerd/Manager"}; - PropertySet* property_set_; - base::Callback<void(ManagerProxy*, const std::string&)> on_property_changed_; - dbus::ObjectProxy* dbus_object_proxy_; - - friend class org::chromium::peerd::ObjectManagerProxy; - DISALLOW_COPY_AND_ASSIGN(ManagerProxy); -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Abstract interface proxy for org::chromium::peerd::Peer. -// Peers represent remote devices. In addition to containing a -// unique identifier or the remote peer, and the time last seen, -// a peer object may have 0 or more services exposed by that peer. -// These services may be found at path_to_owning_peer/services/*. -class PeerProxyInterface { - public: - virtual ~PeerProxyInterface() = default; - - static const char* UUIDName() { return "UUID"; } - virtual const std::string& uuid() const = 0; - static const char* LastSeenName() { return "LastSeen"; } - virtual uint64_t last_seen() const = 0; -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Interface proxy for org::chromium::peerd::Peer. -// Peers represent remote devices. In addition to containing a -// unique identifier or the remote peer, and the time last seen, -// a peer object may have 0 or more services exposed by that peer. -// These services may be found at path_to_owning_peer/services/*. -class PeerProxy final : public PeerProxyInterface { - public: - class PropertySet : public dbus::PropertySet { - public: - PropertySet(dbus::ObjectProxy* object_proxy, - const PropertyChangedCallback& callback) - : dbus::PropertySet{object_proxy, - "org.chromium.peerd.Peer", - callback} { - RegisterProperty(UUIDName(), &uuid); - RegisterProperty(LastSeenName(), &last_seen); - } - - chromeos::dbus_utils::Property<std::string> uuid; - chromeos::dbus_utils::Property<uint64_t> last_seen; - - private: - DISALLOW_COPY_AND_ASSIGN(PropertySet); - }; - - PeerProxy( - const scoped_refptr<dbus::Bus>& bus, - const dbus::ObjectPath& object_path, - PropertySet* property_set) : - bus_{bus}, - object_path_{object_path}, - property_set_{property_set}, - dbus_object_proxy_{ - bus_->GetObjectProxy(service_name_, object_path_)} { - } - - ~PeerProxy() override { - } - - void ReleaseObjectProxy(const base::Closure& callback) { - bus_->RemoveObjectProxy(service_name_, object_path_, callback); - } - - const dbus::ObjectPath& GetObjectPath() const { - return object_path_; - } - - dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } - - void SetPropertyChangedCallback( - const base::Callback<void(PeerProxy*, const std::string&)>& callback) { - on_property_changed_ = callback; - } - - const PropertySet* GetProperties() const { return property_set_; } - PropertySet* GetProperties() { return property_set_; } - - const std::string& uuid() const override { - return property_set_->uuid.value(); - } - - uint64_t last_seen() const override { - return property_set_->last_seen.value(); - } - - private: - void OnPropertyChanged(const std::string& property_name) { - if (!on_property_changed_.is_null()) - on_property_changed_.Run(this, property_name); - } - - scoped_refptr<dbus::Bus> bus_; - const std::string service_name_{"org.chromium.peerd"}; - dbus::ObjectPath object_path_; - PropertySet* property_set_; - base::Callback<void(PeerProxy*, const std::string&)> on_property_changed_; - dbus::ObjectProxy* dbus_object_proxy_; - - friend class org::chromium::peerd::ObjectManagerProxy; - DISALLOW_COPY_AND_ASSIGN(PeerProxy); -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Abstract interface proxy for org::chromium::peerd::Service. -// Service objects represent a service exposed by device. They have -// an associated service ID which will be unique per device, and -// and a map of metadata about a service. Finally, services contain -// a list of IP addresses that a given service from a particular peer -// was discovered over. -class ServiceProxyInterface { - public: - virtual ~ServiceProxyInterface() = default; - - static const char* PeerIdName() { return "PeerId"; } - virtual const std::string& peer_id() const = 0; - static const char* ServiceIdName() { return "ServiceId"; } - virtual const std::string& service_id() const = 0; - static const char* ServiceInfoName() { return "ServiceInfo"; } - virtual const std::map<std::string, std::string>& service_info() const = 0; - static const char* IpInfosName() { return "IpInfos"; } - virtual const std::vector<std::tuple<std::vector<uint8_t>, uint16_t>>& ip_infos() const = 0; -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -// Interface proxy for org::chromium::peerd::Service. -// Service objects represent a service exposed by device. They have -// an associated service ID which will be unique per device, and -// and a map of metadata about a service. Finally, services contain -// a list of IP addresses that a given service from a particular peer -// was discovered over. -class ServiceProxy final : public ServiceProxyInterface { - public: - class PropertySet : public dbus::PropertySet { - public: - PropertySet(dbus::ObjectProxy* object_proxy, - const PropertyChangedCallback& callback) - : dbus::PropertySet{object_proxy, - "org.chromium.peerd.Service", - callback} { - RegisterProperty(PeerIdName(), &peer_id); - RegisterProperty(ServiceIdName(), &service_id); - RegisterProperty(ServiceInfoName(), &service_info); - RegisterProperty(IpInfosName(), &ip_infos); - } - - chromeos::dbus_utils::Property<std::string> peer_id; - chromeos::dbus_utils::Property<std::string> service_id; - chromeos::dbus_utils::Property<std::map<std::string, std::string>> service_info; - chromeos::dbus_utils::Property<std::vector<std::tuple<std::vector<uint8_t>, uint16_t>>> ip_infos; - - private: - DISALLOW_COPY_AND_ASSIGN(PropertySet); - }; - - ServiceProxy( - const scoped_refptr<dbus::Bus>& bus, - const dbus::ObjectPath& object_path, - PropertySet* property_set) : - bus_{bus}, - object_path_{object_path}, - property_set_{property_set}, - dbus_object_proxy_{ - bus_->GetObjectProxy(service_name_, object_path_)} { - } - - ~ServiceProxy() override { - } - - void ReleaseObjectProxy(const base::Closure& callback) { - bus_->RemoveObjectProxy(service_name_, object_path_, callback); - } - - const dbus::ObjectPath& GetObjectPath() const { - return object_path_; - } - - dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } - - void SetPropertyChangedCallback( - const base::Callback<void(ServiceProxy*, const std::string&)>& callback) { - on_property_changed_ = callback; - } - - const PropertySet* GetProperties() const { return property_set_; } - PropertySet* GetProperties() { return property_set_; } - - const std::string& peer_id() const override { - return property_set_->peer_id.value(); - } - - const std::string& service_id() const override { - return property_set_->service_id.value(); - } - - const std::map<std::string, std::string>& service_info() const override { - return property_set_->service_info.value(); - } - - const std::vector<std::tuple<std::vector<uint8_t>, uint16_t>>& ip_infos() const override { - return property_set_->ip_infos.value(); - } - - private: - void OnPropertyChanged(const std::string& property_name) { - if (!on_property_changed_.is_null()) - on_property_changed_.Run(this, property_name); - } - - scoped_refptr<dbus::Bus> bus_; - const std::string service_name_{"org.chromium.peerd"}; - dbus::ObjectPath object_path_; - PropertySet* property_set_; - base::Callback<void(ServiceProxy*, const std::string&)> on_property_changed_; - dbus::ObjectProxy* dbus_object_proxy_; - - friend class org::chromium::peerd::ObjectManagerProxy; - DISALLOW_COPY_AND_ASSIGN(ServiceProxy); -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -namespace org { -namespace chromium { -namespace peerd { - -class ObjectManagerProxy : public dbus::ObjectManager::Interface { - public: - ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus) - : bus_{bus}, - dbus_object_manager_{bus->GetObjectManager( - "org.chromium.peerd", - dbus::ObjectPath{"/org/chromium/peerd"})} { - dbus_object_manager_->RegisterInterface("org.chromium.peerd.Manager", this); - dbus_object_manager_->RegisterInterface("org.chromium.peerd.Peer", this); - dbus_object_manager_->RegisterInterface("org.chromium.peerd.Service", this); - } - - ~ObjectManagerProxy() override { - dbus_object_manager_->UnregisterInterface("org.chromium.peerd.Manager"); - dbus_object_manager_->UnregisterInterface("org.chromium.peerd.Peer"); - dbus_object_manager_->UnregisterInterface("org.chromium.peerd.Service"); - } - - dbus::ObjectManager* GetObjectManagerProxy() const { - return dbus_object_manager_; - } - - org::chromium::peerd::ManagerProxy* GetManagerProxy() { - if (manager_instances_.empty()) - return nullptr; - return manager_instances_.begin()->second.get(); - } - std::vector<org::chromium::peerd::ManagerProxy*> GetManagerInstances() const { - std::vector<org::chromium::peerd::ManagerProxy*> values; - values.reserve(manager_instances_.size()); - for (const auto& pair : manager_instances_) - values.push_back(pair.second.get()); - return values; - } - void SetManagerAddedCallback( - const base::Callback<void(org::chromium::peerd::ManagerProxy*)>& callback) { - on_manager_added_ = callback; - } - void SetManagerRemovedCallback( - const base::Callback<void(const dbus::ObjectPath&)>& callback) { - on_manager_removed_ = callback; - } - - org::chromium::peerd::PeerProxy* GetPeerProxy( - const dbus::ObjectPath& object_path) { - auto p = peer_instances_.find(object_path); - if (p != peer_instances_.end()) - return p->second.get(); - return nullptr; - } - std::vector<org::chromium::peerd::PeerProxy*> GetPeerInstances() const { - std::vector<org::chromium::peerd::PeerProxy*> values; - values.reserve(peer_instances_.size()); - for (const auto& pair : peer_instances_) - values.push_back(pair.second.get()); - return values; - } - void SetPeerAddedCallback( - const base::Callback<void(org::chromium::peerd::PeerProxy*)>& callback) { - on_peer_added_ = callback; - } - void SetPeerRemovedCallback( - const base::Callback<void(const dbus::ObjectPath&)>& callback) { - on_peer_removed_ = callback; - } - - org::chromium::peerd::ServiceProxy* GetServiceProxy( - const dbus::ObjectPath& object_path) { - auto p = service_instances_.find(object_path); - if (p != service_instances_.end()) - return p->second.get(); - return nullptr; - } - std::vector<org::chromium::peerd::ServiceProxy*> GetServiceInstances() const { - std::vector<org::chromium::peerd::ServiceProxy*> values; - values.reserve(service_instances_.size()); - for (const auto& pair : service_instances_) - values.push_back(pair.second.get()); - return values; - } - void SetServiceAddedCallback( - const base::Callback<void(org::chromium::peerd::ServiceProxy*)>& callback) { - on_service_added_ = callback; - } - void SetServiceRemovedCallback( - const base::Callback<void(const dbus::ObjectPath&)>& callback) { - on_service_removed_ = callback; - } - - private: - void OnPropertyChanged(const dbus::ObjectPath& object_path, - const std::string& interface_name, - const std::string& property_name) { - if (interface_name == "org.chromium.peerd.Manager") { - auto p = manager_instances_.find(object_path); - if (p == manager_instances_.end()) - return; - p->second->OnPropertyChanged(property_name); - return; - } - if (interface_name == "org.chromium.peerd.Peer") { - auto p = peer_instances_.find(object_path); - if (p == peer_instances_.end()) - return; - p->second->OnPropertyChanged(property_name); - return; - } - if (interface_name == "org.chromium.peerd.Service") { - auto p = service_instances_.find(object_path); - if (p == service_instances_.end()) - return; - p->second->OnPropertyChanged(property_name); - return; - } - } - - void ObjectAdded( - const dbus::ObjectPath& object_path, - const std::string& interface_name) override { - if (interface_name == "org.chromium.peerd.Manager") { - auto property_set = - static_cast<org::chromium::peerd::ManagerProxy::PropertySet*>( - dbus_object_manager_->GetProperties(object_path, interface_name)); - std::unique_ptr<org::chromium::peerd::ManagerProxy> manager_proxy{ - new org::chromium::peerd::ManagerProxy{bus_, property_set} - }; - auto p = manager_instances_.emplace(object_path, std::move(manager_proxy)); - if (!on_manager_added_.is_null()) - on_manager_added_.Run(p.first->second.get()); - return; - } - if (interface_name == "org.chromium.peerd.Peer") { - auto property_set = - static_cast<org::chromium::peerd::PeerProxy::PropertySet*>( - dbus_object_manager_->GetProperties(object_path, interface_name)); - std::unique_ptr<org::chromium::peerd::PeerProxy> peer_proxy{ - new org::chromium::peerd::PeerProxy{bus_, object_path, property_set} - }; - auto p = peer_instances_.emplace(object_path, std::move(peer_proxy)); - if (!on_peer_added_.is_null()) - on_peer_added_.Run(p.first->second.get()); - return; - } - if (interface_name == "org.chromium.peerd.Service") { - auto property_set = - static_cast<org::chromium::peerd::ServiceProxy::PropertySet*>( - dbus_object_manager_->GetProperties(object_path, interface_name)); - std::unique_ptr<org::chromium::peerd::ServiceProxy> service_proxy{ - new org::chromium::peerd::ServiceProxy{bus_, object_path, property_set} - }; - auto p = service_instances_.emplace(object_path, std::move(service_proxy)); - if (!on_service_added_.is_null()) - on_service_added_.Run(p.first->second.get()); - return; - } - } - - void ObjectRemoved( - const dbus::ObjectPath& object_path, - const std::string& interface_name) override { - if (interface_name == "org.chromium.peerd.Manager") { - auto p = manager_instances_.find(object_path); - if (p != manager_instances_.end()) { - if (!on_manager_removed_.is_null()) - on_manager_removed_.Run(object_path); - manager_instances_.erase(p); - } - return; - } - if (interface_name == "org.chromium.peerd.Peer") { - auto p = peer_instances_.find(object_path); - if (p != peer_instances_.end()) { - if (!on_peer_removed_.is_null()) - on_peer_removed_.Run(object_path); - peer_instances_.erase(p); - } - return; - } - if (interface_name == "org.chromium.peerd.Service") { - auto p = service_instances_.find(object_path); - if (p != service_instances_.end()) { - if (!on_service_removed_.is_null()) - on_service_removed_.Run(object_path); - service_instances_.erase(p); - } - return; - } - } - - dbus::PropertySet* CreateProperties( - dbus::ObjectProxy* object_proxy, - const dbus::ObjectPath& object_path, - const std::string& interface_name) override { - if (interface_name == "org.chromium.peerd.Manager") { - return new org::chromium::peerd::ManagerProxy::PropertySet{ - object_proxy, - base::Bind(&ObjectManagerProxy::OnPropertyChanged, - weak_ptr_factory_.GetWeakPtr(), - object_path, - interface_name) - }; - } - if (interface_name == "org.chromium.peerd.Peer") { - return new org::chromium::peerd::PeerProxy::PropertySet{ - object_proxy, - base::Bind(&ObjectManagerProxy::OnPropertyChanged, - weak_ptr_factory_.GetWeakPtr(), - object_path, - interface_name) - }; - } - if (interface_name == "org.chromium.peerd.Service") { - return new org::chromium::peerd::ServiceProxy::PropertySet{ - object_proxy, - base::Bind(&ObjectManagerProxy::OnPropertyChanged, - weak_ptr_factory_.GetWeakPtr(), - object_path, - interface_name) - }; - } - LOG(FATAL) << "Creating properties for unsupported interface " - << interface_name; - return nullptr; - } - - scoped_refptr<dbus::Bus> bus_; - dbus::ObjectManager* dbus_object_manager_; - std::map<dbus::ObjectPath, - std::unique_ptr<org::chromium::peerd::ManagerProxy>> manager_instances_; - base::Callback<void(org::chromium::peerd::ManagerProxy*)> on_manager_added_; - base::Callback<void(const dbus::ObjectPath&)> on_manager_removed_; - std::map<dbus::ObjectPath, - std::unique_ptr<org::chromium::peerd::PeerProxy>> peer_instances_; - base::Callback<void(org::chromium::peerd::PeerProxy*)> on_peer_added_; - base::Callback<void(const dbus::ObjectPath&)> on_peer_removed_; - std::map<dbus::ObjectPath, - std::unique_ptr<org::chromium::peerd::ServiceProxy>> service_instances_; - base::Callback<void(org::chromium::peerd::ServiceProxy*)> on_service_added_; - base::Callback<void(const dbus::ObjectPath&)> on_service_removed_; - base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; - - DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); -}; - -} // namespace peerd -} // namespace chromium -} // namespace org - -#endif // ____CHROMEOS_DBUS_BINDING___BUILD_LINK_VAR_CACHE_PORTAGE_CHROMEOS_BASE_BUFFET_OUT_DEFAULT_GEN_INCLUDE_PEERD_DBUS_PROXIES_H |