summaryrefslogtreecommitdiffstats
path: root/extensions/common/manifest_handlers
diff options
context:
space:
mode:
authorsammc <sammc@chromium.org>2015-01-22 17:21:54 -0800
committerCommit bot <commit-bot@chromium.org>2015-01-23 01:23:21 +0000
commitc5eb526d0e15b28dbbbcaae87d82e022e413b20d (patch)
tree4712813291096e6b9d16f61dd691b01f6089bc41 /extensions/common/manifest_handlers
parent354304bc08fc693965750b27070e59bee364a922 (diff)
downloadchromium_src-c5eb526d0e15b28dbbbcaae87d82e022e413b20d.zip
chromium_src-c5eb526d0e15b28dbbbcaae87d82e022e413b20d.tar.gz
chromium_src-c5eb526d0e15b28dbbbcaae87d82e022e413b20d.tar.bz2
Add a mimeHandler extension API.
This adds the mimeHandler extension API, which is a simplified version of the streamsPrivateApi. Unlike the streamsPrivateApi, mimeHandler only allows access to streams from RenderFrames within MimeHandlerViewGuests, and in particular only allows access to the stream being handled by that MimeHandlerViewGuest instance. This removes the need for an event to be dispatched to the background page and a user-exposed view_id. BUG=439867 Review URL: https://codereview.chromium.org/797183005 Cr-Commit-Position: refs/heads/master@{#312728}
Diffstat (limited to 'extensions/common/manifest_handlers')
-rw-r--r--extensions/common/manifest_handlers/mime_types_handler.cc117
-rw-r--r--extensions/common/manifest_handlers/mime_types_handler.h68
2 files changed, 185 insertions, 0 deletions
diff --git a/extensions/common/manifest_handlers/mime_types_handler.cc b/extensions/common/manifest_handlers/mime_types_handler.cc
new file mode 100644
index 0000000..7c4f034
--- /dev/null
+++ b/extensions/common/manifest_handlers/mime_types_handler.cc
@@ -0,0 +1,117 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "extensions/common/manifest_handlers/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 "extensions/common/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::kMimeHandlerPrivateTestExtensionId};
+
+// Stored on the Extension.
+struct MimeTypesHandlerInfo : public extensions::Extension::ManifestData {
+ MimeTypesHandler handler_;
+
+ MimeTypesHandlerInfo();
+ ~MimeTypesHandlerInfo() override;
+};
+
+MimeTypesHandlerInfo::MimeTypesHandlerInfo() {
+}
+
+MimeTypesHandlerInfo::~MimeTypesHandlerInfo() {
+}
+
+} // namespace
+
+// static
+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();
+}
+
+// static
+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);
+ }
+
+ std::string mime_types_handler;
+ if (extension->manifest()->GetString(keys::kMimeTypesHandler,
+ &mime_types_handler)) {
+ info->handler_.set_handler_url(mime_types_handler);
+ }
+
+ extension->SetManifestData(keys::kMimeTypesHandler, info.release());
+ return true;
+}
+
+const std::vector<std::string> MimeTypesHandlerParser::Keys() const {
+ std::vector<std::string> keys;
+ keys.push_back(keys::kMIMETypes);
+ keys.push_back(keys::kMimeTypesHandler);
+ return keys;
+}
diff --git a/extensions/common/manifest_handlers/mime_types_handler.h b/extensions/common/manifest_handlers/mime_types_handler.h
new file mode 100644
index 0000000..887e379
--- /dev/null
+++ b/extensions/common/manifest_handlers/mime_types_handler.h
@@ -0,0 +1,68 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef EXTENSIONS_COMMON_MANIFEST_HANDLERS_MIME_TYPES_HANDLER_H_
+#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_MIME_TYPES_HANDLER_H_
+
+#include <set>
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "extensions/common/extension.h"
+#include "extensions/common/manifest_handler.h"
+
+class MimeTypesHandler {
+ public:
+ // Returns list of extensions' ids that are allowed to use MIME type filters.
+ static std::vector<std::string> GetMIMETypeWhitelist();
+
+ static MimeTypesHandler* GetHandler(const extensions::Extension* extension);
+
+ MimeTypesHandler();
+ ~MimeTypesHandler();
+
+ // extension id
+ std::string extension_id() const { return extension_id_; }
+ void set_extension_id(const std::string& extension_id) {
+ extension_id_ = extension_id;
+ }
+
+ // Adds a MIME type filter to the handler.
+ void AddMIMEType(const std::string& mime_type);
+ // Tests if the handler has registered a filter for the MIME type.
+ bool CanHandleMIMEType(const std::string& mime_type) const;
+
+ // Set the URL that will be used to handle MIME type requests.
+ void set_handler_url(const std::string& handler_url) {
+ handler_url_ = handler_url;
+ }
+ // The URL that will be used to handle MIME type requests.
+ const std::string& handler_url() const { return handler_url_; }
+
+ const std::set<std::string>& mime_type_set() const { return mime_type_set_; }
+
+ private:
+ // The id for the extension this action belongs to (as defined in the
+ // extension manifest).
+ std::string extension_id_;
+
+ // A list of MIME type filters.
+ std::set<std::string> mime_type_set_;
+
+ std::string handler_url_;
+};
+
+class MimeTypesHandlerParser : public extensions::ManifestHandler {
+ public:
+ MimeTypesHandlerParser();
+ ~MimeTypesHandlerParser() override;
+
+ bool Parse(extensions::Extension* extension, base::string16* error) override;
+
+ private:
+ const std::vector<std::string> Keys() const override;
+};
+
+#endif // EXTENSIONS_COMMON_MANIFEST_HANDLERS_MIME_TYPES_HANDLER_H_