summaryrefslogtreecommitdiffstats
path: root/extensions/common/permissions
diff options
context:
space:
mode:
authortreib <treib@chromium.org>2016-01-18 02:44:05 -0800
committerCommit bot <commit-bot@chromium.org>2016-01-18 10:45:32 +0000
commitad2f3444413670ed722e218dc851a5713fc2c309 (patch)
tree9f4df3ccfc5b6cb6ffa79304a36745ae5bc20f4a /extensions/common/permissions
parent4e3dad649c0ce296fc8c1c977c36c5532d715234 (diff)
downloadchromium_src-ad2f3444413670ed722e218dc851a5713fc2c309.zip
chromium_src-ad2f3444413670ed722e218dc851a5713fc2c309.tar.gz
chromium_src-ad2f3444413670ed722e218dc851a5713fc2c309.tar.bz2
permission_message_util cleanup/modernization
BUG=none Review URL: https://codereview.chromium.org/1593543003 Cr-Commit-Position: refs/heads/master@{#370010}
Diffstat (limited to 'extensions/common/permissions')
-rw-r--r--extensions/common/permissions/permission_message_util.cc50
-rw-r--r--extensions/common/permissions/permission_message_util.h12
2 files changed, 22 insertions, 40 deletions
diff --git a/extensions/common/permissions/permission_message_util.cc b/extensions/common/permissions/permission_message_util.cc
index 8fa3e6d..b69e65b 100644
--- a/extensions/common/permissions/permission_message_util.cc
+++ b/extensions/common/permissions/permission_message_util.cc
@@ -5,8 +5,9 @@
#include "extensions/common/permissions/permission_message_util.h"
#include <stddef.h>
+#include <vector>
-#include "base/macros.h"
+#include "base/strings/string16.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "extensions/common/permissions/api_permission_set.h"
@@ -42,21 +43,19 @@ namespace permission_message_util {
// hosts.
static const int kNumMessages = 4;
+// Gets a list of hosts to display in a permission message from the given list
+// of hosts from the manifest.
std::vector<base::string16> GetHostListFromHosts(
- const std::set<std::string>& hosts,
- PermissionMessageProperties properties) {
+ const std::set<std::string>& hosts) {
int host_msg_id = hosts.size() < kNumMessages
? IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN
: IDS_EXTENSION_PROMPT_WARNING_HOST_AND_SUBDOMAIN_LIST;
std::vector<base::string16> host_list;
- for (std::set<std::string>::const_iterator it = hosts.begin();
- it != hosts.end();
- ++it) {
- std::string host = *it;
+ for (const std::string& host : hosts) {
host_list.push_back(
host[0] == '*' && host[1] == '.'
? l10n_util::GetStringFUTF16(host_msg_id,
- base::UTF8ToUTF16(host.erase(0, 2)))
+ base::UTF8ToUTF16(host.substr(2)))
: base::UTF8ToUTF16(host));
}
DCHECK(host_list.size());
@@ -66,39 +65,34 @@ std::vector<base::string16> GetHostListFromHosts(
void AddHostPermissions(extensions::PermissionIDSet* permissions,
const std::set<std::string>& hosts,
PermissionMessageProperties properties) {
- std::vector<base::string16> host_list =
- GetHostListFromHosts(hosts, properties);
+ std::vector<base::string16> host_list = GetHostListFromHosts(hosts);
// Create a separate permission for each host, and add it to the permissions
// list.
// TODO(sashab): Add coalescing rules for kHostReadOnly and kHostReadWrite
- // to mimic the current behavior of CreateFromHostList() above.
- for (const auto& host : host_list) {
- permissions->insert(properties == kReadOnly
- ? extensions::APIPermission::kHostReadOnly
- : extensions::APIPermission::kHostReadWrite,
- host);
- }
+ // to mimic the current behavior of GetHostListFromHosts() above.
+ extensions::APIPermission::ID permission_id =
+ properties == kReadOnly ? extensions::APIPermission::kHostReadOnly
+ : extensions::APIPermission::kHostReadWrite;
+ for (const auto& host : host_list)
+ permissions->insert(permission_id, host);
}
std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
bool include_rcd,
bool exclude_file_scheme) {
- // Use a vector to preserve order (also faster than a map on small sets).
// Each item is a host split into two parts: host without RCDs and
// current best RCD.
typedef base::StringPairs HostVector;
HostVector hosts_best_rcd;
- for (URLPatternSet::const_iterator i = host_patterns.begin();
- i != host_patterns.end();
- ++i) {
- if (exclude_file_scheme && i->scheme() == url::kFileScheme)
+ for (const URLPattern& pattern : host_patterns) {
+ if (exclude_file_scheme && pattern.scheme() == url::kFileScheme)
continue;
- std::string host = i->host();
+ std::string host = pattern.host();
// Add the subdomain wildcard back to the host, if necessary.
- if (i->match_subdomains())
+ if (pattern.match_subdomains())
host = "*." + host;
// If the host has an RCD, split it off so we can detect duplicates.
@@ -128,12 +122,10 @@ std::set<std::string> GetDistinctHosts(const URLPatternSet& host_patterns,
}
}
- // Build up the final vector by concatenating hosts and RCDs.
+ // Build up the result by concatenating hosts and RCDs.
std::set<std::string> distinct_hosts;
- for (HostVector::iterator it = hosts_best_rcd.begin();
- it != hosts_best_rcd.end();
- ++it)
- distinct_hosts.insert(it->first + it->second);
+ for (const auto& host_rcd : hosts_best_rcd)
+ distinct_hosts.insert(host_rcd.first + host_rcd.second);
return distinct_hosts;
}
diff --git a/extensions/common/permissions/permission_message_util.h b/extensions/common/permissions/permission_message_util.h
index fc006d7..5e33c07 100644
--- a/extensions/common/permissions/permission_message_util.h
+++ b/extensions/common/permissions/permission_message_util.h
@@ -7,9 +7,6 @@
#include <set>
#include <string>
-#include <vector>
-
-#include "base/strings/string16.h"
namespace extensions {
class PermissionIDSet;
@@ -20,14 +17,7 @@ namespace permission_message_util {
enum PermissionMessageProperties { kReadOnly, kReadWrite };
-// Get a list of hosts to display in a permission message from the given list of
-// hosts from the manifest.
-// TODO(sashab): Merge this into AddHostPermissions().
-std::vector<base::string16> GetHostListFromHosts(
- const std::set<std::string>& hosts,
- PermissionMessageProperties properties);
-
-// Adds the appropriate permissions from given hosts to |permissions|.
+// Adds the appropriate permissions from the given |hosts| to |permissions|.
void AddHostPermissions(extensions::PermissionIDSet* permissions,
const std::set<std::string>& hosts,
PermissionMessageProperties properties);