summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ssl/ssl_add_cert_handler.cc
blob: 8c75d7d36b980e9bbab3b2735f04988aa9750663 (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
// 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 "chrome/browser/ssl/ssl_add_cert_handler.h"

#include "app/l10n_util.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/common/platform_util.h"
#include "grit/generated_resources.h"
#include "net/base/cert_database.h"
#include "net/base/net_errors.h"
#include "net/base/x509_certificate.h"
#include "net/url_request/url_request.h"

SSLAddCertHandler::SSLAddCertHandler(URLRequest* request,
                                     net::X509Certificate* cert)
    : cert_(cert) {
  // Stay alive until the UI completes and Finished() is called.
  AddRef();
  ChromeThread::PostTask(
      ChromeThread::UI, FROM_HERE,
      NewRunnableMethod(this, &SSLAddCertHandler::RunUI));
}

void SSLAddCertHandler::RunUI() {
  int cert_error;
  {
    net::CertDatabase db;
    cert_error = db.CheckUserCert(cert_);
  }
  if (cert_error != net::OK) {
    // TODO(snej): Map cert_error to a more specific error message.
    ShowError(l10n_util::GetStringUTF16(IDS_ADD_CERT_ERR_INVALID_CERT));
    Finished(false);
    return;
  }
  AskToAddCert();
}

#if !defined(OS_MACOSX)
void SSLAddCertHandler::AskToAddCert() {
  // TODO(snej): Someone should add Windows and GTK implementations with UI.
  Finished(true);
}
#endif

void SSLAddCertHandler::Finished(bool add_cert) {
  if (add_cert) {
    net::CertDatabase db;
    int cert_error = db.AddUserCert(cert_);
    if (cert_error != net::OK) {
      // TODO(snej): Map cert_error to a more specific error message.
      ShowError(l10n_util::GetStringUTF16(IDS_ADD_CERT_ERR_FAILED));
    }
  }
  Release();
}

void SSLAddCertHandler::ShowError(const string16& error) {
  Browser* browser = BrowserList::GetLastActive();
  platform_util::SimpleErrorBox(
      browser ? browser->window()->GetNativeHandle() : NULL,
      l10n_util::GetStringUTF16(IDS_ADD_CERT_FAILURE_TITLE),
      error);
}