| 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/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/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/common/extensions/api/manifest_types.h" | 11 #include "extensions/common/api/manifest_types.h" |
| 12 #include "chrome/common/extensions/api/sockets/sockets_manifest_data.h" | 12 #include "extensions/common/api/sockets/sockets_manifest_data.h" |
| 13 #include "extensions/common/error_utils.h" | 13 #include "extensions/common/error_utils.h" |
| 14 #include "extensions/common/extension_messages.h" | 14 #include "extensions/common/extension_messages.h" |
| 15 #include "extensions/common/manifest_constants.h" | 15 #include "extensions/common/manifest_constants.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 17 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 | 21 |
| 22 namespace sockets_errors { | 22 namespace sockets_errors { |
| 23 const char kErrorInvalidHostPattern[] = "Invalid host:port pattern '*'"; | 23 const char kErrorInvalidHostPattern[] = "Invalid host:port pattern '*'"; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace errors = sockets_errors; | 26 namespace errors = sockets_errors; |
| 27 using api::manifest_types::Sockets; | 27 using core_api::manifest_types::Sockets; |
| 28 using api::manifest_types::SocketHostPatterns; | 28 using core_api::manifest_types::SocketHostPatterns; |
| 29 using content::SocketPermissionRequest; | 29 using content::SocketPermissionRequest; |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 static bool ParseHostPattern( | 33 static bool ParseHostPattern( |
| 34 SocketsManifestPermission* permission, | 34 SocketsManifestPermission* permission, |
| 35 content::SocketPermissionRequest::OperationType operation_type, | 35 content::SocketPermissionRequest::OperationType operation_type, |
| 36 const std::string& host_pattern, | 36 const std::string& host_pattern, |
| 37 base::string16* error) { | 37 base::string16* error) { |
| 38 SocketPermissionEntry entry; | 38 SocketPermissionEntry entry; |
| 39 if (!SocketPermissionEntry::ParseHostPattern( | 39 if (!SocketPermissionEntry::ParseHostPattern( |
| 40 operation_type, host_pattern, &entry)) { | 40 operation_type, host_pattern, &entry)) { |
| 41 *error = ErrorUtils::FormatErrorMessageUTF16( | 41 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 42 errors::kErrorInvalidHostPattern, host_pattern); | 42 errors::kErrorInvalidHostPattern, host_pattern); |
| 43 return false; | 43 return false; |
| 44 } | 44 } |
| 45 permission->AddPermission(entry); | 45 permission->AddPermission(entry); |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 static bool ParseHostPatterns( | 49 static bool ParseHostPatterns( |
| 50 SocketsManifestPermission* permission, | 50 SocketsManifestPermission* permission, |
| 51 content::SocketPermissionRequest::OperationType operation_type, | 51 content::SocketPermissionRequest::OperationType operation_type, |
| 52 const scoped_ptr<SocketHostPatterns>& host_patterns, | 52 const scoped_ptr<SocketHostPatterns>& host_patterns, |
| 53 base::string16* error) { | 53 base::string16* error) { |
| 54 if (!host_patterns) | 54 if (!host_patterns) |
| 55 return true; | 55 return true; |
| 56 | 56 |
| 57 if (host_patterns->as_string) { | 57 if (host_patterns->as_string) { |
| 58 return ParseHostPattern(permission, operation_type, | 58 return ParseHostPattern( |
| 59 *host_patterns->as_string, error); | 59 permission, operation_type, *host_patterns->as_string, error); |
| 60 } | 60 } |
| 61 | 61 |
| 62 CHECK(host_patterns->as_strings); | 62 CHECK(host_patterns->as_strings); |
| 63 for (std::vector<std::string>::const_iterator it = | 63 for (std::vector<std::string>::const_iterator it = |
| 64 host_patterns->as_strings->begin(); | 64 host_patterns->as_strings->begin(); |
| 65 it != host_patterns->as_strings->end(); ++it) { | 65 it != host_patterns->as_strings->end(); |
| 66 ++it) { |
| 66 if (!ParseHostPattern(permission, operation_type, *it, error)) { | 67 if (!ParseHostPattern(permission, operation_type, *it, error)) { |
| 67 return false; | 68 return false; |
| 68 } | 69 } |
| 69 } | 70 } |
| 70 return true; | 71 return true; |
| 71 } | 72 } |
| 72 | 73 |
| 73 static void SetHostPatterns( | 74 static void SetHostPatterns( |
| 74 scoped_ptr<SocketHostPatterns>& host_patterns, | 75 scoped_ptr<SocketHostPatterns>& host_patterns, |
| 75 const SocketsManifestPermission* permission, | 76 const SocketsManifestPermission* permission, |
| 76 content::SocketPermissionRequest::OperationType operation_type) { | 77 content::SocketPermissionRequest::OperationType operation_type) { |
| 77 host_patterns.reset(new SocketHostPatterns()); | 78 host_patterns.reset(new SocketHostPatterns()); |
| 78 host_patterns->as_strings.reset(new std::vector<std::string>()); | 79 host_patterns->as_strings.reset(new std::vector<std::string>()); |
| 79 for (SocketsManifestPermission::SocketPermissionEntrySet::const_iterator it = | 80 for (SocketsManifestPermission::SocketPermissionEntrySet::const_iterator it = |
| 80 permission->entries().begin(); it != permission->entries().end() ; ++it) { | 81 permission->entries().begin(); |
| 82 it != permission->entries().end(); |
| 83 ++it) { |
| 81 if (it->pattern().type == operation_type) { | 84 if (it->pattern().type == operation_type) { |
| 82 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); | 85 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); |
| 83 } | 86 } |
| 84 } | 87 } |
| 85 } | 88 } |
| 86 | 89 |
| 87 } // namespace | 90 } // namespace |
| 88 | 91 |
| 89 SocketsManifestPermission::SocketsManifestPermission() {} | 92 SocketsManifestPermission::SocketsManifestPermission() {} |
| 90 | 93 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 return scoped_ptr<SocketsManifestPermission>(); | 138 return scoped_ptr<SocketsManifestPermission>(); |
| 136 } | 139 } |
| 137 } | 140 } |
| 138 return result.Pass(); | 141 return result.Pass(); |
| 139 } | 142 } |
| 140 | 143 |
| 141 bool SocketsManifestPermission::CheckRequest( | 144 bool SocketsManifestPermission::CheckRequest( |
| 142 const Extension* extension, | 145 const Extension* extension, |
| 143 const SocketPermissionRequest& request) const { | 146 const SocketPermissionRequest& request) const { |
| 144 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | 147 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); |
| 145 it != permissions_.end(); ++it) { | 148 it != permissions_.end(); |
| 149 ++it) { |
| 146 if (it->Check(request)) | 150 if (it->Check(request)) |
| 147 return true; | 151 return true; |
| 148 } | 152 } |
| 149 return false; | 153 return false; |
| 150 } | 154 } |
| 151 | 155 |
| 152 std::string SocketsManifestPermission::name() const { | 156 std::string SocketsManifestPermission::name() const { |
| 153 return manifest_keys::kSockets; | 157 return manifest_keys::kSockets; |
| 154 } | 158 } |
| 155 | 159 |
| 156 std::string SocketsManifestPermission::id() const { | 160 std::string SocketsManifestPermission::id() const { return name(); } |
| 157 return name(); | |
| 158 } | |
| 159 | 161 |
| 160 bool SocketsManifestPermission::HasMessages() const { | 162 bool SocketsManifestPermission::HasMessages() const { |
| 161 bool is_empty = permissions_.empty(); | 163 bool is_empty = permissions_.empty(); |
| 162 return !is_empty; | 164 return !is_empty; |
| 163 } | 165 } |
| 164 | 166 |
| 165 PermissionMessages SocketsManifestPermission::GetMessages() const { | 167 PermissionMessages SocketsManifestPermission::GetMessages() const { |
| 166 // TODO(rpaquay): This function and callees is (almost) a copy/paste | 168 // TODO(rpaquay): This function and callees is (almost) a copy/paste |
| 167 // from extensions::SocketPermissiona. | 169 // from extensions::SocketPermissiona. |
| 168 PermissionMessages result; | 170 PermissionMessages result; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 185 return false; | 187 return false; |
| 186 | 188 |
| 187 permissions_ = manifest_permission->permissions_; | 189 permissions_ = manifest_permission->permissions_; |
| 188 return true; | 190 return true; |
| 189 } | 191 } |
| 190 | 192 |
| 191 scoped_ptr<base::Value> SocketsManifestPermission::ToValue() const { | 193 scoped_ptr<base::Value> SocketsManifestPermission::ToValue() const { |
| 192 Sockets sockets; | 194 Sockets sockets; |
| 193 | 195 |
| 194 sockets.udp.reset(new Sockets::Udp()); | 196 sockets.udp.reset(new Sockets::Udp()); |
| 195 SetHostPatterns(sockets.udp->bind, this, | 197 SetHostPatterns(sockets.udp->bind, this, SocketPermissionRequest::UDP_BIND); |
| 196 SocketPermissionRequest::UDP_BIND); | 198 SetHostPatterns( |
| 197 SetHostPatterns(sockets.udp->send, this, | 199 sockets.udp->send, this, SocketPermissionRequest::UDP_SEND_TO); |
| 198 SocketPermissionRequest::UDP_SEND_TO); | 200 SetHostPatterns(sockets.udp->multicast_membership, |
| 199 SetHostPatterns(sockets.udp->multicast_membership, this, | 201 this, |
| 200 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP); | 202 SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP); |
| 201 if (sockets.udp->bind->as_strings->size() == 0 && | 203 if (sockets.udp->bind->as_strings->size() == 0 && |
| 202 sockets.udp->send->as_strings->size() == 0 && | 204 sockets.udp->send->as_strings->size() == 0 && |
| 203 sockets.udp->multicast_membership->as_strings->size() == 0) { | 205 sockets.udp->multicast_membership->as_strings->size() == 0) { |
| 204 sockets.udp.reset(NULL); | 206 sockets.udp.reset(NULL); |
| 205 } | 207 } |
| 206 | 208 |
| 207 sockets.tcp.reset(new Sockets::Tcp()); | 209 sockets.tcp.reset(new Sockets::Tcp()); |
| 208 SetHostPatterns(sockets.tcp->connect, this, | 210 SetHostPatterns( |
| 209 SocketPermissionRequest::TCP_CONNECT); | 211 sockets.tcp->connect, this, SocketPermissionRequest::TCP_CONNECT); |
| 210 if (sockets.tcp->connect->as_strings->size() == 0) { | 212 if (sockets.tcp->connect->as_strings->size() == 0) { |
| 211 sockets.tcp.reset(NULL); | 213 sockets.tcp.reset(NULL); |
| 212 } | 214 } |
| 213 | 215 |
| 214 sockets.tcp_server.reset(new Sockets::TcpServer()); | 216 sockets.tcp_server.reset(new Sockets::TcpServer()); |
| 215 SetHostPatterns(sockets.tcp_server->listen, this, | 217 SetHostPatterns( |
| 216 SocketPermissionRequest::TCP_LISTEN); | 218 sockets.tcp_server->listen, this, SocketPermissionRequest::TCP_LISTEN); |
| 217 if (sockets.tcp_server->listen->as_strings->size() == 0) { | 219 if (sockets.tcp_server->listen->as_strings->size() == 0) { |
| 218 sockets.tcp_server.reset(NULL); | 220 sockets.tcp_server.reset(NULL); |
| 219 } | 221 } |
| 220 | 222 |
| 221 return scoped_ptr<base::Value>(sockets.ToValue().release()).Pass(); | 223 return scoped_ptr<base::Value>(sockets.ToValue().release()).Pass(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 ManifestPermission* SocketsManifestPermission::Clone() const { | 226 ManifestPermission* SocketsManifestPermission::Clone() const { |
| 225 scoped_ptr<SocketsManifestPermission> result(new SocketsManifestPermission()); | 227 scoped_ptr<SocketsManifestPermission> result(new SocketsManifestPermission()); |
| 226 result->permissions_ = permissions_; | 228 result->permissions_ = permissions_; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 } | 291 } |
| 290 | 292 |
| 291 void SocketsManifestPermission::AddPermission( | 293 void SocketsManifestPermission::AddPermission( |
| 292 const SocketPermissionEntry& entry) { | 294 const SocketPermissionEntry& entry) { |
| 293 permissions_.insert(entry); | 295 permissions_.insert(entry); |
| 294 } | 296 } |
| 295 | 297 |
| 296 bool SocketsManifestPermission::AddAnyHostMessage( | 298 bool SocketsManifestPermission::AddAnyHostMessage( |
| 297 PermissionMessages& messages) const { | 299 PermissionMessages& messages) const { |
| 298 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | 300 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); |
| 299 it != permissions_.end(); ++it) { | 301 it != permissions_.end(); |
| 302 ++it) { |
| 300 if (it->IsAddressBoundType() && | 303 if (it->IsAddressBoundType() && |
| 301 it->GetHostType() == SocketPermissionEntry::ANY_HOST) { | 304 it->GetHostType() == SocketPermissionEntry::ANY_HOST) { |
| 302 messages.push_back(PermissionMessage( | 305 messages.push_back( |
| 303 PermissionMessage::kSocketAnyHost, | 306 PermissionMessage(PermissionMessage::kSocketAnyHost, |
| 304 l10n_util::GetStringUTF16( | 307 l10n_util::GetStringUTF16( |
| 305 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST))); | 308 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST))); |
| 306 return true; | 309 return true; |
| 307 } | 310 } |
| 308 } | 311 } |
| 309 return false; | 312 return false; |
| 310 } | 313 } |
| 311 | 314 |
| 312 void SocketsManifestPermission::AddSubdomainHostMessage( | 315 void SocketsManifestPermission::AddSubdomainHostMessage( |
| 313 PermissionMessages& messages) const { | 316 PermissionMessages& messages) const { |
| 314 std::set<base::string16> domains; | 317 std::set<base::string16> domains; |
| 315 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | 318 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); |
| 316 it != permissions_.end(); ++it) { | 319 it != permissions_.end(); |
| 320 ++it) { |
| 317 if (it->GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) | 321 if (it->GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) |
| 318 domains.insert(base::UTF8ToUTF16(it->pattern().host)); | 322 domains.insert(base::UTF8ToUTF16(it->pattern().host)); |
| 319 } | 323 } |
| 320 if (!domains.empty()) { | 324 if (!domains.empty()) { |
| 321 int id = (domains.size() == 1) ? | 325 int id = (domains.size() == 1) |
| 322 IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN : | 326 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN |
| 323 IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS; | 327 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS; |
| 324 messages.push_back(PermissionMessage( | 328 messages.push_back(PermissionMessage( |
| 325 PermissionMessage::kSocketDomainHosts, | 329 PermissionMessage::kSocketDomainHosts, |
| 326 l10n_util::GetStringFUTF16( | 330 l10n_util::GetStringFUTF16( |
| 327 id, | 331 id, |
| 328 JoinString( | 332 JoinString( |
| 329 std::vector<base::string16>( | 333 std::vector<base::string16>(domains.begin(), domains.end()), |
| 330 domains.begin(), domains.end()), ' ')))); | 334 ' ')))); |
| 331 } | 335 } |
| 332 } | 336 } |
| 333 | 337 |
| 334 void SocketsManifestPermission::AddSpecificHostMessage( | 338 void SocketsManifestPermission::AddSpecificHostMessage( |
| 335 PermissionMessages& messages) const { | 339 PermissionMessages& messages) const { |
| 336 std::set<base::string16> hostnames; | 340 std::set<base::string16> hostnames; |
| 337 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | 341 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); |
| 338 it != permissions_.end(); ++it) { | 342 it != permissions_.end(); |
| 343 ++it) { |
| 339 if (it->GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) | 344 if (it->GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) |
| 340 hostnames.insert(base::UTF8ToUTF16(it->pattern().host)); | 345 hostnames.insert(base::UTF8ToUTF16(it->pattern().host)); |
| 341 } | 346 } |
| 342 if (!hostnames.empty()) { | 347 if (!hostnames.empty()) { |
| 343 int id = (hostnames.size() == 1) ? | 348 int id = (hostnames.size() == 1) |
| 344 IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST : | 349 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST |
| 345 IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS; | 350 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS; |
| 346 messages.push_back(PermissionMessage( | 351 messages.push_back(PermissionMessage( |
| 347 PermissionMessage::kSocketSpecificHosts, | 352 PermissionMessage::kSocketSpecificHosts, |
| 348 l10n_util::GetStringFUTF16( | 353 l10n_util::GetStringFUTF16( |
| 349 id, | 354 id, |
| 350 JoinString( | 355 JoinString( |
| 351 std::vector<base::string16>( | 356 std::vector<base::string16>(hostnames.begin(), hostnames.end()), |
| 352 hostnames.begin(), hostnames.end()), ' ')))); | 357 ' ')))); |
| 353 } | 358 } |
| 354 } | 359 } |
| 355 | 360 |
| 356 void SocketsManifestPermission::AddNetworkListMessage( | 361 void SocketsManifestPermission::AddNetworkListMessage( |
| 357 PermissionMessages& messages) const { | 362 PermissionMessages& messages) const { |
| 358 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); | 363 for (SocketPermissionEntrySet::const_iterator it = permissions_.begin(); |
| 359 it != permissions_.end(); ++it) { | 364 it != permissions_.end(); |
| 365 ++it) { |
| 360 if (it->pattern().type == SocketPermissionRequest::NETWORK_STATE) { | 366 if (it->pattern().type == SocketPermissionRequest::NETWORK_STATE) { |
| 361 messages.push_back(PermissionMessage( | 367 messages.push_back( |
| 362 PermissionMessage::kNetworkState, | 368 PermissionMessage(PermissionMessage::kNetworkState, |
| 363 l10n_util::GetStringUTF16( | 369 l10n_util::GetStringUTF16( |
| 364 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE))); | 370 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE))); |
| 365 } | 371 } |
| 366 } | 372 } |
| 367 } | 373 } |
| 368 | 374 |
| 369 } // namespace extensions | 375 } // namespace extensions |
| OLD | NEW |