Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "content/public/common/origin_util.h" | 5 #include "content/public/common/origin_util.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "content/public/common/content_client.h" | 10 #include "content/public/common/content_client.h" |
| 11 #include "content/public/common/url_constants.h" | |
| 11 #include "net/base/url_util.h" | 12 #include "net/base/url_util.h" |
| 12 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 #include "url/url_constants.h" | |
| 13 | 15 |
| 14 namespace content { | 16 namespace content { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 class SchemeAndOriginWhitelist { | 20 class SchemeAndOriginWhitelist { |
| 19 public: | 21 public: |
| 20 SchemeAndOriginWhitelist() { Reset(); } | 22 SchemeAndOriginWhitelist() { Reset(); } |
| 21 ~SchemeAndOriginWhitelist() {} | 23 ~SchemeAndOriginWhitelist() {} |
| 22 | 24 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 return true; | 85 return true; |
| 84 } | 86 } |
| 85 | 87 |
| 86 return false; | 88 return false; |
| 87 } | 89 } |
| 88 | 90 |
| 89 void ResetSchemesAndOriginsWhitelistForTesting() { | 91 void ResetSchemesAndOriginsWhitelistForTesting() { |
| 90 g_trustworthy_whitelist.Get().Reset(); | 92 g_trustworthy_whitelist.Get().Reset(); |
| 91 } | 93 } |
| 92 | 94 |
| 95 bool HasSuborigin(const GURL& url) { | |
| 96 if (!url.is_valid()) | |
| 97 return false; | |
| 98 | |
| 99 if (url.scheme() != kHttpSuboriginScheme && | |
| 100 url.scheme() != kHttpsSuboriginScheme) { | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 std::string host = url.host(); | |
|
nasko
2016/09/29 16:49:55
minor nit: I don't know how perf sensitive this ca
jww
2016/09/29 22:21:00
Done.
| |
| 105 | |
| 106 // This means that there is no suborigin before the dot, so there is no | |
|
nasko
2016/09/29 16:49:56
Technically it means there is no dot at all : ).
jww
2016/09/29 22:21:00
I don't understand. Are you referring to the fact
nasko
2016/09/30 17:40:07
I read the code with the interpretation that it is
| |
| 107 // suborigin. | |
| 108 if (host.find('.') == 0) | |
|
nasko
2016/09/29 16:49:56
s/0/string::npos/?
jww
2016/09/29 22:21:00
See above response. I did intend for this to check
| |
| 109 return false; | |
| 110 | |
| 111 // If there's nothing after the first dot, then there is no host for the | |
| 112 // physical origin, which is not a valid suborigin serialization. | |
| 113 if (host.find('.') == (host.size() - 1)) | |
|
nasko
2016/09/29 16:49:56
You could store the result of find from above and
jww
2016/09/29 22:21:00
Done.
| |
| 114 return false; | |
| 115 | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 std::string SuboriginFromUrl(const GURL& url) { | |
| 120 if (!HasSuborigin(url)) | |
| 121 return ""; | |
| 122 | |
| 123 size_t suborigin_end = url.host().find("."); | |
| 124 return url.host().substr(0, suborigin_end); | |
|
nasko
2016/09/29 16:49:56
Check suborigin_end for equality of string::npos a
jww
2016/09/29 22:21:00
Done.
| |
| 125 } | |
| 126 | |
| 127 GURL StripSuboriginFromUrl(const GURL& url) { | |
| 128 if (!HasSuborigin(url)) | |
| 129 return url; | |
| 130 | |
| 131 GURL::Replacements replacements; | |
| 132 if (url.scheme() == kHttpSuboriginScheme) { | |
| 133 replacements.SetSchemeStr(url::kHttpScheme); | |
| 134 } else { | |
| 135 DCHECK(url.scheme() == kHttpsSuboriginScheme); | |
| 136 replacements.SetSchemeStr(url::kHttpsScheme); | |
| 137 } | |
| 138 | |
| 139 size_t suborigin_end = url.host().find("."); | |
|
nasko
2016/09/29 16:49:56
Again, check for string::npos.
jww
2016/09/29 22:21:00
Done.
| |
| 140 std::string new_host(url.host().substr( | |
| 141 suborigin_end + 1, url.host().length() - suborigin_end - 1)); | |
| 142 replacements.SetHostStr(new_host); | |
| 143 | |
| 144 return url.ReplaceComponents(replacements); | |
| 145 } | |
| 146 | |
| 93 } // namespace content | 147 } // namespace content |
| OLD | NEW |