Flutter Engine
 
Loading...
Searching...
No Matches
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}
static const std::string kPathSeparator
Definition file_loader.h:44
std::string GetDirectoryName(const std::string &path)
std::string SimplifyPath(std::string path)
Definition path_posix.cc:48
Dart_Handle StdStringToDart(const std::string &val)
std::string StdStringFromDart(Dart_Handle handle)

References filesystem::GetDirectoryName(), kPathSeparator, filesystem::SimplifyPath(), tonic::StdStringFromDart(), and tonic::StdStringToDart().

Referenced by HandleLibraryTag().

◆ 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 + "'.";
170 return Dart_NewUnhandledExceptionError(
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 + "'.";
178 return Dart_NewUnhandledExceptionError(
179 Dart_NewStringFromCString(error_message.c_str()));
180 }
181 buffer = result.first;
182 buffer_size = result.second;
183 return Dart_True();
184}
std::string GetAbsoluteFilePath(const std::string &path)
std::pair< uint8_t *, intptr_t > ReadFileToBytes(const std::string &path)
Definition file.cc:95
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 switch_defs.h:52
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98

References filesystem::GetAbsoluteFilePath(), filesystem::ReadFileToBytes(), and filesystem::SimplifyPath().

Referenced by Import(), and Kernel().

◆ 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));
123 TONIC_DCHECK(Dart_IsString(url));
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)
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url)
Dart_Handle Kernel(Dart_Handle url)
#define TONIC_DCHECK
Definition macros.h:32

References CanonicalizeURL(), Import(), Kernel(), and TONIC_DCHECK.

Referenced by tonic::DartState::HandleLibraryTag().

◆ 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;
190 Dart_Handle result = FetchBytes(url_string, buffer, buffer_size);
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);
196 return Dart_LoadLibraryFromKernel(buffer, buffer_size);
197}
Dart_Handle FetchBytes(const std::string &url, uint8_t *&buffer, intptr_t &buffer_size)

References FetchBytes(), and tonic::StdStringFromDart().

Referenced by HandleLibraryTag().

◆ 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;
209 Dart_Handle result = FetchBytes(url_string, buffer, buffer_size);
210 if (Dart_IsError(result)) {
211 return result;
212 }
213 result =
214 Dart_NewExternalTypedData(Dart_TypedData_kUint8, buffer, buffer_size);
215 Dart_NewFinalizableHandle(result, buffer, buffer_size, MallocFinalizer);
216 return result;
217}

References FetchBytes(), and tonic::StdStringFromDart().

Referenced by HandleLibraryTag().

◆ 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

References error, tonic::Log(), and packages().

Referenced by SetPackagesUrl().

◆ packages()

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

Definition at line 27 of file file_loader.h.

27{ return packages_; }

Referenced by LoadPackagesMap(), and SetPackagesUrl().

◆ 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)

References LoadPackagesMap(), packages(), and tonic::StdStringFromDart().

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.

Referenced by CanonicalizeURL().


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