Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: mojo/dart/dart_snapshotter/loader.cc

Issue 1273743005: Dart: Adds a program to create snapshots of Mojo apps. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Fix asan build Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/dart/dart_snapshotter/loader.h ('k') | mojo/dart/dart_snapshotter/main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "tonic/dart_converter.h"
13 #include "tonic/dart_error.h"
14
15 namespace {
16
17 std::string Fetch(const std::string& url) {
18 base::FilePath path(url);
19 std::string source;
20 CHECK(base::ReadFileToString(path, &source)) << url;
21 return source;
22 }
23
24 base::FilePath SimplifyPath(const base::FilePath& path) {
25 std::vector<base::FilePath::StringType> components;
26 path.GetComponents(&components);
27 base::FilePath result;
28 if (path.IsAbsolute()) {
29 base::FilePath root("/");
30 result = root;
31 }
32 for (const auto& component : components) {
33 base::FilePath c(component);
34 if (c.IsAbsolute()) continue;
35 if (component == base::FilePath::kCurrentDirectory)
36 continue;
37 if (component == base::FilePath::kParentDirectory)
38 result = result.DirName();
39 else
40 result = result.Append(component);
41 }
42 return result;
43 }
44
45 class Loader {
46 public:
47 Loader(const base::FilePath& package_root);
48
49 std::string CanonicalizePackageURL(std::string url);
50 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
51 Dart_Handle Import(Dart_Handle url);
52 Dart_Handle Source(Dart_Handle library, Dart_Handle url);
53
54 private:
55 base::FilePath package_root_;
56
57 DISALLOW_COPY_AND_ASSIGN(Loader);
58 };
59
60 Loader::Loader(const base::FilePath& package_root)
61 : package_root_(package_root) {
62 }
63
64 std::string Loader::CanonicalizePackageURL(std::string url) {
65 DCHECK(StartsWithASCII(url, "package:", true));
66 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
67 return package_root_.Append(url).AsUTF8Unsafe();
68 }
69
70 Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
71 std::string string = tonic::StdStringFromDart(url);
72 if (StartsWithASCII(string, "dart:", true))
73 return url;
74 if (StartsWithASCII(string, "package:", true))
75 return tonic::StdStringToDart(CanonicalizePackageURL(string));
76 base::FilePath base_path(tonic::StdStringFromDart(Dart_LibraryUrl(library)));
77 base::FilePath resolved_path = base_path.DirName().Append(string);
78 base::FilePath normalized_path = SimplifyPath(resolved_path);
79 return tonic::StdStringToDart(normalized_path.AsUTF8Unsafe());
80 }
81
82 Dart_Handle Loader::Import(Dart_Handle url) {
83 Dart_Handle source =
84 tonic::StdStringToDart(Fetch(tonic::StdStringFromDart(url)));
85 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
86 tonic::LogIfError(result);
87 return result;
88 }
89
90 Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
91 Dart_Handle source =
92 tonic::StdStringToDart(Fetch(tonic::StdStringFromDart(url)));
93 Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
94 tonic::LogIfError(result);
95 return result;
96 }
97
98 Loader* g_loader = nullptr;
99
100 Loader& GetLoader() {
101 CHECK(g_loader != nullptr);
102 return *g_loader;
103 }
104
105 } // namespace
106
107 void InitLoader(const base::FilePath& package_root) {
108 CHECK(g_loader == nullptr);
109 g_loader = new Loader(package_root);
110 }
111
112 Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
113 Dart_Handle library,
114 Dart_Handle url) {
115 CHECK(Dart_IsLibrary(library));
116 CHECK(Dart_IsString(url));
117
118 if (tag == Dart_kCanonicalizeUrl)
119 return GetLoader().CanonicalizeURL(library, url);
120
121 if (tag == Dart_kImportTag)
122 return GetLoader().Import(url);
123
124 if (tag == Dart_kSourceTag)
125 return GetLoader().Source(library, url);
126
127 return Dart_NewApiError("Unknown library tag.");
128 }
129
130 void LoadScript(const std::string& url) {
131 tonic::LogIfError(Dart_LoadScript(
132 tonic::StdStringToDart(url), tonic::StdStringToDart(Fetch(url)), 0, 0));
133 }
OLDNEW
« no previous file with comments | « mojo/dart/dart_snapshotter/loader.h ('k') | mojo/dart/dart_snapshotter/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698