| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/ssl_client_certificate_selector_cocoa.h" |
| 6 | 6 |
| 7 #import <SecurityInterface/SFChooseIdentityPanel.h> | 7 #import <SecurityInterface/SFChooseIdentityPanel.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/ssl/ssl_client_auth_observer.h" | 14 #include "chrome/browser/ssl/ssl_client_auth_observer.h" |
| 15 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | 15 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" |
| 16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/web_modal/popup_manager.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/client_certificate_delegate.h" |
| 18 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 19 #include "net/cert/x509_certificate.h" | 21 #include "net/cert/x509_certificate.h" |
| 20 #include "net/cert/x509_util_mac.h" | 22 #include "net/cert/x509_util_mac.h" |
| 21 #include "net/ssl/ssl_cert_request_info.h" | 23 #include "net/ssl/ssl_cert_request_info.h" |
| 22 #include "ui/base/cocoa/window_size_constants.h" | 24 #include "ui/base/cocoa/window_size_constants.h" |
| 23 #include "ui/base/l10n/l10n_util_mac.h" | 25 #include "ui/base/l10n/l10n_util_mac.h" |
| 24 | 26 |
| 25 using content::BrowserThread; | 27 using content::BrowserThread; |
| 26 | 28 |
| 27 @interface SFChooseIdentityPanel (SystemPrivate) | 29 @interface SFChooseIdentityPanel (SystemPrivate) |
| 28 // A system-private interface that dismisses a panel whose sheet was started by | 30 // A system-private interface that dismisses a panel whose sheet was started by |
| 29 // -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:mess
age: | 31 // -beginSheetForWindow:modalDelegate:didEndSelector:contextInfo:identities:mess
age: |
| 30 // as though the user clicked the button identified by returnCode. Verified | 32 // as though the user clicked the button identified by returnCode. Verified |
| 31 // present in 10.5 through 10.8. | 33 // present in 10.5 through 10.8. |
| 32 - (void)_dismissWithCode:(NSInteger)code; | 34 - (void)_dismissWithCode:(NSInteger)code; |
| 33 @end | 35 @end |
| 34 | 36 |
| 35 @interface SSLClientCertificateSelectorCocoa () | 37 @interface SSLClientCertificateSelectorCocoa () |
| 36 - (void)onConstrainedWindowClosed; | 38 - (void)onConstrainedWindowClosed; |
| 37 @end | 39 @end |
| 38 | 40 |
| 39 class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver, | 41 class SSLClientAuthObserverCocoaBridge : public SSLClientAuthObserver, |
| 40 public ConstrainedWindowMacDelegate { | 42 public ConstrainedWindowMacDelegate { |
| 41 public: | 43 public: |
| 42 SSLClientAuthObserverCocoaBridge( | 44 SSLClientAuthObserverCocoaBridge( |
| 43 const content::BrowserContext* browser_context, | 45 const content::BrowserContext* browser_context, |
| 44 net::SSLCertRequestInfo* cert_request_info, | 46 net::SSLCertRequestInfo* cert_request_info, |
| 45 const chrome::SelectCertificateCallback& callback, | 47 scoped_ptr<content::ClientCertificateDelegate> delegate, |
| 46 SSLClientCertificateSelectorCocoa* controller) | 48 SSLClientCertificateSelectorCocoa* controller) |
| 47 : SSLClientAuthObserver(browser_context, cert_request_info, callback), | 49 : SSLClientAuthObserver(browser_context, |
| 48 controller_(controller) { | 50 cert_request_info, |
| 49 } | 51 delegate.Pass()), |
| 52 controller_(controller) {} |
| 50 | 53 |
| 51 // SSLClientAuthObserver implementation: | 54 // SSLClientAuthObserver implementation: |
| 52 void OnCertSelectedByNotification() override { | 55 void OnCertSelectedByNotification() override { |
| 53 [controller_ closeWebContentsModalDialog]; | 56 [controller_ closeWebContentsModalDialog]; |
| 54 } | 57 } |
| 55 | 58 |
| 56 // ConstrainedWindowMacDelegate implementation: | 59 // ConstrainedWindowMacDelegate implementation: |
| 57 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { | 60 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { |
| 58 // |onConstrainedWindowClosed| will delete the sheet which might be still | 61 // |onConstrainedWindowClosed| will delete the sheet which might be still |
| 59 // in use higher up the call stack. Wait for the next cycle of the event | 62 // in use higher up the call stack. Wait for the next cycle of the event |
| 60 // loop to call this function. | 63 // loop to call this function. |
| 61 [controller_ performSelector:@selector(onConstrainedWindowClosed) | 64 [controller_ performSelector:@selector(onConstrainedWindowClosed) |
| 62 withObject:nil | 65 withObject:nil |
| 63 afterDelay:0]; | 66 afterDelay:0]; |
| 64 } | 67 } |
| 65 | 68 |
| 66 private: | 69 private: |
| 67 SSLClientCertificateSelectorCocoa* controller_; // weak | 70 SSLClientCertificateSelectorCocoa* controller_; // weak |
| 68 }; | 71 }; |
| 69 | 72 |
| 70 namespace chrome { | 73 namespace chrome { |
| 71 | 74 |
| 72 void ShowSSLClientCertificateSelector( | 75 void ShowSSLClientCertificateSelector( |
| 73 content::WebContents* contents, | 76 content::WebContents* contents, |
| 74 net::SSLCertRequestInfo* cert_request_info, | 77 net::SSLCertRequestInfo* cert_request_info, |
| 75 const SelectCertificateCallback& callback) { | 78 scoped_ptr<content::ClientCertificateDelegate> delegate) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 79 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 80 |
| 81 // Not all WebContentses can show modal dialogs. |
| 82 // |
| 83 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to |
| 84 // show a dialog in Browser's implementation. https://crbug.com/456255 |
| 85 if (web_modal::PopupManager::FromWebContents(contents) == nullptr) { |
| 86 delegate->CancelCertificateSelection(); |
| 87 return; |
| 88 } |
| 89 |
| 77 // The dialog manages its own lifetime. | 90 // The dialog manages its own lifetime. |
| 78 SSLClientCertificateSelectorCocoa* selector = | 91 SSLClientCertificateSelectorCocoa* selector = |
| 79 [[SSLClientCertificateSelectorCocoa alloc] | 92 [[SSLClientCertificateSelectorCocoa alloc] |
| 80 initWithBrowserContext:contents->GetBrowserContext() | 93 initWithBrowserContext:contents->GetBrowserContext() |
| 81 certRequestInfo:cert_request_info | 94 certRequestInfo:cert_request_info |
| 82 callback:callback]; | 95 delegate:delegate.Pass()]; |
| 83 [selector displayForWebContents:contents]; | 96 [selector displayForWebContents:contents]; |
| 84 } | 97 } |
| 85 | 98 |
| 86 } // namespace chrome | 99 } // namespace chrome |
| 87 | 100 |
| 88 @implementation SSLClientCertificateSelectorCocoa | 101 @implementation SSLClientCertificateSelectorCocoa |
| 89 | 102 |
| 90 - (id)initWithBrowserContext:(const content::BrowserContext*)browserContext | 103 - (id)initWithBrowserContext:(const content::BrowserContext*)browserContext |
| 91 certRequestInfo:(net::SSLCertRequestInfo*)certRequestInfo | 104 certRequestInfo:(net::SSLCertRequestInfo*)certRequestInfo |
| 92 callback:(const chrome::SelectCertificateCallback&)callback { | 105 delegate:(scoped_ptr<content::ClientCertificateDelegate>) |
| 106 delegate { |
| 93 DCHECK(browserContext); | 107 DCHECK(browserContext); |
| 94 DCHECK(certRequestInfo); | 108 DCHECK(certRequestInfo); |
| 95 if ((self = [super init])) { | 109 if ((self = [super init])) { |
| 96 observer_.reset(new SSLClientAuthObserverCocoaBridge( | 110 observer_.reset(new SSLClientAuthObserverCocoaBridge( |
| 97 browserContext, certRequestInfo, callback, self)); | 111 browserContext, certRequestInfo, delegate.Pass(), self)); |
| 98 } | 112 } |
| 99 return self; | 113 return self; |
| 100 } | 114 } |
| 101 | 115 |
| 102 - (void)sheetDidEnd:(NSWindow*)parent | 116 - (void)sheetDidEnd:(NSWindow*)parent |
| 103 returnCode:(NSInteger)returnCode | 117 returnCode:(NSInteger)returnCode |
| 104 context:(void*)context { | 118 context:(void*)context { |
| 105 net::X509Certificate* cert = NULL; | 119 net::X509Certificate* cert = NULL; |
| 106 if (returnCode == NSFileHandlingPanelOKButton) { | 120 if (returnCode == NSFileHandlingPanelOKButton) { |
| 107 CFRange range = CFRangeMake(0, CFArrayGetCount(identities_)); | 121 CFRange range = CFRangeMake(0, CFArrayGetCount(identities_)); |
| 108 CFIndex index = | 122 CFIndex index = |
| 109 CFArrayGetFirstIndexOfValue(identities_, range, [panel_ identity]); | 123 CFArrayGetFirstIndexOfValue(identities_, range, [panel_ identity]); |
| 110 if (index != -1) | 124 if (index != -1) |
| 111 cert = certificates_[index].get(); | 125 cert = certificates_[index].get(); |
| 112 else | 126 else |
| 113 NOTREACHED(); | 127 NOTREACHED(); |
| 114 } | 128 } |
| 115 | 129 |
| 116 // Finally, tell the backend which identity (or none) the user selected. | 130 if (!certificateSelected_) { |
| 117 observer_->StopObserving(); | 131 // Finally, tell the backend which identity (or none) the user selected. |
| 118 observer_->CertificateSelected(cert); | 132 certificateSelected_ = YES; |
| 133 observer_->StopObserving(); |
| 134 observer_->CertificateSelected(cert); |
| 135 } |
| 119 | 136 |
| 120 if (!closePending_) | 137 if (!closePending_) |
| 121 constrainedWindow_->CloseWebContentsModalDialog(); | 138 constrainedWindow_->CloseWebContentsModalDialog(); |
| 122 } | 139 } |
| 123 | 140 |
| 124 - (void)displayForWebContents:(content::WebContents*)webContents { | 141 - (void)displayForWebContents:(content::WebContents*)webContents { |
| 125 // Create an array of CFIdentityRefs for the certificates: | 142 // Create an array of CFIdentityRefs for the certificates: |
| 126 size_t numCerts = observer_->cert_request_info()->client_certs.size(); | 143 size_t numCerts = observer_->cert_request_info()->client_certs.size(); |
| 127 identities_.reset(CFArrayCreateMutable( | 144 identities_.reset(CFArrayCreateMutable( |
| 128 kCFAllocatorDefault, numCerts, &kCFTypeArrayCallBacks)); | 145 kCFAllocatorDefault, numCerts, &kCFTypeArrayCallBacks)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 overlayWindow_.reset([window retain]); | 194 overlayWindow_.reset([window retain]); |
| 178 [panel_ beginSheetForWindow:window | 195 [panel_ beginSheetForWindow:window |
| 179 modalDelegate:self | 196 modalDelegate:self |
| 180 didEndSelector:@selector(sheetDidEnd:returnCode:context:) | 197 didEndSelector:@selector(sheetDidEnd:returnCode:context:) |
| 181 contextInfo:NULL | 198 contextInfo:NULL |
| 182 identities:base::mac::CFToNSCast(identities_) | 199 identities:base::mac::CFToNSCast(identities_) |
| 183 message:title]; | 200 message:title]; |
| 184 } | 201 } |
| 185 | 202 |
| 186 - (void)closeSheetWithAnimation:(BOOL)withAnimation { | 203 - (void)closeSheetWithAnimation:(BOOL)withAnimation { |
| 204 if (!certificateSelected_) { |
| 205 certificateSelected_ = YES; |
| 206 observer_->StopObserving(); |
| 207 observer_->CancelCertificateSelection(); |
| 208 } |
| 187 closePending_ = YES; | 209 closePending_ = YES; |
| 188 overlayWindow_.reset(); | 210 overlayWindow_.reset(); |
| 189 // Closing the sheet using -[NSApp endSheet:] doesn't work so use the private | 211 // Closing the sheet using -[NSApp endSheet:] doesn't work so use the private |
| 190 // method. | 212 // method. |
| 191 [panel_ _dismissWithCode:NSFileHandlingPanelCancelButton]; | 213 [panel_ _dismissWithCode:NSFileHandlingPanelCancelButton]; |
| 192 } | 214 } |
| 193 | 215 |
| 194 - (void)hideSheet { | 216 - (void)hideSheet { |
| 195 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; | 217 NSWindow* sheetWindow = [overlayWindow_ attachedSheet]; |
| 196 [sheetWindow setAlphaValue:0.0]; | 218 [sheetWindow setAlphaValue:0.0]; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 250 } |
| 229 | 251 |
| 230 - (void)onConstrainedWindowClosed { | 252 - (void)onConstrainedWindowClosed { |
| 231 observer_->StopObserving(); | 253 observer_->StopObserving(); |
| 232 panel_.reset(); | 254 panel_.reset(); |
| 233 constrainedWindow_.reset(); | 255 constrainedWindow_.reset(); |
| 234 [self release]; | 256 [self release]; |
| 235 } | 257 } |
| 236 | 258 |
| 237 @end | 259 @end |
| OLD | NEW |