• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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#include "ssl_add_cert_handler.h"
6
7#include <SecurityInterface/SFCertificatePanel.h>
8#include <SecurityInterface/SFCertificateView.h>
9
10#include "base/logging.h"
11#include "base/memory/scoped_nsobject.h"
12#include "chrome/common/logging_chrome.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/browser_list.h"
15#include "chrome/browser/ui/browser_window.h"
16#include "grit/generated_resources.h"
17#include "net/base/x509_certificate.h"
18#include "ui/base/l10n/l10n_util_mac.h"
19
20@interface SSLAddCertHandlerCocoa : NSObject
21{
22  scoped_refptr<SSLAddCertHandler> handler_;
23}
24
25- (id)initWithHandler:(SSLAddCertHandler*)handler;
26- (void)askToAddCert;
27@end
28
29
30void SSLAddCertHandler::AskToAddCert() {
31  [[[SSLAddCertHandlerCocoa alloc] initWithHandler: this] askToAddCert];
32  // The new object will release itself when the sheet ends.
33}
34
35
36// The actual implementation of the add-client-cert handler is an Obj-C class.
37@implementation SSLAddCertHandlerCocoa
38
39- (id)initWithHandler:(SSLAddCertHandler*)handler {
40  DCHECK(handler && handler->cert());
41  self = [super init];
42  if (self) {
43    handler_ = handler;
44  }
45  return self;
46}
47
48- (void)sheetDidEnd:(SFCertificatePanel*)panel
49         returnCode:(NSInteger)returnCode
50            context:(void*)context {
51  [panel orderOut:self];
52  [panel autorelease];
53  handler_->Finished(returnCode == NSOKButton);
54  [self release];
55}
56
57- (void)askToAddCert {
58  NSWindow* parentWindow = NULL;
59  Browser* browser = BrowserList::GetLastActive();
60  // TODO(snej): Can I get the Browser that issued the request?
61  if (browser) {
62    parentWindow = browser->window()->GetNativeHandle();
63    if ([parentWindow attachedSheet])
64      parentWindow = nil;
65  }
66
67  // Create the cert panel, which will be released in my -sheetDidEnd: method.
68  SFCertificatePanel* panel = [[SFCertificatePanel alloc] init];
69  [panel setDefaultButtonTitle:l10n_util::GetNSString(IDS_ADD_CERT_DIALOG_ADD)];
70  [panel setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
71  SecCertificateRef cert = handler_->cert()->os_cert_handle();
72  NSArray* certs = [NSArray arrayWithObject: (id)cert];
73
74  if (parentWindow) {
75    // Open the cert panel as a sheet on the browser window.
76    [panel beginSheetForWindow:parentWindow
77                 modalDelegate:self
78                didEndSelector:@selector(sheetDidEnd:returnCode:context:)
79                   contextInfo:NULL
80                  certificates:certs
81                     showGroup:NO];
82  } else {
83    // No available browser window, so run independently as a (blocking) dialog.
84    int returnCode = [panel runModalForCertificates:certs showGroup:NO];
85    [self sheetDidEnd:panel returnCode:returnCode context:NULL];
86  }
87}
88
89@end
90