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.
5 #include "extensions/common/permissions/socket_permission_data.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "extensions/common/permissions/api_permission.h"
17 #include "extensions/common/permissions/socket_permission.h"
18 #include "url/url_canon.h"
22 using content::SocketPermissionRequest
;
23 using extensions::SocketPermissionData
;
25 const char kColon
= ':';
26 const char kInvalid
[] = "invalid";
27 const char kTCPConnect
[] = "tcp-connect";
28 const char kTCPListen
[] = "tcp-listen";
29 const char kUDPBind
[] = "udp-bind";
30 const char kUDPSendTo
[] = "udp-send-to";
31 const char kUDPMulticastMembership
[] = "udp-multicast-membership";
32 const char kResolveHost
[] = "resolve-host";
33 const char kResolveProxy
[] = "resolve-proxy";
34 const char kNetworkState
[] = "network-state";
36 SocketPermissionRequest::OperationType
StringToType(const std::string
& s
) {
38 return SocketPermissionRequest::TCP_CONNECT
;
40 return SocketPermissionRequest::TCP_LISTEN
;
42 return SocketPermissionRequest::UDP_BIND
;
44 return SocketPermissionRequest::UDP_SEND_TO
;
45 if (s
== kUDPMulticastMembership
)
46 return SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP
;
47 if (s
== kResolveHost
)
48 return SocketPermissionRequest::RESOLVE_HOST
;
49 if (s
== kResolveProxy
)
50 return SocketPermissionRequest::RESOLVE_PROXY
;
51 if (s
== kNetworkState
)
52 return SocketPermissionRequest::NETWORK_STATE
;
53 return SocketPermissionRequest::NONE
;
56 const char* TypeToString(SocketPermissionRequest::OperationType type
) {
58 case SocketPermissionRequest::TCP_CONNECT
:
60 case SocketPermissionRequest::TCP_LISTEN
:
62 case SocketPermissionRequest::UDP_BIND
:
64 case SocketPermissionRequest::UDP_SEND_TO
:
66 case SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP
:
67 return kUDPMulticastMembership
;
68 case SocketPermissionRequest::RESOLVE_HOST
:
70 case SocketPermissionRequest::RESOLVE_PROXY
:
72 case SocketPermissionRequest::NETWORK_STATE
:
81 namespace extensions
{
83 SocketPermissionData::SocketPermissionData() {}
85 SocketPermissionData::~SocketPermissionData() {}
87 bool SocketPermissionData::operator<(const SocketPermissionData
& rhs
) const {
88 return entry_
< rhs
.entry_
;
91 bool SocketPermissionData::operator==(const SocketPermissionData
& rhs
) const {
92 return entry_
== rhs
.entry_
;
95 bool SocketPermissionData::Check(const APIPermission::CheckParam
* param
) const {
98 const SocketPermission::CheckParam
& specific_param
=
99 *static_cast<const SocketPermission::CheckParam
*>(param
);
100 const SocketPermissionRequest
& request
= specific_param
.request
;
102 return entry_
.Check(request
);
105 scoped_ptr
<base::Value
> SocketPermissionData::ToValue() const {
106 return scoped_ptr
<base::Value
>(new base::StringValue(GetAsString()));
109 bool SocketPermissionData::FromValue(const base::Value
* value
) {
111 if (!value
->GetAsString(&spec
))
117 SocketPermissionEntry
& SocketPermissionData::entry() {
118 // Clear the spec because the caller could mutate |this|.
123 // TODO(reillyg): Rewrite this method to support IPv6.
124 bool SocketPermissionData::Parse(const std::string
& permission
) {
127 std::vector
<std::string
> tokens
=
128 base::SplitString(permission
, std::string(1, kColon
),
129 base::KEEP_WHITESPACE
, base::SPLIT_WANT_ALL
);
133 SocketPermissionRequest::OperationType type
= StringToType(tokens
[0]);
134 if (type
== SocketPermissionRequest::NONE
)
137 tokens
.erase(tokens
.begin());
138 return SocketPermissionEntry::ParseHostPattern(type
, tokens
, &entry_
);
141 const std::string
& SocketPermissionData::GetAsString() const {
146 spec_
.append(TypeToString(entry_
.pattern().type
));
147 std::string pattern
= entry_
.GetHostPatternAsString();
148 if (!pattern
.empty()) {
149 spec_
.append(1, kColon
).append(pattern
);
154 void SocketPermissionData::Reset() {
155 entry_
= SocketPermissionEntry();
159 } // namespace extensions