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
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_
;
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_
)
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(),
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
,
98 *error
= base::ASCIIToUTF16(errors::kInvalidWebview
);
102 const base::ListValue
* partition_list
= NULL
;
103 if (!dict_value
->GetList(keys::kWebviewPartitions
, &partition_list
)) {
104 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
108 // The partition list must have at least one entry.
109 if (partition_list
->GetSize() == 0) {
110 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
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
));
122 std::string partition_pattern
;
123 if (!partition
->GetString(keys::kWebviewName
, &partition_pattern
)) {
124 *error
= ErrorUtils::FormatErrorMessageUTF16(
125 errors::kInvalidWebviewPartitionName
, base::IntToString(i
));
129 const base::ListValue
* url_list
= NULL
;
130 if (!partition
->GetList(keys::kWebviewAccessibleResources
,
132 *error
= base::ASCIIToUTF16(
133 errors::kInvalidWebviewAccessibleResourcesList
);
137 // The URL list should have at least one entry.
138 if (url_list
->GetSize() == 0) {
139 *error
= base::ASCIIToUTF16(
140 errors::kInvalidWebviewAccessibleResourcesList
);
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
));
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());
166 const std::vector
<std::string
> WebviewHandler::Keys() const {
167 return SingleKey(keys::kWebview
);
170 } // namespace extensions