| Index: ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.mm
|
| diff --git a/ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.mm b/ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fffc7fd09daee9a66c1c57a898c1e53f4c6d6b10
|
| --- /dev/null
|
| +++ b/ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.mm
|
| @@ -0,0 +1,69 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#import "ios/chrome/browser/ui/dialogs/nsurl_protection_space_util.h"
|
| +
|
| +#import "base/mac/scoped_nsobject.h"
|
| +#include "base/numerics/safe_conversions.h"
|
| +#include "base/strings/sys_string_conversions.h"
|
| +#include "components/strings/grit/components_strings.h"
|
| +#include "components/url_formatter/elide_url.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +#include "url/gurl.h"
|
| +#include "url/scheme_host_port.h"
|
| +
|
| +namespace ios_internal {
|
| +namespace nsurlprotectionspace_util {
|
| +
|
| +NSString* MessageForHTTPAuth(NSURLProtectionSpace* protectionSpace) {
|
| + DCHECK(CanShow(protectionSpace));
|
| +
|
| + if (protectionSpace.receivesCredentialSecurely)
|
| + return RequesterIdentity(protectionSpace);
|
| +
|
| + NSString* securityWarning =
|
| + l10n_util::GetNSString(IDS_WEBSITE_SETTINGS_NON_SECURE_TRANSPORT);
|
| + return
|
| + [NSString stringWithFormat:@"%@ %@", RequesterIdentity(protectionSpace),
|
| + securityWarning];
|
| +}
|
| +
|
| +BOOL CanShow(NSURLProtectionSpace* protectionSpace) {
|
| + if (protectionSpace.host.length == 0)
|
| + return NO;
|
| +
|
| + if (!base::IsValueInRangeForNumericType<uint16_t>(protectionSpace.port))
|
| + return NO; // Port is invalid.
|
| +
|
| + if (!protectionSpace.isProxy && !RequesterOrigin(protectionSpace).is_valid())
|
| + return NO; // Can't construct origin for non-proxy requester.
|
| +
|
| + return YES;
|
| +}
|
| +
|
| +NSString* RequesterIdentity(NSURLProtectionSpace* protectionSpace) {
|
| + GURL requesterOrigin = RequesterOrigin(protectionSpace);
|
| + int formatID = protectionSpace.isProxy ? IDS_LOGIN_DIALOG_PROXY_AUTHORITY
|
| + : IDS_LOGIN_DIALOG_AUTHORITY;
|
| + if (!requesterOrigin.is_valid()) {
|
| + // May be invalid for SOCKS proxy type.
|
| + return l10n_util::GetNSStringF(
|
| + formatID, base::SysNSStringToUTF16(protectionSpace.host));
|
| + }
|
| + base::string16 authority =
|
| + url_formatter::FormatUrlForSecurityDisplay(requesterOrigin);
|
| +
|
| + return l10n_util::GetNSStringF(formatID, authority);
|
| +}
|
| +
|
| +GURL RequesterOrigin(NSURLProtectionSpace* protectionSpace) {
|
| + std::string scheme = base::SysNSStringToUTF8(protectionSpace.protocol);
|
| + std::string host = base::SysNSStringToUTF8(protectionSpace.host);
|
| + uint16_t port = base::checked_cast<uint16_t>(protectionSpace.port);
|
| +
|
| + return GURL(url::SchemeHostPort(scheme, host, port).Serialize());
|
| +}
|
| +
|
| +} // namespace nsurlprotectionspace_util
|
| +} // namespace ios_internal
|
|
|