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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/tools/packager/loader.h ('k') | sky/tools/packager/logging.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/tools/packager/loader.cc
diff --git a/sky/tools/packager/loader.cc b/sky/tools/packager/loader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d130bd231a49c5501eddbdaa9283bc612013c914
--- /dev/null
+++ b/sky/tools/packager/loader.cc
@@ -0,0 +1,138 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "sky/tools/packager/loader.h"
+
+#include <utility>
+
+#include "base/command_line.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/logging.h"
+#include "base/strings/string_util.h"
+#include "sky/tools/packager/logging.h"
+#include "sky/tools/packager/scope.h"
+#include "sky/tools/packager/switches.h"
+
+namespace {
+
+static const char kSkyInternalsLibraryName[] = "dart:sky.internals";
+static const char kSkyInternalsURL[] = "package:sky/internals.dart";
+
+std::string Fetch(const std::string& url) {
+ base::FilePath path(url);
+ std::string source;
+ CHECK(base::ReadFileToString(path, &source)) << url;
+ return source;
+}
+
+base::FilePath SimplifyPath(const base::FilePath& path) {
+ std::vector<base::FilePath::StringType> components;
+ path.GetComponents(&components);
+ base::FilePath result;
+ for (const auto& component : components) {
+ if (component == base::FilePath::kCurrentDirectory)
+ continue;
+ if (component == base::FilePath::kParentDirectory)
+ result = result.DirName();
+ else
+ result = result.Append(component);
+ }
+ return result;
+}
+
+class Loader {
+ public:
+ Loader(base::FilePath package_root);
+
+ std::string CanonicalizePackageURL(std::string url);
+ Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
+ Dart_Handle Import(Dart_Handle url);
+ Dart_Handle Source(Dart_Handle library, Dart_Handle url);
+
+ private:
+ base::FilePath package_root_;
+
+ DISALLOW_COPY_AND_ASSIGN(Loader);
+};
+
+Loader::Loader(base::FilePath package_root)
+ : package_root_(std::move(package_root)) {
+}
+
+std::string Loader::CanonicalizePackageURL(std::string url) {
+ DCHECK(StartsWithASCII(url, "package:", true));
+ ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
+ return package_root_.Append(url).AsUTF8Unsafe();
+}
+
+Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
+ std::string string = StringFromDart(url);
+ if (StartsWithASCII(string, "dart:", true))
+ return url;
+ if (StartsWithASCII(string, "package:", true))
+ return StringToDart(CanonicalizePackageURL(std::move(string)));
+ base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library)));
+ base::FilePath resolved_path = base_path.DirName().Append(string);
+ base::FilePath normalized_path = SimplifyPath(resolved_path);
+ return StringToDart(normalized_path.AsUTF8Unsafe());
+}
+
+Dart_Handle Loader::Import(Dart_Handle url) {
+ Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
+ Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0);
+ LogIfError(result);
+ return result;
+}
+
+Dart_Handle Loader::Source(Dart_Handle library, Dart_Handle url) {
+ Dart_Handle source = StringToDart(Fetch(StringFromDart(url)));
+ Dart_Handle result = Dart_LoadSource(library, url, source, 0, 0);
+ LogIfError(result);
+ return result;
+}
+
+Loader* g_loader = nullptr;
+
+Loader& GetLoader() {
+ if (!g_loader) {
+ base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
+ CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root";
+ g_loader = new Loader(command_line.GetSwitchValuePath(kPackageRoot));
+ }
+ return *g_loader;
+}
+
+} // namespace
+
+Dart_Handle HandleLibraryTag(Dart_LibraryTag tag,
+ Dart_Handle library,
+ Dart_Handle url) {
+ CHECK(Dart_IsLibrary(library));
+ CHECK(Dart_IsString(url));
+
+ if (tag == Dart_kCanonicalizeUrl)
+ return GetLoader().CanonicalizeURL(library, url);
+
+ if (tag == Dart_kImportTag)
+ return GetLoader().Import(url);
+
+ if (tag == Dart_kSourceTag)
+ return GetLoader().Source(library, url);
+
+ return Dart_NewApiError("Unknown library tag.");
+}
+
+void LoadSkyInternals() {
+ DartApiScope api_scope;
+
+ Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName);
+ std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL);
+ LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0));
+}
+
+void LoadScript(std::string url) {
+ LogIfError(
+ Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0));
+}
« 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