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

Side by Side Diff: sky/tools/packager/loader.cc

Issue 1178213003: Introduce sky_packager (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 6 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 | « sky/tools/packager/loader.h ('k') | sky/tools/packager/logging.h » ('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 "sky/tools/packager/loader.h"
6
7 #include <utility>
8
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/strings/string_util.h"
14 #include "sky/tools/packager/logging.h"
15 #include "sky/tools/packager/scope.h"
16 #include "sky/tools/packager/switches.h"
17
18 namespace {
19
20 static const char kSkyInternalsLibraryName[] = "dart:sky.internals";
21 static const char kSkyInternalsURL[] = "package:sky/internals.dart";
22
23 std::string Fetch(const std::string& url) {
24 base::FilePath path(url);
25 std::string source;
26 CHECK(base::ReadFileToString(path, &source)) << url;
27 return source;
28 }
29
30 base::FilePath SimplifyPath(const base::FilePath& path) {
31 std::vector<base::FilePath::StringType> components;
32 path.GetComponents(&components);
33 base::FilePath result;
34 for (const auto& component : components) {
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(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(base::FilePath package_root)
61 : package_root_(std::move(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 = StringFromDart(url);
72 if (StartsWithASCII(string, "dart:", true))
73 return url;
74 if (StartsWithASCII(string, "package:", true))
75 return StringToDart(CanonicalizePackageURL(std::move(string)));
76 base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library)));
77 base::FilePath resolved_path = base_path.DirName().Append(string);
78 base::FilePath normalized_path = SimplifyPath(resolved_path);
79 return StringToDart(normalized_path.AsUTF8Unsafe());
80 }
81
82 Dart_Handle Loader::Import(Dart_Handle url) {
83 Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
84 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
85 LogIfError(result);
86 return result;
87 }
88
89 Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
90 Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
91 Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
92 LogIfError(result);
93 return result;
94 }
95
96 Loader* g_loader = nullptr;
97
98 Loader& GetLoader() {
99 if (!g_loader) {
100 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
101 CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root";
102 g_loader = new Loader(command_line.GetSwitchValuePath(kPackageRoot));
103 }
104 return *g_loader;
105 }
106
107 } // namespace
108
109 Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
110 Dart_Handle library,
111 Dart_Handle url) {
112 CHECK(Dart_IsLibrary(library));
113 CHECK(Dart_IsString(url));
114
115 if (tag == Dart_kCanonicalizeUrl)
116 return GetLoader().CanonicalizeURL(library, url);
117
118 if (tag == Dart_kImportTag)
119 return GetLoader().Import(url);
120
121 if (tag == Dart_kSourceTag)
122 return GetLoader().Source(library, url);
123
124 return Dart_NewApiError("Unknown library tag.");
125 }
126
127 void LoadSkyInternals() {
128 DartApiScope api_scope;
129
130 Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName);
131 std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL);
132 LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0));
133 }
134
135 void LoadScript(std::string url) {
136 LogIfError(
137 Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0));
138 }
OLDNEW
« no previous file with comments | « sky/tools/packager/loader.h ('k') | sky/tools/packager/logging.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698