/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include #include "FlipperCertificateProvider.h" #include "FlipperConnectionImpl.h" #include "FlipperConnectionManager.h" #include "FlipperInitConfig.h" #include "FlipperPlugin.h" #include "FlipperState.h" #include "FlipperStep.h" namespace facebook { namespace flipper { class FlipperClient : public FlipperConnectionManager::Callbacks { public: /** Call before accessing instance with FlipperClient::instance(). This will set up all the state needed to establish a Flipper connection. */ static void init(FlipperInitConfig config); /** Standard accessor for the shared FlipperClient instance. This returns a singleton instance to a shared FlipperClient. First call to this function will create the shared FlipperClient. Must call FlipperClient::initDeviceData() before first call to FlipperClient::instance(). */ static FlipperClient* instance(); /** Only public for testing */ FlipperClient( std::unique_ptr socket, std::shared_ptr state) : socket_(std::move(socket)), flipperState_(state) { auto step = flipperState_->start("Create client"); socket_->setCallbacks(this); auto& conn = connections_["flipper-crash-report"]; conn = std::make_shared( socket_.get(), "flipper-crash-report"); step->complete(); } void start() { performAndReportError([this]() { auto step = flipperState_->start("Start client"); socket_->start(); step->complete(); }); } void stop() { performAndReportError([this]() { auto step = flipperState_->start("Stop client"); socket_->stop(); step->complete(); }); } void onConnected() override; void onDisconnected() override; bool isConnected(); void onMessageReceived( const folly::dynamic& message, std::unique_ptr) override; void addPlugin(std::shared_ptr plugin); void removePlugin(std::shared_ptr plugin); void refreshPlugins(); void setStateListener( std::shared_ptr stateListener); void setCertificateProvider( const std::shared_ptr provider); std::shared_ptr getCertificateProvider(); std::shared_ptr getPlugin(const std::string& identifier); std::string getState(); std::vector getStateElements(); template std::shared_ptr

getPlugin(const std::string& identifier) { return std::static_pointer_cast

(getPlugin(identifier)); } bool hasPlugin(const std::string& identifier); void performAndReportError(const std::function& func); private: static FlipperClient* instance_; bool connected_ = false; std::unique_ptr socket_; std::map> plugins_; std::map> connections_; std::mutex mutex_; std::shared_ptr flipperState_; void connect(std::shared_ptr plugin); void disconnect(std::shared_ptr plugin); std::string callstack(); void handleError(std::exception& e); }; } // namespace flipper } // namespace facebook