This source file includes following definitions.
- IsUnsupportedExtension
- GetMediaExtensionList
- ShouldSkip
- Match
- GetType
- EnsureInitialized
- AddExtensionsToMediaFileExtensionMap
- AddAdditionalExtensionsToMediaFileExtensionMap
- AddExtensionToMediaFileExtensionMap
#include "chrome/browser/media_galleries/fileapi/media_path_filter.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#include <algorithm>
#include <string>
#include "base/strings/string_util.h"
#include "net/base/mime_util.h"
namespace {
const base::FilePath::CharType* const kExtraSupportedImageExtensions[] = {
FILE_PATH_LITERAL("3fr"),
FILE_PATH_LITERAL("arw"),
FILE_PATH_LITERAL("dcr"),
FILE_PATH_LITERAL("dng"),
FILE_PATH_LITERAL("erf"),
FILE_PATH_LITERAL("k25"),
FILE_PATH_LITERAL("kdc"),
FILE_PATH_LITERAL("mef"),
FILE_PATH_LITERAL("mos"),
FILE_PATH_LITERAL("nef"),
FILE_PATH_LITERAL("pef"),
FILE_PATH_LITERAL("sr2"),
FILE_PATH_LITERAL("srf"),
FILE_PATH_LITERAL("cr2"),
FILE_PATH_LITERAL("crw"),
FILE_PATH_LITERAL("mrw"),
FILE_PATH_LITERAL("orf"),
FILE_PATH_LITERAL("raf"),
FILE_PATH_LITERAL("rw2"),
FILE_PATH_LITERAL("x3f"),
FILE_PATH_LITERAL("raw"),
};
const base::FilePath::CharType* const kExtraSupportedVideoExtensions[] = {
FILE_PATH_LITERAL("3gp"),
FILE_PATH_LITERAL("3gpp"),
FILE_PATH_LITERAL("avi"),
FILE_PATH_LITERAL("flv"),
FILE_PATH_LITERAL("mkv"),
FILE_PATH_LITERAL("mov"),
FILE_PATH_LITERAL("mpeg"),
FILE_PATH_LITERAL("mpeg4"),
FILE_PATH_LITERAL("mpegps"),
FILE_PATH_LITERAL("mpg"),
FILE_PATH_LITERAL("wmv"),
};
const base::FilePath::CharType* const kExtraSupportedAudioExtensions[] = {
FILE_PATH_LITERAL("aac"),
FILE_PATH_LITERAL("alac"),
FILE_PATH_LITERAL("flac"),
FILE_PATH_LITERAL("m4b"),
FILE_PATH_LITERAL("m4p"),
FILE_PATH_LITERAL("wma"),
};
bool IsUnsupportedExtension(const base::FilePath::StringType& extension) {
std::string mime_type;
return !net::GetMimeTypeFromExtension(extension, &mime_type) ||
!net::IsSupportedMimeType(mime_type);
}
std::vector<base::FilePath::StringType> GetMediaExtensionList(
const std::string& mime_type) {
std::vector<base::FilePath::StringType> extensions;
net::GetExtensionsForMimeType(mime_type, &extensions);
std::vector<base::FilePath::StringType>::iterator new_end =
std::remove_if(extensions.begin(),
extensions.end(),
&IsUnsupportedExtension);
extensions.erase(new_end, extensions.end());
return extensions;
}
}
bool MediaPathFilter::ShouldSkip(const base::FilePath& path) {
const base::FilePath::StringType base_name = path.BaseName().value();
if (base_name.empty())
return false;
if (base_name[0] == '.')
return true;
if (base_name == FILE_PATH_LITERAL("__MACOSX"))
return true;
#if defined(OS_WIN)
DWORD file_attributes = ::GetFileAttributes(path.value().c_str());
if ((file_attributes != INVALID_FILE_ATTRIBUTES) &&
((file_attributes & FILE_ATTRIBUTE_HIDDEN) != 0))
return true;
#else
const char win_98_recycle_bin_name[] = "RECYCLED";
const char win_xp_recycle_bin_name[] = "RECYCLER";
const char win_vista_recycle_bin_name[] = "$Recycle.bin";
if ((base::strncasecmp(base_name.c_str(),
win_98_recycle_bin_name,
strlen(win_98_recycle_bin_name)) == 0) ||
(base::strncasecmp(base_name.c_str(),
win_xp_recycle_bin_name,
strlen(win_xp_recycle_bin_name)) == 0) ||
(base::strncasecmp(base_name.c_str(),
win_vista_recycle_bin_name,
strlen(win_vista_recycle_bin_name)) == 0))
return true;
#endif
return false;
}
MediaPathFilter::MediaPathFilter()
: initialized_(false) {
sequence_checker_.DetachFromSequence();
}
MediaPathFilter::~MediaPathFilter() {
}
bool MediaPathFilter::Match(const base::FilePath& path) {
return GetType(path) != MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN;
}
MediaGalleryScanFileType MediaPathFilter::GetType(const base::FilePath& path) {
EnsureInitialized();
MediaFileExtensionMap::const_iterator it =
media_file_extensions_map_.find(StringToLowerASCII(path.Extension()));
if (it == media_file_extensions_map_.end())
return MEDIA_GALLERY_SCAN_FILE_TYPE_UNKNOWN;
return static_cast<MediaGalleryScanFileType>(it->second);
}
void MediaPathFilter::EnsureInitialized() {
DCHECK(sequence_checker_.CalledOnValidSequencedThread());
if (initialized_)
return;
AddExtensionsToMediaFileExtensionMap(GetMediaExtensionList("image/*"),
MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE);
AddExtensionsToMediaFileExtensionMap(GetMediaExtensionList("audio/*"),
MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO);
AddExtensionsToMediaFileExtensionMap(GetMediaExtensionList("video/*"),
MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO);
AddAdditionalExtensionsToMediaFileExtensionMap(
kExtraSupportedImageExtensions,
arraysize(kExtraSupportedImageExtensions),
MEDIA_GALLERY_SCAN_FILE_TYPE_IMAGE);
AddAdditionalExtensionsToMediaFileExtensionMap(
kExtraSupportedAudioExtensions,
arraysize(kExtraSupportedAudioExtensions),
MEDIA_GALLERY_SCAN_FILE_TYPE_AUDIO);
AddAdditionalExtensionsToMediaFileExtensionMap(
kExtraSupportedVideoExtensions,
arraysize(kExtraSupportedVideoExtensions),
MEDIA_GALLERY_SCAN_FILE_TYPE_VIDEO);
initialized_ = true;
}
void MediaPathFilter::AddExtensionsToMediaFileExtensionMap(
const MediaFileExtensionList& extensions_list,
MediaGalleryScanFileType type) {
for (size_t i = 0; i < extensions_list.size(); ++i)
AddExtensionToMediaFileExtensionMap(extensions_list[i].c_str(), type);
}
void MediaPathFilter::AddAdditionalExtensionsToMediaFileExtensionMap(
const base::FilePath::CharType* const* extensions_list,
size_t extensions_list_size,
MediaGalleryScanFileType type) {
for (size_t i = 0; i < extensions_list_size; ++i)
AddExtensionToMediaFileExtensionMap(extensions_list[i], type);
}
void MediaPathFilter::AddExtensionToMediaFileExtensionMap(
const base::FilePath::CharType* extension,
MediaGalleryScanFileType type) {
base::FilePath::StringType extension_with_sep =
base::FilePath::kExtensionSeparator +
base::FilePath::StringType(extension);
media_file_extensions_map_[extension_with_sep] |= type;
}