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/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "extensions/common/permissions/permission_message.h" | 10 #include "extensions/common/permissions/permission_message.h" |
10 #include "extensions/common/permissions/permission_set.h" | 11 #include "extensions/common/permissions/permission_set.h" |
11 #include "extensions/common/url_pattern_set.h" | 12 #include "extensions/common/url_pattern_set.h" |
12 #include "grit/extensions_strings.h" | 13 #include "grit/extensions_strings.h" |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
14 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
15 #include "url/url_constants.h" | 16 #include "url/url_constants.h" |
16 | 17 |
(...skipping 13 matching lines...) Expand all Loading... | |
30 return b != "com"; | 31 return b != "com"; |
31 if (a == "org") | 32 if (a == "org") |
32 return b != "com" && b != "net"; | 33 return b != "com" && b != "net"; |
33 return false; | 34 return false; |
34 } | 35 } |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 namespace permission_message_util { | 39 namespace permission_message_util { |
39 | 40 |
40 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts) { | 41 PermissionMessage CreateFromHostList(const std::set<std::string>& hosts, |
42 PermissionMessageProperties properties) { | |
41 typedef std::pair<PermissionMessage::ID, int> MsgPair; | 43 typedef std::pair<PermissionMessage::ID, int> MsgPair; |
42 const MsgPair kMessagesList[] = { | 44 static const MsgPair kReadWriteMessagesList[] = { |
43 std::make_pair(PermissionMessage::kHosts1, | 45 std::make_pair(PermissionMessage::kHosts1, |
44 IDS_EXTENSION_PROMPT_WARNING_1_HOST), | 46 IDS_EXTENSION_PROMPT_WARNING_1_HOST), |
45 std::make_pair(PermissionMessage::kHosts2, | 47 std::make_pair(PermissionMessage::kHosts2, |
46 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), | 48 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS), |
47 std::make_pair(PermissionMessage::kHosts3, | 49 std::make_pair(PermissionMessage::kHosts3, |
48 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), | 50 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS), |
49 std::make_pair(PermissionMessage::kHosts4OrMore, | 51 std::make_pair(PermissionMessage::kHosts4OrMore, |
50 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; | 52 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST)}; |
53 static const MsgPair kReadOnlyMessagesList[] = { | |
54 std::make_pair(PermissionMessage::kHosts1ReadOnly, | |
55 IDS_EXTENSION_PROMPT_WARNING_1_HOST_READ_ONLY), | |
56 std::make_pair(PermissionMessage::kHosts2ReadOnly, | |
57 IDS_EXTENSION_PROMPT_WARNING_2_HOSTS_READ_ONLY), | |
58 std::make_pair(PermissionMessage::kHosts3ReadOnly, | |
59 IDS_EXTENSION_PROMPT_WARNING_3_HOSTS_READ_ONLY), | |
60 std::make_pair(PermissionMessage::kHosts4OrMoreReadOnly, | |
61 IDS_EXTENSION_PROMPT_WARNING_HOSTS_LIST_READ_ONLY)}; | |
62 COMPILE_ASSERT( | |
63 arraysize(kReadWriteMessagesList) == arraysize(kReadOnlyMessagesList), | |
64 "Different numbers of entries in kReadWriteMessagesList vs " | |
65 "kReadWriteMessagesList"); | |
66 static const int kNumMessages = arraysize(kReadWriteMessagesList); | |
67 const MsgPair(&messages_list)[kNumMessages] = | |
aboxhall
2014/07/25 19:06:31
Before you ask, this is how clang-format formatted
| |
68 properties & kReadOnly ? kReadOnlyMessagesList : kReadWriteMessagesList; | |
not at google - send to devlin
2014/07/25 20:40:03
shouldn't this be "== kReadOnly"?
aboxhall
2014/07/25 20:54:22
Ugh yeah, I think I was imagining it was a bitmask
| |
51 | 69 |
52 int host_msg_id = hosts.size() < arraysize(kMessagesList) | 70 int host_msg_id = hosts.size() < kNumMessages |
53 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN | 71 ? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN |
54 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; | 72 : IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST; |
55 std::vector<base::string16> host_list; | 73 std::vector<base::string16> host_list; |
56 for (std::set<std::string>::const_iterator it = hosts.begin(); | 74 for (std::set<std::string>::const_iterator it = hosts.begin(); |
57 it != hosts.end(); | 75 it != hosts.end(); |
58 ++it) { | 76 ++it) { |
59 std::string host = *it; | 77 std::string host = *it; |
60 host_list.push_back( | 78 host_list.push_back( |
61 host[0] == '*' && host[1] == '.' | 79 host[0] == '*' && host[1] == '.' |
62 ? l10n_util::GetStringFUTF16(host_msg_id, | 80 ? l10n_util::GetStringFUTF16(host_msg_id, |
63 base::UTF8ToUTF16(host.erase(0, 2))) | 81 base::UTF8ToUTF16(host.erase(0, 2))) |
64 : base::UTF8ToUTF16(host)); | 82 : base::UTF8ToUTF16(host)); |
65 } | 83 } |
66 DCHECK(host_list.size()); | 84 DCHECK(host_list.size()); |
67 | 85 |
68 if (host_list.size() < arraysize(kMessagesList)) { | 86 if (host_list.size() < kNumMessages) { |
69 return PermissionMessage( | 87 return PermissionMessage( |
70 kMessagesList[host_list.size() - 1].first, | 88 messages_list[host_list.size() - 1].first, |
71 l10n_util::GetStringFUTF16( | 89 l10n_util::GetStringFUTF16( |
72 kMessagesList[host_list.size() - 1].second, host_list, NULL)); | 90 messages_list[host_list.size() - 1].second, host_list, NULL)); |
73 } | 91 } |
74 | 92 |
75 base::string16 details; | 93 base::string16 details; |
76 for (size_t i = 0; i < host_list.size(); ++i) { | 94 for (size_t i = 0; i < host_list.size(); ++i) { |
77 if (i > 0) | 95 if (i > 0) |
78 details += base::ASCIIToUTF16("\n"); | 96 details += base::ASCIIToUTF16("\n"); |
79 details += l10n_util::GetStringFUTF16( | 97 details += l10n_util::GetStringFUTF16( |
80 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, host_list[i]); | 98 IDS_EXTENSION_PROMPT_WARNING_HOST_LIST_ENTRY, host_list[i]); |
81 } | 99 } |
82 return PermissionMessage( | 100 return PermissionMessage( |
83 kMessagesList[arraysize(kMessagesList) - 1].first, | 101 messages_list[arraysize(messages_list) - 1].first, |
84 l10n_util::GetStringUTF16( | 102 l10n_util::GetStringUTF16( |
85 kMessagesList[arraysize(kMessagesList) - 1].second), | 103 messages_list[arraysize(messages_list) - 1].second), |
86 details); | 104 details); |
87 } | 105 } |
88 | 106 |
89 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, | 107 std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns, |
90 bool include_rcd, | 108 bool include_rcd, |
91 bool exclude_file_scheme) { | 109 bool exclude_file_scheme) { |
92 // 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). |
93 // 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 |
94 // current best RCD. | 112 // current best RCD. |
95 typedef std::vector<std::pair<std::string, std::string> > HostVector; | 113 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. | 154 // Build up the final vector by concatenating hosts and RCDs. |
137 std::set<std::string> distinct_hosts; | 155 std::set<std::string> distinct_hosts; |
138 for (HostVector::iterator it = hosts_best_rcd.begin(); | 156 for (HostVector::iterator it = hosts_best_rcd.begin(); |
139 it != hosts_best_rcd.end(); | 157 it != hosts_best_rcd.end(); |
140 ++it) | 158 ++it) |
141 distinct_hosts.insert(it->first + it->second); | 159 distinct_hosts.insert(it->first + it->second); |
142 return distinct_hosts; | 160 return distinct_hosts; |
143 } | 161 } |
144 | 162 |
145 } // namespace permission_message_util | 163 } // namespace permission_message_util |
OLD | NEW |