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

Side by Side Diff: net/base/registry_controlled_domains/registry_controlled_domain.cc

Issue 2380523004: Avoid unnecessary std::string allocations. (Closed)
Patch Set: Created 4 years, 2 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene,
6 // later modified by others), but almost entirely rewritten for Chrome. 6 // later modified by others), but almost entirely rewritten for Chrome.
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp)
8 /* ***** BEGIN LICENSE BLOCK ***** 8 /* ***** BEGIN LICENSE BLOCK *****
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * 10 *
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 next_dot = host.find('.', curr_start); 141 next_dot = host.find('.', curr_start);
142 } 142 }
143 143
144 // No rule found in the registry. curr_start now points to the first 144 // No rule found in the registry. curr_start now points to the first
145 // character of the last subcomponent of the host, so if we allow unknown 145 // character of the last subcomponent of the host, so if we allow unknown
146 // registries, return the length of this subcomponent. 146 // registries, return the length of this subcomponent.
147 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ? 147 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ?
148 (host.length() - curr_start) : 0; 148 (host.length() - curr_start) : 0;
149 } 149 }
150 150
151 std::string GetDomainAndRegistryImpl(base::StringPiece host, 151 base::StringPiece GetDomainAndRegistryImpl(
152 PrivateRegistryFilter private_filter) { 152 base::StringPiece host,
153 PrivateRegistryFilter private_filter) {
153 DCHECK(!host.empty()); 154 DCHECK(!host.empty());
154 155
155 // Find the length of the registry for this host. 156 // Find the length of the registry for this host.
156 const size_t registry_length = 157 const size_t registry_length =
157 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); 158 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter);
158 if ((registry_length == std::string::npos) || (registry_length == 0)) 159 if ((registry_length == std::string::npos) || (registry_length == 0))
159 return std::string(); // No registry. 160 return base::StringPiece(); // No registry.
160 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding 161 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding
161 // subcomponent length. 162 // subcomponent length.
162 DCHECK(host.length() >= 2); 163 DCHECK(host.length() >= 2);
163 if (registry_length > (host.length() - 2)) { 164 if (registry_length > (host.length() - 2)) {
164 NOTREACHED() << 165 NOTREACHED() <<
165 "Host does not have at least one subcomponent before registry!"; 166 "Host does not have at least one subcomponent before registry!";
166 return std::string(); 167 return base::StringPiece();
167 } 168 }
168 169
169 // Move past the dot preceding the registry, and search for the next previous 170 // Move past the dot preceding the registry, and search for the next previous
170 // dot. Return the host from after that dot, or the whole host when there is 171 // dot. Return the host from after that dot, or the whole host when there is
171 // no dot. 172 // no dot.
172 const size_t dot = host.rfind('.', host.length() - registry_length - 2); 173 const size_t dot = host.rfind('.', host.length() - registry_length - 2);
173 if (dot == std::string::npos) 174 if (dot == std::string::npos)
174 return host.as_string(); 175 return host;
175 return host.substr(dot + 1).as_string(); 176 return host.substr(dot + 1);
176 } 177 }
177 178
178 } // namespace 179 } // namespace
179 180
180 std::string GetDomainAndRegistry( 181 std::string GetDomainAndRegistry(
181 const GURL& gurl, 182 const GURL& gurl,
182 PrivateRegistryFilter filter) { 183 PrivateRegistryFilter filter) {
184 return GetDomainAndRegistryAsStringPiece(gurl, filter).as_string();
185 }
186
187 base::StringPiece GetDomainAndRegistryAsStringPiece(
188 const GURL& gurl,
189 PrivateRegistryFilter filter) {
183 base::StringPiece host = gurl.host_piece(); 190 base::StringPiece host = gurl.host_piece();
184 if (host.empty() || gurl.HostIsIPAddress()) 191 if (host.empty() || gurl.HostIsIPAddress())
185 return std::string(); 192 return base::StringPiece();
186 return GetDomainAndRegistryImpl(host, filter); 193 return GetDomainAndRegistryImpl(host, filter);
187 } 194 }
188 195
189 std::string GetDomainAndRegistry(base::StringPiece host, 196 std::string GetDomainAndRegistry(base::StringPiece host,
190 PrivateRegistryFilter filter) { 197 PrivateRegistryFilter filter) {
191 url::CanonHostInfo host_info; 198 url::CanonHostInfo host_info;
192 const std::string canon_host(CanonicalizeHost(host, &host_info)); 199 const std::string canon_host(CanonicalizeHost(host, &host_info));
193 if (canon_host.empty() || host_info.IsIPAddress()) 200 if (canon_host.empty() || host_info.IsIPAddress())
194 return std::string(); 201 return std::string();
195 return GetDomainAndRegistryImpl(canon_host, filter); 202 return GetDomainAndRegistryImpl(canon_host, filter).as_string();
196 } 203 }
197 204
198 bool SameDomainOrHost( 205 bool SameDomainOrHost(
199 const GURL& gurl1, 206 const GURL& gurl1,
200 const GURL& gurl2, 207 const GURL& gurl2,
201 PrivateRegistryFilter filter) { 208 PrivateRegistryFilter filter) {
202 // See if both URLs have a known domain + registry, and those values are the 209 // See if both URLs have a known domain + registry, and those values are the
203 // same. 210 // same.
204 const std::string domain1(GetDomainAndRegistry(gurl1, filter)); 211 const base::StringPiece domain1 =
205 const std::string domain2(GetDomainAndRegistry(gurl2, filter)); 212 GetDomainAndRegistryAsStringPiece(gurl1, filter);
213 const base::StringPiece domain2 =
214 GetDomainAndRegistryAsStringPiece(gurl2, filter);
206 if (!domain1.empty() || !domain2.empty()) 215 if (!domain1.empty() || !domain2.empty())
207 return domain1 == domain2; 216 return domain1 == domain2;
208 217
209 // No domains. See if the hosts are identical. 218 // No domains. See if the hosts are identical.
210 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host; 219 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host;
211 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host; 220 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host;
212 if ((host1.len <= 0) || (host1.len != host2.len)) 221 if ((host1.len <= 0) || (host1.len != host2.len))
213 return false; 222 return false;
214 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, 223 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin,
215 gurl2.possibly_invalid_spec().data() + host2.begin, 224 gurl2.possibly_invalid_spec().data() + host2.begin,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 263
255 void SetFindDomainGraph(const unsigned char* domains, size_t length) { 264 void SetFindDomainGraph(const unsigned char* domains, size_t length) {
256 CHECK(domains); 265 CHECK(domains);
257 CHECK_NE(length, 0u); 266 CHECK_NE(length, 0u);
258 g_graph = domains; 267 g_graph = domains;
259 g_graph_length = length; 268 g_graph_length = length;
260 } 269 }
261 270
262 } // namespace registry_controlled_domains 271 } // namespace registry_controlled_domains
263 } // namespace net 272 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698