| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sky/tools/packager/loader.h" | 5 #include "sky/tools/packager/loader.h" |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 12 #include "base/logging.h" | 10 #include "base/logging.h" |
| 13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 14 #include "sky/tools/packager/logging.h" | 12 #include "sky/tools/packager/logging.h" |
| 15 #include "sky/tools/packager/scope.h" | 13 #include "sky/tools/packager/scope.h" |
| 16 #include "sky/tools/packager/switches.h" | 14 #include "sky/tools/packager/switches.h" |
| 17 | 15 |
| 18 namespace { | 16 namespace { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 37 if (component == base::FilePath::kParentDirectory) | 35 if (component == base::FilePath::kParentDirectory) |
| 38 result = result.DirName(); | 36 result = result.DirName(); |
| 39 else | 37 else |
| 40 result = result.Append(component); | 38 result = result.Append(component); |
| 41 } | 39 } |
| 42 return result; | 40 return result; |
| 43 } | 41 } |
| 44 | 42 |
| 45 class Loader { | 43 class Loader { |
| 46 public: | 44 public: |
| 47 Loader(base::FilePath package_root); | 45 Loader(const base::FilePath& package_root); |
| 48 | 46 |
| 49 std::string CanonicalizePackageURL(std::string url); | 47 std::string CanonicalizePackageURL(std::string url); |
| 50 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); | 48 Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); |
| 51 Dart_Handle Import(Dart_Handle url); | 49 Dart_Handle Import(Dart_Handle url); |
| 52 Dart_Handle Source(Dart_Handle library, Dart_Handle url); | 50 Dart_Handle Source(Dart_Handle library, Dart_Handle url); |
| 53 | 51 |
| 54 private: | 52 private: |
| 55 base::FilePath package_root_; | 53 base::FilePath package_root_; |
| 56 | 54 |
| 57 DISALLOW_COPY_AND_ASSIGN(Loader); | 55 DISALLOW_COPY_AND_ASSIGN(Loader); |
| 58 }; | 56 }; |
| 59 | 57 |
| 60 Loader::Loader(base::FilePath package_root) | 58 Loader::Loader(const base::FilePath& package_root) |
| 61 : package_root_(std::move(package_root)) { | 59 : package_root_(package_root) { |
| 62 } | 60 } |
| 63 | 61 |
| 64 std::string Loader::CanonicalizePackageURL(std::string url) { | 62 std::string Loader::CanonicalizePackageURL(std::string url) { |
| 65 DCHECK(StartsWithASCII(url, "package:", true)); | 63 DCHECK(StartsWithASCII(url, "package:", true)); |
| 66 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); | 64 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); |
| 67 return package_root_.Append(url).AsUTF8Unsafe(); | 65 return package_root_.Append(url).AsUTF8Unsafe(); |
| 68 } | 66 } |
| 69 | 67 |
| 70 Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { | 68 Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { |
| 71 std::string string = StringFromDart(url); | 69 std::string string = StringFromDart(url); |
| 72 if (StartsWithASCII(string, "dart:", true)) | 70 if (StartsWithASCII(string, "dart:", true)) |
| 73 return url; | 71 return url; |
| 74 if (StartsWithASCII(string, "package:", true)) | 72 if (StartsWithASCII(string, "package:", true)) |
| 75 return StringToDart(CanonicalizePackageURL(std::move(string))); | 73 return StringToDart(CanonicalizePackageURL(string)); |
| 76 base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library))); | 74 base::FilePath base_path(StringFromDart(Dart_LibraryUrl(library))); |
| 77 base::FilePath resolved_path = base_path.DirName().Append(string); | 75 base::FilePath resolved_path = base_path.DirName().Append(string); |
| 78 base::FilePath normalized_path = SimplifyPath(resolved_path); | 76 base::FilePath normalized_path = SimplifyPath(resolved_path); |
| 79 return StringToDart(normalized_path.AsUTF8Unsafe()); | 77 return StringToDart(normalized_path.AsUTF8Unsafe()); |
| 80 } | 78 } |
| 81 | 79 |
| 82 Dart_Handle Loader::Import(Dart_Handle url) { | 80 Dart_Handle Loader::Import(Dart_Handle url) { |
| 83 Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); | 81 Dart_Handle source = StringToDart(Fetch(StringFromDart(url))); |
| 84 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0); | 82 Dart_Handle result = Dart_LoadLibrary(url, source, 0, 0); |
| 85 LogIfError(result); | 83 LogIfError(result); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 123 } |
| 126 | 124 |
| 127 void LoadSkyInternals() { | 125 void LoadSkyInternals() { |
| 128 DartApiScope api_scope; | 126 DartApiScope api_scope; |
| 129 | 127 |
| 130 Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName); | 128 Dart_Handle library_name = StringToDart(kSkyInternalsLibraryName); |
| 131 std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL); | 129 std::string url = GetLoader().CanonicalizePackageURL(kSkyInternalsURL); |
| 132 LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0)); | 130 LogIfError(Dart_LoadLibrary(library_name, StringToDart(Fetch(url)), 0, 0)); |
| 133 } | 131 } |
| 134 | 132 |
| 135 void LoadScript(std::string url) { | 133 void LoadScript(const std::string& url) { |
| 136 LogIfError( | 134 LogIfError( |
| 137 Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0)); | 135 Dart_LoadScript(StringToDart(url), StringToDart(Fetch(url)), 0, 0)); |
| 138 } | 136 } |
| OLD | NEW |