This source file includes following definitions.
- follow_symlinks_anywhere_
- set_follow_symlinks_anywhere
- GetFilePath
- GetFilePath
- NormalizeSeperators
- ComparePathWithDefault
#include "extensions/common/extension_resource.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
namespace extensions {
ExtensionResource::ExtensionResource() : follow_symlinks_anywhere_(false) {
}
ExtensionResource::ExtensionResource(const std::string& extension_id,
const base::FilePath& extension_root,
const base::FilePath& relative_path)
: extension_id_(extension_id),
extension_root_(extension_root),
relative_path_(relative_path),
follow_symlinks_anywhere_(false) {
}
ExtensionResource::~ExtensionResource() {}
void ExtensionResource::set_follow_symlinks_anywhere() {
follow_symlinks_anywhere_ = true;
}
const base::FilePath& ExtensionResource::GetFilePath() const {
if (extension_root_.empty() || relative_path_.empty()) {
DCHECK(full_resource_path_.empty());
return full_resource_path_;
}
if (!full_resource_path_.empty())
return full_resource_path_;
full_resource_path_ = GetFilePath(
extension_root_, relative_path_,
follow_symlinks_anywhere_ ?
FOLLOW_SYMLINKS_ANYWHERE : SYMLINKS_MUST_RESOLVE_WITHIN_ROOT);
return full_resource_path_;
}
base::FilePath ExtensionResource::GetFilePath(
const base::FilePath& extension_root,
const base::FilePath& relative_path,
SymlinkPolicy symlink_policy) {
base::FilePath clean_extension_root(
base::MakeAbsoluteFilePath(extension_root));
if (clean_extension_root.empty())
return base::FilePath();
base::FilePath full_path = clean_extension_root.Append(relative_path);
if (symlink_policy == FOLLOW_SYMLINKS_ANYWHERE) {
std::vector<base::FilePath::StringType> components;
relative_path.GetComponents(&components);
int depth = 0;
for (std::vector<base::FilePath::StringType>::const_iterator
i = components.begin(); i != components.end(); i++) {
if (*i == base::FilePath::kParentDirectory) {
depth--;
} else if (*i != base::FilePath::kCurrentDirectory) {
depth++;
}
if (depth < 0) {
return base::FilePath();
}
}
}
full_path = base::MakeAbsoluteFilePath(full_path);
if (base::PathExists(full_path) &&
(symlink_policy == FOLLOW_SYMLINKS_ANYWHERE ||
clean_extension_root.IsParent(full_path))) {
return full_path;
}
return base::FilePath();
}
base::FilePath::StringType ExtensionResource::NormalizeSeperators(
const base::FilePath::StringType& path) const {
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
base::FilePath::StringType win_path = path;
for (size_t i = 0; i < win_path.length(); i++) {
if (base::FilePath::IsSeparator(win_path[i]))
win_path[i] = base::FilePath::kSeparators[0];
}
return win_path;
#else
return path;
#endif
}
bool ExtensionResource::ComparePathWithDefault(
const base::FilePath& path) const {
if (full_resource_path_.empty())
GetFilePath();
if (NormalizeSeperators(path.value()) ==
NormalizeSeperators(full_resource_path_.value())) {
return true;
} else {
return false;
}
}
}