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

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: 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
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 "mojo/dart/dart_snapshotter/switches.h"
13 #include "tonic/dart_converter.h"
14 #include "tonic/dart_error.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 std::vector<base::FilePath::StringType> components;
27 path.GetComponents(&components);
28 base::FilePath result;
29 if (path.IsAbsolute()) {
30 base::FilePath root("/");
31 result = root;
32 }
33 for (const auto& component : components) {
34 base::FilePath c(component);
35 if (c.IsAbsolute()) continue;
36 if (component == base::FilePath::kCurrentDirectory)
37 continue;
38 if (component == base::FilePath::kParentDirectory)
39 result = result.DirName();
40 else
41 result = result.Append(component);
42 }
43 return result;
44 }
45
46 class Loader {
47 public:
48 Loader(const base::FilePath& package_root);
49
50 std::string CanonicalizePackageURL(std::string url);
51 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
52 Dart_Handle Import(Dart_Handle url);
53 Dart_Handle Source(Dart_Handle library, Dart_Handle url);
54
55 private:
56 base::FilePath package_root_;
57
58 DISALLOW_COPY_AND_ASSIGN(Loader);
59 };
60
61 Loader::Loader(const base::FilePath& package_root)
62 : package_root_(package_root) {
63 }
64
65 std::string Loader::CanonicalizePackageURL(std::string url) {
66 DCHECK(StartsWithASCII(url, "package:", true));
67 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
68 return package_root_.Append(url).AsUTF8Unsafe();
69 }
70
71 Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
72 std::string string = tonic::StdStringFromDart(url);
73 if (StartsWithASCII(string, "dart:", true))
74 return url;
75 if (StartsWithASCII(string, "package:", true))
76 return tonic::StdStringToDart(CanonicalizePackageURL(string));
77 base::FilePath base_path(tonic::StdStringFromDart(Dart_LibraryUrl(library)));
78 base::FilePath resolved_path = base_path.DirName().Append(string);
79 base::FilePath normalized_path = SimplifyPath(resolved_path);
80 return tonic::StdStringToDart(normalized_path.AsUTF8Unsafe());
81 }
82
83 Dart_Handle Loader::Import(Dart_Handle url) {
84 Dart_Handle source =
85 tonic::StdStringToDart(Fetch(tonic::StdStringFromDart(url)));
86 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
87 tonic::LogIfError(result);
88 return result;
89 }
90
91 Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
92 Dart_Handle source =
93 tonic::StdStringToDart(Fetch(tonic::StdStringFromDart(url)));
94 Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
95 tonic::LogIfError(result);
96 return result;
97 }
98
99 Loader* g_loader = nullptr;
100
101 Loader& GetLoader() {
102 if (!g_loader) {
103 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
104 CHECK(command_line.HasSwitch(switches::kPackageRoot))
105 << "Need --package-root";
106 g_loader =
107 new Loader(command_line.GetSwitchValuePath(switches::kPackageRoot));
108 }
109 return *g_loader;
110 }
111
112 } // namespace
113
114 Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
115 Dart_Handle library,
116 Dart_Handle url) {
117 CHECK(Dart_IsLibrary(library));
118 CHECK(Dart_IsString(url));
119
120 if (tag == Dart_kCanonicalizeUrl)
121 return GetLoader().CanonicalizeURL(library, url);
122
123 if (tag == Dart_kImportTag)
124 return GetLoader().Import(url);
125
126 if (tag == Dart_kSourceTag)
127 return GetLoader().Source(library, url);
128
129 return Dart_NewApiError("Unknown library tag.");
130 }
131
132 void LoadScript(const std::string& url) {
133 tonic::LogIfError(Dart_LoadScript(
134 tonic::StdStringToDart(url), tonic::StdStringToDart(Fetch(url)), 0, 0));
135 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698