Flutter Engine
component.h
Go to the documentation of this file.
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_COMPONENT_H_
6 #define FLUTTER_SHELL_PLATFORM_FUCHSIA_COMPONENT_H_
7 
8 #include <array>
9 #include <memory>
10 #include <set>
11 
12 #include <fuchsia/io/cpp/fidl.h>
13 #include <fuchsia/sys/cpp/fidl.h>
14 #include <fuchsia/ui/app/cpp/fidl.h>
15 #include <lib/async-loop/cpp/loop.h>
16 #include <lib/async/default.h>
17 #include <lib/fidl/cpp/binding_set.h>
18 #include <lib/fidl/cpp/interface_request.h>
19 #include <lib/fit/function.h>
20 #include <lib/sys/cpp/service_directory.h>
21 #include <lib/vfs/cpp/pseudo_dir.h>
22 #include <lib/zx/eventpair.h>
23 
24 #include "engine.h"
25 #include "flutter/common/settings.h"
26 #include "flutter/fml/macros.h"
27 
29 #include "thread.h"
30 #include "unique_fdio_ns.h"
31 
32 namespace flutter_runner {
33 
34 class Application;
35 
37  std::unique_ptr<Thread> thread;
38  std::unique_ptr<Application> application;
39 
41  if (this != &other) {
42  this->thread.reset(other.thread.release());
43  this->application.reset(other.application.release());
44  }
45  return *this;
46  }
47 
48  ~ActiveApplication() = default;
49 };
50 
51 // Represents an instance of a Flutter application that contains one of more
52 // Flutter engine instances.
53 class Application final : public Engine::Delegate,
54  public fuchsia::sys::ComponentController,
55  public fuchsia::ui::app::ViewProvider {
56  public:
57  using TerminationCallback = fit::function<void(const Application*)>;
58 
59  // Creates a dedicated thread to run the application and constructions the
60  // application on it. The application can be accessed only on this thread.
61  // This is a synchronous operation.
62  static ActiveApplication Create(
63  TerminationCallback termination_callback,
64  fuchsia::sys::Package package,
65  fuchsia::sys::StartupInfo startup_info,
66  std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
67  fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller);
68 
69  // Must be called on the same thread returned from the create call. The thread
70  // may be collected after.
71  ~Application();
72 
73  static void ParseProgramMetadata(
74  const fidl::VectorPtr<fuchsia::sys::ProgramMetadata>& program_metadata,
75  std::string* data_path,
76  std::string* assets_path);
77 
78  const std::string& GetDebugLabel() const;
79 
80 #if !defined(DART_PRODUCT)
81  void WriteProfileToTrace() const;
82 #endif // !defined(DART_PRODUCT)
83 
84  private:
86  FlutterRunnerProductConfiguration product_config_;
87  TerminationCallback termination_callback_;
88  const std::string debug_label_;
89  UniqueFDIONS fdio_ns_ = UniqueFDIONSCreate();
90  fml::UniqueFD application_data_directory_;
91  fml::UniqueFD application_assets_directory_;
92 
93  fidl::Binding<fuchsia::sys::ComponentController> application_controller_;
94  fuchsia::io::DirectoryPtr directory_ptr_;
95  fuchsia::io::NodePtr cloned_directory_ptr_;
96  fidl::InterfaceRequest<fuchsia::io::Directory> directory_request_;
97  std::unique_ptr<vfs::PseudoDir> outgoing_dir_;
98  std::shared_ptr<sys::ServiceDirectory> svc_;
99  std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
100  fidl::BindingSet<fuchsia::ui::app::ViewProvider> shells_bindings_;
101 
102  fml::RefPtr<flutter::DartSnapshot> isolate_snapshot_;
103  std::set<std::unique_ptr<Engine>> shell_holders_;
104  std::pair<bool, uint32_t> last_return_code_;
105  fml::WeakPtrFactory<Application> weak_factory_;
106 
107  Application(
108  TerminationCallback termination_callback,
109  fuchsia::sys::Package package,
110  fuchsia::sys::StartupInfo startup_info,
111  std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
112  fidl::InterfaceRequest<fuchsia::sys::ComponentController> controller);
113 
114  // |fuchsia::sys::ComponentController|
115  void Kill() override;
116 
117  // |fuchsia::sys::ComponentController|
118  void Detach() override;
119 
120  // |fuchsia::ui::app::ViewProvider|
121  void CreateView(
122  zx::eventpair token,
123  fidl::InterfaceRequest<fuchsia::sys::ServiceProvider> incoming_services,
124  fidl::InterfaceHandle<fuchsia::sys::ServiceProvider> outgoing_services)
125  override;
126 
127  // |fuchsia::ui::app::ViewProvider|
128  void CreateViewWithViewRef(zx::eventpair view_token,
129  fuchsia::ui::views::ViewRefControl control_ref,
130  fuchsia::ui::views::ViewRef view_ref) override;
131 
132  // |flutter::Engine::Delegate|
133  void OnEngineTerminate(const Engine* holder) override;
134 
135  void AttemptVMLaunchWithCurrentSettings(const flutter::Settings& settings);
136 
138 };
139 
140 } // namespace flutter_runner
141 
142 #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_COMPONENT_H_
Settings settings_
ActiveApplication & operator=(ActiveApplication &&other) noexcept
Definition: component.h:40
std::unique_ptr< Thread > thread
Definition: component.h:37
fit::function< void(const Application *)> TerminationCallback
Definition: component.h:57
UniqueFDIONS UniqueFDIONSCreate()
std::unique_ptr< Application > application
Definition: component.h:38
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27