Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / extensions / common / manifest_handlers / webview_info.cc
blob99e9d40c5de90a112d6c500d6d2f71bc5a011622
1 // Copyright 2014 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.
5 #include "extensions/common/manifest_handlers/webview_info.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/manifest.h"
14 #include "extensions/common/manifest_constants.h"
16 namespace extensions {
18 namespace keys = extensions::manifest_keys;
19 namespace errors = extensions::manifest_errors;
21 // A PartitionItem represents a set of accessible resources given a partition
22 // ID pattern.
23 class PartitionItem {
24 public:
25 explicit PartitionItem(const std::string& partition_pattern)
26 : partition_pattern_(partition_pattern) {
29 virtual ~PartitionItem() {
32 bool Matches(const std::string& partition_id) const {
33 return MatchPattern(partition_id, partition_pattern_);
36 // Adds a pattern to the set. Returns true if a new pattern was inserted,
37 // false if the pattern was already in the set.
38 bool AddPattern(const URLPattern& pattern) {
39 return accessible_resources_.AddPattern(pattern);
42 const URLPatternSet& accessible_resources() const {
43 return accessible_resources_;
45 private:
46 // A pattern string that matches partition IDs.
47 const std::string partition_pattern_;
48 // A URL pattern set of resources accessible to the given
49 // |partition_pattern_|.
50 URLPatternSet accessible_resources_;
53 WebviewInfo::WebviewInfo(const std::string& extension_id)
54 : extension_id_(extension_id) {
57 WebviewInfo::~WebviewInfo() {
60 bool WebviewInfo::IsResourceWebviewAccessible(
61 const Extension* extension,
62 const std::string& partition_id,
63 const std::string& relative_path) const {
64 if (!extension || extension->id() != extension_id_)
65 return false;
67 DCHECK_EQ(this,
68 extension->GetManifestData(keys::kWebviewAccessibleResources));
70 for (size_t i = 0; i < partition_items_.size(); ++i) {
71 const PartitionItem* const item = partition_items_[i];
72 if (item->Matches(partition_id) &&
73 extension->ResourceMatches(item->accessible_resources(),
74 relative_path)) {
75 return true;
79 return false;
82 void WebviewInfo::AddPartitionItem(scoped_ptr<PartitionItem> item) {
83 partition_items_.push_back(item.release());
86 WebviewHandler::WebviewHandler() {
89 WebviewHandler::~WebviewHandler() {
92 bool WebviewHandler::Parse(Extension* extension, base::string16* error) {
93 scoped_ptr<WebviewInfo> info(new WebviewInfo(extension->id()));
95 const base::DictionaryValue* dict_value = NULL;
96 if (!extension->manifest()->GetDictionary(keys::kWebview,
97 &dict_value)) {
98 *error = base::ASCIIToUTF16(errors::kInvalidWebview);
99 return false;
102 const base::ListValue* partition_list = NULL;
103 if (!dict_value->GetList(keys::kWebviewPartitions, &partition_list)) {
104 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
105 return false;
108 // The partition list must have at least one entry.
109 if (partition_list->GetSize() == 0) {
110 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
111 return false;
114 for (size_t i = 0; i < partition_list->GetSize(); ++i) {
115 const base::DictionaryValue* partition = NULL;
116 if (!partition_list->GetDictionary(i, &partition)) {
117 *error = ErrorUtils::FormatErrorMessageUTF16(
118 errors::kInvalidWebviewPartition, base::IntToString(i));
119 return false;
122 std::string partition_pattern;
123 if (!partition->GetString(keys::kWebviewName, &partition_pattern)) {
124 *error = ErrorUtils::FormatErrorMessageUTF16(
125 errors::kInvalidWebviewPartitionName, base::IntToString(i));
126 return false;
129 const base::ListValue* url_list = NULL;
130 if (!partition->GetList(keys::kWebviewAccessibleResources,
131 &url_list)) {
132 *error = base::ASCIIToUTF16(
133 errors::kInvalidWebviewAccessibleResourcesList);
134 return false;
137 // The URL list should have at least one entry.
138 if (url_list->GetSize() == 0) {
139 *error = base::ASCIIToUTF16(
140 errors::kInvalidWebviewAccessibleResourcesList);
141 return false;
144 scoped_ptr<PartitionItem> partition_item(
145 new PartitionItem(partition_pattern));
147 for (size_t i = 0; i < url_list->GetSize(); ++i) {
148 std::string relative_path;
149 if (!url_list->GetString(i, &relative_path)) {
150 *error = ErrorUtils::FormatErrorMessageUTF16(
151 errors::kInvalidWebviewAccessibleResource, base::IntToString(i));
152 return false;
154 URLPattern pattern(URLPattern::SCHEME_EXTENSION,
155 Extension::GetResourceURL(extension->url(),
156 relative_path).spec());
157 partition_item->AddPattern(pattern);
159 info->AddPartitionItem(partition_item.Pass());
162 extension->SetManifestData(keys::kWebviewAccessibleResources, info.release());
163 return true;
166 const std::vector<std::string> WebviewHandler::Keys() const {
167 return SingleKey(keys::kWebview);
170 } // namespace extensions