1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
// Copyright (c) 2012 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 "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h"
#import <SecurityInterface/SFChooseIdentityPanel.h>
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ssl/ssl_client_auth_observer.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "net/cert/x509_certificate.h"
#include "net/cert/x509_util_mac.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "ui/base/cocoa/window_size_constants.h"
#include "ui/base/l10n/l10n_util_mac.h"
using content::BrowserThread;
@interface SFChooseIdentityPanel (SystemPrivate)
// A system-private interface that dismisses a panel whose sheet was started by
// -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:message:
// as though the user clicked the button identified by returnCode. Verified
// present in 10.5 through 10.8.
- (void)_dismissWithCode:(NSInteger)code;
@end
@interface SSLClientCertificateSelectorCocoa ()
- (void)onConstrainedWindowClosed;
@end
class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver,
public ConstrainedWindowMacDelegate {
public:
SSLClientAuthObserverCocoaBridge(
const content::BrowserContext* browser_context,
net::SSLCertRequestInfo* cert_request_info,
const chrome::SelectCertificateCallback& callback,
SSLClientCertificateSelectorCocoa* controller)
: SSLClientAuthObserver(browser_context, cert_request_info, callback),
controller_(controller) {
}
// SSLClientAuthObserver implementation:
void OnCertSelectedByNotification() override {
[controller_ closeWebContentsModalDialog];
}
// ConstrainedWindowMacDelegate implementation:
void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
// |onConstrainedWindowClosed| will delete the sheet which might be still
// in use higher up the call stack. Wait for the next cycle of the event
// loop to call this function.
[controller_ performSelector:@selector(onConstrainedWindowClosed)
withObject:nil
afterDelay:0];
}
private:
SSLClientCertificateSelectorCocoa* controller_; // weak
};
namespace chrome {
void ShowSSLClientCertificateSelector(
content::WebContents* contents,
net::SSLCertRequestInfo* cert_request_info,
const SelectCertificateCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// The dialog manages its own lifetime.
SSLClientCertificateSelectorCocoa* selector =
[[SSLClientCertificateSelectorCocoa alloc]
initWithBrowserContext:contents->GetBrowserContext()
certRequestInfo:cert_request_info
callback:callback];
[selector displayForWebContents:contents];
}
} // namespace chrome
@implementation SSLClientCertificateSelectorCocoa
- (id)initWithBrowserContext:(const content::BrowserContext*)browserContext
certRequestInfo:(net::SSLCertRequestInfo*)certRequestInfo
callback:(const chrome::SelectCertificateCallback&)callback {
DCHECK(browserContext);
DCHECK(certRequestInfo);
if ((self = [super init])) {
observer_.reset(new SSLClientAuthObserverCocoaBridge(
browserContext, certRequestInfo, callback, self));
}
return self;
}
- (void)sheetDidEnd:(NSWindow*)parent
returnCode:(NSInteger)returnCode
context:(void*)context {
net::X509Certificate* cert = NULL;
if (returnCode == NSFileHandlingPanelOKButton) {
CFRange range = CFRangeMake(0, CFArrayGetCount(identities_));
CFIndex index =
CFArrayGetFirstIndexOfValue(identities_, range, [panel_ identity]);
if (index != -1)
cert = certificates_[index].get();
else
NOTREACHED();
}
// Finally, tell the backend which identity (or none) the user selected.
observer_->StopObserving();
observer_->CertificateSelected(cert);
if (!closePending_)
constrainedWindow_->CloseWebContentsModalDialog();
}
- (void)displayForWebContents:(content::WebContents*)webContents {
// Create an array of CFIdentityRefs for the certificates:
size_t numCerts = observer_->cert_request_info()->client_certs.size();
identities_.reset(CFArrayCreateMutable(
kCFAllocatorDefault, numCerts, &kCFTypeArrayCallBacks));
for (size_t i = 0; i < numCerts; ++i) {
SecCertificateRef cert =
observer_->cert_request_info()->client_certs[i]->os_cert_handle();
SecIdentityRef identity;
if (SecIdentityCreateWithCertificate(NULL, cert, &identity) == noErr) {
CFArrayAppendValue(identities_, identity);
CFRelease(identity);
certificates_.push_back(observer_->cert_request_info()->client_certs[i]);
}
}
// Get the message to display:
NSString* message = l10n_util::GetNSStringF(
IDS_CLIENT_CERT_DIALOG_TEXT,
base::ASCIIToUTF16(
observer_->cert_request_info()->host_and_port.ToString()));
// Create and set up a system choose-identity panel.
panel_.reset([[SFChooseIdentityPanel alloc] init]);
[panel_ setInformativeText:message];
[panel_ setDefaultButtonTitle:l10n_util::GetNSString(IDS_OK)];
[panel_ setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
SecPolicyRef sslPolicy;
if (net::x509_util::CreateSSLClientPolicy(&sslPolicy) == noErr) {
[panel_ setPolicies:(id)sslPolicy];
CFRelease(sslPolicy);
}
constrainedWindow_.reset(
new ConstrainedWindowMac(observer_.get(), webContents, self));
observer_->StartObserving();
}
- (void)closeWebContentsModalDialog {
DCHECK(constrainedWindow_);
constrainedWindow_->CloseWebContentsModalDialog();
}
- (NSWindow*)overlayWindow {
return overlayWindow_;
}
- (SFChooseIdentityPanel*)panel {
return panel_;
}
- (void)showSheetForWindow:(NSWindow*)window {
NSString* title = l10n_util::GetNSString(IDS_CLIENT_CERT_DIALOG_TITLE);
overlayWindow_.reset([window retain]);
[panel_ beginSheetForWindow:window
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:context:)
contextInfo:NULL
identities:base::mac::CFToNSCast(identities_)
message:title];
}
- (void)closeSheetWithAnimation:(BOOL)withAnimation {
closePending_ = YES;
overlayWindow_.reset();
// Closing the sheet using -[NSApp endSheet:] doesn't work so use the private
// method.
[panel_ _dismissWithCode:NSFileHandlingPanelCancelButton];
}
- (void)hideSheet {
NSWindow* sheetWindow = [overlayWindow_ attachedSheet];
[sheetWindow setAlphaValue:0.0];
oldResizesSubviews_ = [[sheetWindow contentView] autoresizesSubviews];
[[sheetWindow contentView] setAutoresizesSubviews:NO];
oldSheetFrame_ = [sheetWindow frame];
NSRect overlayFrame = [overlayWindow_ frame];
oldSheetFrame_.origin.x -= NSMinX(overlayFrame);
oldSheetFrame_.origin.y -= NSMinY(overlayFrame);
[sheetWindow setFrame:ui::kWindowSizeDeterminedLater display:NO];
}
- (void)unhideSheet {
NSWindow* sheetWindow = [overlayWindow_ attachedSheet];
NSRect overlayFrame = [overlayWindow_ frame];
oldSheetFrame_.origin.x += NSMinX(overlayFrame);
oldSheetFrame_.origin.y += NSMinY(overlayFrame);
[sheetWindow setFrame:oldSheetFrame_ display:NO];
[[sheetWindow contentView] setAutoresizesSubviews:oldResizesSubviews_];
[[overlayWindow_ attachedSheet] setAlphaValue:1.0];
}
- (void)pulseSheet {
// NOOP
}
- (void)makeSheetKeyAndOrderFront {
[[overlayWindow_ attachedSheet] makeKeyAndOrderFront:nil];
}
- (void)updateSheetPosition {
// NOOP
}
- (void)onConstrainedWindowClosed {
observer_->StopObserving();
panel_.reset();
constrainedWindow_.reset();
[self release];
}
@end
|