Flutter Engine
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)

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.

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

Referenced by HandleLibraryTag(), and packages().

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  if (string.find(kFileScheme) == 0u)
140  return StdStringToDart(SanitizePath(CanonicalizeFileURL(string)));
141 
142  std::string library_url = StdStringFromDart(Dart_LibraryUrl(library));
143  std::string prefix = ExtractSchemePrefix(library_url);
144  std::string base_path = ExtractPath(library_url);
145  std::string simplified_path =
147  FileLoader::kPathSeparator + string);
148  return StdStringToDart(SanitizePath(prefix + simplified_path));
149 }
DEF_SWITCHES_START snapshot asset Path to the directory containing the four files specified by VmSnapshotInstructions and IsolateSnapshotInstructions vm snapshot The VM instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present isolate snapshot The isolate instructions snapshot that will be memory mapped as read and executable SnapshotAssetPath must be present icu symbol prefix
Definition: switches.h:59
static const std::string kPathSeparator
Definition: file_loader.h:44
std::string StdStringFromDart(Dart_Handle handle)
std::string GetDirectoryName(const std::string &path)
Definition: path_posix.cc:160
Dart_Handle StdStringToDart(const std::string &val)
std::string SimplifyPath(std::string path)
Definition: path_posix.cc:48

◆ FetchBytes()

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

Definition at line 159 of file file_loader.cc.

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

Referenced by Import(), Kernel(), and packages().

161  {
162  buffer = nullptr;
163  buffer_size = -1;
164 
165  std::string path = filesystem::SimplifyPath(GetFilePathForURL(url));
166  if (path.empty()) {
167  std::string error_message = "error: Unable to read '" + url + "'.";
168  return Dart_NewUnhandledExceptionError(
169  Dart_NewStringFromCString(error_message.c_str()));
170  }
171  std::string absolute_path = filesystem::GetAbsoluteFilePath(path);
172  auto result = filesystem::ReadFileToBytes(absolute_path);
173  if (result.first == nullptr) {
174  std::string error_message =
175  "error: Unable to read '" + absolute_path + "'.";
176  return Dart_NewUnhandledExceptionError(
177  Dart_NewStringFromCString(error_message.c_str()));
178  }
179  buffer = result.first;
180  buffer_size = result.second;
181  return Dart_True();
182 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::pair< uint8_t *, intptr_t > ReadFileToBytes(const std::string &path)
Definition: file.cc:94
std::string GetAbsoluteFilePath(const std::string &path)
Definition: path_posix.cc:176
std::string SimplifyPath(std::string path)
Definition: path_posix.cc:48

◆ HandleLibraryTag()

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

Definition at line 118 of file file_loader.cc.

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

Referenced by tonic::DartState::HandleLibraryTag(), and packages().

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 }
#define TONIC_DCHECK
Definition: macros.h:32
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url)
Definition: file_loader.cc:133
Dart_Handle Kernel(Dart_Handle url)
Definition: file_loader.cc:203
Dart_Handle Import(Dart_Handle url)
Definition: file_loader.cc:184

◆ Import()

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

Definition at line 184 of file file_loader.cc.

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

Referenced by HandleLibraryTag(), and packages().

184  {
185  std::string url_string = StdStringFromDart(url);
186  uint8_t* buffer = nullptr;
187  intptr_t buffer_size = -1;
188  Dart_Handle result = FetchBytes(url_string, buffer, buffer_size);
189  if (Dart_IsError(result)) {
190  return result;
191  }
192  // The embedder must keep the buffer alive until isolate shutdown.
193  kernel_buffers_.push_back(buffer);
194  return Dart_LoadLibraryFromKernel(buffer, buffer_size);
195 }
std::string StdStringFromDart(Dart_Handle handle)
Dart_Handle FetchBytes(const std::string &url, uint8_t *&buffer, intptr_t &buffer_size)
Definition: file_loader.cc:159

◆ Kernel()

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

Definition at line 203 of file file_loader.cc.

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

Referenced by HandleLibraryTag(), and packages().

203  {
204  std::string url_string = StdStringFromDart(url);
205  uint8_t* buffer = nullptr;
206  intptr_t buffer_size = -1;
207  Dart_Handle result = FetchBytes(url_string, buffer, buffer_size);
208  if (Dart_IsError(result)) {
209  return result;
210  }
211  result =
212  Dart_NewExternalTypedData(Dart_TypedData_kUint8, buffer, buffer_size);
213  Dart_NewFinalizableHandle(result, buffer, buffer_size, MallocFinalizer);
214  return result;
215 }
std::string StdStringFromDart(Dart_Handle handle)
Dart_Handle FetchBytes(const std::string &url, uint8_t *&buffer, intptr_t &buffer_size)
Definition: file_loader.cc:159

◆ LoadPackagesMap()

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

Definition at line 79 of file file_loader.cc.

References filesystem::AbsolutePath(), error, filesystem::GetDirectoryName(), kFileURLPrefix, kFileURLPrefixLength, kPathSeparator, tonic::Log(), packages(), and TONIC_DCHECK.

Referenced by SetPackagesUrl().

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
void Log(const char *format,...)
Definition: log.cc:19
FlMethodResponse GError ** error
bool ReadFileToString(const std::string &path, std::string *result)
Definition: files.cc:56

◆ packages()

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

Definition at line 27 of file file_loader.h.

References CanonicalizeURL(), FetchBytes(), HandleLibraryTag(), Import(), Kernel(), and SetPackagesUrl().

Referenced by LoadPackagesMap(), and SetPackagesUrl().

27 { return packages_; }

◆ SetPackagesUrl()

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

Definition at line 218 of file file_loader.cc.

References kFileURLPrefix, kFileURLPrefixLength, LoadPackagesMap(), packages(), flutter::path, tonic::StdStringFromDart(), and TONIC_DCHECK.

Referenced by packages().

218  {
219  if (url == Dart_Null()) {
220  // No packages url specified.
222  return;
223  }
224  const std::string& packages_url = StdStringFromDart(url);
225  LoadPackagesMap(packages_url);
226 }
const std::string & packages() const
Definition: file_loader.h:27
bool LoadPackagesMap(const std::string &packages)
Definition: file_loader.cc:79
std::string StdStringFromDart(Dart_Handle handle)

Member Data Documentation

◆ kFileURLPrefix

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

Definition at line 42 of file file_loader.h.

Referenced by LoadPackagesMap(), and SetPackagesUrl().

◆ kFileURLPrefixLength

const size_t tonic::FileLoader::kFileURLPrefixLength
static
Initial value:

Definition at line 43 of file file_loader.h.

Referenced by LoadPackagesMap(), and SetPackagesUrl().

◆ kPathSeparator

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

Definition at line 44 of file file_loader.h.

Referenced by CanonicalizeURL(), and LoadPackagesMap().


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