| 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/permissions/permission_message_util.h" | 5 #include "extensions/common/permissions/permission_message_util.h" |
| 6 | 6 |
| 7 #include "base/macros.h" |
| 7 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "extensions/common/permissions/permission_message.h" | 11 #include "extensions/common/permissions/permission_message.h" |
| 11 #include "extensions/common/permissions/permission_set.h" | 12 #include "extensions/common/permissions/permission_set.h" |
| 12 #include "extensions/common/url_pattern_set.h" | 13 #include "extensions/common/url_pattern_set.h" |
| 13 #include "grit/extensions_strings.h" | 14 #include "grit/extensions_strings.h" |
| 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "url/url_constants.h" | 17 #include "url/url_constants.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 32 return b != "com"; | 33 return b != "com"; |
| 33 if (a == "org") | 34 if (a == "org") |
| 34 return b != "com" && b != "net"; | 35 return b != "com" && b != "net"; |
| 35 return false; | 36 return false; |
| 36 } | 37 } |
| 37 | 38 |
| 38 } // namespace | 39 } // namespace |
| 39 | 40 |
| 40 namespace permission_message_util { | 41 namespace permission_message_util { |
| 41 | 42 |
| 42 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { | 43 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts, |
| 44 PermissionMessageProperties properties) { |
| 43 typedef std::pair<PermissionMessage::ID, int> MsgPair; | 45 typedef std::pair<PermissionMessage::ID, int> MsgPair; |
| 44 const MsgPair kMessagesList[] = { | 46 static const MsgPair kReadWriteMessagesList[] = { |
| 45 std::make_pair(PermissionMessage::kHosts1, | 47 std::make_pair(PermissionMessage::kHosts1, |
| 46 IDS_EXTENSION_PROMPT_WARNING_1_HOST), | 48 IDS_EXTENSION_PROMPT_WARNING_1_HOST), |
| 47 std::make_pair(PermissionMessage::kHosts2, | 49 std::make_pair(PermissionMessage::kHosts2, |
| 48 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), | 50 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), |
| 49 std::make_pair(PermissionMessage::kHosts3, | 51 std::make_pair(PermissionMessage::kHosts3, |
| 50 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), | 52 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), |
| 51 std::make_pair(PermissionMessage::kHosts4OrMore, | 53 std::make_pair(PermissionMessage::kHosts4OrMore, |
| 52 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; | 54 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; |
| 55 static const MsgPair kReadOnlyMessagesList[] = { |
| 56 std::make_pair(PermissionMessage::kHosts1ReadOnly, |
| 57 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY), |
| 58 std::make_pair(PermissionMessage::kHosts2ReadOnly, |
| 59 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY), |
| 60 std::make_pair(PermissionMessage::kHosts3ReadOnly, |
| 61 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY), |
| 62 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly, |
| 63 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY)}; |
| 64 COMPILE_ASSERT( |
| 65 arraysize(kReadWriteMessagesList) == arraysize(kReadOnlyMessagesList), |
| 66 message_lists_different_size); |
| 67 static const int kNumMessages = arraysize(kReadWriteMessagesList); |
| 68 const MsgPair(&messages_list)[kNumMessages] = |
| 69 properties == kReadOnly ? kReadOnlyMessagesList : kReadWriteMessagesList; |
| 53 | 70 |
| 54 int host_msg_id = hosts.size() < arraysize(kMessagesList) | 71 int host_msg_id = hosts.size() < kNumMessages |
| 55 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN | 72 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN |
| 56 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; | 73 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; |
| 57 std::vector<base::string16> host_list; | 74 std::vector<base::string16> host_list; |
| 58 for (std::set<std::string>::const_iterator it = hosts.begin(); | 75 for (std::set<std::string>::const_iterator it = hosts.begin(); |
| 59 it != hosts.end(); | 76 it != hosts.end(); |
| 60 ++it) { | 77 ++it) { |
| 61 std::string host = *it; | 78 std::string host = *it; |
| 62 host_list.push_back( | 79 host_list.push_back( |
| 63 host[0] == '*' && host[1] == '.' | 80 host[0] == '*' && host[1] == '.' |
| 64 ? l10n_util::GetStringFUTF16(host_msg_id, | 81 ? l10n_util::GetStringFUTF16(host_msg_id, |
| 65 base::UTF8ToUTF16(host.erase(0, 2))) | 82 base::UTF8ToUTF16(host.erase(0, 2))) |
| 66 : base::UTF8ToUTF16(host)); | 83 : base::UTF8ToUTF16(host)); |
| 67 } | 84 } |
| 68 DCHECK(host_list.size()); | 85 DCHECK(host_list.size()); |
| 69 | 86 |
| 70 if (host_list.size() < arraysize(kMessagesList)) { | 87 if (host_list.size() < kNumMessages) { |
| 71 return PermissionMessage( | 88 return PermissionMessage( |
| 72 kMessagesList[host_list.size() - 1].first, | 89 messages_list[host_list.size() - 1].first, |
| 73 l10n_util::GetStringFUTF16( | 90 l10n_util::GetStringFUTF16(messages_list[host_list.size() - 1].second, |
| 74 kMessagesList[host_list.size() - 1].second, host_list, NULL)); | 91 host_list, NULL)); |
| 75 } | 92 } |
| 76 | 93 |
| 77 base::string16 details; | 94 base::string16 details; |
| 78 for (size_t i = 0; i < host_list.size(); ++i) { | 95 for (size_t i = 0; i < host_list.size(); ++i) { |
| 79 if (i > 0) | 96 if (i > 0) |
| 80 details += base::ASCIIToUTF16("\n"); | 97 details += base::ASCIIToUTF16("\n"); |
| 81 details += host_list[i]; | 98 details += host_list[i]; |
| 82 } | 99 } |
| 83 return PermissionMessage( | 100 return PermissionMessage( |
| 84 kMessagesList[arraysize(kMessagesList) - 1].first, | 101 messages_list[arraysize(messages_list) - 1].first, |
| 85 l10n_util::GetStringUTF16( | 102 l10n_util::GetStringUTF16( |
| 86 kMessagesList[arraysize(kMessagesList) - 1].second), | 103 messages_list[arraysize(messages_list) - 1].second), |
| 87 details); | 104 details); |
| 88 } | 105 } |
| 89 | 106 |
| 90 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, | 107 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, |
| 91 bool include_rcd, | 108 bool include_rcd, |
| 92 bool exclude_file_scheme) { | 109 bool exclude_file_scheme) { |
| 93 // Use a vector to preserve order (also faster than a map on small sets). | 110 // Use a vector to preserve order (also faster than a map on small sets). |
| 94 // Each item is a host split into two parts: host without RCDs and | 111 // Each item is a host split into two parts: host without RCDs and |
| 95 // current best RCD. | 112 // current best RCD. |
| 96 typedef base::StringPairs HostVector; | 113 typedef base::StringPairs HostVector; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // Build up the final vector by concatenating hosts and RCDs. | 154 // Build up the final vector by concatenating hosts and RCDs. |
| 138 std::set<std::string> distinct_hosts; | 155 std::set<std::string> distinct_hosts; |
| 139 for (HostVector::iterator it = hosts_best_rcd.begin(); | 156 for (HostVector::iterator it = hosts_best_rcd.begin(); |
| 140 it != hosts_best_rcd.end(); | 157 it != hosts_best_rcd.end(); |
| 141 ++it) | 158 ++it) |
| 142 distinct_hosts.insert(it->first + it->second); | 159 distinct_hosts.insert(it->first + it->second); |
| 143 return distinct_hosts; | 160 return distinct_hosts; |
| 144 } | 161 } |
| 145 | 162 |
| 146 } // namespace permission_message_util | 163 } // namespace permission_message_util |
| OLD | NEW |