1 // Copyright (c) 2012 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 "chrome/common/extensions/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 "chrome/common/extensions/permissions/set_disjunction_permission.h"
13 #include "extensions/common/permissions/permissions_info.h"
14 #include "grit/generated_resources.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 }
22
~SocketPermission()23 SocketPermission::~SocketPermission() {
24 }
25
GetMessages() const26 PermissionMessages SocketPermission::GetMessages() const {
27 DCHECK(HasMessages());
28 PermissionMessages result;
29 if (!AddAnyHostMessage(result)) {
30 AddSpecificHostMessage(result);
31 AddSubdomainHostMessage(result);
32 }
33 AddNetworkListMessage(result);
34 return result;
35 }
36
AddAnyHostMessage(PermissionMessages & messages) const37 bool SocketPermission::AddAnyHostMessage(PermissionMessages& messages) const {
38 std::set<SocketPermissionData>::const_iterator i;
39 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
40 if (i->entry().IsAddressBoundType() &&
41 i->entry().GetHostType() == SocketPermissionEntry::ANY_HOST) {
42 messages.push_back(PermissionMessage(
43 PermissionMessage::kSocketAnyHost,
44 l10n_util::GetStringUTF16(
45 IDS_EXTENSION_PROMPT_WARNING_SOCKET_ANY_HOST)));
46 return true;
47 }
48 }
49 return false;
50 }
51
AddSubdomainHostMessage(PermissionMessages & messages) const52 void SocketPermission::AddSubdomainHostMessage(
53 PermissionMessages& messages) const {
54 std::set<base::string16> domains;
55 std::set<SocketPermissionData>::const_iterator i;
56 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
57 if (i->entry().GetHostType() == SocketPermissionEntry::HOSTS_IN_DOMAINS)
58 domains.insert(UTF8ToUTF16(i->entry().pattern().host));
59 }
60 if (!domains.empty()) {
61 int id = (domains.size() == 1) ?
62 IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAIN :
63 IDS_EXTENSION_PROMPT_WARNING_SOCKET_HOSTS_IN_DOMAINS;
64 messages.push_back(PermissionMessage(
65 PermissionMessage::kSocketDomainHosts,
66 l10n_util::GetStringFUTF16(
67 id,
68 JoinString(
69 std::vector<base::string16>(
70 domains.begin(), domains.end()), ' '))));
71 }
72 }
73
AddSpecificHostMessage(PermissionMessages & messages) const74 void SocketPermission::AddSpecificHostMessage(
75 PermissionMessages& messages) const {
76 std::set<base::string16> hostnames;
77 std::set<SocketPermissionData>::const_iterator i;
78 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
79 if (i->entry().GetHostType() == SocketPermissionEntry::SPECIFIC_HOSTS)
80 hostnames.insert(UTF8ToUTF16(i->entry().pattern().host));
81 }
82 if (!hostnames.empty()) {
83 int id = (hostnames.size() == 1) ?
84 IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOST :
85 IDS_EXTENSION_PROMPT_WARNING_SOCKET_SPECIFIC_HOSTS;
86 messages.push_back(PermissionMessage(
87 PermissionMessage::kSocketSpecificHosts,
88 l10n_util::GetStringFUTF16(
89 id,
90 JoinString(
91 std::vector<base::string16>(
92 hostnames.begin(), hostnames.end()), ' '))));
93 }
94 }
95
AddNetworkListMessage(PermissionMessages & messages) const96 void SocketPermission::AddNetworkListMessage(
97 PermissionMessages& messages) const {
98 std::set<SocketPermissionData>::const_iterator i;
99 for (i = data_set_.begin(); i != data_set_.end(); ++i) {
100 if (i->entry().pattern().type ==
101 content::SocketPermissionRequest::NETWORK_STATE) {
102 messages.push_back(PermissionMessage(
103 PermissionMessage::kNetworkState,
104 l10n_util::GetStringUTF16(
105 IDS_EXTENSION_PROMPT_WARNING_NETWORK_STATE)));
106 }
107 }
108 }
109
110 } // namespace extensions
111