| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/manifest_handlers/externally_connectable.h" | 5 #include "extensions/common/manifest_handlers/externally_connectable.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> |
| 10 | 11 |
| 12 #include "base/memory/ptr_util.h" |
| 11 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/crx_file/id_util.h" | 15 #include "components/crx_file/id_util.h" |
| 14 #include "extensions/common/api/extensions_manifest_types.h" | 16 #include "extensions/common/api/extensions_manifest_types.h" |
| 15 #include "extensions/common/error_utils.h" | 17 #include "extensions/common/error_utils.h" |
| 16 #include "extensions/common/manifest_constants.h" | 18 #include "extensions/common/manifest_constants.h" |
| 17 #include "extensions/common/manifest_handlers/permissions_parser.h" | 19 #include "extensions/common/manifest_handlers/permissions_parser.h" |
| 18 #include "extensions/common/permissions/api_permission_set.h" | 20 #include "extensions/common/permissions/api_permission_set.h" |
| 19 #include "extensions/common/url_pattern.h" | 21 #include "extensions/common/url_pattern.h" |
| 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 22 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 64 |
| 63 bool ExternallyConnectableHandler::Parse(Extension* extension, | 65 bool ExternallyConnectableHandler::Parse(Extension* extension, |
| 64 base::string16* error) { | 66 base::string16* error) { |
| 65 const base::Value* externally_connectable = NULL; | 67 const base::Value* externally_connectable = NULL; |
| 66 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, | 68 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, |
| 67 &externally_connectable)); | 69 &externally_connectable)); |
| 68 bool allow_all_urls = PermissionsParser::HasAPIPermission( | 70 bool allow_all_urls = PermissionsParser::HasAPIPermission( |
| 69 extension, APIPermission::kExternallyConnectableAllUrls); | 71 extension, APIPermission::kExternallyConnectableAllUrls); |
| 70 | 72 |
| 71 std::vector<InstallWarning> install_warnings; | 73 std::vector<InstallWarning> install_warnings; |
| 72 scoped_ptr<ExternallyConnectableInfo> info = | 74 std::unique_ptr<ExternallyConnectableInfo> info = |
| 73 ExternallyConnectableInfo::FromValue( | 75 ExternallyConnectableInfo::FromValue( |
| 74 *externally_connectable, allow_all_urls, &install_warnings, error); | 76 *externally_connectable, allow_all_urls, &install_warnings, error); |
| 75 if (!info) | 77 if (!info) |
| 76 return false; | 78 return false; |
| 77 if (!info->matches.is_empty()) { | 79 if (!info->matches.is_empty()) { |
| 78 PermissionsParser::AddAPIPermission(extension, | 80 PermissionsParser::AddAPIPermission(extension, |
| 79 APIPermission::kWebConnectable); | 81 APIPermission::kWebConnectable); |
| 80 } | 82 } |
| 81 extension->AddInstallWarnings(install_warnings); | 83 extension->AddInstallWarnings(install_warnings); |
| 82 extension->SetManifestData(keys::kExternallyConnectable, info.release()); | 84 extension->SetManifestData(keys::kExternallyConnectable, info.release()); |
| 83 return true; | 85 return true; |
| 84 } | 86 } |
| 85 | 87 |
| 86 const std::vector<std::string> ExternallyConnectableHandler::Keys() const { | 88 const std::vector<std::string> ExternallyConnectableHandler::Keys() const { |
| 87 return SingleKey(keys::kExternallyConnectable); | 89 return SingleKey(keys::kExternallyConnectable); |
| 88 } | 90 } |
| 89 | 91 |
| 90 // static | 92 // static |
| 91 ExternallyConnectableInfo* ExternallyConnectableInfo::Get( | 93 ExternallyConnectableInfo* ExternallyConnectableInfo::Get( |
| 92 const Extension* extension) { | 94 const Extension* extension) { |
| 93 return static_cast<ExternallyConnectableInfo*>( | 95 return static_cast<ExternallyConnectableInfo*>( |
| 94 extension->GetManifestData(keys::kExternallyConnectable)); | 96 extension->GetManifestData(keys::kExternallyConnectable)); |
| 95 } | 97 } |
| 96 | 98 |
| 97 // static | 99 // static |
| 98 scoped_ptr<ExternallyConnectableInfo> ExternallyConnectableInfo::FromValue( | 100 std::unique_ptr<ExternallyConnectableInfo> ExternallyConnectableInfo::FromValue( |
| 99 const base::Value& value, | 101 const base::Value& value, |
| 100 bool allow_all_urls, | 102 bool allow_all_urls, |
| 101 std::vector<InstallWarning>* install_warnings, | 103 std::vector<InstallWarning>* install_warnings, |
| 102 base::string16* error) { | 104 base::string16* error) { |
| 103 scoped_ptr<ExternallyConnectable> externally_connectable = | 105 std::unique_ptr<ExternallyConnectable> externally_connectable = |
| 104 ExternallyConnectable::FromValue(value, error); | 106 ExternallyConnectable::FromValue(value, error); |
| 105 if (!externally_connectable) | 107 if (!externally_connectable) |
| 106 return scoped_ptr<ExternallyConnectableInfo>(); | 108 return std::unique_ptr<ExternallyConnectableInfo>(); |
| 107 | 109 |
| 108 URLPatternSet matches; | 110 URLPatternSet matches; |
| 109 | 111 |
| 110 if (externally_connectable->matches) { | 112 if (externally_connectable->matches) { |
| 111 for (std::vector<std::string>::iterator it = | 113 for (std::vector<std::string>::iterator it = |
| 112 externally_connectable->matches->begin(); | 114 externally_connectable->matches->begin(); |
| 113 it != externally_connectable->matches->end(); | 115 it != externally_connectable->matches->end(); |
| 114 ++it) { | 116 ++it) { |
| 115 // Safe to use SCHEME_ALL here; externally_connectable gives a page -> | 117 // Safe to use SCHEME_ALL here; externally_connectable gives a page -> |
| 116 // extension communication path, not the other way. | 118 // extension communication path, not the other way. |
| 117 URLPattern pattern(URLPattern::SCHEME_ALL); | 119 URLPattern pattern(URLPattern::SCHEME_ALL); |
| 118 if (pattern.Parse(*it) != URLPattern::PARSE_SUCCESS) { | 120 if (pattern.Parse(*it) != URLPattern::PARSE_SUCCESS) { |
| 119 *error = ErrorUtils::FormatErrorMessageUTF16( | 121 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 120 errors::kErrorInvalidMatchPattern, *it); | 122 errors::kErrorInvalidMatchPattern, *it); |
| 121 return scoped_ptr<ExternallyConnectableInfo>(); | 123 return std::unique_ptr<ExternallyConnectableInfo>(); |
| 122 } | 124 } |
| 123 | 125 |
| 124 if (allow_all_urls && pattern.match_all_urls()) { | 126 if (allow_all_urls && pattern.match_all_urls()) { |
| 125 matches.AddPattern(pattern); | 127 matches.AddPattern(pattern); |
| 126 continue; | 128 continue; |
| 127 } | 129 } |
| 128 | 130 |
| 129 // Wildcard hosts are not allowed. | 131 // Wildcard hosts are not allowed. |
| 130 if (pattern.host().empty()) { | 132 if (pattern.host().empty()) { |
| 131 // Warning not error for forwards compatibility. | 133 // Warning not error for forwards compatibility. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 rcd::INCLUDE_UNKNOWN_REGISTRIES, | 147 rcd::INCLUDE_UNKNOWN_REGISTRIES, |
| 146 // This means that effective TLDs like appspot.com count as TLDs; | 148 // This means that effective TLDs like appspot.com count as TLDs; |
| 147 // codereview.appspot.com and evil.appspot.com are different. | 149 // codereview.appspot.com and evil.appspot.com are different. |
| 148 rcd::INCLUDE_PRIVATE_REGISTRIES); | 150 rcd::INCLUDE_PRIVATE_REGISTRIES); |
| 149 | 151 |
| 150 if (registry_length == std::string::npos) { | 152 if (registry_length == std::string::npos) { |
| 151 // The URL parsing combined with host().empty() should have caught this. | 153 // The URL parsing combined with host().empty() should have caught this. |
| 152 NOTREACHED() << *it; | 154 NOTREACHED() << *it; |
| 153 *error = ErrorUtils::FormatErrorMessageUTF16( | 155 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 154 errors::kErrorInvalidMatchPattern, *it); | 156 errors::kErrorInvalidMatchPattern, *it); |
| 155 return scoped_ptr<ExternallyConnectableInfo>(); | 157 return std::unique_ptr<ExternallyConnectableInfo>(); |
| 156 } | 158 } |
| 157 | 159 |
| 158 // Broad match patterns like "*.com", "*.co.uk", and even "*.appspot.com" | 160 // Broad match patterns like "*.com", "*.co.uk", and even "*.appspot.com" |
| 159 // are not allowed. However just "appspot.com" is ok. | 161 // are not allowed. However just "appspot.com" is ok. |
| 160 if (registry_length == 0 && pattern.match_subdomains()) { | 162 if (registry_length == 0 && pattern.match_subdomains()) { |
| 161 // Warning not error for forwards compatibility. | 163 // Warning not error for forwards compatibility. |
| 162 install_warnings->push_back( | 164 install_warnings->push_back( |
| 163 InstallWarning(ErrorUtils::FormatErrorMessage( | 165 InstallWarning(ErrorUtils::FormatErrorMessage( |
| 164 errors::kErrorTopLevelDomainsNotAllowed, | 166 errors::kErrorTopLevelDomainsNotAllowed, |
| 165 pattern.host().c_str(), | 167 pattern.host().c_str(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 181 externally_connectable->ids->begin(); | 183 externally_connectable->ids->begin(); |
| 182 it != externally_connectable->ids->end(); | 184 it != externally_connectable->ids->end(); |
| 183 ++it) { | 185 ++it) { |
| 184 if (*it == kAllIds) { | 186 if (*it == kAllIds) { |
| 185 all_ids = true; | 187 all_ids = true; |
| 186 } else if (crx_file::id_util::IdIsValid(*it)) { | 188 } else if (crx_file::id_util::IdIsValid(*it)) { |
| 187 ids.push_back(*it); | 189 ids.push_back(*it); |
| 188 } else { | 190 } else { |
| 189 *error = | 191 *error = |
| 190 ErrorUtils::FormatErrorMessageUTF16(errors::kErrorInvalidId, *it); | 192 ErrorUtils::FormatErrorMessageUTF16(errors::kErrorInvalidId, *it); |
| 191 return scoped_ptr<ExternallyConnectableInfo>(); | 193 return std::unique_ptr<ExternallyConnectableInfo>(); |
| 192 } | 194 } |
| 193 } | 195 } |
| 194 } | 196 } |
| 195 | 197 |
| 196 if (!externally_connectable->matches && !externally_connectable->ids) { | 198 if (!externally_connectable->matches && !externally_connectable->ids) { |
| 197 install_warnings->push_back(InstallWarning(errors::kErrorNothingSpecified, | 199 install_warnings->push_back(InstallWarning(errors::kErrorNothingSpecified, |
| 198 keys::kExternallyConnectable)); | 200 keys::kExternallyConnectable)); |
| 199 } | 201 } |
| 200 | 202 |
| 201 bool accepts_tls_channel_id = | 203 bool accepts_tls_channel_id = |
| 202 externally_connectable->accepts_tls_channel_id.get() && | 204 externally_connectable->accepts_tls_channel_id.get() && |
| 203 *externally_connectable->accepts_tls_channel_id; | 205 *externally_connectable->accepts_tls_channel_id; |
| 204 return make_scoped_ptr(new ExternallyConnectableInfo( | 206 return base::WrapUnique(new ExternallyConnectableInfo( |
| 205 matches, ids, all_ids, accepts_tls_channel_id)); | 207 matches, ids, all_ids, accepts_tls_channel_id)); |
| 206 } | 208 } |
| 207 | 209 |
| 208 ExternallyConnectableInfo::~ExternallyConnectableInfo() { | 210 ExternallyConnectableInfo::~ExternallyConnectableInfo() { |
| 209 } | 211 } |
| 210 | 212 |
| 211 ExternallyConnectableInfo::ExternallyConnectableInfo( | 213 ExternallyConnectableInfo::ExternallyConnectableInfo( |
| 212 const URLPatternSet& matches, | 214 const URLPatternSet& matches, |
| 213 const std::vector<std::string>& ids, | 215 const std::vector<std::string>& ids, |
| 214 bool all_ids, | 216 bool all_ids, |
| 215 bool accepts_tls_channel_id) | 217 bool accepts_tls_channel_id) |
| 216 : matches(matches), | 218 : matches(matches), |
| 217 ids(Sorted(ids)), | 219 ids(Sorted(ids)), |
| 218 all_ids(all_ids), | 220 all_ids(all_ids), |
| 219 accepts_tls_channel_id(accepts_tls_channel_id) { | 221 accepts_tls_channel_id(accepts_tls_channel_id) { |
| 220 } | 222 } |
| 221 | 223 |
| 222 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) { | 224 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) { |
| 223 if (all_ids) | 225 if (all_ids) |
| 224 return true; | 226 return true; |
| 225 DCHECK(base::STLIsSorted(ids)); | 227 DCHECK(base::STLIsSorted(ids)); |
| 226 return std::binary_search(ids.begin(), ids.end(), id); | 228 return std::binary_search(ids.begin(), ids.end(), id); |
| 227 } | 229 } |
| 228 | 230 |
| 229 } // namespace extensions | 231 } // namespace extensions |
| OLD | NEW |