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
;
23 const WebviewInfo
* GetResourcesInfo(
24 const Extension
& extension
) {
25 return static_cast<WebviewInfo
*>(
26 extension
.GetManifestData(keys::kWebviewAccessibleResources
));
31 // A PartitionItem represents a set of accessible resources given a partition
35 explicit PartitionItem(const std::string
& partition_pattern
)
36 : partition_pattern_(partition_pattern
) {
39 virtual ~PartitionItem() {
42 bool Matches(const std::string
& partition_id
) const {
43 return MatchPattern(partition_id
, partition_pattern_
);
46 // Adds a pattern to the set. Returns true if a new pattern was inserted,
47 // false if the pattern was already in the set.
48 bool AddPattern(const URLPattern
& pattern
) {
49 return accessible_resources_
.AddPattern(pattern
);
52 const URLPatternSet
& accessible_resources() const {
53 return accessible_resources_
;
56 // A pattern string that matches partition IDs.
57 const std::string partition_pattern_
;
58 // A URL pattern set of resources accessible to the given
59 // |partition_pattern_|.
60 URLPatternSet accessible_resources_
;
64 WebviewInfo::WebviewInfo() {
67 WebviewInfo::~WebviewInfo() {
71 bool WebviewInfo::IsResourceWebviewAccessible(
72 const Extension
* extension
,
73 const std::string
& partition_id
,
74 const std::string
& relative_path
) {
78 const WebviewInfo
* info
= GetResourcesInfo(*extension
);
82 for (size_t i
= 0; i
< info
->partition_items_
.size(); ++i
) {
83 const PartitionItem
* const item
= info
->partition_items_
[i
];
84 if (item
->Matches(partition_id
) &&
85 extension
->ResourceMatches(item
->accessible_resources(),
94 void WebviewInfo::AddPartitionItem(scoped_ptr
<PartitionItem
> item
) {
95 partition_items_
.push_back(item
.release());
98 WebviewHandler::WebviewHandler() {
101 WebviewHandler::~WebviewHandler() {
104 bool WebviewHandler::Parse(Extension
* extension
, base::string16
* error
) {
105 scoped_ptr
<WebviewInfo
> info(new WebviewInfo());
107 const base::DictionaryValue
* dict_value
= NULL
;
108 if (!extension
->manifest()->GetDictionary(keys::kWebview
,
110 *error
= base::ASCIIToUTF16(errors::kInvalidWebview
);
114 const base::ListValue
* partition_list
= NULL
;
115 if (!dict_value
->GetList(keys::kWebviewPartitions
, &partition_list
)) {
116 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
120 // The partition list must have at least one entry.
121 if (partition_list
->GetSize() == 0) {
122 *error
= base::ASCIIToUTF16(errors::kInvalidWebviewPartitionsList
);
126 for (size_t i
= 0; i
< partition_list
->GetSize(); ++i
) {
127 const base::DictionaryValue
* partition
= NULL
;
128 if (!partition_list
->GetDictionary(i
, &partition
)) {
129 *error
= ErrorUtils::FormatErrorMessageUTF16(
130 errors::kInvalidWebviewPartition
, base::IntToString(i
));
134 std::string partition_pattern
;
135 if (!partition
->GetString(keys::kWebviewName
, &partition_pattern
)) {
136 *error
= ErrorUtils::FormatErrorMessageUTF16(
137 errors::kInvalidWebviewPartitionName
, base::IntToString(i
));
141 const base::ListValue
* url_list
= NULL
;
142 if (!partition
->GetList(keys::kWebviewAccessibleResources
,
144 *error
= base::ASCIIToUTF16(
145 errors::kInvalidWebviewAccessibleResourcesList
);
149 // The URL list should have at least one entry.
150 if (url_list
->GetSize() == 0) {
151 *error
= base::ASCIIToUTF16(
152 errors::kInvalidWebviewAccessibleResourcesList
);
156 scoped_ptr
<PartitionItem
> partition_item(
157 new PartitionItem(partition_pattern
));
159 for (size_t i
= 0; i
< url_list
->GetSize(); ++i
) {
160 std::string relative_path
;
161 if (!url_list
->GetString(i
, &relative_path
)) {
162 *error
= ErrorUtils::FormatErrorMessageUTF16(
163 errors::kInvalidWebviewAccessibleResource
, base::IntToString(i
));
166 URLPattern
pattern(URLPattern::SCHEME_EXTENSION
,
167 Extension::GetResourceURL(extension
->url(),
168 relative_path
).spec());
169 partition_item
->AddPattern(pattern
);
171 info
->AddPartitionItem(partition_item
.Pass());
174 extension
->SetManifestData(keys::kWebviewAccessibleResources
, info
.release());
178 const std::vector
<std::string
> WebviewHandler::Keys() const {
179 return SingleKey(keys::kWebview
);
182 } // namespace extensions