| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/common/extensions/extension_resource.h" | 5 #include "chrome/common/extensions/extension_resource.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/extensions/extension_l10n_util.h" | 10 #include "chrome/browser/extensions/extension_l10n_util.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 return full_resource_path_; | 34 return full_resource_path_; |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Static version... | 37 // Static version... |
| 38 FilePath ExtensionResource::GetFilePath(const FilePath& extension_root, | 38 FilePath ExtensionResource::GetFilePath(const FilePath& extension_root, |
| 39 const FilePath& relative_path) { | 39 const FilePath& relative_path) { |
| 40 std::vector<FilePath> l10n_relative_paths; | 40 std::vector<FilePath> l10n_relative_paths; |
| 41 extension_l10n_util::GetL10nRelativePaths(relative_path, | 41 extension_l10n_util::GetL10nRelativePaths(relative_path, |
| 42 &l10n_relative_paths); | 42 &l10n_relative_paths); |
| 43 | 43 |
| 44 // We need to resolve the parent references in the extension_root |
| 45 // path on its own because IsParent doesn't like parent references. |
| 46 FilePath clean_extension_root(extension_root); |
| 47 if (!file_util::AbsolutePath(&clean_extension_root)) |
| 48 return FilePath(); |
| 49 |
| 44 // Stat l10n file(s), and return new path if it exists. | 50 // Stat l10n file(s), and return new path if it exists. |
| 45 for (size_t i = 0; i < l10n_relative_paths.size(); ++i) { | 51 for (size_t i = 0; i < l10n_relative_paths.size(); ++i) { |
| 46 FilePath full_path; | 52 FilePath full_path = clean_extension_root.Append(l10n_relative_paths[i]); |
| 47 if (extension_root.AppendAndResolveRelative(l10n_relative_paths[i], | 53 if (file_util::AbsolutePath(&full_path) && |
| 48 &full_path) && | 54 clean_extension_root.IsParent(full_path) && |
| 49 extension_root.IsParent(full_path) && | |
| 50 file_util::PathExists(full_path)) { | 55 file_util::PathExists(full_path)) { |
| 51 return full_path; | 56 return full_path; |
| 52 } | 57 } |
| 53 } | 58 } |
| 54 | 59 |
| 55 // Fall back to root resource. | 60 // Fall back to root resource. |
| 56 FilePath full_path; | 61 FilePath full_path = clean_extension_root.Append(relative_path); |
| 57 if (extension_root.AppendAndResolveRelative(relative_path, &full_path) && | 62 |
| 58 extension_root.IsParent(full_path)) { | 63 // We must resolve the absolute path of the combined path when |
| 64 // the relative path contains references to a parent folder (i.e., '..'). |
| 65 // We also check if the path exists because the posix version of AbsolutePath |
| 66 // will fail if the path doesn't exist, and we want the same behavior on |
| 67 // Windows... So until the posix and Windows version of AbsolutePath are |
| 68 // unified, we need an extra call to PathExists, unfortunately. |
| 69 // TODO(mad): Fix this once AbsolutePath is unified. |
| 70 if (file_util::AbsolutePath(&full_path) && |
| 71 file_util::PathExists(full_path) && |
| 72 clean_extension_root.IsParent(full_path)) { |
| 59 return full_path; | 73 return full_path; |
| 60 } | 74 } |
| 61 | 75 |
| 62 return FilePath(); | 76 return FilePath(); |
| 63 } | 77 } |
| 64 | 78 |
| 65 // Unittesting helpers. | 79 // Unittesting helpers. |
| 66 FilePath::StringType ExtensionResource::NormalizeSeperators( | 80 FilePath::StringType ExtensionResource::NormalizeSeperators( |
| 67 FilePath::StringType path) const { | 81 FilePath::StringType path) const { |
| 68 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 82 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 79 // Make sure we have a cached value to test against... | 93 // Make sure we have a cached value to test against... |
| 80 if (full_resource_path_.empty()) | 94 if (full_resource_path_.empty()) |
| 81 GetFilePath(); | 95 GetFilePath(); |
| 82 if (NormalizeSeperators(path.value()) == | 96 if (NormalizeSeperators(path.value()) == |
| 83 NormalizeSeperators(full_resource_path_.value())) { | 97 NormalizeSeperators(full_resource_path_.value())) { |
| 84 return true; | 98 return true; |
| 85 } else { | 99 } else { |
| 86 return false; | 100 return false; |
| 87 } | 101 } |
| 88 } | 102 } |
| OLD | NEW |