Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/common/extensions/extension_resource.cc

Issue 267051: Minimize dependency of user scripts.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "googleurl/src/gurl.h" 11 #include "googleurl/src/gurl.h"
12 #include "net/base/net_util.h" 12 #include "net/base/net_util.h"
13 13
14 ExtensionResource::ExtensionResource() { 14 ExtensionResource::ExtensionResource() {
15 } 15 }
16 16
17 ExtensionResource::ExtensionResource(const FilePath& extension_root, 17 ExtensionResource::ExtensionResource(const FilePath& extension_root,
18 const FilePath& relative_path) 18 const FilePath& relative_path)
19 : extension_root_(extension_root), 19 : extension_root_(extension_root),
20 relative_path_(relative_path) { 20 relative_path_(relative_path) {
21 } 21 }
22 22
23 const FilePath& ExtensionResource::GetFilePath() const { 23 const FilePath& ExtensionResource::GetFilePath() const {
24 if (extension_root_.empty() || relative_path_.empty()) 24 if (extension_root_.empty() || relative_path_.empty()) {
25 DCHECK(full_resource_path_.empty());
25 return full_resource_path_; 26 return full_resource_path_;
27 }
26 28
27 // We've already checked, just return last value. 29 // We've already checked, just return last value.
28 if (!full_resource_path_.empty()) 30 if (!full_resource_path_.empty())
29 return full_resource_path_; 31 return full_resource_path_;
30 32
33 full_resource_path_ = GetFilePath(extension_root_, relative_path_);
34 return full_resource_path_;
35 }
36
37 // Static version...
38 FilePath ExtensionResource::GetFilePath(const FilePath& extension_root,
39 const FilePath& relative_path) {
31 // Stat l10n file, and return new path if it exists. 40 // Stat l10n file, and return new path if it exists.
32 FilePath l10n_relative_path = 41 FilePath l10n_relative_path =
33 extension_l10n_util::GetL10nRelativePath(relative_path_); 42 extension_l10n_util::GetL10nRelativePath(relative_path);
34 full_resource_path_ = CombinePathsSafely(extension_root_, l10n_relative_path); 43 FilePath full_path;
35 if (file_util::PathExists(full_resource_path_)) { 44 if (extension_root.AppendAndResolveRelative(l10n_relative_path, &full_path) &&
36 return full_resource_path_; 45 extension_root.IsParent(full_path) &&
46 file_util::PathExists(full_path)) {
47 return full_path;
37 } 48 }
38 49
39 // Fall back to root resource. 50 // Fall back to root resource.
40 full_resource_path_ = CombinePathsSafely(extension_root_, relative_path_); 51 if (extension_root.AppendAndResolveRelative(relative_path, &full_path) &&
41 return full_resource_path_; 52 extension_root.IsParent(full_path)) {
42 } 53 return full_path;
54 }
43 55
44 FilePath ExtensionResource::CombinePathsSafely( 56 return FilePath();
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 // Converting the extension_url back to a path removes all .. and . references
73 // that may have been in extension_path that would cause isParent to break.
74 FilePath sanitized_extension_path;
75 if (!net::FileURLToFilePath(extension_url, &sanitized_extension_path))
76 return FilePath();
77
78 // Double-check that the path we ended up with is actually inside the
79 // extension root.
80 if (!sanitized_extension_path.IsParent(ret_val))
81 return FilePath();
82
83 return ret_val;
84 } 57 }
85 58
86 // Unittesting helpers. 59 // Unittesting helpers.
87 FilePath::StringType ExtensionResource::NormalizeSeperators( 60 FilePath::StringType ExtensionResource::NormalizeSeperators(
88 FilePath::StringType path) const { 61 FilePath::StringType path) const {
89 #if defined(FILE_PATH_USES_WIN_SEPARATORS) 62 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
90 FilePath::StringType ret_val; 63 FilePath::StringType ret_val;
91 for (size_t i = 0; i < path.length(); i++) { 64 for (size_t i = 0; i < path.length(); i++) {
92 if (FilePath::IsSeparator(path[i])) 65 if (FilePath::IsSeparator(path[i]))
93 path[i] = FilePath::kSeparators[0]; 66 path[i] = FilePath::kSeparators[0];
94 } 67 }
95 #endif // FILE_PATH_USES_WIN_SEPARATORS 68 #endif // FILE_PATH_USES_WIN_SEPARATORS
96 return path; 69 return path;
97 } 70 }
98 71
99 bool ExtensionResource::ComparePathWithDefault(const FilePath& path) const { 72 bool ExtensionResource::ComparePathWithDefault(const FilePath& path) const {
73 // Make sure we have a cached value to test against...
74 if (full_resource_path_.empty())
75 GetFilePath();
100 if (NormalizeSeperators(path.value()) == 76 if (NormalizeSeperators(path.value()) ==
101 NormalizeSeperators(full_resource_path_.value())) { 77 NormalizeSeperators(full_resource_path_.value())) {
102 return true; 78 return true;
103 } else { 79 } else {
104 return false; 80 return false;
105 } 81 }
106 } 82 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698