This source file includes following definitions.
- GetMIMETypeWhitelist
- AddMIMEType
- CanHandleMIMEType
- GetHandler
- Keys
#include "chrome/common/extensions/mime_types_handler.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/common/extensions/extension_constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
namespace keys = extensions::manifest_keys;
namespace errors = extensions::manifest_errors;
namespace {
const char* const kMIMETypeHandlersWhitelist[] = {
extension_misc::kPdfExtensionId,
extension_misc::kQuickOfficeComponentExtensionId,
extension_misc::kQuickOfficeInternalExtensionId,
extension_misc::kQuickOfficeExtensionId,
extension_misc::kStreamsPrivateTestExtensionId};
struct MimeTypesHandlerInfo : public extensions::Extension::ManifestData {
MimeTypesHandler handler_;
MimeTypesHandlerInfo();
virtual ~MimeTypesHandlerInfo();
};
MimeTypesHandlerInfo::MimeTypesHandlerInfo() {
}
MimeTypesHandlerInfo::~MimeTypesHandlerInfo() {
}
}
std::vector<std::string> MimeTypesHandler::GetMIMETypeWhitelist() {
std::vector<std::string> whitelist;
for (size_t i = 0; i < arraysize(kMIMETypeHandlersWhitelist); ++i)
whitelist.push_back(kMIMETypeHandlersWhitelist[i]);
return whitelist;
}
MimeTypesHandler::MimeTypesHandler() {
}
MimeTypesHandler::~MimeTypesHandler() {
}
void MimeTypesHandler::AddMIMEType(const std::string& mime_type) {
mime_type_set_.insert(mime_type);
}
bool MimeTypesHandler::CanHandleMIMEType(const std::string& mime_type) const {
return mime_type_set_.find(mime_type) != mime_type_set_.end();
}
MimeTypesHandler* MimeTypesHandler::GetHandler(
const extensions::Extension* extension) {
MimeTypesHandlerInfo* info = static_cast<MimeTypesHandlerInfo*>(
extension->GetManifestData(keys::kMimeTypesHandler));
if (info)
return &info->handler_;
return NULL;
}
MimeTypesHandlerParser::MimeTypesHandlerParser() {
}
MimeTypesHandlerParser::~MimeTypesHandlerParser() {
}
bool MimeTypesHandlerParser::Parse(extensions::Extension* extension,
base::string16* error) {
const base::ListValue* mime_types_value = NULL;
if (!extension->manifest()->GetList(keys::kMIMETypes,
&mime_types_value)) {
*error = base::ASCIIToUTF16(errors::kInvalidMimeTypesHandler);
return false;
}
scoped_ptr<MimeTypesHandlerInfo> info(new MimeTypesHandlerInfo);
info->handler_.set_extension_id(extension->id());
for (size_t i = 0; i < mime_types_value->GetSize(); ++i) {
std::string filter;
if (!mime_types_value->GetString(i, &filter)) {
*error = base::ASCIIToUTF16(errors::kInvalidMIMETypes);
return false;
}
info->handler_.AddMIMEType(filter);
}
extension->SetManifestData(keys::kMimeTypesHandler, info.release());
return true;
}
const std::vector<std::string> MimeTypesHandlerParser::Keys() const {
return SingleKey(keys::kMIMETypes);
}