| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 for (SocketPermissionEntrySet::const_iterator it = | 80 for (SocketPermissionEntrySet::const_iterator it = |
| 81 permission->entries().begin(); | 81 permission->entries().begin(); |
| 82 it != permission->entries().end(); ++it) { | 82 it != permission->entries().end(); ++it) { |
| 83 if (it->pattern().type == operation_type) { | 83 if (it->pattern().type == operation_type) { |
| 84 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); | 84 host_patterns->as_strings->push_back(it->GetHostPatternAsString()); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 // Helper function for adding the 'any host' permission. Determines if the | 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 | 90 // message is needed from |sockets|, and adds the permission to |ids|. |
| 91 // |messages|, ignoring them if they are NULL. Returns true if it added the | 91 // Returns true if it added the message. |
| 92 // message. | |
| 93 bool AddAnyHostMessage(const SocketPermissionEntrySet& sockets, | 92 bool AddAnyHostMessage(const SocketPermissionEntrySet& sockets, |
| 94 PermissionIDSet* ids, | 93 PermissionIDSet* ids) { |
| 95 PermissionMessages* messages) { | |
| 96 for (const auto& socket : sockets) { | 94 for (const auto& socket : sockets) { |
| 97 if (socket.IsAddressBoundType() && | 95 if (socket.IsAddressBoundType() && |
| 98 socket.GetHostType() == SocketPermissionEntry::ANY_HOST) { | 96 socket.GetHostType() == SocketPermissionEntry::ANY_HOST) { |
| 99 if (ids) | 97 ids->insert(APIPermission::kSocketAnyHost); |
| 100 ids->insert(APIPermission::kSocketAnyHost); | |
| 101 if (messages) { | |
| 102 messages->push_back(PermissionMessage( | |
| 103 PermissionMessage::kSocketAnyHost, | |
| 104 l10n_util::GetStringUTF16( | |
| 105 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST))); | |
| 106 } | |
| 107 return true; | 98 return true; |
| 108 } | 99 } |
| 109 } | 100 } |
| 110 return false; | 101 return false; |
| 111 } | 102 } |
| 112 | 103 |
| 113 // Helper function for adding subdomain socket permissions. Determines what | 104 // Helper function for adding subdomain socket permissions. Determines what |
| 114 // messages are needed from |sockets|, and adds permissions to |ids| and/or | 105 // messages are needed from |sockets|, and adds permissions to |ids|. |
| 115 // |messages|, ignoring them if they are NULL. | |
| 116 void AddSubdomainHostMessage(const SocketPermissionEntrySet& sockets, | 106 void AddSubdomainHostMessage(const SocketPermissionEntrySet& sockets, |
| 117 PermissionIDSet* ids, | 107 PermissionIDSet* ids) { |
| 118 PermissionMessages* messages) { | |
| 119 std::set<base::string16> domains; | 108 std::set<base::string16> domains; |
| 120 for (const auto& socket : sockets) { | 109 for (const auto& socket : sockets) { |
| 121 if (socket.GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) | 110 if (socket.GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS) |
| 122 domains.insert(base::UTF8ToUTF16(socket.pattern().host)); | 111 domains.insert(base::UTF8ToUTF16(socket.pattern().host)); |
| 123 } | 112 } |
| 124 if (!domains.empty()) { | 113 if (!domains.empty()) { |
| 125 // TODO(sashab): This is not correct for all languages - add proper | 114 for (const auto& domain : domains) |
| 126 // internationalization of this string for all plural states. | 115 ids->insert(APIPermission::kSocketDomainHosts, domain); |
| 127 if (messages) { | |
| 128 int id = (domains.size() == 1) | |
| 129 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN | |
| 130 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS; | |
| 131 messages->push_back(PermissionMessage( | |
| 132 PermissionMessage::kSocketDomainHosts, | |
| 133 l10n_util::GetStringFUTF16( | |
| 134 id, base::JoinString(std::vector<base::string16>(domains.begin(), | |
| 135 domains.end()), | |
| 136 base::ASCIIToUTF16(" "))))); | |
| 137 } | |
| 138 if (ids) { | |
| 139 for (const auto& domain : domains) | |
| 140 ids->insert(APIPermission::kSocketDomainHosts, domain); | |
| 141 } | |
| 142 } | 116 } |
| 143 } | 117 } |
| 144 | 118 |
| 145 // Helper function for adding specific host socket permissions. Determines what | 119 // Helper function for adding specific host socket permissions. Determines what |
| 146 // messages are needed from |sockets|, and adds permissions to |ids| and/or | 120 // messages are needed from |sockets|, and adds permissions to |ids|. |
| 147 // |messages|, ignoring them if they are NULL. | |
| 148 void AddSpecificHostMessage(const SocketPermissionEntrySet& sockets, | 121 void AddSpecificHostMessage(const SocketPermissionEntrySet& sockets, |
| 149 PermissionIDSet* ids, | 122 PermissionIDSet* ids) { |
| 150 PermissionMessages* messages) { | |
| 151 std::set<base::string16> hostnames; | 123 std::set<base::string16> hostnames; |
| 152 for (const auto& socket : sockets) { | 124 for (const auto& socket : sockets) { |
| 153 if (socket.GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) | 125 if (socket.GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS) |
| 154 hostnames.insert(base::UTF8ToUTF16(socket.pattern().host)); | 126 hostnames.insert(base::UTF8ToUTF16(socket.pattern().host)); |
| 155 } | 127 } |
| 156 if (!hostnames.empty()) { | 128 if (!hostnames.empty()) { |
| 157 // TODO(sashab): This is not correct for all languages - add proper | 129 for (const auto& hostname : hostnames) |
| 158 // internationalization of this string for all plural states. | 130 ids->insert(APIPermission::kSocketSpecificHosts, hostname); |
| 159 if (messages) { | |
| 160 int id = (hostnames.size() == 1) | |
| 161 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST | |
| 162 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS; | |
| 163 messages->push_back(PermissionMessage( | |
| 164 PermissionMessage::kSocketSpecificHosts, | |
| 165 l10n_util::GetStringFUTF16( | |
| 166 id, base::JoinString(std::vector<base::string16>( | |
| 167 hostnames.begin(), hostnames.end()), | |
| 168 base::ASCIIToUTF16(" "))))); | |
| 169 } | |
| 170 if (ids) { | |
| 171 for (const auto& hostname : hostnames) | |
| 172 ids->insert(APIPermission::kSocketSpecificHosts, hostname); | |
| 173 } | |
| 174 } | 131 } |
| 175 } | 132 } |
| 176 | 133 |
| 177 // Helper function for adding the network list socket permission. Determines if | 134 // Helper function for adding the network list socket permission. Determines if |
| 178 // the message is needed from |sockets|, and adds the permission to |ids| and/or | 135 // the message is needed from |sockets|, and adds the permission to |ids|. |
| 179 // |messages|, ignoring them if they are NULL. | |
| 180 void AddNetworkListMessage(const SocketPermissionEntrySet& sockets, | 136 void AddNetworkListMessage(const SocketPermissionEntrySet& sockets, |
| 181 PermissionIDSet* ids, | 137 PermissionIDSet* ids) { |
| 182 PermissionMessages* messages) { | |
| 183 for (const auto& socket : sockets) { | 138 for (const auto& socket : sockets) { |
| 184 if (socket.pattern().type == SocketPermissionRequest::NETWORK_STATE) { | 139 if (socket.pattern().type == SocketPermissionRequest::NETWORK_STATE) { |
| 185 if (ids) | 140 ids->insert(APIPermission::kNetworkState); |
| 186 ids->insert(APIPermission::kNetworkState); | |
| 187 if (messages) { | |
| 188 messages->push_back( | |
| 189 PermissionMessage(PermissionMessage::kNetworkState, | |
| 190 l10n_util::GetStringUTF16( | |
| 191 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE))); | |
| 192 } | |
| 193 } | 141 } |
| 194 } | 142 } |
| 195 } | 143 } |
| 196 | 144 |
| 197 } // namespace | 145 } // namespace |
| 198 | 146 |
| 199 SocketsManifestPermission::SocketsManifestPermission() {} | 147 SocketsManifestPermission::SocketsManifestPermission() {} |
| 200 | 148 |
| 201 SocketsManifestPermission::~SocketsManifestPermission() {} | 149 SocketsManifestPermission::~SocketsManifestPermission() {} |
| 202 | 150 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 209 } |
| 262 | 210 |
| 263 std::string SocketsManifestPermission::name() const { | 211 std::string SocketsManifestPermission::name() const { |
| 264 return manifest_keys::kSockets; | 212 return manifest_keys::kSockets; |
| 265 } | 213 } |
| 266 | 214 |
| 267 std::string SocketsManifestPermission::id() const { return name(); } | 215 std::string SocketsManifestPermission::id() const { return name(); } |
| 268 | 216 |
| 269 PermissionIDSet SocketsManifestPermission::GetPermissions() const { | 217 PermissionIDSet SocketsManifestPermission::GetPermissions() const { |
| 270 PermissionIDSet ids; | 218 PermissionIDSet ids; |
| 271 AddSocketHostPermissions(permissions_, &ids, NULL); | 219 AddSocketHostPermissions(permissions_, &ids); |
| 272 return ids; | 220 return ids; |
| 273 } | 221 } |
| 274 | 222 |
| 275 bool SocketsManifestPermission::FromValue(const base::Value* value) { | 223 bool SocketsManifestPermission::FromValue(const base::Value* value) { |
| 276 if (!value) | 224 if (!value) |
| 277 return false; | 225 return false; |
| 278 base::string16 error; | 226 base::string16 error; |
| 279 scoped_ptr<SocketsManifestPermission> manifest_permission( | 227 scoped_ptr<SocketsManifestPermission> manifest_permission( |
| 280 SocketsManifestPermission::FromValue(*value, &error)); | 228 SocketsManifestPermission::FromValue(*value, &error)); |
| 281 | 229 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 } | 301 } |
| 354 | 302 |
| 355 void SocketsManifestPermission::AddPermission( | 303 void SocketsManifestPermission::AddPermission( |
| 356 const SocketPermissionEntry& entry) { | 304 const SocketPermissionEntry& entry) { |
| 357 permissions_.insert(entry); | 305 permissions_.insert(entry); |
| 358 } | 306 } |
| 359 | 307 |
| 360 // static | 308 // static |
| 361 void SocketsManifestPermission::AddSocketHostPermissions( | 309 void SocketsManifestPermission::AddSocketHostPermissions( |
| 362 const SocketPermissionEntrySet& sockets, | 310 const SocketPermissionEntrySet& sockets, |
| 363 PermissionIDSet* ids, | 311 PermissionIDSet* ids) { |
| 364 PermissionMessages* messages) { | 312 if (!AddAnyHostMessage(sockets, ids)) { |
| 365 if (!AddAnyHostMessage(sockets, ids, messages)) { | 313 AddSpecificHostMessage(sockets, ids); |
| 366 AddSpecificHostMessage(sockets, ids, messages); | 314 AddSubdomainHostMessage(sockets, ids); |
| 367 AddSubdomainHostMessage(sockets, ids, messages); | |
| 368 } | 315 } |
| 369 AddNetworkListMessage(sockets, ids, messages); | 316 AddNetworkListMessage(sockets, ids); |
| 370 } | 317 } |
| 371 | 318 |
| 372 } // namespace extensions | 319 } // namespace extensions |
| OLD | NEW |