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/api/sockets/sockets_manifest_permission.h" | 5 #include "extensions/common/api/sockets/sockets_manifest_permission.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 return true; | 71 return true; |
72 } | 72 } |
73 | 73 |
74 static void SetHostPatterns( | 74 static void SetHostPatterns( |
75 scoped_ptr<SocketHostPatterns>& host_patterns, | 75 scoped_ptr<SocketHostPatterns>& host_patterns, |
76 const SocketsManifestPermission* permission, | 76 const SocketsManifestPermission* permission, |
77 content::SocketPermissionRequest::OperationType operation_type) { | 77 content::SocketPermissionRequest::OperationType operation_type) { |
78 host_patterns.reset(new SocketHostPatterns()); | 78 host_patterns.reset(new SocketHostPatterns()); |
79 host_patterns->as_strings.reset(new std::vector<std::string>()); | 79 host_patterns->as_strings.reset(new std::vector<std::string>()); |
80 for (SocketsManifestPermission::SocketPermissionEntrySet::const_iterator it = | 80 for (SocketPermissionEntrySet::const_iterator it = |
81 permission->entries().begin(); | 81 permission->entries().begin(); |
82 it != permission->entries().end(); | 82 it != permission->entries().end(); ++it) { |
83 ++it) { | |
84 if (it->pattern().type == operation_type) { | 83 if (it->pattern().type == operation_type) { |
85 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); | 84 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); |
86 } | 85 } |
87 } | 86 } |
88 } | 87 } |
89 | 88 |
| 89 // Helper function for adding the 'any host' permission. Determines if the |
| 90 // message is needed from |sockets|, and adds the permission to |ids| and/or |
| 91 // |messages|, ignoring them if they are NULL. Returns true if it added the |
| 92 // message. |
| 93 bool AddAnyHostMessage(const SocketPermissionEntrySet& sockets, |
| 94 PermissionIDSet* ids, |
| 95 PermissionMessages* messages) { |
| 96 for (const auto& socket : sockets) { |
| 97 if (socket.IsAddressBoundType() && |
| 98 socket.GetHostType() == SocketPermissionEntry::ANY_HOST) { |
| 99 // TODO(sashab): Add a rule to ChromePermissionMessageProvider: |
| 100 // kSocketAnyHost -> IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST |
| 101 if (ids) |
| 102 ids->insert(APIPermission::kSocketAnyHost); |
| 103 if (messages) { |
| 104 messages->push_back(PermissionMessage( |
| 105 PermissionMessage::kSocketAnyHost, |
| 106 l10n_util::GetStringUTF16( |
| 107 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST))); |
| 108 } |
| 109 return true; |
| 110 } |
| 111 } |
| 112 return false; |
| 113 } |
| 114 |
| 115 // Helper function for adding subdomain socket permissions. Determines what |
| 116 // messages are needed from |sockets|, and adds permissions to |ids| and/or |
| 117 // |messages|, ignoring them if they are NULL. |
| 118 void AddSubdomainHostMessage(const SocketPermissionEntrySet& sockets, |
| 119 PermissionIDSet* ids, |
| 120 PermissionMessages* messages) { |
| 121 std::set<base::string16> domains; |
| 122 for (const auto& socket : sockets) { |
| 123 if (socket.GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) |
| 124 domains.insert(base::UTF8ToUTF16(socket.pattern().host)); |
| 125 } |
| 126 if (!domains.empty()) { |
| 127 // TODO(sashab): This is not correct for all languages - add proper |
| 128 // internationalization of this string for all plural states. |
| 129 if (messages) { |
| 130 int id = (domains.size() == 1) |
| 131 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN |
| 132 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS; |
| 133 messages->push_back(PermissionMessage( |
| 134 PermissionMessage::kSocketDomainHosts, |
| 135 l10n_util::GetStringFUTF16( |
| 136 id, JoinString(std::vector<base::string16>(domains.begin(), |
| 137 domains.end()), |
| 138 ' ')))); |
| 139 } |
| 140 // TODO(sashab): Add rules to ChromePermissionMessageProvider: |
| 141 // kSocketDomainHosts -> |
| 142 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN if 1 |
| 143 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS if many |
| 144 if (ids) { |
| 145 for (const auto& domain : domains) |
| 146 ids->insert(APIPermission::kSocketDomainHosts, domain); |
| 147 } |
| 148 } |
| 149 } |
| 150 |
| 151 // Helper function for adding specific host socket permissions. Determines what |
| 152 // messages are needed from |sockets|, and adds permissions to |ids| and/or |
| 153 // |messages|, ignoring them if they are NULL. |
| 154 void AddSpecificHostMessage(const SocketPermissionEntrySet& sockets, |
| 155 PermissionIDSet* ids, |
| 156 PermissionMessages* messages) { |
| 157 std::set<base::string16> hostnames; |
| 158 for (const auto& socket : sockets) { |
| 159 if (socket.GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) |
| 160 hostnames.insert(base::UTF8ToUTF16(socket.pattern().host)); |
| 161 } |
| 162 if (!hostnames.empty()) { |
| 163 // TODO(sashab): This is not correct for all languages - add proper |
| 164 // internationalization of this string for all plural states. |
| 165 if (messages) { |
| 166 int id = (hostnames.size() == 1) |
| 167 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST |
| 168 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS; |
| 169 messages->push_back(PermissionMessage( |
| 170 PermissionMessage::kSocketSpecificHosts, |
| 171 l10n_util::GetStringFUTF16( |
| 172 id, JoinString(std::vector<base::string16>(hostnames.begin(), |
| 173 hostnames.end()), |
| 174 ' ')))); |
| 175 } |
| 176 // TODO(sashab): Add rules to ChromePermissionMessageProvider: |
| 177 // kSocketSpecificHosts -> |
| 178 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST if 1 |
| 179 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS if many |
| 180 if (ids) { |
| 181 for (const auto& hostname : hostnames) |
| 182 ids->insert(APIPermission::kSocketSpecificHosts, hostname); |
| 183 } |
| 184 } |
| 185 } |
| 186 |
| 187 // Helper function for adding the network list socket permission. Determines if |
| 188 // the message is needed from |sockets|, and adds the permission to |ids| and/or |
| 189 // |messages|, ignoring them if they are NULL. |
| 190 void AddNetworkListMessage(const SocketPermissionEntrySet& sockets, |
| 191 PermissionIDSet* ids, |
| 192 PermissionMessages* messages) { |
| 193 for (const auto& socket : sockets) { |
| 194 if (socket.pattern().type == SocketPermissionRequest::NETWORK_STATE) { |
| 195 // TODO(sashab): Add a rule to ChromePermissionMessageProvider: |
| 196 // kNetworkState -> IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE |
| 197 if (ids) |
| 198 ids->insert(APIPermission::kNetworkState); |
| 199 if (messages) { |
| 200 messages->push_back( |
| 201 PermissionMessage(PermissionMessage::kNetworkState, |
| 202 l10n_util::GetStringUTF16( |
| 203 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE))); |
| 204 } |
| 205 } |
| 206 } |
| 207 } |
| 208 |
90 } // namespace | 209 } // namespace |
91 | 210 |
92 SocketsManifestPermission::SocketsManifestPermission() {} | 211 SocketsManifestPermission::SocketsManifestPermission() {} |
93 | 212 |
94 SocketsManifestPermission::~SocketsManifestPermission() {} | 213 SocketsManifestPermission::~SocketsManifestPermission() {} |
95 | 214 |
96 // static | 215 // static |
97 scoped_ptr<SocketsManifestPermission> SocketsManifestPermission::FromValue( | 216 scoped_ptr<SocketsManifestPermission> SocketsManifestPermission::FromValue( |
98 const base::Value& value, | 217 const base::Value& value, |
99 base::string16* error) { | 218 base::string16* error) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 return false; | 272 return false; |
154 } | 273 } |
155 | 274 |
156 std::string SocketsManifestPermission::name() const { | 275 std::string SocketsManifestPermission::name() const { |
157 return manifest_keys::kSockets; | 276 return manifest_keys::kSockets; |
158 } | 277 } |
159 | 278 |
160 std::string SocketsManifestPermission::id() const { return name(); } | 279 std::string SocketsManifestPermission::id() const { return name(); } |
161 | 280 |
162 PermissionIDSet SocketsManifestPermission::GetPermissions() const { | 281 PermissionIDSet SocketsManifestPermission::GetPermissions() const { |
163 PermissionMessages messages; | |
164 PermissionIDSet ids; | 282 PermissionIDSet ids; |
165 AddAllHostMessages(messages, ids); | 283 AddSocketHostPermissions(permissions_, &ids, NULL); |
166 return ids; | 284 return ids; |
167 } | 285 } |
168 | 286 |
169 bool SocketsManifestPermission::HasMessages() const { | 287 bool SocketsManifestPermission::HasMessages() const { |
170 bool is_empty = permissions_.empty(); | 288 bool is_empty = permissions_.empty(); |
171 return !is_empty; | 289 return !is_empty; |
172 } | 290 } |
173 | 291 |
174 PermissionMessages SocketsManifestPermission::GetMessages() const { | 292 PermissionMessages SocketsManifestPermission::GetMessages() const { |
175 PermissionMessages messages; | 293 PermissionMessages messages; |
176 PermissionIDSet ids; | 294 AddSocketHostPermissions(permissions_, NULL, &messages); |
177 AddAllHostMessages(messages, ids); | |
178 return messages; | 295 return messages; |
179 } | 296 } |
180 | 297 |
181 bool SocketsManifestPermission::FromValue(const base::Value* value) { | 298 bool SocketsManifestPermission::FromValue(const base::Value* value) { |
182 if (!value) | 299 if (!value) |
183 return false; | 300 return false; |
184 base::string16 error; | 301 base::string16 error; |
185 scoped_ptr<SocketsManifestPermission> manifest_permission( | 302 scoped_ptr<SocketsManifestPermission> manifest_permission( |
186 SocketsManifestPermission::FromValue(*value, &error)); | 303 SocketsManifestPermission::FromValue(*value, &error)); |
187 | 304 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 result->permissions_ = base::STLSetIntersection<SocketPermissionEntrySet>( | 373 result->permissions_ = base::STLSetIntersection<SocketPermissionEntrySet>( |
257 permissions_, other->permissions_); | 374 permissions_, other->permissions_); |
258 return result.release(); | 375 return result.release(); |
259 } | 376 } |
260 | 377 |
261 void SocketsManifestPermission::AddPermission( | 378 void SocketsManifestPermission::AddPermission( |
262 const SocketPermissionEntry& entry) { | 379 const SocketPermissionEntry& entry) { |
263 permissions_.insert(entry); | 380 permissions_.insert(entry); |
264 } | 381 } |
265 | 382 |
266 void SocketsManifestPermission::AddAllHostMessages(PermissionMessages& messages, | 383 // static |
267 PermissionIDSet& ids) const { | 384 void SocketsManifestPermission::AddSocketHostPermissions( |
268 // TODO(rpaquay): This function and callees is (almost) a copy/paste from | 385 const SocketPermissionEntrySet& sockets, |
269 // extensions::SocketPermission. | 386 PermissionIDSet* ids, |
270 if (!AddAnyHostMessage(messages, ids)) { | 387 PermissionMessages* messages) { |
271 AddSpecificHostMessage(messages, ids); | 388 if (!AddAnyHostMessage(sockets, ids, messages)) { |
272 AddSubdomainHostMessage(messages, ids); | 389 AddSpecificHostMessage(sockets, ids, messages); |
| 390 AddSubdomainHostMessage(sockets, ids, messages); |
273 } | 391 } |
274 AddNetworkListMessage(messages, ids); | 392 AddNetworkListMessage(sockets, ids, messages); |
275 } | |
276 | |
277 bool SocketsManifestPermission::AddAnyHostMessage(PermissionMessages& messages, | |
278 PermissionIDSet& ids) const { | |
279 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | |
280 it != permissions_.end(); | |
281 ++it) { | |
282 if (it->IsAddressBoundType() && | |
283 it->GetHostType() == SocketPermissionEntry::ANY_HOST) { | |
284 // TODO(sashab): Add a rule to ChromePermissionMessageProvider: | |
285 // kSocketAnyHost -> IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST | |
286 ids.insert(APIPermission::kSocketAnyHost); | |
287 messages.push_back( | |
288 PermissionMessage(PermissionMessage::kSocketAnyHost, | |
289 l10n_util::GetStringUTF16( | |
290 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST))); | |
291 return true; | |
292 } | |
293 } | |
294 return false; | |
295 } | |
296 | |
297 void SocketsManifestPermission::AddSubdomainHostMessage( | |
298 PermissionMessages& messages, | |
299 PermissionIDSet& ids) const { | |
300 std::set<base::string16> domains; | |
301 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | |
302 it != permissions_.end(); | |
303 ++it) { | |
304 if (it->GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) | |
305 domains.insert(base::UTF8ToUTF16(it->pattern().host)); | |
306 } | |
307 if (!domains.empty()) { | |
308 // TODO(sashab): This is not correct for all languages - add proper | |
309 // internationalization of this string for all plural states. | |
310 int id = (domains.size() == 1) | |
311 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN | |
312 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS; | |
313 messages.push_back(PermissionMessage( | |
314 PermissionMessage::kSocketDomainHosts, | |
315 l10n_util::GetStringFUTF16( | |
316 id, | |
317 JoinString( | |
318 std::vector<base::string16>(domains.begin(), domains.end()), | |
319 ' ')))); | |
320 // TODO(sashab): Add rules to ChromePermissionMessageProvider: | |
321 // kSocketDomainHosts -> | |
322 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN if 1 | |
323 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS if many | |
324 for (const auto& domain : domains) | |
325 ids.insert(APIPermission::kSocketDomainHosts, domain); | |
326 } | |
327 } | |
328 | |
329 void SocketsManifestPermission::AddSpecificHostMessage( | |
330 PermissionMessages& messages, | |
331 PermissionIDSet& ids) const { | |
332 std::set<base::string16> hostnames; | |
333 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | |
334 it != permissions_.end(); | |
335 ++it) { | |
336 if (it->GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) | |
337 hostnames.insert(base::UTF8ToUTF16(it->pattern().host)); | |
338 } | |
339 if (!hostnames.empty()) { | |
340 // TODO(sashab): This is not correct for all languages - add proper | |
341 // internationalization of this string for all plural states. | |
342 int id = (hostnames.size() == 1) | |
343 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST | |
344 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS; | |
345 messages.push_back(PermissionMessage( | |
346 PermissionMessage::kSocketSpecificHosts, | |
347 l10n_util::GetStringFUTF16( | |
348 id, | |
349 JoinString( | |
350 std::vector<base::string16>(hostnames.begin(), hostnames.end()), | |
351 ' ')))); | |
352 // TODO(sashab): Add rules to ChromePermissionMessageProvider: | |
353 // kSocketSpecificHosts -> | |
354 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST if 1 | |
355 // IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS if many | |
356 for (const auto& hostname : hostnames) | |
357 ids.insert(APIPermission::kSocketSpecificHosts, hostname); | |
358 } | |
359 } | |
360 | |
361 void SocketsManifestPermission::AddNetworkListMessage( | |
362 PermissionMessages& messages, | |
363 PermissionIDSet& ids) const { | |
364 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | |
365 it != permissions_.end(); | |
366 ++it) { | |
367 if (it->pattern().type == SocketPermissionRequest::NETWORK_STATE) { | |
368 // TODO(sashab): Add a rule to ChromePermissionMessageProvider: | |
369 // kNetworkState -> IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE | |
370 ids.insert(APIPermission::kNetworkState); | |
371 messages.push_back( | |
372 PermissionMessage(PermissionMessage::kNetworkState, | |
373 l10n_util::GetStringUTF16( | |
374 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE))); | |
375 } | |
376 } | |
377 } | 393 } |
378 | 394 |
379 } // namespace extensions | 395 } // namespace extensions |
OLD | NEW |