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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "extensions/common/permissions/permission_message.h" | 9 #include "extensions/common/permissions/permission_message.h" |
10 #include "extensions/common/permissions/permission_set.h" | 10 #include "extensions/common/permissions/permission_set.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 if (a == "org") | 31 if (a == "org") |
32 return b != "com" && b != "net"; | 32 return b != "com" && b != "net"; |
33 return false; | 33 return false; |
34 } | 34 } |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 namespace permission_message_util { | 38 namespace permission_message_util { |
39 | 39 |
40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { | 40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { |
41 return CreateFromHostList(hosts, false); | |
42 } | |
43 | |
44 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts, | |
45 bool read_only) { | |
41 typedef std::pair<PermissionMessage::ID, int> MsgPair; | 46 typedef std::pair<PermissionMessage::ID, int> MsgPair; |
42 const MsgPair kMessagesList[] = { | 47 static const int kNumMessages = 4; |
not at google - send to devlin
2014/07/25 18:17:42
we went to great lengths to avoid hard-coding a nu
aboxhall
2014/07/25 19:06:31
Done.
| |
48 static const MsgPair kMessagesList[kNumMessages] = { | |
43 std::make_pair(PermissionMessage::kHosts1, | 49 std::make_pair(PermissionMessage::kHosts1, |
44 IDS_EXTENSION_PROMPT_WARNING_1_HOST), | 50 IDS_EXTENSION_PROMPT_WARNING_1_HOST), |
45 std::make_pair(PermissionMessage::kHosts2, | 51 std::make_pair(PermissionMessage::kHosts2, |
46 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), | 52 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), |
47 std::make_pair(PermissionMessage::kHosts3, | 53 std::make_pair(PermissionMessage::kHosts3, |
48 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), | 54 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), |
49 std::make_pair(PermissionMessage::kHosts4OrMore, | 55 std::make_pair(PermissionMessage::kHosts4OrMore, |
50 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; | 56 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; |
57 static const MsgPair kReadOnlyMessagesList[kNumMessages] = { | |
58 std::make_pair(PermissionMessage::kHosts1ReadOnly, | |
59 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY), | |
60 std::make_pair(PermissionMessage::kHosts2ReadOnly, | |
61 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY), | |
62 std::make_pair(PermissionMessage::kHosts3ReadOnly, | |
63 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY), | |
64 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly, | |
65 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY)}; | |
66 const MsgPair (&messages_list)[kNumMessages] = | |
67 read_only ? kReadOnlyMessagesList : kMessagesList; | |
51 | 68 |
52 int host_msg_id = hosts.size() < arraysize(kMessagesList) | 69 int host_msg_id = hosts.size() < kNumMessages |
meacer
2014/07/25 18:13:10
nit: This style seems to be more common:
bool ?
t
aboxhall
2014/07/25 19:06:31
Funny: this is how clang-format formats it. I'm go
| |
53 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN | 70 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN |
54 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; | 71 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; |
55 std::vector<base::string16> host_list; | 72 std::vector<base::string16> host_list; |
56 for (std::set<std::string>::const_iterator it = hosts.begin(); | 73 for (std::set<std::string>::const_iterator it = hosts.begin(); |
57 it != hosts.end(); | 74 it != hosts.end(); |
58 ++it) { | 75 ++it) { |
59 std::string host = *it; | 76 std::string host = *it; |
60 host_list.push_back( | 77 host_list.push_back( |
61 host[0] == '*' && host[1] == '.' | 78 host[0] == '*' && host[1] == '.' |
62 ? l10n_util::GetStringFUTF16(host_msg_id, | 79 ? l10n_util::GetStringFUTF16(host_msg_id, |
63 base::UTF8ToUTF16(host.erase(0, 2))) | 80 base::UTF8ToUTF16(host.erase(0, 2))) |
64 : base::UTF8ToUTF16(host)); | 81 : base::UTF8ToUTF16(host)); |
65 } | 82 } |
66 DCHECK(host_list.size()); | 83 DCHECK(host_list.size()); |
67 | 84 |
68 if (host_list.size() < arraysize(kMessagesList)) { | 85 if (host_list.size() < kNumMessages) { |
69 return PermissionMessage( | 86 return PermissionMessage( |
70 kMessagesList[host_list.size() - 1].first, | 87 messages_list[host_list.size() - 1].first, |
71 l10n_util::GetStringFUTF16( | 88 l10n_util::GetStringFUTF16( |
72 kMessagesList[host_list.size() - 1].second, host_list, NULL)); | 89 messages_list[host_list.size() - 1].second, host_list, NULL)); |
73 } | 90 } |
74 | 91 |
75 base::string16 details; | 92 base::string16 details; |
76 for (size_t i = 0; i < host_list.size(); ++i) { | 93 for (size_t i = 0; i < host_list.size(); ++i) { |
77 if (i > 0) | 94 if (i > 0) |
78 details += base::ASCIIToUTF16("\n"); | 95 details += base::ASCIIToUTF16("\n"); |
79 details += l10n_util::GetStringFUTF16( | 96 details += l10n_util::GetStringFUTF16( |
80 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, host_list[i]); | 97 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, host_list[i]); |
81 } | 98 } |
82 return PermissionMessage( | 99 return PermissionMessage( |
83 kMessagesList[arraysize(kMessagesList) - 1].first, | 100 messages_list[arraysize(kMessagesList) - 1].first, |
84 l10n_util::GetStringUTF16( | 101 l10n_util::GetStringUTF16( |
85 kMessagesList[arraysize(kMessagesList) - 1].second), | 102 messages_list[arraysize(kMessagesList) - 1].second), |
86 details); | 103 details); |
87 } | 104 } |
88 | 105 |
89 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, | 106 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, |
90 bool include_rcd, | 107 bool include_rcd, |
91 bool exclude_file_scheme) { | 108 bool exclude_file_scheme) { |
92 // Use a vector to preserve order (also faster than a map on small sets). | 109 // Use a vector to preserve order (also faster than a map on small sets). |
93 // Each item is a host split into two parts: host without RCDs and | 110 // Each item is a host split into two parts: host without RCDs and |
94 // current best RCD. | 111 // current best RCD. |
95 typedef std::vector<std::pair<std::string, std::string> > HostVector; | 112 typedef std::vector<std::pair<std::string, std::string> > HostVector; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 // Build up the final vector by concatenating hosts and RCDs. | 153 // Build up the final vector by concatenating hosts and RCDs. |
137 std::set<std::string> distinct_hosts; | 154 std::set<std::string> distinct_hosts; |
138 for (HostVector::iterator it = hosts_best_rcd.begin(); | 155 for (HostVector::iterator it = hosts_best_rcd.begin(); |
139 it != hosts_best_rcd.end(); | 156 it != hosts_best_rcd.end(); |
140 ++it) | 157 ++it) |
141 distinct_hosts.insert(it->first + it->second); | 158 distinct_hosts.insert(it->first + it->second); |
142 return distinct_hosts; | 159 return distinct_hosts; |
143 } | 160 } |
144 | 161 |
145 } // namespace permission_message_util | 162 } // namespace permission_message_util |
OLD | NEW |