OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/common/extensions/extension_resource.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/extensions/extension_l10n_util.h" |
| 11 #include "googleurl/src/gurl.h" |
| 12 #include "net/base/net_util.h" |
| 13 |
| 14 ExtensionResource::ExtensionResource() { |
| 15 } |
| 16 |
| 17 ExtensionResource::ExtensionResource(const FilePath& extension_root, |
| 18 const FilePath& relative_path) |
| 19 : extension_root_(extension_root), |
| 20 relative_path_(relative_path) { |
| 21 } |
| 22 |
| 23 const FilePath& ExtensionResource::GetFilePath() const { |
| 24 if (extension_root_.empty() || relative_path_.empty()) |
| 25 return full_resource_path_; |
| 26 |
| 27 // We've already checked, just return last value. |
| 28 if (!full_resource_path_.empty()) |
| 29 return full_resource_path_; |
| 30 |
| 31 // Stat l10n file, and return new path if it exists. |
| 32 FilePath l10n_relative_path = |
| 33 extension_l10n_util::GetL10nRelativePath(relative_path_); |
| 34 full_resource_path_ = CombinePathsSafely(extension_root_, l10n_relative_path); |
| 35 if (file_util::PathExists(full_resource_path_)) { |
| 36 return full_resource_path_; |
| 37 } |
| 38 |
| 39 // Fall back to root resource. |
| 40 full_resource_path_ = CombinePathsSafely(extension_root_, relative_path_); |
| 41 return full_resource_path_; |
| 42 } |
| 43 |
| 44 FilePath ExtensionResource::CombinePathsSafely( |
| 45 const FilePath& extension_path, |
| 46 const FilePath& relative_resource_path) const { |
| 47 // Build up a file:// URL and convert that back to a FilePath. This avoids |
| 48 // URL encoding and path separator issues. |
| 49 |
| 50 // Convert the extension's root to a file:// URL. |
| 51 GURL extension_url = net::FilePathToFileURL(extension_path); |
| 52 if (!extension_url.is_valid()) |
| 53 return FilePath(); |
| 54 |
| 55 // Append the requested path. |
| 56 std::string relative_path = |
| 57 WideToUTF8(relative_resource_path.ToWStringHack()); |
| 58 GURL::Replacements replacements; |
| 59 std::string new_path(extension_url.path()); |
| 60 new_path += "/"; |
| 61 new_path += relative_path; |
| 62 replacements.SetPathStr(new_path); |
| 63 GURL file_url = extension_url.ReplaceComponents(replacements); |
| 64 if (!file_url.is_valid()) |
| 65 return FilePath(); |
| 66 |
| 67 // Convert the result back to a FilePath. |
| 68 FilePath ret_val; |
| 69 if (!net::FileURLToFilePath(file_url, &ret_val)) |
| 70 return FilePath(); |
| 71 |
| 72 // Double-check that the path we ended up with is actually inside the |
| 73 // extension root. |
| 74 if (!extension_path.IsParent(ret_val)) |
| 75 return FilePath(); |
| 76 |
| 77 return ret_val; |
| 78 } |
| 79 |
| 80 // Unittesting helpers. |
| 81 FilePath::StringType ExtensionResource::NormalizeSeperators( |
| 82 FilePath::StringType path) const { |
| 83 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 84 FilePath::StringType ret_val; |
| 85 for (size_t i = 0; i < path.length(); i++) { |
| 86 if (FilePath::IsSeparator(path[i])) |
| 87 path[i] = FilePath::kSeparators[0]; |
| 88 } |
| 89 #endif // FILE_PATH_USES_WIN_SEPARATORS |
| 90 return path; |
| 91 } |
| 92 |
| 93 bool ExtensionResource::ComparePathWithDefault(const FilePath& path) const { |
| 94 if (NormalizeSeperators(path.value()) == |
| 95 NormalizeSeperators(full_resource_path_.value())) { |
| 96 return true; |
| 97 } else { |
| 98 return false; |
| 99 } |
| 100 } |
OLD | NEW |