| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 56 } |
| 57 | 57 |
| 58 ExternallyConnectableHandler::~ExternallyConnectableHandler() { | 58 ExternallyConnectableHandler::~ExternallyConnectableHandler() { |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool ExternallyConnectableHandler::Parse(Extension* extension, | 61 bool ExternallyConnectableHandler::Parse(Extension* extension, |
| 62 base::string16* error) { | 62 base::string16* error) { |
| 63 const base::Value* externally_connectable = NULL; | 63 const base::Value* externally_connectable = NULL; |
| 64 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, | 64 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, |
| 65 &externally_connectable)); | 65 &externally_connectable)); |
| 66 bool allow_all_urls = PermissionsParser::HasAPIPermission( |
| 67 extension, APIPermission::kExternallyConnectableAllUrls); |
| 68 |
| 66 std::vector<InstallWarning> install_warnings; | 69 std::vector<InstallWarning> install_warnings; |
| 67 scoped_ptr<ExternallyConnectableInfo> info = | 70 scoped_ptr<ExternallyConnectableInfo> info = |
| 68 ExternallyConnectableInfo::FromValue(*externally_connectable, | 71 ExternallyConnectableInfo::FromValue( |
| 69 &install_warnings, | 72 *externally_connectable, allow_all_urls, &install_warnings, error); |
| 70 error); | |
| 71 if (!info) | 73 if (!info) |
| 72 return false; | 74 return false; |
| 73 if (!info->matches.is_empty()) { | 75 if (!info->matches.is_empty()) { |
| 74 PermissionsParser::AddAPIPermission(extension, | 76 PermissionsParser::AddAPIPermission(extension, |
| 75 APIPermission::kWebConnectable); | 77 APIPermission::kWebConnectable); |
| 76 } | 78 } |
| 77 extension->AddInstallWarnings(install_warnings); | 79 extension->AddInstallWarnings(install_warnings); |
| 78 extension->SetManifestData(keys::kExternallyConnectable, info.release()); | 80 extension->SetManifestData(keys::kExternallyConnectable, info.release()); |
| 79 return true; | 81 return true; |
| 80 } | 82 } |
| 81 | 83 |
| 82 const std::vector<std::string> ExternallyConnectableHandler::Keys() const { | 84 const std::vector<std::string> ExternallyConnectableHandler::Keys() const { |
| 83 return SingleKey(keys::kExternallyConnectable); | 85 return SingleKey(keys::kExternallyConnectable); |
| 84 } | 86 } |
| 85 | 87 |
| 86 // static | 88 // static |
| 87 ExternallyConnectableInfo* ExternallyConnectableInfo::Get( | 89 ExternallyConnectableInfo* ExternallyConnectableInfo::Get( |
| 88 const Extension* extension) { | 90 const Extension* extension) { |
| 89 return static_cast<ExternallyConnectableInfo*>( | 91 return static_cast<ExternallyConnectableInfo*>( |
| 90 extension->GetManifestData(keys::kExternallyConnectable)); | 92 extension->GetManifestData(keys::kExternallyConnectable)); |
| 91 } | 93 } |
| 92 | 94 |
| 93 // static | 95 // static |
| 94 scoped_ptr<ExternallyConnectableInfo> ExternallyConnectableInfo::FromValue( | 96 scoped_ptr<ExternallyConnectableInfo> ExternallyConnectableInfo::FromValue( |
| 95 const base::Value& value, | 97 const base::Value& value, |
| 98 bool allow_all_urls, |
| 96 std::vector<InstallWarning>* install_warnings, | 99 std::vector<InstallWarning>* install_warnings, |
| 97 base::string16* error) { | 100 base::string16* error) { |
| 98 scoped_ptr<ExternallyConnectable> externally_connectable = | 101 scoped_ptr<ExternallyConnectable> externally_connectable = |
| 99 ExternallyConnectable::FromValue(value, error); | 102 ExternallyConnectable::FromValue(value, error); |
| 100 if (!externally_connectable) | 103 if (!externally_connectable) |
| 101 return scoped_ptr<ExternallyConnectableInfo>(); | 104 return scoped_ptr<ExternallyConnectableInfo>(); |
| 102 | 105 |
| 103 URLPatternSet matches; | 106 URLPatternSet matches; |
| 104 | 107 |
| 105 if (externally_connectable->matches) { | 108 if (externally_connectable->matches) { |
| 106 for (std::vector<std::string>::iterator it = | 109 for (std::vector<std::string>::iterator it = |
| 107 externally_connectable->matches->begin(); | 110 externally_connectable->matches->begin(); |
| 108 it != externally_connectable->matches->end(); | 111 it != externally_connectable->matches->end(); |
| 109 ++it) { | 112 ++it) { |
| 110 // Safe to use SCHEME_ALL here; externally_connectable gives a page -> | 113 // Safe to use SCHEME_ALL here; externally_connectable gives a page -> |
| 111 // extension communication path, not the other way. | 114 // extension communication path, not the other way. |
| 112 URLPattern pattern(URLPattern::SCHEME_ALL); | 115 URLPattern pattern(URLPattern::SCHEME_ALL); |
| 113 if (pattern.Parse(*it) != URLPattern::PARSE_SUCCESS) { | 116 if (pattern.Parse(*it) != URLPattern::PARSE_SUCCESS) { |
| 114 *error = ErrorUtils::FormatErrorMessageUTF16( | 117 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 115 errors::kErrorInvalidMatchPattern, *it); | 118 errors::kErrorInvalidMatchPattern, *it); |
| 116 return scoped_ptr<ExternallyConnectableInfo>(); | 119 return scoped_ptr<ExternallyConnectableInfo>(); |
| 117 } | 120 } |
| 118 | 121 |
| 122 if (allow_all_urls && pattern.match_all_urls()) { |
| 123 matches.AddPattern(pattern); |
| 124 continue; |
| 125 } |
| 126 |
| 119 // Wildcard hosts are not allowed. | 127 // Wildcard hosts are not allowed. |
| 120 if (pattern.host().empty()) { | 128 if (pattern.host().empty()) { |
| 121 // Warning not error for forwards compatibility. | 129 // Warning not error for forwards compatibility. |
| 122 install_warnings->push_back( | 130 install_warnings->push_back( |
| 123 InstallWarning(ErrorUtils::FormatErrorMessage( | 131 InstallWarning(ErrorUtils::FormatErrorMessage( |
| 124 errors::kErrorWildcardHostsNotAllowed, *it), | 132 errors::kErrorWildcardHostsNotAllowed, *it), |
| 125 keys::kExternallyConnectable, | 133 keys::kExternallyConnectable, |
| 126 *it)); | 134 *it)); |
| 127 continue; | 135 continue; |
| 128 } | 136 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 218 } |
| 211 | 219 |
| 212 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) { | 220 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) { |
| 213 if (all_ids) | 221 if (all_ids) |
| 214 return true; | 222 return true; |
| 215 DCHECK(base::STLIsSorted(ids)); | 223 DCHECK(base::STLIsSorted(ids)); |
| 216 return std::binary_search(ids.begin(), ids.end(), id); | 224 return std::binary_search(ids.begin(), ids.end(), id); |
| 217 } | 225 } |
| 218 | 226 |
| 219 } // namespace extensions | 227 } // namespace extensions |
| OLD | NEW |