This source file includes following definitions.
- ExtensionURLToRelativeFilePath
- ExtensionResourceURLToFilePath
#include "extensions/common/file_util.h"
#include <string>
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "net/base/escape.h"
#include "url/gurl.h"
namespace extensions {
namespace file_util {
base::FilePath ExtensionURLToRelativeFilePath(const GURL& url) {
std::string url_path = url.path();
if (url_path.empty() || url_path[0] != '/')
return base::FilePath();
std::string file_path = net::UnescapeURLComponent(url_path,
net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
size_t skip = file_path.find_first_not_of("/\\");
if (skip != file_path.npos)
file_path = file_path.substr(skip);
base::FilePath path = base::FilePath::FromUTF8Unsafe(file_path);
if (path.IsAbsolute())
return base::FilePath();
return path;
}
base::FilePath ExtensionResourceURLToFilePath(const GURL& url,
const base::FilePath& root) {
std::string host = net::UnescapeURLComponent(url.host(),
net::UnescapeRule::SPACES | net::UnescapeRule::URL_SPECIAL_CHARS);
if (host.empty())
return base::FilePath();
base::FilePath relative_path = ExtensionURLToRelativeFilePath(url);
if (relative_path.empty())
return base::FilePath();
base::FilePath path = root.AppendASCII(host).Append(relative_path);
if (!base::PathExists(path))
return base::FilePath();
path = base::MakeAbsoluteFilePath(path);
if (path.empty() || !root.IsParent(path))
return base::FilePath();
return path;
}
}
}