OLD | NEW |
(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/shell/dart/dart_library_provider_files.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/threading/worker_pool.h" |
| 10 #include "mojo/common/data_pipe_utils.h" |
| 11 #include "sky/engine/tonic/dart_converter.h" |
| 12 |
| 13 namespace sky { |
| 14 namespace shell { |
| 15 namespace { |
| 16 |
| 17 void Ignored(bool) { |
| 18 } |
| 19 |
| 20 base::FilePath SimplifyPath(const base::FilePath& path) { |
| 21 std::vector<base::FilePath::StringType> components; |
| 22 path.GetComponents(&components); |
| 23 base::FilePath result; |
| 24 for (const auto& component : components) { |
| 25 if (component == base::FilePath::kCurrentDirectory) |
| 26 continue; |
| 27 if (component == base::FilePath::kParentDirectory) |
| 28 result = result.DirName(); |
| 29 else |
| 30 result = result.Append(component); |
| 31 } |
| 32 return result; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 DartLibraryProviderFiles::DartLibraryProviderFiles( |
| 38 const base::FilePath& package_root) |
| 39 : package_root_(package_root) { |
| 40 } |
| 41 |
| 42 DartLibraryProviderFiles::~DartLibraryProviderFiles() { |
| 43 } |
| 44 |
| 45 void DartLibraryProviderFiles::GetLibraryAsStream( |
| 46 const String& name, |
| 47 blink::DataPipeConsumerCallback callback) { |
| 48 mojo::DataPipe pipe; |
| 49 callback.Run(pipe.consumer_handle.Pass()); |
| 50 |
| 51 base::FilePath source(name.toUTF8()); |
| 52 scoped_refptr<base::TaskRunner> runner = |
| 53 base::WorkerPool::GetTaskRunner(true); |
| 54 mojo::common::CopyFromFile(source, pipe.producer_handle.Pass(), 0, |
| 55 runner.get(), base::Bind(&Ignored)); |
| 56 } |
| 57 |
| 58 std::string DartLibraryProviderFiles::CanonicalizePackageURL(std::string url) { |
| 59 DCHECK(StartsWithASCII(url, "package:", true)); |
| 60 ReplaceFirstSubstringAfterOffset(&url, 0, "package:", ""); |
| 61 return package_root_.Append(url).AsUTF8Unsafe(); |
| 62 } |
| 63 |
| 64 Dart_Handle DartLibraryProviderFiles::CanonicalizeURL(Dart_Handle library, |
| 65 Dart_Handle url) { |
| 66 std::string string = blink::StdStringFromDart(url); |
| 67 if (StartsWithASCII(string, "dart:", true)) |
| 68 return url; |
| 69 if (StartsWithASCII(string, "package:", true)) |
| 70 return blink::StdStringToDart(CanonicalizePackageURL(string)); |
| 71 base::FilePath base_path(blink::StdStringFromDart(Dart_LibraryUrl(library))); |
| 72 base::FilePath resolved_path = base_path.DirName().Append(string); |
| 73 base::FilePath normalized_path = SimplifyPath(resolved_path); |
| 74 return blink::StdStringToDart(normalized_path.AsUTF8Unsafe()); |
| 75 } |
| 76 |
| 77 } // namespace shell |
| 78 } // namespace sky |
OLD | NEW |