Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(770)

Side by Side Diff: extensions/common/api/declarative_net_request/rules_manifest_info.cc

Issue 2881453002: DNR Prototype: With flatbuffers
Patch Set: -- Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "extensions/common/api/declarative_net_request/rules_manifest_info.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/ptr_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "extensions/common/manifest_handlers/permissions_parser.h"
14 #include "extensions/common/permissions/permissions_data.h"
15
16 namespace extensions {
17 namespace declarative_net_request {
18
19 namespace {
20 constexpr char kInvalidManifestKeyValue[] = "Invalid value for key %s. %s";
21 constexpr char kJsonFileExtension[] = ".json";
22 // TODO is this safe? Constants referring to other constants.
23 const auto& kManifestKey = manifest_keys::kDeclarativeNetRequestRulesetLocation;
24 }
25
26 RulesManifestData::RulesManifestData(base::FilePath path)
27 : json_ruleset_path(std::move(path)) {}
28 RulesManifestData::~RulesManifestData() = default;
29
30 // static
31 const base::FilePath* RulesManifestData::GetJSONRulesetPath(
32 const Extension* extension) {
33 Extension::ManifestData* data = extension->GetManifestData(
34 manifest_keys::kDeclarativeNetRequestRulesetLocation);
35 return data ? &(static_cast<RulesManifestData*>(data)->json_ruleset_path)
36 : nullptr;
37 }
38
39 RulesManifestHandler::RulesManifestHandler() = default;
40 RulesManifestHandler::~RulesManifestHandler() = default;
41
42 bool RulesManifestHandler::Parse(Extension* extension, base::string16* error) {
43 // TODO doing this again is redundant for files installed through CrxInstaller
44 // after installation?
45 DCHECK(extension->manifest()->HasKey(kManifestKey));
46
47 std::string json_ruleset_location;
48 if (!extension->manifest()->GetString(kManifestKey, &json_ruleset_location)) {
49 *error = base::UTF8ToUTF16(base::StringPrintf(
50 kInvalidManifestKeyValue, kManifestKey, "It should be a string."));
51 return false;
52 }
53
54 base::FilePath path(json_ruleset_location);
55 if (path.ReferencesParent()) {
56 *error = base::UTF8ToUTF16(
57 base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
58 "It should not refer the parent directory"));
59 return false;
60 }
61 if (!path.MatchesExtension(kJsonFileExtension)) {
62 *error = base::UTF8ToUTF16(base::StringPrintf(
63 kInvalidManifestKeyValue, kManifestKey, "It should be a json file."));
64 return false;
65 }
66
67 if (path.IsAbsolute()) {
68 *error = base::UTF8ToUTF16(
69 base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
70 "It should be a relative path."));
71 return false;
72 }
73
74 path = extension->path().Append(path);
75 if (!extension->path().IsParent(path)) {
76 *error = base::UTF8ToUTF16(base::StringPrintf(
77 kInvalidManifestKeyValue, kManifestKey, "Invalid path."));
78 return false;
79 }
80
81 extension->SetManifestData(
82 kManifestKey, base::MakeUnique<RulesManifestData>(std::move(path)));
83
84 // If an extension specifies a rules file, it gets the declarativeNetRequest
85 // permission implicitly.
86 PermissionsParser::AddAPIPermission(extension,
87 APIPermission::kDeclarativeNetRequest);
88 return true;
89 }
90
91 bool RulesManifestHandler::Validate(
92 const Extension* extension,
93 std::string* error,
94 std::vector<InstallWarning>* warnings) const {
95 const base::FilePath* json_ruleset_path =
96 declarative_net_request::RulesManifestData::GetJSONRulesetPath(extension);
97 if (base::PathExists(*json_ruleset_path))
98 return true;
99 *error = base::StringPrintf(kInvalidManifestKeyValue, kManifestKey,
100 "Invalid path.");
101 return false;
102 }
103
104 const std::vector<std::string> RulesManifestHandler::Keys() const {
105 return ManifestHandler::SingleKey(kManifestKey);
106 }
107
108 } // namespace declarative_net_request
109 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698