Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / manifest_handlers / webview_info.cc
blob50400023bd82021b7edb639a1fc2406c06861e3d
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/pattern.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/manifest.h"
15 #include "extensions/common/manifest_constants.h"
17 namespace extensions {
19 namespace keys = extensions::manifest_keys;
20 namespace errors = extensions::manifest_errors;
22 // A PartitionItem represents a set of accessible resources given a partition
23 // ID pattern.
24 class PartitionItem {
25 public:
26 explicit PartitionItem(const std::string& partition_pattern)
27 : partition_pattern_(partition_pattern) {
30 virtual ~PartitionItem() {
33 bool Matches(const std::string& partition_id) const {
34 return base::MatchPattern(partition_id, partition_pattern_);
37 // Adds a pattern to the set. Returns true if a new pattern was inserted,
38 // false if the pattern was already in the set.
39 bool AddPattern(const URLPattern& pattern) {
40 return accessible_resources_.AddPattern(pattern);
43 const URLPatternSet& accessible_resources() const {
44 return accessible_resources_;
46 private:
47 // A pattern string that matches partition IDs.
48 const std::string partition_pattern_;
49 // A URL pattern set of resources accessible to the given
50 // |partition_pattern_|.
51 URLPatternSet accessible_resources_;
54 WebviewInfo::WebviewInfo(const std::string& extension_id)
55 : extension_id_(extension_id) {
58 WebviewInfo::~WebviewInfo() {
61 bool WebviewInfo::IsResourceWebviewAccessible(
62 const Extension* extension,
63 const std::string& partition_id,
64 const std::string& relative_path) const {
65 if (!extension || extension->id() != extension_id_)
66 return false;
68 DCHECK_EQ(this,
69 extension->GetManifestData(keys::kWebviewAccessibleResources));
71 for (size_t i = 0; i < partition_items_.size(); ++i) {
72 const PartitionItem* const item = partition_items_[i];
73 if (item->Matches(partition_id) &&
74 extension->ResourceMatches(item->accessible_resources(),
75 relative_path)) {
76 return true;
80 return false;
83 void WebviewInfo::AddPartitionItem(scoped_ptr<PartitionItem> item) {
84 partition_items_.push_back(item.release());
87 WebviewHandler::WebviewHandler() {
90 WebviewHandler::~WebviewHandler() {
93 bool WebviewHandler::Parse(Extension* extension, base::string16* error) {
94 scoped_ptr<WebviewInfo> info(new WebviewInfo(extension->id()));
96 const base::DictionaryValue* dict_value = NULL;
97 if (!extension->manifest()->GetDictionary(keys::kWebview,
98 &dict_value)) {
99 *error = base::ASCIIToUTF16(errors::kInvalidWebview);
100 return false;
103 const base::ListValue* partition_list = NULL;
104 if (!dict_value->GetList(keys::kWebviewPartitions, &partition_list)) {
105 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
106 return false;
109 // The partition list must have at least one entry.
110 if (partition_list->GetSize() == 0) {
111 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
112 return false;
115 for (size_t i = 0; i < partition_list->GetSize(); ++i) {
116 const base::DictionaryValue* partition = NULL;
117 if (!partition_list->GetDictionary(i, &partition)) {
118 *error = ErrorUtils::FormatErrorMessageUTF16(
119 errors::kInvalidWebviewPartition, base::IntToString(i));
120 return false;
123 std::string partition_pattern;
124 if (!partition->GetString(keys::kWebviewName, &partition_pattern)) {
125 *error = ErrorUtils::FormatErrorMessageUTF16(
126 errors::kInvalidWebviewPartitionName, base::IntToString(i));
127 return false;
130 const base::ListValue* url_list = NULL;
131 if (!partition->GetList(keys::kWebviewAccessibleResources,
132 &url_list)) {
133 *error = base::ASCIIToUTF16(
134 errors::kInvalidWebviewAccessibleResourcesList);
135 return false;
138 // The URL list should have at least one entry.
139 if (url_list->GetSize() == 0) {
140 *error = base::ASCIIToUTF16(
141 errors::kInvalidWebviewAccessibleResourcesList);
142 return false;
145 scoped_ptr<PartitionItem> partition_item(
146 new PartitionItem(partition_pattern));
148 for (size_t i = 0; i < url_list->GetSize(); ++i) {
149 std::string relative_path;
150 if (!url_list->GetString(i, &relative_path)) {
151 *error = ErrorUtils::FormatErrorMessageUTF16(
152 errors::kInvalidWebviewAccessibleResource, base::IntToString(i));
153 return false;
155 URLPattern pattern(URLPattern::SCHEME_EXTENSION,
156 Extension::GetResourceURL(extension->url(),
157 relative_path).spec());
158 partition_item->AddPattern(pattern);
160 info->AddPartitionItem(partition_item.Pass());
163 extension->SetManifestData(keys::kWebviewAccessibleResources, info.release());
164 return true;
167 const std::vector<std::string> WebviewHandler::Keys() const {
168 return SingleKey(keys::kWebview);
171 } // namespace extensions