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
3 // found in the LICENSE file.
4
5 #include "extensions/common/permissions/socket_permission.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "extensions/common/permissions/permissions_info.h"
13 #include "extensions/common/permissions/set_disjunction_permission.h"
14 #include "grit/extensions_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
16
17 namespace extensions {
18
SocketPermission(const APIPermissionInfo * info)19 SocketPermission::SocketPermission(const APIPermissionInfo* info)
20 : SetDisjunctionPermission<SocketPermissionData, SocketPermission>(info) {}
21
~SocketPermission()22 SocketPermission::~SocketPermission() {}
23
GetMessages() const24 PermissionMessages SocketPermission::GetMessages() const {
25 DCHECK(HasMessages());
26 PermissionMessages result;
27 if (!AddAnyHostMessage(result)) {
28 AddSpecificHostMessage(result);
29 AddSubdomainHostMessage(result);
30 }
31 AddNetworkListMessage(result);
32 return result;
33 }
34
AddAnyHostMessage(PermissionMessages & messages) const35 bool SocketPermission::AddAnyHostMessage(PermissionMessages& messages) const {
36 std::set<SocketPermissionData>::const_iterator i;
37 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
38 if (i->entry().IsAddressBoundType() &&
39 i->entry().GetHostType() == SocketPermissionEntry::ANY_HOST) {
40 messages.push_back(
41 PermissionMessage(PermissionMessage::kSocketAnyHost,
42 l10n_util::GetStringUTF16(
43 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST)));
44 return true;
45 }
46 }
47 return false;
48 }
49
AddSubdomainHostMessage(PermissionMessages & messages) const50 void SocketPermission::AddSubdomainHostMessage(
51 PermissionMessages& messages) const {
52 std::set<base::string16> domains;
53 std::set<SocketPermissionData>::const_iterator i;
54 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
55 if (i->entry().GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS)
56 domains.insert(base::UTF8ToUTF16(i->entry().pattern().host));
57 }
58 if (!domains.empty()) {
59 int id = (domains.size() == 1)
60 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN
61 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS;
62 messages.push_back(PermissionMessage(
63 PermissionMessage::kSocketDomainHosts,
64 l10n_util::GetStringFUTF16(
65 id,
66 JoinString(
67 std::vector<base::string16>(domains.begin(), domains.end()),
68 ' '))));
69 }
70 }
71
AddSpecificHostMessage(PermissionMessages & messages) const72 void SocketPermission::AddSpecificHostMessage(
73 PermissionMessages& messages) const {
74 std::set<base::string16> hostnames;
75 std::set<SocketPermissionData>::const_iterator i;
76 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
77 if (i->entry().GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS)
78 hostnames.insert(base::UTF8ToUTF16(i->entry().pattern().host));
79 }
80 if (!hostnames.empty()) {
81 int id = (hostnames.size() == 1)
82 ? IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST
83 : IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS;
84 messages.push_back(PermissionMessage(
85 PermissionMessage::kSocketSpecificHosts,
86 l10n_util::GetStringFUTF16(
87 id,
88 JoinString(
89 std::vector<base::string16>(hostnames.begin(), hostnames.end()),
90 ' '))));
91 }
92 }
93
AddNetworkListMessage(PermissionMessages & messages) const94 void SocketPermission::AddNetworkListMessage(
95 PermissionMessages& messages) const {
96 std::set<SocketPermissionData>::const_iterator i;
97 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
98 if (i->entry().pattern().type ==
99 content::SocketPermissionRequest::NETWORK_STATE) {
100 messages.push_back(
101 PermissionMessage(PermissionMessage::kNetworkState,
102 l10n_util::GetStringUTF16(
103 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE)));
104 }
105 }
106 }
107
108 } // namespace extensions
109