提交 2ee12a84 编写于 作者: A Adam Barth 提交者: GitHub

We need to make these paths absolute before reading them (#2787)

上级 d95b73bc
...@@ -70,7 +70,8 @@ DartLibraryProviderFiles::~DartLibraryProviderFiles() { ...@@ -70,7 +70,8 @@ DartLibraryProviderFiles::~DartLibraryProviderFiles() {
void DartLibraryProviderFiles::LoadPackagesMap(const base::FilePath& packages) { void DartLibraryProviderFiles::LoadPackagesMap(const base::FilePath& packages) {
packages_ = packages; packages_ = packages;
std::string packages_source; std::string packages_source;
if (!base::ReadFileToString(packages_, &packages_source)) { if (!base::ReadFileToString(base::MakeAbsoluteFilePath(packages_),
&packages_source)) {
LOG(ERROR) << "error: Unable to load .packages file '" LOG(ERROR) << "error: Unable to load .packages file '"
<< packages_.AsUTF8Unsafe() << "'."; << packages_.AsUTF8Unsafe() << "'.";
exit(1); exit(1);
...@@ -88,8 +89,8 @@ void DartLibraryProviderFiles::GetLibraryAsStream( ...@@ -88,8 +89,8 @@ void DartLibraryProviderFiles::GetLibraryAsStream(
blink::DataPipeConsumerCallback callback) { blink::DataPipeConsumerCallback callback) {
mojo::DataPipe pipe; mojo::DataPipe pipe;
callback.Run(pipe.consumer_handle.Pass()); callback.Run(pipe.consumer_handle.Pass());
std::string path = GetFilePathForURL(name); base::FilePath path = GetFilePathForURL(name);
base::FilePath source(path); base::FilePath source = base::MakeAbsoluteFilePath(path);
scoped_refptr<base::TaskRunner> runner = scoped_refptr<base::TaskRunner> runner =
base::WorkerPool::GetTaskRunner(true); base::WorkerPool::GetTaskRunner(true);
mojo::common::CopyFromFile(source, pipe.producer_handle.Pass(), 0, mojo::common::CopyFromFile(source, pipe.producer_handle.Pass(), 0,
...@@ -118,39 +119,38 @@ Dart_Handle DartLibraryProviderFiles::CanonicalizeURL(Dart_Handle library, ...@@ -118,39 +119,38 @@ Dart_Handle DartLibraryProviderFiles::CanonicalizeURL(Dart_Handle library,
return blink::StdStringToDart(prefix + normalized_path.AsUTF8Unsafe()); return blink::StdStringToDart(prefix + normalized_path.AsUTF8Unsafe());
} }
std::string DartLibraryProviderFiles::GetFilePathForURL(std::string url) { base::FilePath DartLibraryProviderFiles::GetFilePathForURL(std::string url) {
if (base::StartsWithASCII(url, "package:", true)) if (base::StartsWithASCII(url, "package:", true))
return GetFilePathForPackageURL(url); return GetFilePathForPackageURL(url);
if (base::StartsWithASCII(url, "file:", true)) if (base::StartsWithASCII(url, "file:", true))
return GetFilePathForFileURL(url); return GetFilePathForFileURL(url);
return url; return base::FilePath(url);
} }
std::string DartLibraryProviderFiles::GetFilePathForPackageURL( base::FilePath DartLibraryProviderFiles::GetFilePathForPackageURL(
std::string url) { std::string url) {
DCHECK(base::StartsWithASCII(url, "package:", true)); DCHECK(base::StartsWithASCII(url, "package:", true));
base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
size_t slash = url.find('/'); size_t slash = url.find('/');
if (slash == std::string::npos) if (slash == std::string::npos)
return std::string(); return base::FilePath();
std::string package = url.substr(0, slash); std::string package = url.substr(0, slash);
std::string library_path = url.substr(slash + 1); std::string library_path = url.substr(slash + 1);
std::string package_path = packages_map_.Resolve(package); std::string package_path = packages_map_.Resolve(package);
if (package_path.empty()) if (package_path.empty())
return std::string(); return base::FilePath();
if (base::StartsWithASCII(package_path, "file://", true)) { if (base::StartsWithASCII(package_path, "file://", true)) {
base::ReplaceFirstSubstringAfterOffset(&package_path, 0, "file://", ""); base::ReplaceFirstSubstringAfterOffset(&package_path, 0, "file://", "");
return package_path + library_path; return base::FilePath(package_path + library_path);
} }
auto path = packages_.DirName().Append(package_path).Append(library_path); return packages_.DirName().Append(package_path).Append(library_path);
return SimplifyPath(path).AsUTF8Unsafe();
} }
std::string DartLibraryProviderFiles::GetFilePathForFileURL(std::string url) { base::FilePath DartLibraryProviderFiles::GetFilePathForFileURL(std::string url) {
DCHECK(base::StartsWithASCII(url, "file://", true)); DCHECK(base::StartsWithASCII(url, "file://", true));
base::ReplaceFirstSubstringAfterOffset(&url, 0, "file://", ""); base::ReplaceFirstSubstringAfterOffset(&url, 0, "file://", "");
return url; return base::FilePath(url);
} }
} // namespace shell } // namespace shell
......
...@@ -26,9 +26,9 @@ class DartLibraryProviderFiles : public blink::DartLibraryProvider { ...@@ -26,9 +26,9 @@ class DartLibraryProviderFiles : public blink::DartLibraryProvider {
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url) override; Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url) override;
private: private:
std::string GetFilePathForURL(std::string url); base::FilePath GetFilePathForURL(std::string url);
std::string GetFilePathForPackageURL(std::string url); base::FilePath GetFilePathForPackageURL(std::string url);
std::string GetFilePathForFileURL(std::string url); base::FilePath GetFilePathForFileURL(std::string url);
base::FilePath packages_; base::FilePath packages_;
tonic::PackagesMap packages_map_; tonic::PackagesMap packages_map_;
......
...@@ -64,9 +64,9 @@ class Loader { ...@@ -64,9 +64,9 @@ class Loader {
const std::set<std::string>& dependencies() const { return dependencies_; } const std::set<std::string>& dependencies() const { return dependencies_; }
Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url); Dart_Handle CanonicalizeURL(Dart_Handle library, Dart_Handle url);
std::string GetFilePathForURL(std::string url); base::FilePath GetFilePathForURL(std::string url);
std::string GetFilePathForPackageURL(std::string url); base::FilePath GetFilePathForPackageURL(std::string url);
std::string GetFilePathForFileURL(std::string url); base::FilePath GetFilePathForFileURL(std::string url);
std::string Fetch(const std::string& url); std::string Fetch(const std::string& url);
Dart_Handle Import(Dart_Handle url); Dart_Handle Import(Dart_Handle url);
Dart_Handle Source(Dart_Handle library, Dart_Handle url); Dart_Handle Source(Dart_Handle library, Dart_Handle url);
...@@ -125,50 +125,48 @@ Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) { ...@@ -125,50 +125,48 @@ Dart_Handle Loader::CanonicalizeURL(Dart_Handle library, Dart_Handle url) {
return StringToDart(prefix + normalized_path.AsUTF8Unsafe()); return StringToDart(prefix + normalized_path.AsUTF8Unsafe());
} }
std::string Loader::GetFilePathForURL(std::string url) { base::FilePath Loader::GetFilePathForURL(std::string url) {
if (base::StartsWithASCII(url, "package:", true)) if (base::StartsWithASCII(url, "package:", true))
return GetFilePathForPackageURL(url); return GetFilePathForPackageURL(url);
if (base::StartsWithASCII(url, "file:", true)) if (base::StartsWithASCII(url, "file:", true))
return GetFilePathForFileURL(url); return GetFilePathForFileURL(url);
return url; return base::FilePath(url);
} }
std::string Loader::GetFilePathForPackageURL( base::FilePath Loader::GetFilePathForPackageURL(
std::string url) { std::string url) {
DCHECK(base::StartsWithASCII(url, "package:", true)); DCHECK(base::StartsWithASCII(url, "package:", true));
base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); base::ReplaceFirstSubstringAfterOffset(&url, 0, "package:", "");
size_t slash = url.find('/'); size_t slash = url.find('/');
if (slash == std::string::npos) if (slash == std::string::npos)
return std::string(); return base::FilePath();
std::string package = url.substr(0, slash); std::string package = url.substr(0, slash);
std::string library_path = url.substr(slash + 1); std::string library_path = url.substr(slash + 1);
std::string package_path = packages_map_->Resolve(package); std::string package_path = packages_map_->Resolve(package);
if (package_path.empty()) if (package_path.empty())
return std::string(); return base::FilePath();
if (base::StartsWithASCII(package_path, "file://", true)) { if (base::StartsWithASCII(package_path, "file://", true)) {
base::ReplaceFirstSubstringAfterOffset(&package_path, 0, "file://", ""); base::ReplaceFirstSubstringAfterOffset(&package_path, 0, "file://", "");
return package_path + library_path; return base::FilePath(package_path + library_path);
} }
auto path = packages_.DirName().Append(package_path).Append(library_path); return packages_.DirName().Append(package_path).Append(library_path);
return SimplifyPath(path).AsUTF8Unsafe();
} }
std::string Loader::GetFilePathForFileURL(std::string url) { base::FilePath Loader::GetFilePathForFileURL(std::string url) {
DCHECK(base::StartsWithASCII(url, "file://", true)); DCHECK(base::StartsWithASCII(url, "file://", true));
base::ReplaceFirstSubstringAfterOffset(&url, 0, "file://", ""); base::ReplaceFirstSubstringAfterOffset(&url, 0, "file://", "");
return url; return base::FilePath(url);
} }
std::string Loader::Fetch(const std::string& url) { std::string Loader::Fetch(const std::string& url) {
std::string url_path = GetFilePathForURL(url); base::FilePath path = GetFilePathForURL(url);
base::FilePath path(url_path);
std::string source; std::string source;
if (!base::ReadFileToString(path, &source)) { if (!base::ReadFileToString(base::MakeAbsoluteFilePath(path), &source)) {
fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str()); fprintf(stderr, "error: Unable to find Dart library '%s'.\n", url.c_str());
exit(1); exit(1);
} }
dependencies_.insert(url_path); dependencies_.insert(path.AsUTF8Unsafe());
return source; return source;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册