OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/common/extensions/manifest_handlers/externally_connectable.h" | 5 #include "chrome/common/extensions/manifest_handlers/externally_connectable.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/stl_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/common/extensions/api/manifest_types.h" | 11 #include "chrome/common/extensions/api/manifest_types.h" |
9 #include "chrome/common/extensions/extension_manifest_constants.h" | 12 #include "chrome/common/extensions/extension_manifest_constants.h" |
10 #include "chrome/common/extensions/permissions/api_permission_set.h" | 13 #include "chrome/common/extensions/permissions/api_permission_set.h" |
11 #include "chrome/common/extensions/permissions/permissions_data.h" | 14 #include "chrome/common/extensions/permissions/permissions_data.h" |
12 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
13 #include "extensions/common/url_pattern.h" | 16 #include "extensions/common/url_pattern.h" |
14 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
16 | 19 |
(...skipping 10 matching lines...) Expand all Loading... |
27 "as \"*\" are not allowed"; | 30 "as \"*\" are not allowed"; |
28 const char kErrorWildcardHostsNotAllowed[] = | 31 const char kErrorWildcardHostsNotAllowed[] = |
29 "Wildcard domain patterns such as \"*\" are not allowed"; | 32 "Wildcard domain patterns such as \"*\" are not allowed"; |
30 } | 33 } |
31 | 34 |
32 namespace keys = extension_manifest_keys; | 35 namespace keys = extension_manifest_keys; |
33 namespace errors = externally_connectable_errors; | 36 namespace errors = externally_connectable_errors; |
34 using api::manifest_types::ExternallyConnectable; | 37 using api::manifest_types::ExternallyConnectable; |
35 | 38 |
36 namespace { | 39 namespace { |
| 40 |
37 const char kAllIds[] = "*"; | 41 const char kAllIds[] = "*"; |
| 42 |
| 43 template <typename T> |
| 44 std::vector<T> Sorted(const std::vector<T>& in) { |
| 45 std::vector<T> out = in; |
| 46 std::sort(out.begin(), out.end()); |
| 47 return out; |
38 } | 48 } |
39 | 49 |
| 50 } // namespace |
| 51 |
40 ExternallyConnectableHandler::ExternallyConnectableHandler() {} | 52 ExternallyConnectableHandler::ExternallyConnectableHandler() {} |
41 | 53 |
42 ExternallyConnectableHandler::~ExternallyConnectableHandler() {} | 54 ExternallyConnectableHandler::~ExternallyConnectableHandler() {} |
43 | 55 |
44 bool ExternallyConnectableHandler::Parse(Extension* extension, | 56 bool ExternallyConnectableHandler::Parse(Extension* extension, |
45 string16* error) { | 57 string16* error) { |
46 const base::Value* externally_connectable = NULL; | 58 const base::Value* externally_connectable = NULL; |
47 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, | 59 CHECK(extension->manifest()->Get(keys::kExternallyConnectable, |
48 &externally_connectable)); | 60 &externally_connectable)); |
49 scoped_ptr<ExternallyConnectableInfo> info = | 61 scoped_ptr<ExternallyConnectableInfo> info = |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 pattern.host().c_str(), | 140 pattern.host().c_str(), |
129 *it); | 141 *it); |
130 return scoped_ptr<ExternallyConnectableInfo>(); | 142 return scoped_ptr<ExternallyConnectableInfo>(); |
131 } | 143 } |
132 | 144 |
133 matches.AddPattern(pattern); | 145 matches.AddPattern(pattern); |
134 } | 146 } |
135 } | 147 } |
136 | 148 |
137 std::vector<std::string> ids; | 149 std::vector<std::string> ids; |
138 bool matches_all_ids = false; | 150 bool all_ids = false; |
139 | 151 |
140 if (externally_connectable->ids) { | 152 if (externally_connectable->ids) { |
141 for (std::vector<std::string>::iterator it = | 153 for (std::vector<std::string>::iterator it = |
142 externally_connectable->ids->begin(); | 154 externally_connectable->ids->begin(); |
143 it != externally_connectable->ids->end(); ++it) { | 155 it != externally_connectable->ids->end(); ++it) { |
144 if (*it == kAllIds) { | 156 if (*it == kAllIds) { |
145 matches_all_ids = true; | 157 all_ids = true; |
146 } else if (Extension::IdIsValid(*it)) { | 158 } else if (Extension::IdIsValid(*it)) { |
147 ids.push_back(*it); | 159 ids.push_back(*it); |
148 } else { | 160 } else { |
149 *error = ErrorUtils::FormatErrorMessageUTF16( | 161 *error = ErrorUtils::FormatErrorMessageUTF16( |
150 errors::kErrorInvalidId, *it); | 162 errors::kErrorInvalidId, *it); |
151 return scoped_ptr<ExternallyConnectableInfo>(); | 163 return scoped_ptr<ExternallyConnectableInfo>(); |
152 } | 164 } |
153 } | 165 } |
154 } | 166 } |
155 | 167 |
156 return make_scoped_ptr( | 168 return make_scoped_ptr( |
157 new ExternallyConnectableInfo(matches, ids, matches_all_ids)); | 169 new ExternallyConnectableInfo(matches, ids, all_ids)); |
158 } | 170 } |
159 | 171 |
160 ExternallyConnectableInfo::~ExternallyConnectableInfo() {} | 172 ExternallyConnectableInfo::~ExternallyConnectableInfo() {} |
161 | 173 |
162 ExternallyConnectableInfo::ExternallyConnectableInfo( | 174 ExternallyConnectableInfo::ExternallyConnectableInfo( |
163 const URLPatternSet& matches, | 175 const URLPatternSet& matches, |
164 const std::vector<std::string>& ids, | 176 const std::vector<std::string>& ids, |
165 bool matches_all_ids) | 177 bool all_ids) |
166 : matches(matches), ids(ids), matches_all_ids(matches_all_ids) {} | 178 : matches(matches), ids(Sorted(ids)), all_ids(all_ids) {} |
| 179 |
| 180 bool ExternallyConnectableInfo::IdCanConnect(const std::string& id) { |
| 181 if (all_ids) |
| 182 return true; |
| 183 DCHECK(base::STLIsSorted(ids)); |
| 184 return std::binary_search(ids.begin(), ids.end(), id); |
| 185 } |
167 | 186 |
168 } // namespace extensions | 187 } // namespace extensions |
OLD | NEW |