Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: chrome/common/extensions/permissions/socket_permission_data.cc

Issue 10692160: Support socket endpoint permissions for AppsV2 Socket API. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Support socket endpoint permissions Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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_data.h"
6
7 #include <cstdlib>
8 #include <sstream>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/string_split.h"
13 #include "base/string_util.h"
14
15 namespace extensions {
16
17 SocketPermissionData::SocketPermissionData()
18 : type_(NONE),
19 host_("*"),
miket_OOO 2012/08/06 21:04:06 Consider making these special characters (*, :, .,
Peng 2012/08/07 21:31:55 Done.
miket_OOO 2012/08/07 23:02:47 I don't see that this is done.
Peng 2012/08/08 15:40:07 Done.
20 match_subdomains_(false),
21 port_(0) {
22 }
23
24 SocketPermissionData::~SocketPermissionData() {
25 }
26
27 bool SocketPermissionData::operator<(const SocketPermissionData& rhs) const {
28 if (type_ < rhs.type_)
29 return true;
30 if (type_ > rhs.type_)
31 return false;
32
33 if (host_ < rhs.host_)
34 return true;
35 if (host_ > rhs.host_)
36 return false;
37
38 if (match_subdomains_ < rhs.match_subdomains_)
39 return true;
40 if (match_subdomains_ > rhs.match_subdomains_)
41 return false;
42
43 if (port_ < rhs.port_)
44 return true;
45 return false;
46 }
47
48 bool SocketPermissionData::operator==(const SocketPermissionData& rhs) const {
49 return (type_ == rhs.type_) && (host_ == rhs.host_) &&
50 (match_subdomains_ == rhs.match_subdomains_) &&
51 (port_ == rhs.port_);
52 }
53
54 bool SocketPermissionData::Match(
55 OperationType type, const std::string& host, int port) const {
56 if (type_ != type)
57 return false;
58
59 if (host_ != host) {
60 if (!match_subdomains_)
61 return false;
62
63 if (host.length() < host_.length() + 2)
miket_OOO 2012/08/06 21:04:06 Can you add a comment about the + 2? It's not at a
Peng 2012/08/07 21:31:55 Done.
64 return false;
65
66 if (host.compare(host.length() - host_.length(),
67 host_.length(), host_) != 0)
miket_OOO 2012/08/06 21:04:06 This doesn't appear to be properly indented.
Peng 2012/08/07 21:31:55 Done.
68 return false;
69 }
70
71 if (port_ != port && port_ != 0)
72 return false;
73
74 return true;
75 }
76
77 bool SocketPermissionData::Parse(const std::string& permission) {
78 // Reset
79 type_ = NONE;
miket_OOO 2012/08/06 21:04:06 Ideally the reset functionality would be in a sepa
Peng 2012/08/07 21:31:55 Done. I changed it to *this = SocketPermissionData
miket_OOO 2012/08/07 23:02:47 Hmmm. This looks weird. Why can't you call reset()
80 host_.clear();
81 match_subdomains_ = true;
82 port_ = 0;
83 spec_.clear();
84
85 std::vector<std::string> tokens;
86 base::SplitString(permission, ':', &tokens);
miket_OOO 2012/08/06 21:04:06 Please have a look at base/string_tokenizer.h and
Peng 2012/08/07 21:31:55 Seems StringTokenizer can not handle "aaa::cc" wel
87
88 if (tokens.size() > 3)
89 return false;
90
miket_OOO 2012/08/06 21:04:06 Does this crash if tokens is empty at this point?
Peng 2012/08/07 21:31:55 Done.
91 if (tokens[0] == "tcp-connect")
miket_OOO 2012/08/06 21:04:06 These should be constants in a common file.
Peng 2012/08/07 21:31:55 Done.
92 type_ = TCP_CONNECT;
93 else if (tokens[0] == "tcp-listen")
94 type_ = TCP_LISTEN;
95 else if (tokens[0] == "udp-bind")
96 type_ = UDP_BIND;
97 else if (tokens[0] == "udp-send-to")
98 type_ = UDP_SEND_TO;
99 else
100 return false;
101
102 if (tokens.size() == 1)
103 return true;
104
105 // The first component can optionally be '*' to match all subdomains.
106 host_ = tokens[1];
107 if (!host_.empty()) {
108 std::vector<std::string> host_components;
109 base::SplitString(host_, '.', &host_components);
miket_OOO 2012/08/06 21:04:06 Same question. I'm not sure how you can guarantee
Peng 2012/08/07 21:31:55 Done. host_ is checked at 107 line. And it is from
110 if (host_components[0] == "*") {
111 host_components.erase(host_components.begin(),
112 host_components.begin() + 1);
113 } else {
114 match_subdomains_ = false;
115 }
116 host_ = JoinString(host_components, '.');
117 }
118
119 if (tokens.size() == 2)
120 return true;
121
122 if (tokens.empty() || tokens[2] == "*")
123 return true;
124
125 port_ = atoi(tokens[2].c_str());
miket_OOO 2012/08/06 21:04:06 Please look at base/string_number_conversions.h. G
Peng 2012/08/07 21:31:55 Done.
126 if (port_ < 0 || port_ > 65535)
127 return false;
128
129 return true;
130 }
131
132 const std::string& SocketPermissionData::GetAsString() const {
133 if (!spec_.empty())
miket_OOO 2012/08/06 21:04:06 Are you sure a stringstream is necessary here? It
Peng 2012/08/07 21:31:55 Done.
134 return spec_;
135
136 std::stringstream spec;
137 switch (type_) {
miket_OOO 2012/08/06 21:04:06 Consider two separate methods that map type to str
Peng 2012/08/07 21:31:55 Done.
138 case TCP_CONNECT:
139 spec << "tcp-connect";
140 break;
141 case TCP_LISTEN:
142 spec << "tcp-listen";
143 break;
144 case UDP_BIND:
145 spec << "udp-bind";
146 break;
147 case UDP_SEND_TO:
148 spec << "udp-send-to";
149 break;
150 default:
151 return spec_;
152 }
153
154 if (match_subdomains_) {
155 spec << ":*";
156 if (!host_.empty())
157 spec << "." << host_;
158 } else {
159 spec << ":" << host_;
160 }
161
162 if (port_ == 0)
163 spec << ":*";
164 else
165 spec << ":" << port_;
166
167 spec_ = spec.str();
168 return spec_;
169 }
170
171 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698