summaryrefslogtreecommitdiffstats
path: root/extensions
diff options
context:
space:
mode:
authorrdevlin.cronin@chromium.org <rdevlin.cronin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-20 21:14:08 +0000
committerrdevlin.cronin@chromium.org <rdevlin.cronin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-20 21:14:08 +0000
commit53c7e6f1fe76c02d7f1796eb218a2964efecb575 (patch)
tree5a102c9bf13f13492e5f409b20656fda1c1283c9 /extensions
parent5d648cea4c17461ac61f850dad35d4154c2c1ed0 (diff)
downloadchromium_src-53c7e6f1fe76c02d7f1796eb218a2964efecb575.zip
chromium_src-53c7e6f1fe76c02d7f1796eb218a2964efecb575.tar.gz
chromium_src-53c7e6f1fe76c02d7f1796eb218a2964efecb575.tar.bz2
Move FileHandlersInfo to /extensions and remove struct FileHandlersInfo
Move FileHandlersParser to /extensions. FileHandlersInfo was a struct which only contained a vector of FileHandlers. Instead, typedef a vector of FileHandlers as a FileHandlersInfo. BUG=374980 Review URL: https://codereview.chromium.org/280963002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@271750 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r--extensions/common/common_manifest_handlers.cc2
-rw-r--r--extensions/common/manifest_handlers/file_handler_info.cc160
-rw-r--r--extensions/common/manifest_handlers/file_handler_info.h61
-rw-r--r--extensions/common/manifest_handlers/file_handler_manifest_unittest.cc75
-rw-r--r--extensions/extensions.gyp2
5 files changed, 300 insertions, 0 deletions
diff --git a/extensions/common/common_manifest_handlers.cc b/extensions/common/common_manifest_handlers.cc
index 2dc14f2..c92c746 100644
--- a/extensions/common/common_manifest_handlers.cc
+++ b/extensions/common/common_manifest_handlers.cc
@@ -7,6 +7,7 @@
#include "extensions/common/manifest_handler.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/csp_info.h"
+#include "extensions/common/manifest_handlers/file_handler_info.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "extensions/common/manifest_handlers/kiosk_mode_info.h"
#include "extensions/common/manifest_handlers/offline_enabled_info.h"
@@ -22,6 +23,7 @@ void RegisterCommonManifestHandlers() {
(new BackgroundManifestHandler)->Register();
(new CSPHandler(false))->Register();
(new CSPHandler(true))->Register();
+ (new FileHandlersParser)->Register();
(new IncognitoHandler)->Register();
(new KioskModeHandler)->Register();
(new OfflineEnabledHandler)->Register();
diff --git a/extensions/common/manifest_handlers/file_handler_info.cc b/extensions/common/manifest_handlers/file_handler_info.cc
new file mode 100644
index 0000000..126e409
--- /dev/null
+++ b/extensions/common/manifest_handlers/file_handler_info.cc
@@ -0,0 +1,160 @@
+// 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/file_handler_info.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "extensions/common/error_utils.h"
+#include "extensions/common/manifest.h"
+#include "extensions/common/manifest_constants.h"
+
+namespace extensions {
+
+namespace keys = manifest_keys;
+namespace errors = manifest_errors;
+
+namespace {
+const int kMaxTypeAndExtensionHandlers = 200;
+}
+
+FileHandlerInfo::FileHandlerInfo() {}
+FileHandlerInfo::~FileHandlerInfo() {}
+
+FileHandlers::FileHandlers() {}
+FileHandlers::~FileHandlers() {}
+
+// static
+const FileHandlersInfo* FileHandlers::GetFileHandlers(
+ const Extension* extension) {
+ FileHandlers* info = static_cast<FileHandlers*>(
+ extension->GetManifestData(keys::kFileHandlers));
+ return info ? &info->file_handlers : NULL;
+}
+
+FileHandlersParser::FileHandlersParser() {
+}
+
+FileHandlersParser::~FileHandlersParser() {
+}
+
+bool LoadFileHandler(const std::string& handler_id,
+ const base::DictionaryValue& handler_info,
+ FileHandlersInfo* file_handlers,
+ base::string16* error) {
+ DCHECK(error);
+ FileHandlerInfo handler;
+
+ handler.id = handler_id;
+
+ const base::ListValue* mime_types = NULL;
+ if (handler_info.HasKey(keys::kFileHandlerTypes) &&
+ !handler_info.GetList(keys::kFileHandlerTypes, &mime_types)) {
+ *error = ErrorUtils::FormatErrorMessageUTF16(
+ errors::kInvalidFileHandlerType, handler_id);
+ return false;
+ }
+
+ const base::ListValue* file_extensions = NULL;
+ if (handler_info.HasKey(keys::kFileHandlerExtensions) &&
+ !handler_info.GetList(keys::kFileHandlerExtensions, &file_extensions)) {
+ *error = ErrorUtils::FormatErrorMessageUTF16(
+ errors::kInvalidFileHandlerExtension, handler_id);
+ return false;
+ }
+
+ if ((!mime_types || mime_types->empty()) &&
+ (!file_extensions || file_extensions->empty())) {
+ *error = ErrorUtils::FormatErrorMessageUTF16(
+ errors::kInvalidFileHandlerNoTypeOrExtension,
+ handler_id);
+ return false;
+ }
+
+ if (handler_info.HasKey(keys::kFileHandlerTitle) &&
+ !handler_info.GetString(keys::kFileHandlerTitle, &handler.title)) {
+ *error = base::ASCIIToUTF16(errors::kInvalidFileHandlerTitle);
+ return false;
+ }
+
+ if (mime_types) {
+ std::string type;
+ for (size_t i = 0; i < mime_types->GetSize(); ++i) {
+ if (!mime_types->GetString(i, &type)) {
+ *error = ErrorUtils::FormatErrorMessageUTF16(
+ errors::kInvalidFileHandlerTypeElement,
+ handler_id,
+ std::string(base::IntToString(i)));
+ return false;
+ }
+ handler.types.insert(type);
+ }
+ }
+
+ if (file_extensions) {
+ std::string file_extension;
+ for (size_t i = 0; i < file_extensions->GetSize(); ++i) {
+ if (!file_extensions->GetString(i, &file_extension)) {
+ *error = ErrorUtils::FormatErrorMessageUTF16(
+ errors::kInvalidFileHandlerExtensionElement,
+ handler_id,
+ std::string(base::IntToString(i)));
+ return false;
+ }
+ handler.extensions.insert(file_extension);
+ }
+ }
+
+ file_handlers->push_back(handler);
+ return true;
+}
+
+bool FileHandlersParser::Parse(Extension* extension, base::string16* error) {
+ scoped_ptr<FileHandlers> info(new FileHandlers);
+ const base::DictionaryValue* all_handlers = NULL;
+ if (!extension->manifest()->GetDictionary(keys::kFileHandlers,
+ &all_handlers)) {
+ *error = base::ASCIIToUTF16(errors::kInvalidFileHandlers);
+ return false;
+ }
+
+ for (base::DictionaryValue::Iterator iter(*all_handlers);
+ !iter.IsAtEnd();
+ iter.Advance()) {
+ // A file handler entry is a title and a list of MIME types to handle.
+ const base::DictionaryValue* handler = NULL;
+ if (iter.value().GetAsDictionary(&handler)) {
+ if (!LoadFileHandler(iter.key(), *handler, &info->file_handlers, error))
+ return false;
+ } else {
+ *error = base::ASCIIToUTF16(errors::kInvalidFileHandlers);
+ return false;
+ }
+ }
+
+ int filter_count = 0;
+ for (FileHandlersInfo::const_iterator iter = info->file_handlers.begin();
+ iter != info->file_handlers.end();
+ iter++) {
+ filter_count += iter->types.size();
+ filter_count += iter->extensions.size();
+ }
+
+ if (filter_count > kMaxTypeAndExtensionHandlers) {
+ *error = base::ASCIIToUTF16(
+ errors::kInvalidFileHandlersTooManyTypesAndExtensions);
+ return false;
+ }
+
+ extension->SetManifestData(keys::kFileHandlers, info.release());
+ return true;
+}
+
+const std::vector<std::string> FileHandlersParser::Keys() const {
+ return SingleKey(keys::kFileHandlers);
+}
+
+} // namespace extensions
diff --git a/extensions/common/manifest_handlers/file_handler_info.h b/extensions/common/manifest_handlers/file_handler_info.h
new file mode 100644
index 0000000..57c6e7a
--- /dev/null
+++ b/extensions/common/manifest_handlers/file_handler_info.h
@@ -0,0 +1,61 @@
+// 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_FILE_HANDLER_INFO_H_
+#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_FILE_HANDLER_INFO_H_
+
+#include <set>
+#include <string>
+#include <vector>
+
+#include "extensions/common/extension.h"
+#include "extensions/common/manifest_handler.h"
+
+namespace extensions {
+
+struct FileHandlerInfo {
+ FileHandlerInfo();
+ ~FileHandlerInfo();
+
+ // The id of this handler.
+ std::string id;
+
+ // The title of this handler.
+ std::string title;
+
+ // File extensions associated with this handler.
+ std::set<std::string> extensions;
+
+ // MIME types associated with this handler.
+ std::set<std::string> types;
+};
+
+typedef std::vector<FileHandlerInfo> FileHandlersInfo;
+
+struct FileHandlers : public Extension::ManifestData {
+ FileHandlers();
+ virtual ~FileHandlers();
+
+ FileHandlersInfo file_handlers;
+
+ static const FileHandlersInfo* GetFileHandlers(const Extension* extension);
+};
+
+// Parses the "file_handlers" manifest key.
+class FileHandlersParser : public ManifestHandler {
+ public:
+ FileHandlersParser();
+ virtual ~FileHandlersParser();
+
+ virtual bool Parse(Extension* extension, base::string16* error) OVERRIDE;
+
+ private:
+ virtual const std::vector<std::string> Keys() const OVERRIDE;
+
+ DISALLOW_COPY_AND_ASSIGN(FileHandlersParser);
+};
+
+} // namespace extensions
+
+#endif // EXTENSIONS_COMMON_MANIFEST_HANDLERS_FILE_HANDLER_INFO_H_
diff --git a/extensions/common/manifest_handlers/file_handler_manifest_unittest.cc b/extensions/common/manifest_handlers/file_handler_manifest_unittest.cc
new file mode 100644
index 0000000..2e08096
--- /dev/null
+++ b/extensions/common/manifest_handlers/file_handler_manifest_unittest.cc
@@ -0,0 +1,75 @@
+// 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 "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
+#include "extensions/common/manifest_constants.h"
+#include "extensions/common/manifest_handlers/file_handler_info.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+namespace errors = manifest_errors;
+
+class FileHandlersManifestTest : public ExtensionManifestTest {
+};
+
+TEST_F(FileHandlersManifestTest, InvalidFileHandlers) {
+ Testcase testcases[] = {
+ Testcase("file_handlers_invalid_handlers.json",
+ errors::kInvalidFileHandlers),
+ Testcase("file_handlers_invalid_type.json",
+ errors::kInvalidFileHandlerType),
+ Testcase("file_handlers_invalid_extension.json",
+ errors::kInvalidFileHandlerExtension),
+ Testcase("file_handlers_invalid_no_type_or_extension.json",
+ errors::kInvalidFileHandlerNoTypeOrExtension),
+ Testcase("file_handlers_invalid_title.json",
+ errors::kInvalidFileHandlerTitle),
+ Testcase("file_handlers_invalid_type_element.json",
+ errors::kInvalidFileHandlerTypeElement),
+ Testcase("file_handlers_invalid_extension_element.json",
+ errors::kInvalidFileHandlerExtensionElement),
+ Testcase("file_handlers_invalid_too_many.json",
+ errors::kInvalidFileHandlersTooManyTypesAndExtensions),
+ };
+ RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
+}
+
+TEST_F(FileHandlersManifestTest, ValidFileHandlers) {
+ scoped_refptr<const Extension> extension =
+ LoadAndExpectSuccess("file_handlers_valid.json");
+
+ ASSERT_TRUE(extension.get());
+ const FileHandlersInfo* handlers = FileHandlers::GetFileHandlers(extension);
+ ASSERT_TRUE(handlers != NULL);
+ ASSERT_EQ(2U, handlers->size());
+
+ FileHandlerInfo handler = handlers->at(0);
+ EXPECT_EQ("image", handler.id);
+ EXPECT_EQ("Image editor", handler.title);
+ EXPECT_EQ(1U, handler.types.size());
+ EXPECT_EQ(1U, handler.types.count("image/*"));
+ EXPECT_EQ(2U, handler.extensions.size());
+ EXPECT_EQ(1U, handler.extensions.count(".png"));
+ EXPECT_EQ(1U, handler.extensions.count(".gif"));
+
+ handler = handlers->at(1);
+ EXPECT_EQ("text", handler.id);
+ EXPECT_EQ("Text editor", handler.title);
+ EXPECT_EQ(1U, handler.types.size());
+ EXPECT_EQ(1U, handler.types.count("text/*"));
+ EXPECT_EQ(0U, handler.extensions.size());
+}
+
+TEST_F(FileHandlersManifestTest, NotPlatformApp) {
+ // This should load successfully but have the file handlers ignored.
+ scoped_refptr<const Extension> extension =
+ LoadAndExpectSuccess("file_handlers_invalid_not_app.json");
+
+ ASSERT_TRUE(extension.get());
+ const FileHandlersInfo* handlers = FileHandlers::GetFileHandlers(extension);
+ ASSERT_TRUE(handlers == NULL);
+}
+
+} // namespace extensions
diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp
index 8a2bf4c..7b43638 100644
--- a/extensions/extensions.gyp
+++ b/extensions/extensions.gyp
@@ -125,6 +125,8 @@
'common/manifest_handlers/csp_info.h',
'common/manifest_handlers/externally_connectable.cc',
'common/manifest_handlers/externally_connectable.h',
+ 'common/manifest_handlers/file_handler_info.cc',
+ 'common/manifest_handlers/file_handler_info.h',
'common/manifest_handlers/icons_handler.cc',
'common/manifest_handlers/icons_handler.h',
'common/manifest_handlers/incognito_info.cc',