diff --git a/android/src/main/cpp/sonar.cpp b/android/src/main/cpp/sonar.cpp index 593823d95..12c0eb30a 100644 --- a/android/src/main/cpp/sonar.cpp +++ b/android/src/main/cpp/sonar.cpp @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include @@ -175,9 +175,9 @@ class JFlipperConnectionImpl : public jni::HybridClass _connection; + std::shared_ptr _connection; - JFlipperConnectionImpl(std::shared_ptr connection): _connection(std::move(connection)) {} + JFlipperConnectionImpl(std::shared_ptr connection): _connection(std::move(connection)) {} }; class JFlipperPlugin : public jni::JavaClass { @@ -189,7 +189,7 @@ class JFlipperPlugin : public jni::JavaClass { return method(self())->toStdString(); } - void didConnect(std::shared_ptr conn) { + void didConnect(std::shared_ptr conn) { static const auto method = javaClassStatic()->getMethod)>("onConnect"); method(self(), JFlipperConnectionImpl::newObjectCxxArgs(conn)); } @@ -239,7 +239,7 @@ class JFlipperPluginWrapper : public SonarPlugin { return jplugin->identifier(); } - virtual void didConnect(std::shared_ptr conn) override { + virtual void didConnect(std::shared_ptr conn) override { jplugin->didConnect(conn); } diff --git a/docs/create-plugin.md b/docs/create-plugin.md index 9a00c3679..c99746193 100644 --- a/docs/create-plugin.md +++ b/docs/create-plugin.md @@ -52,7 +52,7 @@ public class MySonarPlugin implements SonarPlugin { class MySonarPlugin : public SonarPlugin { public: std::string identifier() const override { return "MySonarPlugin"; } - void didConnect(std::shared_ptr conn) override; + void didConnect(std::shared_ptr conn) override; void didDisconnect() override; }; ``` @@ -102,7 +102,7 @@ connection.receive("getData", new SonarReceiver() { ### C++ ```c++ -void MySonarPlugin::didConnect(std::shared_ptr conn) { +void MySonarPlugin::didConnect(std::shared_ptr conn) { conn->receive("getData", [](const folly::dynamic ¶ms, std::unique_ptr responder) { dynamic response = folly::dynamic::object("data", getMyData()); @@ -133,7 +133,7 @@ connection.send("MyMessage", ### C++ ```c++ -void MySonarPlugin::didConnect(std::shared_ptr conn) { +void MySonarPlugin::didConnect(std::shared_ptr conn) { dynamic message = folly::dynamic::object("message", "hello"); conn->send("getData", message); } diff --git a/docs/error-handling.md b/docs/error-handling.md index 7dc93b8cc..ee972ebe1 100644 --- a/docs/error-handling.md +++ b/docs/error-handling.md @@ -25,7 +25,7 @@ Always use `ErrorReportingRunnable` for error handling instead of `try`/`catch` ## C++ -To gracefully handle errors in Flipper we perform all transactions inside of a try block which catches all exceptions, stopping them from crashing the application and reporting them to the plugin developer. This includes your own customs implementations of `SonarPlugin::didConnect()` and `SonarConnection::send()` and `::receive()`! +To gracefully handle errors in Flipper we perform all transactions inside of a try block which catches all exceptions, stopping them from crashing the application and reporting them to the plugin developer. This includes your own customs implementations of `SonarPlugin::didConnect()` and `FlipperConnection::send()` and `::receive()`! That means you can safely throw exceptions in your plugin code. The exception messages will be sent to the Flipper desktop app. During plugin development the exception messages are surfaced in the Chrome dev console. diff --git a/docs/testing.md b/docs/testing.md index 5a628b774..719421324 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -54,7 +54,7 @@ Start by creating your first test file in this directory `MySonarPluginTests.cpp ``` #include -#include +#include #include #include @@ -79,7 +79,7 @@ Here is a simple test using these mock utilities to create a plugin, send some d TEST(MySonarPluginTests, testDummy) { std::vector successfulResponses; auto responder = std::make_unique(&successfulResponses); - auto conn = std::make_shared(); + auto conn = std::make_shared(); MySonarPlugin plugin; plugin.didConnect(conn); diff --git a/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.h b/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.h index 15b017f38..6d4bba4ed 100644 --- a/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.h +++ b/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.h @@ -5,7 +5,7 @@ * file in the root directory of this source tree. * */ -#import +#import #import /** @@ -14,5 +14,5 @@ that forwards messages to the underlying C++ connection. This class allows pure Objective-C plugins to send messages to the underlying connection. */ @interface FlipperCppBridgingConnection : NSObject -- (instancetype)initWithCppConnection:(std::shared_ptr)conn; +- (instancetype)initWithCppConnection:(std::shared_ptr)conn; @end diff --git a/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.mm b/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.mm index cdd379042..8432654cb 100644 --- a/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.mm +++ b/iOS/FlipperKit/CppBridge/FlipperCppBridgingConnection.mm @@ -13,10 +13,10 @@ @implementation FlipperCppBridgingConnection { - std::shared_ptr conn_; + std::shared_ptr conn_; } -- (instancetype)initWithCppConnection:(std::shared_ptr)conn +- (instancetype)initWithCppConnection:(std::shared_ptr)conn { if (self = [super init]) { conn_ = conn; diff --git a/iOS/FlipperKit/CppBridge/SonarCppWrapperPlugin.h b/iOS/FlipperKit/CppBridge/SonarCppWrapperPlugin.h index 24b01dbc5..10d31a69e 100644 --- a/iOS/FlipperKit/CppBridge/SonarCppWrapperPlugin.h +++ b/iOS/FlipperKit/CppBridge/SonarCppWrapperPlugin.h @@ -30,7 +30,7 @@ public: std::string identifier() const override { return [[_objCPlugin identifier] UTF8String]; } - void didConnect(std::shared_ptr conn) override + void didConnect(std::shared_ptr conn) override { FlipperCppBridgingConnection *const bridgingConn = [[FlipperCppBridgingConnection alloc] initWithCppConnection:conn]; [_objCPlugin didConnect:bridgingConn]; diff --git a/xplat/Flipper/FlipperClient.cpp b/xplat/Flipper/FlipperClient.cpp index a8d7bd159..728cb4089 100644 --- a/xplat/Flipper/FlipperClient.cpp +++ b/xplat/Flipper/FlipperClient.cpp @@ -7,7 +7,7 @@ */ #include "FlipperClient.h" -#include "SonarConnectionImpl.h" +#include "FlipperConnectionImpl.h" #include "SonarResponderImpl.h" #include "SonarState.h" #include "SonarStep.h" @@ -159,7 +159,7 @@ void FlipperClient::onMessageReceived(const dynamic& message) { } const auto plugin = plugins_.at(identifier); auto& conn = connections_[plugin->identifier()]; - conn = std::make_shared( + conn = std::make_shared( socket_.get(), plugin->identifier()); plugin->didConnect(conn); return; diff --git a/xplat/Flipper/FlipperClient.h b/xplat/Flipper/FlipperClient.h index 8c005621d..6e03719c8 100644 --- a/xplat/Flipper/FlipperClient.h +++ b/xplat/Flipper/FlipperClient.h @@ -8,7 +8,7 @@ #pragma once -#include "SonarConnectionImpl.h" +#include "FlipperConnectionImpl.h" #include "SonarInitConfig.h" #include "SonarPlugin.h" #include "SonarState.h" @@ -92,7 +92,7 @@ class FlipperClient : public SonarWebSocket::Callbacks { bool connected_ = false; std::unique_ptr socket_; std::map> plugins_; - std::map> connections_; + std::map> connections_; std::mutex mutex_; std::shared_ptr sonarState_; diff --git a/xplat/Flipper/SonarConnection.h b/xplat/Flipper/FlipperConnection.h similarity index 95% rename from xplat/Flipper/SonarConnection.h rename to xplat/Flipper/FlipperConnection.h index cb5da39e8..baf0f8669 100644 --- a/xplat/Flipper/SonarConnection.h +++ b/xplat/Flipper/FlipperConnection.h @@ -20,12 +20,12 @@ namespace flipper { Represents a connection between the Desktop and mobile plugins with corresponding identifiers. */ -class SonarConnection { +class FlipperConnection { public: using SonarReceiver = std::function< void(const folly::dynamic&, std::unique_ptr)>; - virtual ~SonarConnection() {} + virtual ~FlipperConnection() {} /** Invoke a method on the Sonar desktop plugin with with a matching identifier. diff --git a/xplat/Flipper/SonarConnectionImpl.h b/xplat/Flipper/FlipperConnectionImpl.h similarity index 90% rename from xplat/Flipper/SonarConnectionImpl.h rename to xplat/Flipper/FlipperConnectionImpl.h index b3abc5561..5a2eea495 100644 --- a/xplat/Flipper/SonarConnectionImpl.h +++ b/xplat/Flipper/FlipperConnectionImpl.h @@ -8,7 +8,7 @@ #pragma once -#include "SonarConnection.h" +#include "FlipperConnection.h" #include "SonarWebSocket.h" #include #include @@ -16,9 +16,9 @@ namespace facebook { namespace flipper { -class SonarConnectionImpl : public SonarConnection { +class FlipperConnectionImpl : public FlipperConnection { public: - SonarConnectionImpl(SonarWebSocket* socket, const std::string& name) + FlipperConnectionImpl(SonarWebSocket* socket, const std::string& name) : socket_(socket), name_(name) {} void call( diff --git a/xplat/Flipper/SonarPlugin.h b/xplat/Flipper/SonarPlugin.h index 22c0ea5aa..9a7a79acd 100644 --- a/xplat/Flipper/SonarPlugin.h +++ b/xplat/Flipper/SonarPlugin.h @@ -7,7 +7,7 @@ */ #pragma once -#include "SonarConnection.h" +#include "FlipperConnection.h" #include namespace facebook { @@ -29,10 +29,10 @@ class SonarPlugin { connection can be used to register method receivers as well as send messages back to the desktop app. */ - virtual void didConnect(std::shared_ptr conn) = 0; + virtual void didConnect(std::shared_ptr conn) = 0; /** - Called when a plugin has been disconnected and the SonarConnection + Called when a plugin has been disconnected and the FlipperConnection provided in didConnect is no longer valid to use. */ virtual void didDisconnect() = 0; diff --git a/xplat/FlipperTestLib/SonarConnectionMock.h b/xplat/FlipperTestLib/FlipperConnectionMock.h similarity index 88% rename from xplat/FlipperTestLib/SonarConnectionMock.h rename to xplat/FlipperTestLib/FlipperConnectionMock.h index 97f6a7754..16304c4fd 100644 --- a/xplat/FlipperTestLib/SonarConnectionMock.h +++ b/xplat/FlipperTestLib/FlipperConnectionMock.h @@ -8,14 +8,14 @@ #pragma once -#include +#include #include #include namespace facebook { namespace flipper { -class SonarConnectionMock : public SonarConnection { +class FlipperConnectionMock : public FlipperConnection { public: void send(const std::string& method, const folly::dynamic& params) override { sent_[method] = params; diff --git a/xplat/FlipperTestLib/SonarPluginMock.h b/xplat/FlipperTestLib/SonarPluginMock.h index 310f96055..51db4919a 100644 --- a/xplat/FlipperTestLib/SonarPluginMock.h +++ b/xplat/FlipperTestLib/SonarPluginMock.h @@ -16,7 +16,7 @@ namespace test { class SonarPluginMock : public SonarPlugin { using ConnectionCallback = - std::function)>; + std::function)>; using DisconnectionCallback = std::function; public: @@ -39,7 +39,7 @@ class SonarPluginMock : public SonarPlugin { return identifier_; } - void didConnect(std::shared_ptr conn) override { + void didConnect(std::shared_ptr conn) override { if (connectionCallback_) { connectionCallback_(conn); } diff --git a/xplat/FlipperTests/SonarClientTests.cpp b/xplat/FlipperTests/SonarClientTests.cpp index fee76732a..0f52d79da 100644 --- a/xplat/FlipperTests/SonarClientTests.cpp +++ b/xplat/FlipperTests/SonarClientTests.cpp @@ -103,7 +103,7 @@ TEST(SonarClientTests, testConnectDisconnect) { FlipperClient client(std::unique_ptr{socket}, state); bool pluginConnected = false; - const auto connectionCallback = [&](std::shared_ptr conn) { + const auto connectionCallback = [&](std::shared_ptr conn) { pluginConnected = true; }; const auto disconnectionCallback = [&]() { pluginConnected = false; }; @@ -126,7 +126,7 @@ TEST(SonarClientTests, testInitDeinit) { FlipperClient client(std::unique_ptr{socket}, state); bool pluginConnected = false; - const auto connectionCallback = [&](std::shared_ptr conn) { + const auto connectionCallback = [&](std::shared_ptr conn) { pluginConnected = true; }; const auto disconnectionCallback = [&]() { pluginConnected = false; }; @@ -164,7 +164,7 @@ TEST(SonarClientTests, testRemovePluginWhenConnected) { FlipperClient client(std::unique_ptr{socket}, state); bool pluginConnected = false; - const auto connectionCallback = [&](std::shared_ptr conn) { + const auto connectionCallback = [&](std::shared_ptr conn) { pluginConnected = true; }; const auto disconnectionCallback = [&]() { pluginConnected = false; }; @@ -205,7 +205,7 @@ TEST(SonarClientTests, testExecute) { FlipperClient client(std::unique_ptr{socket}, state); client.start(); - const auto connectionCallback = [](std::shared_ptr conn) { + const auto connectionCallback = [](std::shared_ptr conn) { const auto receiver = [](const dynamic ¶ms, std::unique_ptr responder) { dynamic payload = dynamic::object("message", "yes_i_hear_u"); @@ -234,7 +234,7 @@ TEST(SonarClientTests, testExecuteWithParams) { auto socket = new SonarWebSocketMock; FlipperClient client(std::unique_ptr{socket}, state); - const auto connectionCallback = [&](std::shared_ptr conn) { + const auto connectionCallback = [&](std::shared_ptr conn) { const auto receiver = [](const dynamic ¶ms, std::unique_ptr responder) { const auto &first = params["first"].asString();