summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ssl/ssl_add_cert_handler_mac.mm
blob: 0f79a1c42a4111a21fd2f57a2e041f5612c92ea7 (plain)
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
// Copyright (c) 2010 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.

#include "ssl_add_cert_handler.h"

#include <SecurityInterface/SFCertificatePanel.h>
#include <SecurityInterface/SFCertificateView.h>

#include "app/l10n_util_mac.h"
#include "base/logging.h"
#include "base/scoped_nsobject.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_window.h"
#include "grit/generated_resources.h"
#include "net/base/x509_certificate.h"

@interface SSLAddCertHandlerCocoa : NSObject
{
  scoped_refptr<SSLAddCertHandler> handler_;
}

- (id)initWithHandler:(SSLAddCertHandler*)handler;
- (void)askToAddCert;
@end


void SSLAddCertHandler::AskToAddCert() {
  [[[SSLAddCertHandlerCocoa alloc] initWithHandler: this] askToAddCert];
  // The new object will release itself when the sheet ends.
}


// The actual implementation of the add-client-cert handler is an Obj-C class.
@implementation SSLAddCertHandlerCocoa

- (id)initWithHandler:(SSLAddCertHandler*)handler {
  DCHECK(handler && handler->cert());
  self = [super init];
  if (self) {
    handler_ = handler;
  }
  return self;
}

- (void)sheetDidEnd:(SFCertificatePanel*)panel
         returnCode:(NSInteger)returnCode
            context:(void*)context {
  [panel orderOut:self];
  [panel autorelease];
  handler_->Finished(returnCode == NSOKButton);
  [self release];
}

- (void)askToAddCert {
  NSWindow* parentWindow = NULL;
  Browser* browser = BrowserList::GetLastActive();
  // TODO(snej): Can I get the Browser that issued the request?
  if (browser) {
    parentWindow = browser->window()->GetNativeHandle();
    if ([parentWindow attachedSheet])
      parentWindow = nil;
  }

  // Create the cert panel, which will be released in my -sheetDidEnd: method.
  SFCertificatePanel* panel = [[SFCertificatePanel alloc] init];
  [panel setDefaultButtonTitle:l10n_util::GetNSString(IDS_ADD_CERT_DIALOG_ADD)];
  [panel setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
  SecCertificateRef cert = handler_->cert()->os_cert_handle();
  NSArray* certs = [NSArray arrayWithObject: (id)cert];

  if (parentWindow) {
    // Open the cert panel as a sheet on the browser window.
    [panel beginSheetForWindow:parentWindow
                 modalDelegate:self
                didEndSelector:@selector(sheetDidEnd:returnCode:context:)
                   contextInfo:NULL
                  certificates:certs
                     showGroup:NO];
  } else {
    // No available browser window, so run independently as a (blocking) dialog.
    int returnCode = [panel runModalForCertificates:certs showGroup:NO];
    [self sheetDidEnd:panel returnCode:returnCode context:NULL];
  }
}

@end