1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/common/extensions/extension_constants.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/manifest.h"
14 #include "extensions/common/manifest_constants.h"
15
16 namespace keys = extensions::manifest_keys;
17 namespace errors = extensions::manifest_errors;
18
19 namespace {
20
21 const char* const kMIMETypeHandlersWhitelist[] = {
22 extension_misc::kPdfExtensionId,
23 extension_misc::kQuickOfficeComponentExtensionId,
24 extension_misc::kQuickOfficeInternalExtensionId,
25 extension_misc::kQuickOfficeExtensionId,
26 extension_misc::kStreamsPrivateTestExtensionId};
27
28 // Stored on the Extension.
29 struct MimeTypesHandlerInfo : public extensions::Extension::ManifestData {
30 MimeTypesHandler handler_;
31
32 MimeTypesHandlerInfo();
33 virtual ~MimeTypesHandlerInfo();
34 };
35
MimeTypesHandlerInfo()36 MimeTypesHandlerInfo::MimeTypesHandlerInfo() {
37 }
38
~MimeTypesHandlerInfo()39 MimeTypesHandlerInfo::~MimeTypesHandlerInfo() {
40 }
41
42 } // namespace
43
44 // static
GetMIMETypeWhitelist()45 std::vector<std::string> MimeTypesHandler::GetMIMETypeWhitelist() {
46 std::vector<std::string> whitelist;
47 for (size_t i = 0; i < arraysize(kMIMETypeHandlersWhitelist); ++i)
48 whitelist.push_back(kMIMETypeHandlersWhitelist[i]);
49 return whitelist;
50 }
51
MimeTypesHandler()52 MimeTypesHandler::MimeTypesHandler() {
53 }
54
~MimeTypesHandler()55 MimeTypesHandler::~MimeTypesHandler() {
56 }
57
AddMIMEType(const std::string & mime_type)58 void MimeTypesHandler::AddMIMEType(const std::string& mime_type) {
59 mime_type_set_.insert(mime_type);
60 }
61
CanHandleMIMEType(const std::string & mime_type) const62 bool MimeTypesHandler::CanHandleMIMEType(const std::string& mime_type) const {
63 return mime_type_set_.find(mime_type) != mime_type_set_.end();
64 }
65
66 // static
GetHandler(const extensions::Extension * extension)67 MimeTypesHandler* MimeTypesHandler::GetHandler(
68 const extensions::Extension* extension) {
69 MimeTypesHandlerInfo* info = static_cast<MimeTypesHandlerInfo*>(
70 extension->GetManifestData(keys::kMimeTypesHandler));
71 if (info)
72 return &info->handler_;
73 return NULL;
74 }
75
MimeTypesHandlerParser()76 MimeTypesHandlerParser::MimeTypesHandlerParser() {
77 }
78
~MimeTypesHandlerParser()79 MimeTypesHandlerParser::~MimeTypesHandlerParser() {
80 }
81
Parse(extensions::Extension * extension,base::string16 * error)82 bool MimeTypesHandlerParser::Parse(extensions::Extension* extension,
83 base::string16* error) {
84 const base::ListValue* mime_types_value = NULL;
85 if (!extension->manifest()->GetList(keys::kMIMETypes,
86 &mime_types_value)) {
87 *error = base::ASCIIToUTF16(errors::kInvalidMimeTypesHandler);
88 return false;
89 }
90
91 scoped_ptr<MimeTypesHandlerInfo> info(new MimeTypesHandlerInfo);
92 info->handler_.set_extension_id(extension->id());
93 for (size_t i = 0; i < mime_types_value->GetSize(); ++i) {
94 std::string filter;
95 if (!mime_types_value->GetString(i, &filter)) {
96 *error = base::ASCIIToUTF16(errors::kInvalidMIMETypes);
97 return false;
98 }
99 info->handler_.AddMIMEType(filter);
100 }
101
102 std::string mime_types_handler;
103 if (extension->manifest()->GetString(keys::kMimeTypesHandler,
104 &mime_types_handler)) {
105 info->handler_.set_handler_url(mime_types_handler);
106 }
107
108 extension->SetManifestData(keys::kMimeTypesHandler, info.release());
109 return true;
110 }
111
Keys() const112 const std::vector<std::string> MimeTypesHandlerParser::Keys() const {
113 std::vector<std::string> keys;
114 keys.push_back(keys::kMIMETypes);
115 keys.push_back(keys::kMimeTypesHandler);
116 return keys;
117 }
118