OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/common/file_util.h" | 5 #include "extensions/common/file_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "extensions/common/extension.h" |
| 13 #include "extensions/common/extension_icon_set.h" |
11 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
12 #include "url/gurl.h" | 16 #include "url/gurl.h" |
13 | 17 |
14 namespace extensions { | 18 namespace extensions { |
15 namespace file_util { | 19 namespace file_util { |
| 20 namespace { |
| 21 |
| 22 // Returns true if the given file path exists and is not zero-length. |
| 23 bool ValidateFilePath(const base::FilePath& path) { |
| 24 int64 size = 0; |
| 25 if (!base::PathExists(path) || |
| 26 !base::GetFileSize(path, &size) || |
| 27 size == 0) { |
| 28 return false; |
| 29 } |
| 30 |
| 31 return true; |
| 32 } |
| 33 |
| 34 } // namespace |
16 | 35 |
17 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { | 36 base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) { |
18 std::string url_path = url.path(); | 37 std::string url_path = url.path(); |
19 if (url_path.empty() || url_path[0] != '/') | 38 if (url_path.empty() || url_path[0] != '/') |
20 return base::FilePath(); | 39 return base::FilePath(); |
21 | 40 |
22 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8. | 41 // Drop the leading slashes and convert %-encoded UTF8 to regular UTF8. |
23 std::string file_path = net::UnescapeURLComponent(url_path, | 42 std::string file_path = net::UnescapeURLComponent(url_path, |
24 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); | 43 net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS); |
25 size_t skip = file_path.find_first_not_of("/\\"); | 44 size_t skip = file_path.find_first_not_of("/\\"); |
(...skipping 24 matching lines...) Expand all Loading... |
50 | 69 |
51 base::FilePath path = root.AppendASCII(host).Append(relative_path); | 70 base::FilePath path = root.AppendASCII(host).Append(relative_path); |
52 if (!base::PathExists(path)) | 71 if (!base::PathExists(path)) |
53 return base::FilePath(); | 72 return base::FilePath(); |
54 path = base::MakeAbsoluteFilePath(path); | 73 path = base::MakeAbsoluteFilePath(path); |
55 if (path.empty() || !root.IsParent(path)) | 74 if (path.empty() || !root.IsParent(path)) |
56 return base::FilePath(); | 75 return base::FilePath(); |
57 return path; | 76 return path; |
58 } | 77 } |
59 | 78 |
| 79 bool ValidateExtensionIconSet(const ExtensionIconSet& icon_set, |
| 80 const Extension* extension, |
| 81 int error_message_id, |
| 82 std::string* error) { |
| 83 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set.map().begin(); |
| 84 iter != icon_set.map().end(); |
| 85 ++iter) { |
| 86 const base::FilePath path = |
| 87 extension->GetResource(iter->second).GetFilePath(); |
| 88 if (!ValidateFilePath(path)) { |
| 89 *error = l10n_util::GetStringFUTF8(error_message_id, |
| 90 base::UTF8ToUTF16(iter->second)); |
| 91 return false; |
| 92 } |
| 93 } |
| 94 return true; |
| 95 } |
| 96 |
60 } // namespace file_util | 97 } // namespace file_util |
61 } // namespace extensions | 98 } // namespace extensions |
OLD | NEW |