OLD | NEW |
---|---|
1 //* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 // Copyright (c) 2011 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | |
6 // later modified by others), but almost entirely rewritten for Chrome. | |
7 // (netwerk/dns/src/nsEffectiveTLDService.h) | |
2 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 * | 10 * |
5 * The contents of this file are subject to the Mozilla Public License Version | 11 * The contents of this file are subject to the Mozilla Public License Version |
6 * 1.1 (the "License"); you may not use this file except in compliance with | 12 * 1.1 (the "License"); you may not use this file except in compliance with |
7 * the License. You may obtain a copy of the License at | 13 * the License. You may obtain a copy of the License at |
8 * http://www.mozilla.org/MPL/ | 14 * http://www.mozilla.org/MPL/ |
9 * | 15 * |
10 * Software distributed under the License is distributed on an "AS IS" basis, | 16 * Software distributed under the License is distributed on an "AS IS" basis, |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | 17 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
(...skipping 17 matching lines...) Expand all Loading... | |
29 * of those above. If you wish to allow use of your version of this file only | 35 * of those above. If you wish to allow use of your version of this file only |
30 * under the terms of either the GPL or the LGPL, and not to allow others to | 36 * under the terms of either the GPL or the LGPL, and not to allow others to |
31 * use your version of this file under the terms of the MPL, indicate your | 37 * use your version of this file under the terms of the MPL, indicate your |
32 * decision by deleting the provisions above and replace them with the notice | 38 * decision by deleting the provisions above and replace them with the notice |
33 * and other provisions required by the GPL or the LGPL. If you do not delete | 39 * and other provisions required by the GPL or the LGPL. If you do not delete |
34 * the provisions above, a recipient may use your version of this file under | 40 * the provisions above, a recipient may use your version of this file under |
35 * the terms of any one of the MPL, the GPL or the LGPL. | 41 * the terms of any one of the MPL, the GPL or the LGPL. |
36 * | 42 * |
37 * ***** END LICENSE BLOCK ***** */ | 43 * ***** END LICENSE BLOCK ***** */ |
38 | 44 |
39 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | |
40 // later modified by others), but almost entirely rewritten for Chrome. | |
41 | |
42 /* | 45 /* |
43 (Documentation based on the Mozilla documentation currently at | 46 (Documentation based on the Mozilla documentation currently at |
44 http://wiki.mozilla.org/Gecko:Effective_TLD_Service, written by the same | 47 http://wiki.mozilla.org/Gecko:Effective_TLD_Service, written by the same |
45 author.) | 48 author.) |
46 | 49 |
47 The RegistryControlledDomainService examines the hostname of a GURL passed to | 50 The RegistryControlledDomainService examines the hostname of a GURL passed to |
48 it and determines the longest portion that is controlled by a registrar. | 51 it and determines the longest portion that is controlled by a registrar. |
49 Although technically the top-level domain (TLD) for a hostname is the last | 52 Although technically the top-level domain (TLD) for a hostname is the last |
50 dot-portion of the name (such as .com or .org), many domains (such as co.uk) | 53 dot-portion of the name (such as .com or .org), many domains (such as co.uk) |
51 function as though they were TLDs, allocating any number of more specific, | 54 function as though they were TLDs, allocating any number of more specific, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 #define NET_BASE_REGISTRY_CONTROLLED_DOMAIN_H_ | 114 #define NET_BASE_REGISTRY_CONTROLLED_DOMAIN_H_ |
112 #pragma once | 115 #pragma once |
113 | 116 |
114 #include <string> | 117 #include <string> |
115 | 118 |
116 #include "base/basictypes.h" | 119 #include "base/basictypes.h" |
117 #include "net/base/net_export.h" | 120 #include "net/base/net_export.h" |
118 | 121 |
119 class GURL; | 122 class GURL; |
120 | 123 |
121 template <typename T> | |
122 struct DefaultSingletonTraits; | |
123 struct DomainRule; | 124 struct DomainRule; |
124 | 125 |
125 namespace net { | 126 namespace net { |
126 | 127 |
127 struct RegistryControlledDomainServiceSingletonTraits; | |
128 | |
129 // This class is a singleton. | |
130 class NET_EXPORT RegistryControlledDomainService { | 128 class NET_EXPORT RegistryControlledDomainService { |
131 public: | 129 public: |
132 ~RegistryControlledDomainService() { } | |
133 | |
134 // Returns the registered, organization-identifying host and all its registry | 130 // Returns the registered, organization-identifying host and all its registry |
135 // information, but no subdomains, from the given GURL. Returns an empty | 131 // information, but no subdomains, from the given GURL. Returns an empty |
136 // string if the GURL is invalid, has no host (e.g. a file: URL), has multiple | 132 // string if the GURL is invalid, has no host (e.g. a file: URL), has multiple |
137 // trailing dots, is an IP address, has only one subcomponent (i.e. no dots | 133 // trailing dots, is an IP address, has only one subcomponent (i.e. no dots |
138 // other than leading/trailing ones), or is itself a recognized registry | 134 // other than leading/trailing ones), or is itself a recognized registry |
139 // identifier. If no matching rule is found in the effective-TLD data (or in | 135 // identifier. If no matching rule is found in the effective-TLD data (or in |
140 // the default data, if the resource failed to load), the last subcomponent of | 136 // the default data, if the resource failed to load), the last subcomponent of |
141 // the host is assumed to be the registry. | 137 // the host is assumed to be the registry. |
142 // | 138 // |
143 // Examples: | 139 // Examples: |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 // http://foo.bar/file.html -> 0 or 3, depending (no rule; assume | 185 // http://foo.bar/file.html -> 0 or 3, depending (no rule; assume |
190 // bar) | 186 // bar) |
191 static size_t GetRegistryLength(const GURL& gurl, | 187 static size_t GetRegistryLength(const GURL& gurl, |
192 bool allow_unknown_registries); | 188 bool allow_unknown_registries); |
193 | 189 |
194 // Like the GURL version, but takes a host (which is canonicalized internally) | 190 // Like the GURL version, but takes a host (which is canonicalized internally) |
195 // instead of a full GURL. | 191 // instead of a full GURL. |
196 static size_t GetRegistryLength(const std::string& host, | 192 static size_t GetRegistryLength(const std::string& host, |
197 bool allow_unknown_registries); | 193 bool allow_unknown_registries); |
198 | 194 |
199 // Returns the singleton instance, after attempting to initialize it. | |
200 // NOTE that if the effective-TLD data resource can't be found, the instance | |
201 // will be initialized and continue operation with simple default TLD data. | |
202 static RegistryControlledDomainService* GetInstance(); | |
203 | |
204 protected: | |
205 typedef const struct DomainRule* (*FindDomainPtr)(const char *, unsigned int); | |
206 | |
207 // The entire protected API is only for unit testing. I mean it. Don't make | |
208 // me come over there! | |
209 RegistryControlledDomainService(); | |
210 | |
211 // Set the RegistryControledDomainService instance to be used internally. | |
212 // |instance| will supersede the singleton instance normally used. If | |
213 // |instance| is NULL, normal behavior is restored, and internal operations | |
214 // will return to using the singleton. This function always returns the | |
215 // instance set by the most recent call to SetInstance. | |
216 static RegistryControlledDomainService* SetInstance( | |
217 RegistryControlledDomainService* instance); | |
218 | |
219 // Used for unit tests, so that a different perfect hash map from the full | |
220 // list is used. | |
221 static void UseFindDomainFunction(FindDomainPtr function); | |
222 | |
223 private: | 195 private: |
224 // To allow construction of the internal singleton instance. | 196 friend class RegistryControlledDomainTest; |
225 friend struct DefaultSingletonTraits<RegistryControlledDomainService>; | |
226 | 197 |
227 // Internal workings of the static public methods. See above. | 198 // Internal workings of the static public methods. See above. |
228 static std::string GetDomainAndRegistryImpl(const std::string& host); | 199 static std::string GetDomainAndRegistryImpl(const std::string& host); |
229 size_t GetRegistryLengthImpl(const std::string& host, | 200 static size_t GetRegistryLengthImpl(const std::string& host, |
230 bool allow_unknown_registries); | 201 bool allow_unknown_registries); |
202 | |
203 typedef const struct DomainRule* (*FindDomainPtr)(const char *, unsigned int); | |
wtc
2011/10/27 22:44:09
Nit: list this typedef at the beginning of the pri
Ryan Sleevi
2011/10/27 22:48:04
Will send a quick cleanup CL.
| |
204 | |
205 // Used for unit tests, so that a different perfect hash map from the full | |
206 // list is used. Set to NULL to use the Default function. | |
207 static void UseFindDomainFunction(FindDomainPtr function); | |
231 | 208 |
232 // Function that returns a DomainRule given a domain. | 209 // Function that returns a DomainRule given a domain. |
233 FindDomainPtr find_domain_function_; | 210 static FindDomainPtr find_domain_function_; |
234 | 211 |
235 DISALLOW_COPY_AND_ASSIGN(RegistryControlledDomainService); | 212 |
213 DISALLOW_IMPLICIT_CONSTRUCTORS(RegistryControlledDomainService); | |
236 }; | 214 }; |
237 | 215 |
238 } // namespace net | 216 } // namespace net |
239 | 217 |
240 #endif // NET_BASE_REGISTRY_CONTROLLED_DOMAIN_H_ | 218 #endif // NET_BASE_REGISTRY_CONTROLLED_DOMAIN_H_ |
OLD | NEW |