Flutter Engine
The Flutter Engine
component_v2.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_FLUTTER_COMPONENT_V2_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_COMPONENT_V2_H_
7
8#include <array>
9#include <memory>
10#include <set>
11
12#include <fuchsia/component/runner/cpp/fidl.h>
13#include <fuchsia/io/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 "flutter/common/settings.h"
25#include "flutter/fml/macros.h"
26
27#include "engine.h"
29#include "program_metadata.h"
30#include "unique_fdio_ns.h"
31
32namespace flutter_runner {
33
34class ComponentV2;
35
37 std::unique_ptr<fml::Thread> platform_thread;
38 std::unique_ptr<ComponentV2> component;
39
41 if (this != &other) {
42 this->platform_thread.reset(other.platform_thread.release());
43 this->component.reset(other.component.release());
44 }
45 return *this;
46 }
47
48 ~ActiveComponentV2() = default;
49};
50
51// Represents an instance of a CF v2 Flutter component that contains one or more
52// Flutter engine instances.
53//
54// TODO(fxb/50694): Add unit tests once we've verified that the current behavior
55// is working correctly.
56class ComponentV2 final
57 : public Engine::Delegate,
58 public fuchsia::component::runner::ComponentController,
59 public fuchsia::ui::app::ViewProvider {
60 public:
62
63 // Creates a dedicated thread to run the component and creates the
64 // component on it. The component can be accessed only on this thread.
65 // This is a synchronous operation.
67 TerminationCallback termination_callback,
68 fuchsia::component::runner::ComponentStartInfo start_info,
69 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
70 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
71 controller);
72
73 // Must be called on the same thread returned from the create call. The thread
74 // may be collected after.
76
77 /// Parses the program metadata that was provided for the component.
78 ///
79 /// |old_gen_heap_size| will be set to -1 if no value was specified.
81 const fuchsia::data::Dictionary& program_metadata);
82
83 const std::string& GetDebugLabel() const;
84
85#if !defined(DART_PRODUCT)
86 void WriteProfileToTrace() const;
87#endif // !defined(DART_PRODUCT)
88
89 private:
91 TerminationCallback termination_callback,
92 fuchsia::component::runner::ComponentStartInfo start_info,
93 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
94 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
95 controller);
96
97 // |fuchsia::component::runner::ComponentController|
98 void Kill() override;
99
100 /// Helper to actually |Kill| the component, closing the connection via an
101 /// epitaph with the given |epitaph_status|. Call this instead of
102 /// Kill() in the implementation of this class, as |Kill| is only intended for
103 /// clients of the ComponentController protocol to call.
104 ///
105 /// To determine what |epitaph_status| is appropriate for your situation,
106 /// see the documentation for |fuchsia.component.runner.ComponentController|.
107 void KillWithEpitaph(zx_status_t epitaph_status);
108
109 // |fuchsia::component::runner::ComponentController|
110 void Stop() override;
111
112 // |fuchsia::ui::app::ViewProvider|
113 void CreateView2(fuchsia::ui::app::CreateView2Args view_args) override;
114 void CreateViewWithViewRef(
115 ::zx::eventpair token,
116 ::fuchsia::ui::views::ViewRefControl view_ref_control,
117 ::fuchsia::ui::views::ViewRef view_ref) override {}
118
119 // |flutter::Engine::Delegate|
120 void OnEngineTerminate(const Engine* holder) override;
121
122 flutter::Settings settings_;
123 FlutterRunnerProductConfiguration product_config_;
124 TerminationCallback termination_callback_;
125 const std::string debug_label_;
126 UniqueFDIONS fdio_ns_ = UniqueFDIONSCreate();
127 fml::UniqueFD component_data_directory_;
128 fml::UniqueFD component_assets_directory_;
129
130 fidl::Binding<fuchsia::component::runner::ComponentController>
131 component_controller_;
132 fuchsia::io::DirectoryPtr directory_ptr_;
133 fuchsia::io::NodePtr cloned_directory_ptr_;
134 fidl::InterfaceRequest<fuchsia::io::Directory> directory_request_;
135 std::unique_ptr<vfs::PseudoDir> outgoing_dir_;
136 std::unique_ptr<vfs::PseudoDir> runtime_dir_;
137 std::shared_ptr<sys::ServiceDirectory> svc_;
138 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
139 fidl::BindingSet<fuchsia::ui::app::ViewProvider> shells_bindings_;
140
141 fml::RefPtr<flutter::DartSnapshot> isolate_snapshot_;
142 std::set<std::unique_ptr<Engine>> shell_holders_;
143 std::pair<bool, uint32_t> last_return_code_;
144 fml::WeakPtrFactory<ComponentV2> weak_factory_; // Must be the last member.
145 FML_DISALLOW_COPY_AND_ASSIGN(ComponentV2);
146};
147
148} // namespace flutter_runner
149
150#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_COMPONENT_V2_H_
static ProgramMetadata ParseProgramMetadata(const fuchsia::data::Dictionary &program_metadata)
static ActiveComponentV2 Create(TerminationCallback termination_callback, fuchsia::component::runner::ComponentStartInfo start_info, std::shared_ptr< sys::ServiceDirectory > runner_incoming_services, fidl::InterfaceRequest< fuchsia::component::runner::ComponentController > controller)
const std::string & GetDebugLabel() const
fit::function< void(const ComponentV2 *)> TerminationCallback
Definition: component_v2.h:61
Dart_NativeFunction function
Definition: fuchsia.cc:51
UniqueFDIONS UniqueFDIONSCreate()
std::unique_ptr< fml::Thread > platform_thread
Definition: component_v2.h:37
ActiveComponentV2 & operator=(ActiveComponentV2 &&other) noexcept
Definition: component_v2.h:40
std::unique_ptr< ComponentV2 > component
Definition: component_v2.h:38