OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium 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 #include "mojo/dart/dart_snapshotter/loader.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "mojo/dart/dart_snapshotter/logging.h" | |
13 #include "mojo/dart/dart_snapshotter/scope.h" | |
14 #include "mojo/dart/dart_snapshotter/switches.h" | |
15 | |
16 namespace { | |
17 | |
18 std::string Fetch(const std::string& url) { | |
19 base::FilePath path(url); | |
20 std::string source; | |
21 CHECK(base::ReadFileToString(path, &source)) << url; | |
22 return source; | |
23 } | |
24 | |
25 base::FilePath SimplifyPath(const base::FilePath& path) { | |
26 //if (path.IsAbsolute()) return path; | |
Cutch
2015/08/10 20:45:02
Un-comment or remove before committing
zra
2015/08/10 22:59:21
Done.
| |
27 std::vector<base::FilePath::StringType> components; | |
28 path.GetComponents(&components); | |
29 base::FilePath result; | |
30 if (path.IsAbsolute()) { | |
31 base::FilePath root("/"); | |
32 result = root; | |
33 } | |
34 for (const auto& component : components) { | |
35 base::FilePath c(component); | |
36 if (c.IsAbsolute()) continue; | |
37 if (component == base::FilePath::kCurrentDirectory) | |
38 continue; | |
39 if (component == base::FilePath::kParentDirectory) | |
40 result = result.DirName(); | |
41 else | |
42 result = result.Append(component); | |
43 } | |
44 return result; | |
45 } | |
46 | |
47 class Loader { | |
48 public: | |
49 Loader(const base::FilePath& package_root); | |
50 | |
51 std::string CanonicalizePackageURL(std::string url); | |
52 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); | |
53 Dart_Handle Import(Dart_Handle url); | |
54 Dart_Handle Source(Dart_Handle library, Dart_Handle url); | |
55 | |
56 private: | |
57 base::FilePath package_root_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(Loader); | |
60 }; | |
61 | |
62 Loader::Loader(const base::FilePath& package_root) | |
63 : package_root_(package_root) { | |
64 } | |
65 | |
66 std::string Loader::CanonicalizePackageURL(std::string url) { | |
67 DCHECK(StartsWithASCII(url, "package:", true)); | |
68 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); | |
69 return package_root_.Append(url).AsUTF8Unsafe(); | |
70 } | |
71 | |
72 Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { | |
73 std::string string = StringFromDart(url); | |
74 if (StartsWithASCII(string, "dart:", true)) | |
75 return url; | |
76 if (StartsWithASCII(string, "package:", true)) | |
77 return StringToDart(CanonicalizePackageURL(string)); | |
78 base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library))); | |
79 base::FilePath resolved_path = base_path.DirName().Append(string); | |
80 base::FilePath normalized_path = SimplifyPath(resolved_path); | |
81 return StringToDart(normalized_path.AsUTF8Unsafe()); | |
82 } | |
83 | |
84 Dart_Handle Loader::Import(Dart_Handle url) { | |
85 Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); | |
86 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0); | |
87 LogIfError(result); | |
88 return result; | |
89 } | |
90 | |
91 Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) { | |
92 Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); | |
93 Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0); | |
94 LogIfError(result); | |
95 return result; | |
96 } | |
97 | |
98 Loader* g_loader = nullptr; | |
99 | |
100 Loader& GetLoader() { | |
101 if (!g_loader) { | |
102 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); | |
103 CHECK(command_line.HasSwitch(switches::kPackageRoot)) | |
104 << "Need --package-root"; | |
105 g_loader = | |
106 new Loader(command_line.GetSwitchValuePath(switches::kPackageRoot)); | |
107 } | |
108 return *g_loader; | |
109 } | |
110 | |
111 } // namespace | |
112 | |
113 Dart_Handle HandleLibraryTag(Dart_LibraryTag tag, | |
Cutch
2015/08/10 20:45:02
Why can't you just use:
tonic::DartLibraryLoader:
zra
2015/08/10 22:59:21
I tried doing this. I'm not sure it's a good trade
| |
114 Dart_Handle library, | |
115 Dart_Handle url) { | |
116 CHECK(Dart_IsLibrary(library)); | |
117 CHECK(Dart_IsString(url)); | |
118 | |
119 if (tag == Dart_kCanonicalizeUrl) | |
120 return GetLoader().CanonicalizeURL(library, url); | |
121 | |
122 if (tag == Dart_kImportTag) | |
123 return GetLoader().Import(url); | |
124 | |
125 if (tag == Dart_kSourceTag) | |
126 return GetLoader().Source(library, url); | |
127 | |
128 return Dart_NewApiError("Unknown library tag."); | |
129 } | |
130 | |
131 void LoadScript(const std::string& url) { | |
132 LogIfError( | |
133 Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0)); | |
134 } | |
OLD | NEW |