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_SHELL_PLATFORM_FUCHSIA_SERVICE_PROVIDER_DIR_H_ 6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_SERVICE_PROVIDER_DIR_H_ 7 8 #include <map> 9 #include <string> 10 #include <unordered_set> 11 #include <utility> 12 #include <vector> 13 14 #include <fuchsia/io/cpp/fidl.h> 15 #include <fuchsia/sys/cpp/fidl.h> 16 #include <lib/vfs/cpp/pseudo_dir.h> 17 #include <lib/vfs/cpp/service.h> 18 19 #include "lib/fidl/cpp/binding_set.h" 20 21 namespace flutter_runner { 22 23 // A directory-like object which dynamically creates Service nodes 24 // for any file lookup. It also exposes service provider interface. 25 // 26 // It supports enumeration for only first level of services. 27 class ServiceProviderDir : public vfs::Directory { 28 public: 29 ServiceProviderDir(); 30 ~ServiceProviderDir() override; 31 32 void set_fallback(fidl::InterfaceHandle<fuchsia::io::Directory> fallback_dir); 33 34 void AddService(const std::string& service_name, 35 std::unique_ptr<vfs::Service> service); 36 37 // 38 // Overridden from |vfs::Node|: 39 // 40 41 zx_status_t Lookup(const std::string& name, vfs::Node** out_node) const final; 42 43 zx_status_t GetAttr(fuchsia::io::NodeAttributes* out_attributes) const final; 44 45 zx_status_t Readdir(uint64_t offset, 46 void* data, 47 uint64_t len, 48 uint64_t* out_offset, 49 uint64_t* out_actual) final; 50 51 private: 52 // |root_| has all services offered by this provider (including those 53 // inherited from the parent, if any). 54 std::unique_ptr<vfs::PseudoDir> root_; 55 zx::channel fallback_dir_; 56 // The collection of services that have been looked up on the fallback 57 // directory. These services are just passthrough in the sense that they 58 // forward connection requests to the fallback directory. Since there is no 59 // good way in the present context to know whether these service entries 60 // actually match an existing service, and since the present object must own 61 // these entries, we keep them around until the present object gets deleted. 62 // Needs to be marked mutable so that it can be altered by the Lookup method. 63 mutable std::map<std::string, std::unique_ptr<vfs::Service>> 64 fallback_services_; 65 66 // Disallow copy and assignment. 67 ServiceProviderDir(const ServiceProviderDir&) = delete; 68 ServiceProviderDir& operator=(const ServiceProviderDir&) = delete; 69 }; 70 71 } // namespace flutter_runner 72 73 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_SERVICE_PROVIDER_DIR_H_ 74