| Index: extensions/common/api/declarative_net_request/rules_manifest_info.cc
|
| diff --git a/extensions/common/api/declarative_net_request/rules_manifest_info.cc b/extensions/common/api/declarative_net_request/rules_manifest_info.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b0c984fac313d23f42835d76bef18c9243333425
|
| --- /dev/null
|
| +++ b/extensions/common/api/declarative_net_request/rules_manifest_info.cc
|
| @@ -0,0 +1,109 @@
|
| +// Copyright 2017 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/api/declarative_net_request/rules_manifest_info.h"
|
| +
|
| +#include "base/files/file_path.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/strings/stringprintf.h"
|
| +#include "base/strings/utf_string_conversions.h"
|
| +#include "extensions/common/manifest_constants.h"
|
| +#include "extensions/common/manifest_handlers/permissions_parser.h"
|
| +#include "extensions/common/permissions/permissions_data.h"
|
| +
|
| +namespace extensions {
|
| +namespace declarative_net_request {
|
| +
|
| +namespace {
|
| +constexpr char kInvalidManifestKeyValue[] = "Invalid value for key %s. %s";
|
| +constexpr char kJsonFileExtension[] = ".json";
|
| +// TODO is this safe? Constants referring to other constants.
|
| +const auto& kManifestKey = manifest_keys::kDeclarativeNetRequestRulesetLocation;
|
| +}
|
| +
|
| +RulesManifestData::RulesManifestData(base::FilePath path)
|
| + : json_ruleset_path(std::move(path)) {}
|
| +RulesManifestData::~RulesManifestData() = default;
|
| +
|
| +// static
|
| +const base::FilePath* RulesManifestData::GetJSONRulesetPath(
|
| + const Extension* extension) {
|
| + Extension::ManifestData* data = extension->GetManifestData(
|
| + manifest_keys::kDeclarativeNetRequestRulesetLocation);
|
| + return data ? &(static_cast<RulesManifestData*>(data)->json_ruleset_path)
|
| + : nullptr;
|
| +}
|
| +
|
| +RulesManifestHandler::RulesManifestHandler() = default;
|
| +RulesManifestHandler::~RulesManifestHandler() = default;
|
| +
|
| +bool RulesManifestHandler::Parse(Extension* extension, base::string16* error) {
|
| + // TODO doing this again is redundant for files installed through CrxInstaller
|
| + // after installation?
|
| + DCHECK(extension->manifest()->HasKey(kManifestKey));
|
| +
|
| + std::string json_ruleset_location;
|
| + if (!extension->manifest()->GetString(kManifestKey, &json_ruleset_location)) {
|
| + *error = base::UTF8ToUTF16(base::StringPrintf(
|
| + kInvalidManifestKeyValue, kManifestKey, "It should be a string."));
|
| + return false;
|
| + }
|
| +
|
| + base::FilePath path(json_ruleset_location);
|
| + if (path.ReferencesParent()) {
|
| + *error = base::UTF8ToUTF16(
|
| + base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
|
| + "It should not refer the parent directory"));
|
| + return false;
|
| + }
|
| + if (!path.MatchesExtension(kJsonFileExtension)) {
|
| + *error = base::UTF8ToUTF16(base::StringPrintf(
|
| + kInvalidManifestKeyValue, kManifestKey, "It should be a json file."));
|
| + return false;
|
| + }
|
| +
|
| + if (path.IsAbsolute()) {
|
| + *error = base::UTF8ToUTF16(
|
| + base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
|
| + "It should be a relative path."));
|
| + return false;
|
| + }
|
| +
|
| + path = extension->path().Append(path);
|
| + if (!extension->path().IsParent(path)) {
|
| + *error = base::UTF8ToUTF16(base::StringPrintf(
|
| + kInvalidManifestKeyValue, kManifestKey, "Invalid path."));
|
| + return false;
|
| + }
|
| +
|
| + extension->SetManifestData(
|
| + kManifestKey, base::MakeUnique<RulesManifestData>(std::move(path)));
|
| +
|
| + // If an extension specifies a rules file, it gets the declarativeNetRequest
|
| + // permission implicitly.
|
| + PermissionsParser::AddAPIPermission(extension,
|
| + APIPermission::kDeclarativeNetRequest);
|
| + return true;
|
| +}
|
| +
|
| +bool RulesManifestHandler::Validate(
|
| + const Extension* extension,
|
| + std::string* error,
|
| + std::vector<InstallWarning>* warnings) const {
|
| + const base::FilePath* json_ruleset_path =
|
| + declarative_net_request::RulesManifestData::GetJSONRulesetPath(extension);
|
| + if (base::PathExists(*json_ruleset_path))
|
| + return true;
|
| + *error = base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
|
| + "Invalid path.");
|
| + return false;
|
| +}
|
| +
|
| +const std::vector<std::string> RulesManifestHandler::Keys() const {
|
| + return ManifestHandler::SingleKey(kManifestKey);
|
| +}
|
| +
|
| +} // namespace declarative_net_request
|
| +} // namespace extensions
|
|
|