Flutter Engine
The Flutter Engine
run_configuration.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_COMMON_RUN_CONFIGURATION_H_
6#define FLUTTER_SHELL_COMMON_RUN_CONFIGURATION_H_
7
8#include <memory>
9#include <string>
10
11#include "flutter/assets/asset_manager.h"
12#include "flutter/assets/asset_resolver.h"
13#include "flutter/common/settings.h"
14#include "flutter/fml/macros.h"
15#include "flutter/fml/mapping.h"
16#include "flutter/fml/unique_fd.h"
17#include "flutter/runtime/isolate_configuration.h"
18
19namespace flutter {
20
21//------------------------------------------------------------------------------
22/// @brief Specifies all the configuration required by the runtime library
23/// to launch the root isolate. This object may be created on any
24/// thread but must be given to the |Run| call of the |Engine| on
25/// the UI thread. The configuration object is used to specify how
26/// the root isolate finds its snapshots, assets, root library and
27/// the "main" entrypoint.
28///
30 public:
31 //----------------------------------------------------------------------------
32 /// @brief Attempts to infer a run configuration from the settings
33 /// object. This tries to create a run configuration with sensible
34 /// defaults for the given Dart VM runtime mode. In JIT mode, this
35 /// will attempt to look for the VM and isolate snapshots in the
36 /// assets directory (must be specified in settings). In AOT mode,
37 /// it will attempt to look for known snapshot symbols in the
38 /// currently loaded process. The entrypoint defaults to
39 /// the "main" method in the root library.
40 ///
41 /// @param[in] settings The settings object used to look for the various
42 /// snapshots and settings. This is usually initialized
43 /// from command line arguments.
44 /// @param[in] io_worker An optional IO worker. Resolving and reading the
45 /// various snapshots may be slow. Providing an IO
46 /// worker will ensure that realization of these
47 /// snapshots happens on a worker thread instead of the
48 /// calling thread. Note that the work done to realize
49 /// the snapshots may occur after this call returns. If
50 /// is the embedder's responsibility to make sure the
51 /// serial worker is kept alive for the lifetime of the
52 /// shell associated with the engine that this run
53 /// configuration is given to.
54 /// @param[in] launch_type Whether to launch the new isolate into an existing
55 /// group or a new one.
56 ///
57 /// @return A run configuration. Depending on the completeness of the
58 /// settings, This object may potentially be invalid.
59 ///
61 const Settings& settings,
62 const fml::RefPtr<fml::TaskRunner>& io_worker = nullptr,
64
65 //----------------------------------------------------------------------------
66 /// @brief Creates a run configuration with only an isolate
67 /// configuration. There is no asset manager and default
68 /// entrypoint and root library are used ("main" in root library).
69 ///
70 /// @param[in] configuration The configuration
71 ///
72 explicit RunConfiguration(
73 std::unique_ptr<IsolateConfiguration> configuration);
74
75 //----------------------------------------------------------------------------
76 /// @brief Creates a run configuration with the specified isolate
77 /// configuration and asset manager. The default entrypoint and
78 /// root library are used ("main" in root library).
79 ///
80 /// @param[in] configuration The configuration
81 /// @param[in] asset_manager The asset manager
82 ///
83 RunConfiguration(std::unique_ptr<IsolateConfiguration> configuration,
84 std::shared_ptr<AssetManager> asset_manager);
85
86 //----------------------------------------------------------------------------
87 /// @brief Run configurations cannot be copied because it may not always
88 /// be possible to copy the underlying isolate snapshots. If
89 /// multiple run configurations share the same underlying
90 /// snapshots, creating a configuration from isolate snapshots
91 /// sharing the same underlying buffers is recommended.
92 ///
93 /// @param config The run configuration to move.
94 ///
96
97 //----------------------------------------------------------------------------
98 /// @brief There are no threading restrictions on the destruction of the
99 /// run configuration.
100 ///
102
103 //----------------------------------------------------------------------------
104 /// @brief A valid run configuration only guarantees that the engine
105 /// should be able to find the assets and the isolate snapshots
106 /// when it attempts to launch the root isolate. The validity of
107 /// the snapshot cannot be determined yet. That determination can
108 /// only be made when the configuration is used to run the root
109 /// isolate in the engine. However, the engine will always reject
110 /// an invalid run configuration.
111 ///
112 /// @attention A valid run configuration does not mean that the root isolate
113 /// will always be launched. It only indicates that the various
114 /// snapshots are isolate snapshots and asset managers are present
115 /// and accounted for. The validity of the snapshots will only be
116 /// checked when the engine attempts to launch the root isolate.
117 ///
118 /// @return Returns whether the snapshots and asset manager registrations
119 /// are valid.
120 ///
121 bool IsValid() const;
122
123 //----------------------------------------------------------------------------
124 /// @brief Asset managers maintain a list of resolvers that are checked
125 /// in order when attempting to locate an asset. This method adds
126 /// a resolver to the end of the list.
127 ///
128 /// @param[in] resolver The asset resolver to add to the engine of the list
129 /// resolvers maintained by the asset manager.
130 ///
131 /// @return Returns whether the resolver was successfully registered. The
132 /// resolver must be valid for its registration to be successful.
133 ///
134 bool AddAssetResolver(std::unique_ptr<AssetResolver> resolver);
135
136 //----------------------------------------------------------------------------
137 /// @brief Updates the main application entrypoint. If this is not set,
138 /// the
139 /// "main" method is used as the entrypoint.
140 ///
141 /// @param[in] entrypoint The entrypoint to use.
142 void SetEntrypoint(std::string entrypoint);
143
144 //----------------------------------------------------------------------------
145 /// @brief Specifies the main Dart entrypoint and the library to find
146 /// that entrypoint in. By default, this is the "main" method in
147 /// the root library. The root library may be specified by
148 /// entering the empty string as the second argument.
149 ///
150 /// @see SetEntrypoint()
151 ///
152 /// @param[in] entrypoint The entrypoint
153 /// @param[in] library The library
154 ///
155 void SetEntrypointAndLibrary(std::string entrypoint, std::string library);
156
157 //----------------------------------------------------------------------------
158 /// @brief Updates the main application entrypoint arguments.
159 ///
160 /// @param[in] entrypoint_args The entrypoint arguments to use.
161 void SetEntrypointArgs(std::vector<std::string> entrypoint_args);
162
163 //----------------------------------------------------------------------------
164 /// @return The asset manager referencing all previously registered asset
165 /// resolvers.
166 ///
167 std::shared_ptr<AssetManager> GetAssetManager() const;
168
169 //----------------------------------------------------------------------------
170 /// @return The main Dart entrypoint to be used for the root isolate.
171 ///
172 const std::string& GetEntrypoint() const;
173
174 //----------------------------------------------------------------------------
175 /// @return The name of the library in which the main entrypoint resides.
176 /// If empty, the root library is used.
177 ///
178 const std::string& GetEntrypointLibrary() const;
179
180 //----------------------------------------------------------------------------
181 /// @return Arguments passed as a List<String> to Dart's entrypoint
182 /// function.
183 ///
184 const std::vector<std::string>& GetEntrypointArgs() const;
185
186 //----------------------------------------------------------------------------
187 /// @brief The engine uses this to take the isolate configuration from
188 /// the run configuration. The run configuration is no longer
189 /// valid after this call is made. The non-copyable nature of some
190 /// of the snapshots referenced in the isolate configuration is
191 /// why the run configuration as a whole is not copyable.
192 ///
193 /// @return The run configuration if one is present.
194 ///
195 std::unique_ptr<IsolateConfiguration> TakeIsolateConfiguration();
196
197 private:
198 std::unique_ptr<IsolateConfiguration> isolate_configuration_;
199 std::shared_ptr<AssetManager> asset_manager_;
200 std::string entrypoint_ = "main";
201 std::string entrypoint_library_ = "";
202 std::vector<std::string> entrypoint_args_;
203
204 FML_DISALLOW_COPY_AND_ASSIGN(RunConfiguration);
205};
206
207} // namespace flutter
208
209#endif // FLUTTER_SHELL_COMMON_RUN_CONFIGURATION_H_
Specifies all the configuration required by the runtime library to launch the root isolate....
std::unique_ptr< IsolateConfiguration > TakeIsolateConfiguration()
The engine uses this to take the isolate configuration from the run configuration....
std::shared_ptr< AssetManager > GetAssetManager() const
const std::string & GetEntrypoint() const
void SetEntrypointArgs(std::vector< std::string > entrypoint_args)
Updates the main application entrypoint arguments.
const std::vector< std::string > & GetEntrypointArgs() const
void SetEntrypointAndLibrary(std::string entrypoint, std::string library)
Specifies the main Dart entrypoint and the library to find that entrypoint in. By default,...
bool AddAssetResolver(std::unique_ptr< AssetResolver > resolver)
Asset managers maintain a list of resolvers that are checked in order when attempting to locate an as...
bool IsValid() const
A valid run configuration only guarantees that the engine should be able to find the assets and the i...
void SetEntrypoint(std::string entrypoint)
Updates the main application entrypoint. If this is not set, the "main" method is used as the entrypo...
RunConfiguration(RunConfiguration &&config)
Run configurations cannot be copied because it may not always be possible to copy the underlying isol...
RunConfiguration(std::unique_ptr< IsolateConfiguration > configuration)
Creates a run configuration with only an isolate configuration. There is no asset manager and default...
~RunConfiguration()
There are no threading restrictions on the destruction of the run configuration.
const std::string & GetEntrypointLibrary() const
static RunConfiguration InferFromSettings(const Settings &settings, const fml::RefPtr< fml::TaskRunner > &io_worker=nullptr, IsolateLaunchType launch_type=IsolateLaunchType::kNewGroup)
Attempts to infer a run configuration from the settings object. This tries to create a run configurat...
@ kNewGroup
The isolate is launched as a solo isolate or to start a new group.