1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_ 6 #define FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <string_view> 12 13 #include "flutter/fml/compiler_specific.h" 14 #include "flutter/fml/macros.h" 15 #include "flutter/fml/synchronization/atomic_object.h" 16 #include "flutter/fml/synchronization/shared_mutex.h" 17 #include "flutter/fml/synchronization/thread_annotations.h" 18 #include "flutter/fml/task_runner.h" 19 #include "rapidjson/document.h" 20 21 namespace flutter { 22 23 class ServiceProtocol { 24 public: 25 static const std::string_view kScreenshotExtensionName; 26 static const std::string_view kScreenshotSkpExtensionName; 27 static const std::string_view kRunInViewExtensionName; 28 static const std::string_view kFlushUIThreadTasksExtensionName; 29 static const std::string_view kSetAssetBundlePathExtensionName; 30 static const std::string_view kGetDisplayRefreshRateExtensionName; 31 32 class Handler { 33 public: 34 struct Description { 35 int64_t isolate_port = 0 /* illegal port by default. */; 36 std::string isolate_name; 37 DescriptionDescription38 Description() {} 39 DescriptionDescription40 Description(int64_t p_isolate_port, std::string p_isolate_name) 41 : isolate_port(p_isolate_port), 42 isolate_name(std::move(p_isolate_name)) {} 43 44 void Write(Handler* handler, 45 rapidjson::Value& value, 46 rapidjson::MemoryPoolAllocator<>& allocator) const; 47 }; 48 49 using ServiceProtocolMap = std::map<std::string_view, std::string_view>; 50 51 virtual fml::RefPtr<fml::TaskRunner> GetServiceProtocolHandlerTaskRunner( 52 std::string_view method) const = 0; 53 54 virtual Description GetServiceProtocolDescription() const = 0; 55 56 virtual bool HandleServiceProtocolMessage( 57 std::string_view method, // one if the extension names specified above. 58 const ServiceProtocolMap& params, 59 rapidjson::Document& response) = 0; 60 }; 61 62 ServiceProtocol(); 63 64 ~ServiceProtocol(); 65 66 void ToggleHooks(bool set); 67 68 void AddHandler(Handler* handler, Handler::Description description); 69 70 void RemoveHandler(Handler* handler); 71 72 void SetHandlerDescription(Handler* handler, 73 Handler::Description description); 74 75 private: 76 const std::set<std::string_view> endpoints_; 77 std::unique_ptr<fml::SharedMutex> handlers_mutex_; 78 std::map<Handler*, fml::AtomicObject<Handler::Description>> handlers_; 79 80 FML_WARN_UNUSED_RESULT 81 static bool HandleMessage(const char* method, 82 const char** param_keys, 83 const char** param_values, 84 intptr_t num_params, 85 void* user_data, 86 const char** json_object); 87 FML_WARN_UNUSED_RESULT 88 static bool HandleMessage(std::string_view method, 89 const Handler::ServiceProtocolMap& params, 90 ServiceProtocol* service_protocol, 91 rapidjson::Document& response); 92 FML_WARN_UNUSED_RESULT 93 bool HandleMessage(std::string_view method, 94 const Handler::ServiceProtocolMap& params, 95 rapidjson::Document& response) const; 96 97 FML_WARN_UNUSED_RESULT 98 bool HandleListViewsMethod(rapidjson::Document& response) const; 99 100 FML_DISALLOW_COPY_AND_ASSIGN(ServiceProtocol); 101 }; 102 103 } // namespace flutter 104 105 #endif // FLUTTER_RUNTIME_SERVICE_PROTOCOL_H_ 106