From 474b1f765525cd4946819bf9fa8971711d9bdef7 Mon Sep 17 00:00:00 2001 From: "jamescook@chromium.org" Date: Thu, 16 Jan 2014 02:38:07 +0000 Subject: Move webview and web_accessible_resources manifest handlers to src/extensions This is a mechanical change -- these classes have no Chrome dependencies. This is a step toward moving extension_protocol.h, which is needed for app_shell. BUG=none TEST=compiles, unit_tests Review URL: https://codereview.chromium.org/134643011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245070 0039d316-1c4b-4281-b951-d872f2087c98 --- extensions/common/common_manifest_handlers.cc | 4 + .../web_accessible_resources_info.cc | 99 ++++++++++++++++ .../web_accessible_resources_info.h | 50 ++++++++ .../common/manifest_handlers/webview_info.cc | 128 +++++++++++++++++++++ extensions/common/manifest_handlers/webview_info.h | 53 +++++++++ extensions/extensions.gyp | 4 + 6 files changed, 338 insertions(+) create mode 100644 extensions/common/manifest_handlers/web_accessible_resources_info.cc create mode 100644 extensions/common/manifest_handlers/web_accessible_resources_info.h create mode 100644 extensions/common/manifest_handlers/webview_info.cc create mode 100644 extensions/common/manifest_handlers/webview_info.h (limited to 'extensions') diff --git a/extensions/common/common_manifest_handlers.cc b/extensions/common/common_manifest_handlers.cc index bbea1c2..2dc14f2 100644 --- a/extensions/common/common_manifest_handlers.cc +++ b/extensions/common/common_manifest_handlers.cc @@ -12,6 +12,8 @@ #include "extensions/common/manifest_handlers/offline_enabled_info.h" #include "extensions/common/manifest_handlers/sandboxed_page_info.h" #include "extensions/common/manifest_handlers/shared_module_info.h" +#include "extensions/common/manifest_handlers/web_accessible_resources_info.h" +#include "extensions/common/manifest_handlers/webview_info.h" namespace extensions { @@ -25,6 +27,8 @@ void RegisterCommonManifestHandlers() { (new OfflineEnabledHandler)->Register(); (new SandboxedPageHandler)->Register(); (new SharedModuleHandler)->Register(); + (new WebAccessibleResourcesHandler)->Register(); + (new WebviewHandler)->Register(); } } // namespace extensions diff --git a/extensions/common/manifest_handlers/web_accessible_resources_info.cc b/extensions/common/manifest_handlers/web_accessible_resources_info.cc new file mode 100644 index 0000000..8da6bf5 --- /dev/null +++ b/extensions/common/manifest_handlers/web_accessible_resources_info.cc @@ -0,0 +1,99 @@ +// 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/web_accessible_resources_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 WebAccessibleResourcesInfo* GetResourcesInfo(const Extension* extension) { + return static_cast( + extension->GetManifestData(keys::kWebAccessibleResources)); +} + +} // namespace + +WebAccessibleResourcesInfo::WebAccessibleResourcesInfo() { +} + +WebAccessibleResourcesInfo::~WebAccessibleResourcesInfo() { +} + +// static +bool WebAccessibleResourcesInfo::IsResourceWebAccessible( + const Extension* extension, + const std::string& relative_path) { + // For old manifest versions which do not specify web_accessible_resources + // we always allow resource loads. + if (extension->manifest_version() < 2 && + !WebAccessibleResourcesInfo::HasWebAccessibleResources(extension)) + return true; + + const WebAccessibleResourcesInfo* info = GetResourcesInfo(extension); + return info && + extension->ResourceMatches( + info->web_accessible_resources_, relative_path); +} + +// static +bool WebAccessibleResourcesInfo::HasWebAccessibleResources( + const Extension* extension) { + const WebAccessibleResourcesInfo* info = GetResourcesInfo(extension); + return info && info->web_accessible_resources_.size() > 0; +} + +WebAccessibleResourcesHandler::WebAccessibleResourcesHandler() { +} + +WebAccessibleResourcesHandler::~WebAccessibleResourcesHandler() { +} + +bool WebAccessibleResourcesHandler::Parse(Extension* extension, + base::string16* error) { + scoped_ptr info(new WebAccessibleResourcesInfo); + const base::ListValue* list_value = NULL; + if (!extension->manifest()->GetList(keys::kWebAccessibleResources, + &list_value)) { + *error = base::ASCIIToUTF16(errors::kInvalidWebAccessibleResourcesList); + return false; + } + for (size_t i = 0; i < list_value->GetSize(); ++i) { + std::string relative_path; + if (!list_value->GetString(i, &relative_path)) { + *error = ErrorUtils::FormatErrorMessageUTF16( + errors::kInvalidWebAccessibleResource, base::IntToString(i)); + return false; + } + URLPattern pattern(URLPattern::SCHEME_EXTENSION); + if (pattern.Parse(extension->url().spec()) != URLPattern::PARSE_SUCCESS) { + *error = ErrorUtils::FormatErrorMessageUTF16( + errors::kInvalidURLPatternError, extension->url().spec()); + return false; + } + while (relative_path[0] == '/') + relative_path = relative_path.substr(1, relative_path.length() - 1); + pattern.SetPath(pattern.path() + relative_path); + info->web_accessible_resources_.AddPattern(pattern); + } + extension->SetManifestData(keys::kWebAccessibleResources, info.release()); + return true; +} + +const std::vector WebAccessibleResourcesHandler::Keys() const { + return SingleKey(keys::kWebAccessibleResources); +} + +} // namespace extensions diff --git a/extensions/common/manifest_handlers/web_accessible_resources_info.h b/extensions/common/manifest_handlers/web_accessible_resources_info.h new file mode 100644 index 0000000..2d0ed81 --- /dev/null +++ b/extensions/common/manifest_handlers/web_accessible_resources_info.h @@ -0,0 +1,50 @@ +// 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_WEB_ACCESSIBLE_RESOURCES_INFO_H_ +#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_WEB_ACCESSIBLE_RESOURCES_INFO_H_ + +#include + +#include "extensions/common/extension.h" +#include "extensions/common/manifest_handler.h" + +namespace extensions { + +// A structure to hold the web accessible extension resources +// that may be specified in the manifest of an extension using +// "web_accessible_resources" key. +struct WebAccessibleResourcesInfo : public Extension::ManifestData { + // Define out of line constructor/destructor to please Clang. + WebAccessibleResourcesInfo(); + virtual ~WebAccessibleResourcesInfo(); + + // Returns true if the specified resource is web accessible. + static bool IsResourceWebAccessible(const Extension* extension, + const std::string& relative_path); + + // Returns true when 'web_accessible_resources' are defined for the extension. + static bool HasWebAccessibleResources(const Extension* extension); + + // Optional list of web accessible extension resources. + URLPatternSet web_accessible_resources_; +}; + +// Parses the "web_accessible_resources" manifest key. +class WebAccessibleResourcesHandler : public ManifestHandler { + public: + WebAccessibleResourcesHandler(); + virtual ~WebAccessibleResourcesHandler(); + + virtual bool Parse(Extension* extension, base::string16* error) OVERRIDE; + + private: + virtual const std::vector Keys() const OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(WebAccessibleResourcesHandler); +}; + +} // namespace extensions + +#endif // EXTENSIONS_COMMON_MANIFEST_HANDLERS_WEB_ACCESSIBLE_RESOURCES_INFO_H_ diff --git a/extensions/common/manifest_handlers/webview_info.cc b/extensions/common/manifest_handlers/webview_info.cc new file mode 100644 index 0000000..5240099 --- /dev/null +++ b/extensions/common/manifest_handlers/webview_info.cc @@ -0,0 +1,128 @@ +// 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/webview_info.h" + +#include "base/memory/scoped_ptr.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_util.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 = extensions::manifest_keys; +namespace errors = extensions::manifest_errors; + +namespace { + +const WebviewInfo* GetResourcesInfo( + const Extension& extension) { + return static_cast( + extension.GetManifestData(keys::kWebviewAccessibleResources)); +} + +} // namespace + +WebviewInfo::WebviewInfo() { +} + +WebviewInfo::~WebviewInfo() { +} + +// static +bool WebviewInfo::IsResourceWebviewAccessible( + const Extension* extension, + const std::string& partition_id, + const std::string& relative_path) { + if (!extension) + return false; + + const WebviewInfo* info = GetResourcesInfo(*extension); + if (!info) + return false; + + bool partition_is_privileged = false; + for (size_t i = 0; + i < info->webview_privileged_partitions_.size(); + ++i) { + if (MatchPattern(partition_id, info->webview_privileged_partitions_[i])) { + partition_is_privileged = true; + break; + } + } + + return partition_is_privileged && extension->ResourceMatches( + info->webview_accessible_resources_, relative_path); +} + +WebviewHandler::WebviewHandler() { +} + +WebviewHandler::~WebviewHandler() { +} + +bool WebviewHandler::Parse(Extension* extension, base::string16* error) { + scoped_ptr info(new WebviewInfo()); + + const base::DictionaryValue* dict_value = NULL; + if (!extension->manifest()->GetDictionary(keys::kWebview, + &dict_value)) { + *error = base::ASCIIToUTF16(errors::kInvalidWebview); + return false; + } + + const base::ListValue* url_list = NULL; + if (!dict_value->GetList(keys::kWebviewAccessibleResources, + &url_list)) { + *error = base::ASCIIToUTF16(errors::kInvalidWebviewAccessibleResourcesList); + return false; + } + + for (size_t i = 0; i < url_list->GetSize(); ++i) { + std::string relative_path; + if (!url_list->GetString(i, &relative_path)) { + *error = ErrorUtils::FormatErrorMessageUTF16( + errors::kInvalidWebviewAccessibleResource, base::IntToString(i)); + return false; + } + URLPattern pattern(URLPattern::SCHEME_EXTENSION); + if (pattern.Parse(extension->url().spec()) != URLPattern::PARSE_SUCCESS) { + *error = ErrorUtils::FormatErrorMessageUTF16( + errors::kInvalidURLPatternError, extension->url().spec()); + return false; + } + while (relative_path[0] == '/') + relative_path = relative_path.substr(1, relative_path.length() - 1); + pattern.SetPath(pattern.path() + relative_path); + info->webview_accessible_resources_.AddPattern(pattern); + } + + const base::ListValue* partition_list = NULL; + if (!dict_value->GetList(keys::kWebviewPrivilegedPartitions, + &partition_list)) { + *error = base::ASCIIToUTF16(errors::kInvalidWebviewPrivilegedPartitionList); + return false; + } + for (size_t i = 0; i < partition_list->GetSize(); ++i) { + std::string partition_wildcard; + if (!partition_list->GetString(i, &partition_wildcard)) { + *error = ErrorUtils::FormatErrorMessageUTF16( + errors::kInvalidWebviewPrivilegedPartition, base::IntToString(i)); + return false; + } + info->webview_privileged_partitions_.push_back(partition_wildcard); + } + extension->SetManifestData(keys::kWebviewAccessibleResources, info.release()); + return true; +} + +const std::vector WebviewHandler::Keys() const { + return SingleKey(keys::kWebview); +} + +} // namespace extensions diff --git a/extensions/common/manifest_handlers/webview_info.h b/extensions/common/manifest_handlers/webview_info.h new file mode 100644 index 0000000..d32d145 --- /dev/null +++ b/extensions/common/manifest_handlers/webview_info.h @@ -0,0 +1,53 @@ +// 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_WEBVIEW_INFO_H_ +#define EXTENSIONS_COMMON_MANIFEST_HANDLERS_WEBVIEW_INFO_H_ + +#include + +#include "extensions/common/extension.h" +#include "extensions/common/manifest_handler.h" + +namespace extensions { + +// A structure to hold the accessible extension resources +// that may be specified in the manifest of an extension using the +// "webview" key. +struct WebviewInfo : public Extension::ManifestData { + // Define out of line constructor/destructor to please Clang. + WebviewInfo(); + virtual ~WebviewInfo(); + + // Returns true if the specified resource is web accessible. + static bool IsResourceWebviewAccessible(const Extension* extension, + const std::string& partition_id, + const std::string& relative_path); + + // Returns true when 'webview_accessible_resources' are defined for the + // app. + static bool HasWebviewAccessibleResources(const Extension* extension); + + // Optional list of webview accessible extension resources. + std::vector webview_privileged_partitions_; + URLPatternSet webview_accessible_resources_; +}; + +// Parses the "webview" manifest key. +class WebviewHandler : public ManifestHandler { + public: + WebviewHandler(); + virtual ~WebviewHandler(); + + virtual bool Parse(Extension* extension, base::string16* error) OVERRIDE; + + private: + virtual const std::vector Keys() const OVERRIDE; + + DISALLOW_COPY_AND_ASSIGN(WebviewHandler); +}; + +} // namespace extensions + +#endif // EXTENSIONS_COMMON_MANIFEST_HANDLERS_WEBVIEW_INFO_H_ diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index ba10feb..dfc66c10 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -92,6 +92,10 @@ 'common/manifest_handlers/sandboxed_page_info.h', 'common/manifest_handlers/shared_module_info.cc', 'common/manifest_handlers/shared_module_info.h', + 'common/manifest_handlers/web_accessible_resources_info.cc', + 'common/manifest_handlers/web_accessible_resources_info.h', + 'common/manifest_handlers/webview_info.cc', + 'common/manifest_handlers/webview_info.h', 'common/one_shot_event.cc', 'common/one_shot_event.h', 'common/permissions/api_permission.cc', -- cgit v1.1