Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / common / manifest_handlers / externally_connectable.cc
blob0a06c019ae2b375fb9c86ec71e205dbb7778b914
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/externally_connectable.h"
7 #include <algorithm>
9 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/crx_file/id_util.h"
12 #include "extensions/common/api/extensions_manifest_types.h"
13 #include "extensions/common/error_utils.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/manifest_handlers/permissions_parser.h"
16 #include "extensions/common/permissions/api_permission_set.h"
17 #include "extensions/common/url_pattern.h"
18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
19 #include "url/gurl.h"
21 namespace rcd = net::registry_controlled_domains;
23 namespace extensions {
25 namespace externally_connectable_errors {
26 const char kErrorInvalidMatchPattern[] = "Invalid match pattern '*'";
27 const char kErrorInvalidId[] = "Invalid ID '*'";
28 const char kErrorNothingSpecified[] =
29 "'externally_connectable' specifies neither 'matches' nor 'ids'; "
30 "nothing will be able to connect";
31 const char kErrorTopLevelDomainsNotAllowed[] =
32 "\"*\" is an effective top level domain for which wildcard subdomains such "
33 "as \"*\" are not allowed";
34 const char kErrorWildcardHostsNotAllowed[] =
35 "Wildcard domain patterns such as \"*\" are not allowed";
36 } // namespace externally_connectable_errors
38 namespace keys = extensions::manifest_keys;
39 namespace errors = externally_connectable_errors;
40 using core_api::extensions_manifest_types::ExternallyConnectable;
42 namespace {
44 const char kAllIds[] = "*";
46 template <typename T>
47 std::vector<T> Sorted(const std::vector<T>& in) {
48 std::vector<T> out = in;
49 std::sort(out.begin(), out.end());
50 return out;
53 } // namespace
55 ExternallyConnectableHandler::ExternallyConnectableHandler() {
58 ExternallyConnectableHandler::~ExternallyConnectableHandler() {
61 bool ExternallyConnectableHandler::Parse(Extension* extension,
62 base::string16* error) {
63 const base::Value* externally_connectable = NULL;
64 CHECK(extension->manifest()->Get(keys::kExternallyConnectable,
65 &externally_connectable));
66 std::vector<InstallWarning> install_warnings;
67 scoped_ptr<ExternallyConnectableInfo> info =
68 ExternallyConnectableInfo::FromValue(*externally_connectable,
69 &install_warnings,
70 error);
71 if (!info)
72 return false;
73 if (!info->matches.is_empty()) {
74 PermissionsParser::AddAPIPermission(extension,
75 APIPermission::kWebConnectable);
77 extension->AddInstallWarnings(install_warnings);
78 extension->SetManifestData(keys::kExternallyConnectable, info.release());
79 return true;
82 const std::vector<std::string> ExternallyConnectableHandler::Keys() const {
83 return SingleKey(keys::kExternallyConnectable);
86 // static
87 ExternallyConnectableInfo* ExternallyConnectableInfo::Get(
88 const Extension* extension) {
89 return static_cast<ExternallyConnectableInfo*>(
90 extension->GetManifestData(keys::kExternallyConnectable));
93 // static
94 scoped_ptr<ExternallyConnectableInfo> ExternallyConnectableInfo::FromValue(
95 const base::Value& value,
96 std::vector<InstallWarning>* install_warnings,
97 base::string16* error) {
98 scoped_ptr<ExternallyConnectable> externally_connectable =
99 ExternallyConnectable::FromValue(value, error);
100 if (!externally_connectable)
101 return scoped_ptr<ExternallyConnectableInfo>();
103 URLPatternSet matches;
105 if (externally_connectable->matches) {
106 for (std::vector<std::string>::iterator it =
107 externally_connectable->matches->begin();
108 it != externally_connectable->matches->end();
109 ++it) {
110 // Safe to use SCHEME_ALL here; externally_connectable gives a page ->
111 // extension communication path, not the other way.
112 URLPattern pattern(URLPattern::SCHEME_ALL);
113 if (pattern.Parse(*it) != URLPattern::PARSE_SUCCESS) {
114 *error = ErrorUtils::FormatErrorMessageUTF16(
115 errors::kErrorInvalidMatchPattern, *it);
116 return scoped_ptr<ExternallyConnectableInfo>();
119 // Wildcard hosts are not allowed.
120 if (pattern.host().empty()) {
121 // Warning not error for forwards compatibility.
122 install_warnings->push_back(
123 InstallWarning(ErrorUtils::FormatErrorMessage(
124 errors::kErrorWildcardHostsNotAllowed, *it),
125 keys::kExternallyConnectable,
126 *it));
127 continue;
130 // Wildcards on subdomains of a TLD are not allowed.
131 size_t registry_length = rcd::GetRegistryLength(
132 pattern.host(),
133 // This means that things that look like TLDs - the foobar in
134 // http://google.foobar - count as TLDs.
135 rcd::INCLUDE_UNKNOWN_REGISTRIES,
136 // This means that effective TLDs like appspot.com count as TLDs;
137 // codereview.appspot.com and evil.appspot.com are different.
138 rcd::INCLUDE_PRIVATE_REGISTRIES);
140 if (registry_length == std::string::npos) {
141 // The URL parsing combined with host().empty() should have caught this.
142 NOTREACHED() << *it;
143 *error = ErrorUtils::FormatErrorMessageUTF16(
144 errors::kErrorInvalidMatchPattern, *it);
145 return scoped_ptr<ExternallyConnectableInfo>();
148 // Broad match patterns like "*.com", "*.co.uk", and even "*.appspot.com"
149 // are not allowed. However just "appspot.com" is ok.
150 if (registry_length == 0 && pattern.match_subdomains()) {
151 // Warning not error for forwards compatibility.
152 install_warnings->push_back(
153 InstallWarning(ErrorUtils::FormatErrorMessage(
154 errors::kErrorTopLevelDomainsNotAllowed,
155 pattern.host().c_str(),
156 *it),
157 keys::kExternallyConnectable,
158 *it));
159 continue;
162 matches.AddPattern(pattern);
166 std::vector<std::string> ids;
167 bool all_ids = false;
169 if (externally_connectable->ids) {
170 for (std::vector<std::string>::iterator it =
171 externally_connectable->ids->begin();
172 it != externally_connectable->ids->end();
173 ++it) {
174 if (*it == kAllIds) {
175 all_ids = true;
176 } else if (crx_file::id_util::IdIsValid(*it)) {
177 ids.push_back(*it);
178 } else {
179 *error =
180 ErrorUtils::FormatErrorMessageUTF16(errors::kErrorInvalidId, *it);
181 return scoped_ptr<ExternallyConnectableInfo>();
186 if (!externally_connectable->matches && !externally_connectable->ids) {
187 install_warnings->push_back(InstallWarning(errors::kErrorNothingSpecified,
188 keys::kExternallyConnectable));
191 bool accepts_tls_channel_id =
192 externally_connectable->accepts_tls_channel_id.get() &&
193 *externally_connectable->accepts_tls_channel_id;
194 return make_scoped_ptr(new ExternallyConnectableInfo(
195 matches, ids, all_ids, accepts_tls_channel_id));
198 ExternallyConnectableInfo::~ExternallyConnectableInfo() {
201 ExternallyConnectableInfo::ExternallyConnectableInfo(
202 const URLPatternSet& matches,
203 const std::vector<std::string>& ids,
204 bool all_ids,
205 bool accepts_tls_channel_id)
206 : matches(matches),
207 ids(Sorted(ids)),
208 all_ids(all_ids),
209 accepts_tls_channel_id(accepts_tls_channel_id) {
212 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) {
213 if (all_ids)
214 return true;
215 DCHECK(base::STLIsSorted(ids));
216 return std::binary_search(ids.begin(), ids.end(), id);
219 } // namespace extensions