OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.h" |
| 6 |
| 7 #import "base/mac/scoped_nsobject.h" |
| 8 #include "base/numerics/safe_conversions.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #include "components/url_formatter/elide_url.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "url/gurl.h" |
| 14 #include "url/scheme_host_port.h" |
| 15 |
| 16 namespace ios_internal { |
| 17 namespace nsurlprotectionspace_util { |
| 18 |
| 19 NSString* MessageForHTTPAuth(NSURLProtectionSpace* protectionSpace) { |
| 20 DCHECK(CanShow(protectionSpace)); |
| 21 |
| 22 if (protectionSpace.receivesCredentialSecurely) |
| 23 return RequesterIdentity(protectionSpace); |
| 24 |
| 25 NSString* securityWarning = |
| 26 l10n_util::GetNSString(IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT); |
| 27 return |
| 28 [NSString stringWithFormat:@"%@ %@", RequesterIdentity(protectionSpace), |
| 29 securityWarning]; |
| 30 } |
| 31 |
| 32 BOOL CanShow(NSURLProtectionSpace* protectionSpace) { |
| 33 if (protectionSpace.host.length == 0) |
| 34 return NO; |
| 35 |
| 36 if (!base::IsValueInRangeForNumericType<uint16_t>(protectionSpace.port)) |
| 37 return NO; // Port is invalid. |
| 38 |
| 39 if (!protectionSpace.isProxy && !RequesterOrigin(protectionSpace).is_valid()) |
| 40 return NO; // Can't construct origin for non-proxy requester. |
| 41 |
| 42 return YES; |
| 43 } |
| 44 |
| 45 NSString* RequesterIdentity(NSURLProtectionSpace* protectionSpace) { |
| 46 GURL requesterOrigin = RequesterOrigin(protectionSpace); |
| 47 int formatID = protectionSpace.isProxy ? IDS_LOGIN_DIALOG_PROXY_AUTHORITY |
| 48 : IDS_LOGIN_DIALOG_AUTHORITY; |
| 49 if (!requesterOrigin.is_valid()) { |
| 50 // May be invalid for SOCKS proxy type. |
| 51 return l10n_util::GetNSStringF( |
| 52 formatID, base::SysNSStringToUTF16(protectionSpace.host)); |
| 53 } |
| 54 base::string16 authority = |
| 55 url_formatter::FormatUrlForSecurityDisplay(requesterOrigin); |
| 56 |
| 57 return l10n_util::GetNSStringF(formatID, authority); |
| 58 } |
| 59 |
| 60 GURL RequesterOrigin(NSURLProtectionSpace* protectionSpace) { |
| 61 std::string scheme = base::SysNSStringToUTF8(protectionSpace.protocol); |
| 62 std::string host = base::SysNSStringToUTF8(protectionSpace.host); |
| 63 uint16_t port = base::checked_cast<uint16_t>(protectionSpace.port); |
| 64 |
| 65 return GURL(url::SchemeHostPort(scheme, host, port).Serialize()); |
| 66 } |
| 67 |
| 68 } // namespace nsurlprotectionspace_util |
| 69 } // namespace ios_internal |
OLD | NEW |