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 LIB_TONIC_FILE_LOADER_FILE_LOADER_H_
6#define LIB_TONIC_FILE_LOADER_FILE_LOADER_H_
7
8#include <memory>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "third_party/dart/runtime/include/dart_api.h"
14#include "tonic/common/macros.h"
15#include "tonic/parsers/packages_map.h"
16
17namespace tonic {
18
19class FileLoader {
20 public:
21 FileLoader(int dirfd = -1);
22 ~FileLoader();
23
24 bool LoadPackagesMap(const std::string& packages);
25
26 // The path to the `.packages` file the packages map was loaded from.
27 const std::string& packages() const { return packages_; }
28
29 Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
30 Dart_Handle library,
31 Dart_Handle url);
32
33 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
34 Dart_Handle Import(Dart_Handle url);
35 Dart_Handle Kernel(Dart_Handle url);
36 void SetPackagesUrl(Dart_Handle url);
37
38 Dart_Handle FetchBytes(const std::string& url,
39 uint8_t*& buffer,
40 intptr_t& buffer_size);
41
42 static const char kFileURLPrefix[];
43 static const size_t kFileURLPrefixLength;
44 static const std::string kPathSeparator;
45
46 private:
47 static std::string SanitizeURIEscapedCharacters(const std::string& str);
48 static std::string SanitizePath(const std::string& path);
49 static std::string CanonicalizeFileURL(const std::string& url);
50
51 std::string GetFilePathForURL(std::string url);
52 std::string GetFilePathForPackageURL(std::string url);
53 std::string GetFilePathForFileURL(std::string url);
54
55 std::string GetFileURLForPath(const std::string& path);
56
57 bool ReadFileToString(const std::string& path, std::string* result);
58 std::pair<uint8_t*, intptr_t> ReadFileToBytes(const std::string& path);
59
60 int dirfd_;
61 std::string packages_;
62 std::unique_ptr<PackagesMap> packages_map_;
63 std::vector<uint8_t*> kernel_buffers_;
64
65 TONIC_DISALLOW_COPY_AND_ASSIGN(FileLoader);
66};
67
68} // namespace tonic
69
70#endif // LIB_TONIC_FILE_LOADER_FILE_LOADER_H_
71