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