Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / common / manifest_handlers / webview_info.cc
blob3359c8009773408139ef5d4ce27b89c75b7da3b4
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 // static
62 bool WebviewInfo::IsResourceWebviewAccessible(
63 const Extension* extension,
64 const std::string& partition_id,
65 const std::string& relative_path) {
66 if (!extension)
67 return false;
69 const WebviewInfo* webview_info = static_cast<const WebviewInfo*>(
70 extension->GetManifestData(keys::kWebviewAccessibleResources));
71 if (!webview_info)
72 return false;
74 for (const PartitionItem* item : webview_info->partition_items_) {
75 if (item->Matches(partition_id) &&
76 extension->ResourceMatches(item->accessible_resources(),
77 relative_path)) {
78 return true;
82 return false;
85 void WebviewInfo::AddPartitionItem(scoped_ptr<PartitionItem> item) {
86 partition_items_.push_back(item.release());
89 WebviewHandler::WebviewHandler() {
92 WebviewHandler::~WebviewHandler() {
95 bool WebviewHandler::Parse(Extension* extension, base::string16* error) {
96 scoped_ptr<WebviewInfo> info(new WebviewInfo(extension->id()));
98 const base::DictionaryValue* dict_value = NULL;
99 if (!extension->manifest()->GetDictionary(keys::kWebview,
100 &dict_value)) {
101 *error = base::ASCIIToUTF16(errors::kInvalidWebview);
102 return false;
105 const base::ListValue* partition_list = NULL;
106 if (!dict_value->GetList(keys::kWebviewPartitions, &partition_list)) {
107 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
108 return false;
111 // The partition list must have at least one entry.
112 if (partition_list->GetSize() == 0) {
113 *error = base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList);
114 return false;
117 for (size_t i = 0; i < partition_list->GetSize(); ++i) {
118 const base::DictionaryValue* partition = NULL;
119 if (!partition_list->GetDictionary(i, &partition)) {
120 *error = ErrorUtils::FormatErrorMessageUTF16(
121 errors::kInvalidWebviewPartition, base::IntToString(i));
122 return false;
125 std::string partition_pattern;
126 if (!partition->GetString(keys::kWebviewName, &partition_pattern)) {
127 *error = ErrorUtils::FormatErrorMessageUTF16(
128 errors::kInvalidWebviewPartitionName, base::IntToString(i));
129 return false;
132 const base::ListValue* url_list = NULL;
133 if (!partition->GetList(keys::kWebviewAccessibleResources,
134 &url_list)) {
135 *error = base::ASCIIToUTF16(
136 errors::kInvalidWebviewAccessibleResourcesList);
137 return false;
140 // The URL list should have at least one entry.
141 if (url_list->GetSize() == 0) {
142 *error = base::ASCIIToUTF16(
143 errors::kInvalidWebviewAccessibleResourcesList);
144 return false;
147 scoped_ptr<PartitionItem> partition_item(
148 new PartitionItem(partition_pattern));
150 for (size_t i = 0; i < url_list->GetSize(); ++i) {
151 std::string relative_path;
152 if (!url_list->GetString(i, &relative_path)) {
153 *error = ErrorUtils::FormatErrorMessageUTF16(
154 errors::kInvalidWebviewAccessibleResource, base::IntToString(i));
155 return false;
157 URLPattern pattern(URLPattern::SCHEME_EXTENSION,
158 Extension::GetResourceURL(extension->url(),
159 relative_path).spec());
160 partition_item->AddPattern(pattern);
162 info->AddPartitionItem(partition_item.Pass());
165 extension->SetManifestData(keys::kWebviewAccessibleResources, info.release());
166 return true;
169 const std::vector<std::string> WebviewHandler::Keys() const {
170 return SingleKey(keys::kWebview);
173 } // namespace extensions