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
3 // found in the LICENSE file.
5 #include "ssl_add_cert_handler.h"
7 #include <SecurityInterface/SFCertificatePanel.h>
8 #include <SecurityInterface/SFCertificateView.h>
10 #include "base/logging.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
14 #include "chrome/common/logging_chrome.h"
15 #include "grit/generated_resources.h"
16 #include "net/cert/x509_certificate.h"
17 #include "ui/base/l10n/l10n_util_mac.h"
19 @interface SSLAddCertHandlerCocoa : NSObject
21 scoped_refptr<SSLAddCertHandler> handler_;
24 - (id)initWithHandler:(SSLAddCertHandler*)handler;
29 void SSLAddCertHandler::AskToAddCert() {
30 [[[SSLAddCertHandlerCocoa alloc] initWithHandler: this] askToAddCert];
31 // The new object will release itself when the sheet ends.
35 // The actual implementation of the add-client-cert handler is an Obj-C class.
36 @implementation SSLAddCertHandlerCocoa
38 - (id)initWithHandler:(SSLAddCertHandler*)handler {
39 DCHECK(handler && handler->cert());
47 - (void)sheetDidEnd:(SFCertificatePanel*)panel
48 returnCode:(NSInteger)returnCode
49 context:(void*)context {
50 [panel orderOut:self];
52 handler_->Finished(returnCode == NSOKButton);
56 - (void)askToAddCert {
57 NSWindow* parentWindow = NULL;
58 Browser* browser = chrome::GetLastActiveBrowser();
59 // TODO(snej): Can I get the Browser that issued the request?
61 parentWindow = browser->window()->GetNativeWindow();
62 if ([parentWindow attachedSheet])
66 // Create the cert panel, which will be released in my -sheetDidEnd: method.
67 SFCertificatePanel* panel = [[SFCertificatePanel alloc] init];
68 [panel setDefaultButtonTitle:l10n_util::GetNSString(IDS_ADD_CERT_DIALOG_ADD)];
69 [panel setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
70 SecCertificateRef cert = handler_->cert()->os_cert_handle();
71 NSArray* certs = [NSArray arrayWithObject: (id)cert];
74 // Open the cert panel as a sheet on the browser window.
75 [panel beginSheetForWindow:parentWindow
77 didEndSelector:@selector(sheetDidEnd:returnCode:context:)
82 // No available browser window, so run independently as a (blocking) dialog.
83 int returnCode = [panel runModalForCertificates:certs showGroup:NO];
84 [self sheetDidEnd:panel returnCode:returnCode context:NULL];