Move GetAsarArchivePath to asar_util.h

This commit is contained in:
Cheng Zhao 2015-02-12 17:31:14 +08:00
parent 3b3a025ed4
commit 98a7f08be2
3 changed files with 31 additions and 30 deletions

View file

@ -14,35 +14,6 @@
namespace asar {
namespace {
const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
// Get the relative path in asar archive.
bool GetAsarPath(const base::FilePath& full_path,
base::FilePath* asar_path,
base::FilePath* relative_path) {
base::FilePath iter = full_path;
while (true) {
base::FilePath dirname = iter.DirName();
if (iter.MatchesExtension(kAsarExtension))
break;
else if (iter == dirname)
return false;
iter = dirname;
}
base::FilePath tail;
if (!iter.AppendRelativePath(full_path, &tail))
return false;
*asar_path = iter;
*relative_path = tail;
return true;
}
} // namespace
AsarProtocolHandler::AsarProtocolHandler(
const scoped_refptr<base::TaskRunner>& file_task_runner)
: file_task_runner_(file_task_runner) {}
@ -59,7 +30,7 @@ net::URLRequestJob* AsarProtocolHandler::MaybeCreateJob(
// Create asar:// job when the path contains "xxx.asar/", otherwise treat the
// URL request as file://.
base::FilePath asar_path, relative_path;
if (!GetAsarPath(full_path, &asar_path, &relative_path))
if (!GetAsarArchivePath(full_path, &asar_path, &relative_path))
return new net::URLRequestFileJob(request, network_delegate, full_path,
file_task_runner_);

View file

@ -17,8 +17,11 @@ typedef std::map<base::FilePath, std::shared_ptr<Archive>> ArchiveMap;
namespace {
// The global instance of ArchiveMap, will be destroyed on exit.
static base::LazyInstance<ArchiveMap> g_archive_map = LAZY_INSTANCE_INITIALIZER;
const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
} // namespace
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
@ -32,4 +35,26 @@ std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
return archive_map[path];
}
bool GetAsarArchivePath(const base::FilePath& full_path,
base::FilePath* asar_path,
base::FilePath* relative_path) {
base::FilePath iter = full_path;
while (true) {
base::FilePath dirname = iter.DirName();
if (iter.MatchesExtension(kAsarExtension))
break;
else if (iter == dirname)
return false;
iter = dirname;
}
base::FilePath tail;
if (!iter.AppendRelativePath(full_path, &tail))
return false;
*asar_path = iter;
*relative_path = tail;
return true;
}
} // namespace asar

View file

@ -18,6 +18,11 @@ class Archive;
// Gets or creates a new Archive from the path.
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path);
// Separates the path to Archive out.
bool GetAsarArchivePath(const base::FilePath& full_path,
base::FilePath* asar_path,
base::FilePath* relative_path);
} // namespace asar
#endif // ATOM_COMMON_ASAR_ASAR_UTIL_H_