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
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_
;
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() {
62 bool WebviewInfo::IsResourceWebviewAccessible(
63 const Extension
* extension
,
64 const std::string
& partition_id
,
65 const std::string
& relative_path
) {
69 const WebviewInfo
* webview_info
= static_cast<const WebviewInfo
*>(
70 extension
->GetManifestData(keys::kWebviewAccessibleResources
));
74 for (const PartitionItem
* item
: webview_info
->partition_items_
) {
75 if (item
->Matches(partition_id
) &&
76 extension
->ResourceMatches(item
->accessible_resources(),
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
,
101 *error
= base::ASCIIToUTF16(errors::kInvalidWebview
);
105 const base::ListValue
* partition_list
= NULL
;
106 if (!dict_value
->GetList(keys::kWebviewPartitions
, &partition_list
)) {
107 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
111 // The partition list must have at least one entry.
112 if (partition_list
->GetSize() == 0) {
113 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
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
));
125 std::string partition_pattern
;
126 if (!partition
->GetString(keys::kWebviewName
, &partition_pattern
)) {
127 *error
= ErrorUtils::FormatErrorMessageUTF16(
128 errors::kInvalidWebviewPartitionName
, base::IntToString(i
));
132 const base::ListValue
* url_list
= NULL
;
133 if (!partition
->GetList(keys::kWebviewAccessibleResources
,
135 *error
= base::ASCIIToUTF16(
136 errors::kInvalidWebviewAccessibleResourcesList
);
140 // The URL list should have at least one entry.
141 if (url_list
->GetSize() == 0) {
142 *error
= base::ASCIIToUTF16(
143 errors::kInvalidWebviewAccessibleResourcesList
);
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
));
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());
169 const std::vector
<std::string
> WebviewHandler::Keys() const {
170 return SingleKey(keys::kWebview
);
173 } // namespace extensions