Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Attributes | List of all members
tonic::FileLoader Class Reference

#include <file_loader.h>

Public Member Functions

 FileLoader (int dirfd=-1)
 
 ~FileLoader ()
 
bool LoadPackagesMap (const std::string &packages)
 
const std::string & packages () const
 
Dart_Handle HandleLibraryTag (Dart_LibraryTag tag, Dart_Handle library, Dart_Handle url)
 
Dart_Handle CanonicalizeURL (Dart_Handle library, Dart_Handle url)
 
Dart_Handle Import (Dart_Handle url)
 
Dart_Handle Kernel (Dart_Handle url)
 
void SetPackagesUrl (Dart_Handle url)
 
Dart_Handle FetchBytes (const std::string &url, uint8_t *&buffer, intptr_t &buffer_size)
 

Static Public Attributes

static const char kFileURLPrefix [] = "file://"
 
static const size_t kFileURLPrefixLength
 
static const std::string kPathSeparator = "/"
 

Detailed Description

Definition at line 19 of file file_loader.h.

Constructor & Destructor Documentation

◆ FileLoader()

tonic::FileLoader::FileLoader ( int  dirfd = -1)
explicit

Definition at line 49 of file file_loader.cc.

49: dirfd_(dirfd) {}

◆ ~FileLoader()

tonic::FileLoader::~FileLoader ( )

Definition at line 51 of file file_loader.cc.

51 {
52 for (auto kernel_buffer : kernel_buffers_)
53 free(kernel_buffer);
54
55 if (dirfd_ >= 0)
56 close(dirfd_);
57}

Member Function Documentation

◆ CanonicalizeURL()

Dart_Handle tonic::FileLoader::CanonicalizeURL ( Dart_Handle  library,
Dart_Handle  url 
)

Definition at line 133 of file file_loader.cc.

133 {
134 std::string string = StdStringFromDart(url);
135 if (string.find(kDartScheme) == 0u)
136 return url;
137 if (string.find(kPackageScheme) == 0u)
138 return StdStringToDart(SanitizePath(string));
139
140 if (string.find(kFileScheme) == 0u) {
141 return StdStringToDart(SanitizeURIEscapedCharacters(string));
142 }
143
144 std::string library_url = StdStringFromDart(Dart_LibraryUrl(library));
145 std::string prefix = ExtractSchemePrefix(library_url);
146 std::string base_path = ExtractPath(library_url);
147 std::string simplified_path =
150 return StdStringToDart(SanitizePath(prefix + simplified_path));
151}
int find(T *array, int N, T item)
static const std::string kPathSeparator
Definition: file_loader.h:44
DART_EXPORT Dart_Handle Dart_LibraryUrl(Dart_Handle library)
std::string GetDirectoryName(const std::string &path)
Definition: path_posix.cc:160
std::string SimplifyPath(std::string path)
Definition: path_posix.cc:48
Dart_Handle StdStringToDart(const std::string &val)
std::string StdStringFromDart(Dart_Handle handle)

◆ FetchBytes()

Dart_Handle tonic::FileLoader::FetchBytes ( const std::string &  url,
uint8_t *&  buffer,
intptr_t &  buffer_size 
)

Definition at line 161 of file file_loader.cc.

163 {
164 buffer = nullptr;
165 buffer_size = -1;
166
167 std::string path = filesystem::SimplifyPath(GetFilePathForURL(url));
168 if (path.empty()) {
169 std::string error_message = "error: Unable to read '" + url + "'.";
171 Dart_NewStringFromCString(error_message.c_str()));
172 }
173 std::string absolute_path = filesystem::GetAbsoluteFilePath(path);
174 auto result = filesystem::ReadFileToBytes(absolute_path);
175 if (result.first == nullptr) {
176 std::string error_message =
177 "error: Unable to read '" + absolute_path + "'.";
179 Dart_NewStringFromCString(error_message.c_str()));
180 }
181 buffer = result.first;
182 buffer_size = result.second;
183 return Dart_True();
184}
static uint32_t buffer_size(uint32_t offset, uint32_t maxAlignment)
DART_EXPORT Dart_Handle Dart_True(void)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception)
GAsyncResult * result
std::string GetAbsoluteFilePath(const std::string &path)
Definition: path_posix.cc:176
std::pair< uint8_t *, intptr_t > ReadFileToBytes(const std::string &path)
Definition: file.cc:94
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126

◆ HandleLibraryTag()

Dart_Handle tonic::FileLoader::HandleLibraryTag ( Dart_LibraryTag  tag,
Dart_Handle  library,
Dart_Handle  url 
)

Definition at line 118 of file file_loader.cc.

120 {
121 TONIC_DCHECK(Dart_IsNull(library) || Dart_IsLibrary(library) ||
122 Dart_IsString(library));
124 if (tag == Dart_kCanonicalizeUrl)
125 return CanonicalizeURL(library, url);
126 if (tag == Dart_kKernelTag)
127 return Kernel(url);
128 if (tag == Dart_kImportTag)
129 return Import(url);
130 return Dart_NewApiError("Unknown library tag.");
131}
Dart_Handle Import(Dart_Handle url)
Definition: file_loader.cc:186
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url)
Definition: file_loader.cc:133
Dart_Handle Kernel(Dart_Handle url)
Definition: file_loader.cc:205
DART_EXPORT Dart_Handle Dart_NewApiError(const char *error)
@ Dart_kImportTag
Definition: dart_api.h:3421
@ Dart_kCanonicalizeUrl
Definition: dart_api.h:3420
@ Dart_kKernelTag
Definition: dart_api.h:3422
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
DART_EXPORT bool Dart_IsString(Dart_Handle object)
DART_EXPORT bool Dart_IsLibrary(Dart_Handle object)
#define TONIC_DCHECK
Definition: macros.h:32

◆ Import()

Dart_Handle tonic::FileLoader::Import ( Dart_Handle  url)

Definition at line 186 of file file_loader.cc.

186 {
187 std::string url_string = StdStringFromDart(url);
188 uint8_t* buffer = nullptr;
189 intptr_t buffer_size = -1;
191 if (Dart_IsError(result)) {
192 return result;
193 }
194 // The embedder must keep the buffer alive until isolate shutdown.
195 kernel_buffers_.push_back(buffer);
197}
Dart_Handle FetchBytes(const std::string &url, uint8_t *&buffer, intptr_t &buffer_size)
Definition: file_loader.cc:161
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_LoadLibraryFromKernel(const uint8_t *kernel_buffer, intptr_t kernel_buffer_size)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)

◆ Kernel()

Dart_Handle tonic::FileLoader::Kernel ( Dart_Handle  url)

Definition at line 205 of file file_loader.cc.

205 {
206 std::string url_string = StdStringFromDart(url);
207 uint8_t* buffer = nullptr;
208 intptr_t buffer_size = -1;
210 if (Dart_IsError(result)) {
211 return result;
212 }
213 result =
216 return result;
217}
DART_EXPORT Dart_FinalizableHandle Dart_NewFinalizableHandle(Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT Dart_Handle Dart_NewExternalTypedData(Dart_TypedData_Type type, void *data, intptr_t length)
@ Dart_TypedData_kUint8
Definition: dart_api.h:2615
static void MallocFinalizer(void *isolate_callback_data, void *peer)

◆ LoadPackagesMap()

bool tonic::FileLoader::LoadPackagesMap ( const std::string &  packages)

Definition at line 79 of file file_loader.cc.

79 {
80 packages_ = packages;
81 std::string packages_source;
82 if (!ReadFileToString(packages_, &packages_source)) {
83 tonic::Log("error: Unable to load .packages file '%s'.", packages_.c_str());
84 return false;
85 }
86 packages_map_.reset(new PackagesMap());
87 std::string error;
88 if (!packages_map_->Parse(packages_source, &error)) {
89 tonic::Log("error: Unable to parse .packages file '%s'. %s",
90 packages_.c_str(), error.c_str());
91 return false;
92 }
93 return true;
94}
const std::string & packages() const
Definition: file_loader.h:27
const uint8_t uint32_t uint32_t GError ** error
void Log(const char *format,...)
Definition: log.cc:19

◆ packages()

const std::string & tonic::FileLoader::packages ( ) const
inline

Definition at line 27 of file file_loader.h.

27{ return packages_; }

◆ SetPackagesUrl()

void tonic::FileLoader::SetPackagesUrl ( Dart_Handle  url)

Definition at line 220 of file file_loader.cc.

220 {
221 if (url == Dart_Null()) {
222 // No packages url specified.
224 return;
225 }
226 const std::string& packages_url = StdStringFromDart(url);
227 LoadPackagesMap(packages_url);
228}
bool LoadPackagesMap(const std::string &packages)
Definition: file_loader.cc:79
DART_EXPORT Dart_Handle Dart_Null(void)

Member Data Documentation

◆ kFileURLPrefix

const char tonic::FileLoader::kFileURLPrefix = "file://"
static

Definition at line 42 of file file_loader.h.

◆ kFileURLPrefixLength

const size_t tonic::FileLoader::kFileURLPrefixLength
static
Initial value:
=
static const char kFileURLPrefix[]
Definition: file_loader.h:42

Definition at line 43 of file file_loader.h.

◆ kPathSeparator

const std::string tonic::FileLoader::kPathSeparator = "/"
static

Definition at line 44 of file file_loader.h.


The documentation for this class was generated from the following files: