Commit 0d5f8742 authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

Extensions: Use a single CSPHandler instance for both extensions and platform apps.

This CL changes CSPHandler so that it can be used for both extensions and
platform apps without creating a different instance for the two. This helps
simplify the code.

BUG=914224

Change-Id: Ib99fd306071514706d6a3a6b032870f016736a9e
Reviewed-on: https://chromium-review.googlesource.com/c/1394912Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619807}
parent 22b050d8
...@@ -52,8 +52,7 @@ void RegisterCommonManifestHandlers() { ...@@ -52,8 +52,7 @@ void RegisterCommonManifestHandlers() {
registry->RegisterHandler(std::make_unique<BluetoothManifestHandler>()); registry->RegisterHandler(std::make_unique<BluetoothManifestHandler>());
registry->RegisterHandler(std::make_unique<ContentCapabilitiesHandler>()); registry->RegisterHandler(std::make_unique<ContentCapabilitiesHandler>());
registry->RegisterHandler(std::make_unique<ContentScriptsHandler>()); registry->RegisterHandler(std::make_unique<ContentScriptsHandler>());
registry->RegisterHandler(std::make_unique<CSPHandler>(false)); registry->RegisterHandler(std::make_unique<CSPHandler>());
registry->RegisterHandler(std::make_unique<CSPHandler>(true));
registry->RegisterHandler( registry->RegisterHandler(
std::make_unique<declarative_net_request::DNRManifestHandler>()); std::make_unique<declarative_net_request::DNRManifestHandler>());
registry->RegisterHandler(std::make_unique<DeclarativeManifestHandler>()); registry->RegisterHandler(std::make_unique<DeclarativeManifestHandler>());
......
...@@ -110,15 +110,15 @@ const std::string& CSPInfo::GetResourceContentSecurityPolicy( ...@@ -110,15 +110,15 @@ const std::string& CSPInfo::GetResourceContentSecurityPolicy(
GetContentSecurityPolicy(extension); GetContentSecurityPolicy(extension);
} }
CSPHandler::CSPHandler(bool is_platform_app) CSPHandler::CSPHandler() = default;
: is_platform_app_(is_platform_app) {
}
CSPHandler::~CSPHandler() { CSPHandler::~CSPHandler() = default;
}
bool CSPHandler::Parse(Extension* extension, base::string16* error) { bool CSPHandler::Parse(Extension* extension, base::string16* error) {
const std::string key = Keys()[0]; const char* key = extension->GetType() == Manifest::TYPE_PLATFORM_APP
? keys::kPlatformAppContentSecurityPolicy
: keys::kContentSecurityPolicy;
// The "content_security_policy" manifest key can either be a string or a // The "content_security_policy" manifest key can either be a string or a
// dictionary of the format // dictionary of the format
// "content_security_policy" : { // "content_security_policy" : {
...@@ -131,7 +131,7 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) { ...@@ -131,7 +131,7 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) {
// TODO(crbug.com/914224): Remove the channel check once the support for the // TODO(crbug.com/914224): Remove the channel check once the support for the
// dictionary key is launched to other channels. // dictionary key is launched to other channels.
bool csp_dictionary_supported = bool csp_dictionary_supported =
!is_platform_app_ && extension->GetType() == Manifest::TYPE_EXTENSION &&
GetCurrentChannel() == version_info::Channel::UNKNOWN; GetCurrentChannel() == version_info::Channel::UNKNOWN;
if (csp_dictionary_supported && csp && csp->is_dict()) if (csp_dictionary_supported && csp && csp->is_dict())
return ParseCSPDictionary(extension, error, *csp); return ParseCSPDictionary(extension, error, *csp);
...@@ -185,8 +185,9 @@ bool CSPHandler::SetDefaultExtensionPagesCSP(Extension* extension) { ...@@ -185,8 +185,9 @@ bool CSPHandler::SetDefaultExtensionPagesCSP(Extension* extension) {
// TODO(abarth): Should we continue to let extensions override the // TODO(abarth): Should we continue to let extensions override the
// default Content-Security-Policy? // default Content-Security-Policy?
const char* content_security_policy = const char* content_security_policy =
is_platform_app_ ? kDefaultPlatformAppContentSecurityPolicy extension->GetType() == Manifest::TYPE_PLATFORM_APP
: kDefaultContentSecurityPolicy; ? kDefaultPlatformAppContentSecurityPolicy
: kDefaultContentSecurityPolicy;
DCHECK_EQ( DCHECK_EQ(
content_security_policy, content_security_policy,
...@@ -199,20 +200,14 @@ bool CSPHandler::SetDefaultExtensionPagesCSP(Extension* extension) { ...@@ -199,20 +200,14 @@ bool CSPHandler::SetDefaultExtensionPagesCSP(Extension* extension) {
} }
bool CSPHandler::AlwaysParseForType(Manifest::Type type) const { bool CSPHandler::AlwaysParseForType(Manifest::Type type) const {
if (is_platform_app_) return type == Manifest::TYPE_PLATFORM_APP ||
return type == Manifest::TYPE_PLATFORM_APP; type == Manifest::TYPE_EXTENSION ||
else type == Manifest::TYPE_LEGACY_PACKAGED_APP;
return type == Manifest::TYPE_EXTENSION ||
type == Manifest::TYPE_LEGACY_PACKAGED_APP;
} }
base::span<const char* const> CSPHandler::Keys() const { base::span<const char* const> CSPHandler::Keys() const {
if (is_platform_app_) { static constexpr const char* kKeys[] = {
static constexpr const char* kKeys[] = { keys::kContentSecurityPolicy, keys::kPlatformAppContentSecurityPolicy};
keys::kPlatformAppContentSecurityPolicy};
return kKeys;
}
static constexpr const char* kKeys[] = {keys::kContentSecurityPolicy};
return kKeys; return kKeys;
} }
......
...@@ -36,7 +36,7 @@ struct CSPInfo : public Extension::ManifestData { ...@@ -36,7 +36,7 @@ struct CSPInfo : public Extension::ManifestData {
// Parses "content_security_policy" and "app.content_security_policy" keys. // Parses "content_security_policy" and "app.content_security_policy" keys.
class CSPHandler : public ManifestHandler { class CSPHandler : public ManifestHandler {
public: public:
explicit CSPHandler(bool is_platform_app); CSPHandler();
~CSPHandler() override; ~CSPHandler() override;
bool Parse(Extension* extension, base::string16* error) override; bool Parse(Extension* extension, base::string16* error) override;
...@@ -60,8 +60,6 @@ class CSPHandler : public ManifestHandler { ...@@ -60,8 +60,6 @@ class CSPHandler : public ManifestHandler {
base::span<const char* const> Keys() const override; base::span<const char* const> Keys() const override;
bool is_platform_app_;
DISALLOW_COPY_AND_ASSIGN(CSPHandler); DISALLOW_COPY_AND_ASSIGN(CSPHandler);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment