Flutter Engine
 
Loading...
Searching...
No Matches
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
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:
61 using TerminationCallback = fit::function<void(const ComponentV2*)>;
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 void handle_unknown_method(uint64_t ordinal,
90 bool method_has_response) override;
91
92 private:
94 TerminationCallback termination_callback,
95 fuchsia::component::runner::ComponentStartInfo start_info,
96 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services,
97 fidl::InterfaceRequest<fuchsia::component::runner::ComponentController>
98 controller);
99
100 // |fuchsia::component::runner::ComponentController|
101 void Kill() override;
102
103 /// Helper to actually |Kill| the component, closing the connection via an
104 /// epitaph with the given |epitaph_status|. Call this instead of
105 /// Kill() in the implementation of this class, as |Kill| is only intended for
106 /// clients of the ComponentController protocol to call.
107 ///
108 /// To determine what |epitaph_status| is appropriate for your situation,
109 /// see the documentation for |fuchsia.component.runner.ComponentController|.
110 void KillWithEpitaph(zx_status_t epitaph_status);
111
112 // |fuchsia::component::runner::ComponentController|
113 void Stop() override;
114
115 // |fuchsia::ui::app::ViewProvider|
116 void CreateView2(fuchsia::ui::app::CreateView2Args view_args) override;
117 void CreateViewWithViewRef(
118 ::zx::eventpair token,
119 ::fuchsia::ui::views::ViewRefControl view_ref_control,
120 ::fuchsia::ui::views::ViewRef view_ref) override {}
121
122 // |flutter::Engine::Delegate|
123 void OnEngineTerminate(const Engine* holder) override;
124
125 flutter::Settings settings_;
126 FlutterRunnerProductConfiguration product_config_;
127 TerminationCallback termination_callback_;
128 const std::string debug_label_;
129 UniqueFDIONS fdio_ns_ = UniqueFDIONSCreate();
130 fml::UniqueFD component_data_directory_;
131 fml::UniqueFD component_assets_directory_;
132
133 fidl::Binding<fuchsia::component::runner::ComponentController>
134 component_controller_;
135 fuchsia::io::DirectoryPtr directory_ptr_;
136 fuchsia::io::NodePtr cloned_directory_ptr_;
137 fidl::InterfaceRequest<fuchsia::io::Directory> directory_request_;
138 std::unique_ptr<vfs::PseudoDir> outgoing_dir_;
139 std::unique_ptr<vfs::PseudoDir> runtime_dir_;
140 std::shared_ptr<sys::ServiceDirectory> svc_;
141 std::shared_ptr<sys::ServiceDirectory> runner_incoming_services_;
142 fidl::BindingSet<fuchsia::ui::app::ViewProvider> shells_bindings_;
143
144 fml::RefPtr<flutter::DartSnapshot> isolate_snapshot_;
145 std::set<std::unique_ptr<Engine>> shell_holders_;
146 std::pair<bool, uint32_t> last_return_code_;
147 fml::WeakPtrFactory<ComponentV2> weak_factory_; // Must be the last member.
149};
150
151} // namespace flutter_runner
152
153#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_COMPONENT_V2_H_
static ProgramMetadata ParseProgramMetadata(const fuchsia::data::Dictionary &program_metadata)
void handle_unknown_method(uint64_t ordinal, bool method_has_response) override
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
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
UniqueFDIONS UniqueFDIONSCreate()
std::unique_ptr< fml::Thread > platform_thread
ActiveComponentV2 & operator=(ActiveComponentV2 &&other) noexcept
std::unique_ptr< ComponentV2 > component