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 #include "chrome/browser/ui/certificate_dialogs.h" | 5 #include "chrome/browser/ui/certificate_dialogs.h" |
6 | 6 |
7 | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "chrome/browser/ui/chrome_select_file_policy.h" | 15 #include "chrome/browser/ui/chrome_select_file_policy.h" |
16 #include "chrome/common/net/x509_certificate_model.h" | 16 #include "chrome/common/net/x509_certificate_model.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 return "-----BEGIN CERTIFICATE-----\r\n" + | 58 return "-----BEGIN CERTIFICATE-----\r\n" + |
59 WrapAt64(base64) + | 59 WrapAt64(base64) + |
60 "-----END CERTIFICATE-----\r\n"; | 60 "-----END CERTIFICATE-----\r\n"; |
61 } | 61 } |
62 | 62 |
63 //////////////////////////////////////////////////////////////////////////////// | 63 //////////////////////////////////////////////////////////////////////////////// |
64 // General utility functions. | 64 // General utility functions. |
65 | 65 |
66 class Exporter : public ui::SelectFileDialog::Listener { | 66 class Exporter : public ui::SelectFileDialog::Listener { |
67 public: | 67 public: |
68 Exporter(WebContents* web_contents, gfx::NativeWindow parent, | 68 Exporter(WebContents* web_contents, |
69 net::X509Certificate::OSCertHandle cert); | 69 gfx::NativeWindow parent, |
| 70 net::X509Certificate::OSCertHandles::iterator certs_begin, |
| 71 net::X509Certificate::OSCertHandles::iterator certs_end); |
70 virtual ~Exporter(); | 72 virtual ~Exporter(); |
71 | 73 |
72 // SelectFileDialog::Listener implemenation. | 74 // SelectFileDialog::Listener implemenation. |
73 virtual void FileSelected(const base::FilePath& path, | 75 virtual void FileSelected(const base::FilePath& path, |
74 int index, void* params) OVERRIDE; | 76 int index, void* params) OVERRIDE; |
75 virtual void FileSelectionCanceled(void* params) OVERRIDE; | 77 virtual void FileSelectionCanceled(void* params) OVERRIDE; |
76 private: | 78 private: |
77 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | 79 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
78 | 80 |
79 // The certificate hierarchy (leaf cert first). | 81 // The certificate hierarchy (leaf cert first). |
80 net::X509Certificate::OSCertHandles cert_chain_list_; | 82 net::X509Certificate::OSCertHandles cert_chain_list_; |
81 }; | 83 }; |
82 | 84 |
83 Exporter::Exporter(WebContents* web_contents, | 85 Exporter::Exporter(WebContents* web_contents, |
84 gfx::NativeWindow parent, | 86 gfx::NativeWindow parent, |
85 net::X509Certificate::OSCertHandle cert) | 87 net::X509Certificate::OSCertHandles::iterator certs_begin, |
| 88 net::X509Certificate::OSCertHandles::iterator certs_end) |
86 : select_file_dialog_(ui::SelectFileDialog::Create( | 89 : select_file_dialog_(ui::SelectFileDialog::Create( |
87 this, new ChromeSelectFilePolicy(web_contents))) { | 90 this, |
88 x509_certificate_model::GetCertChainFromCert(cert, &cert_chain_list_); | 91 new ChromeSelectFilePolicy(web_contents))) { |
| 92 DCHECK(certs_begin != certs_end); |
| 93 for (net::X509Certificate::OSCertHandles::iterator i = certs_begin; |
| 94 i != certs_end; |
| 95 ++i) { |
| 96 cert_chain_list_.push_back(net::X509Certificate::DupOSCertHandle(*i)); |
| 97 } |
89 | 98 |
90 // TODO(mattm): should this default to some directory? | 99 // TODO(mattm): should this default to some directory? |
91 // Maybe SavePackage::GetSaveDirPreference? (Except that it's private.) | 100 // Maybe SavePackage::GetSaveDirPreference? (Except that it's private.) |
92 std::string cert_title = x509_certificate_model::GetTitle(cert); | 101 std::string cert_title = x509_certificate_model::GetTitle(*certs_begin); |
93 base::FilePath suggested_path = | 102 base::FilePath suggested_path = |
94 net::GenerateFileName(GURL::EmptyGURL(), // url | 103 net::GenerateFileName(GURL::EmptyGURL(), // url |
95 std::string(), // content_disposition | 104 std::string(), // content_disposition |
96 std::string(), // referrer_charset | 105 std::string(), // referrer_charset |
97 cert_title, // suggested_name | 106 cert_title, // suggested_name |
98 std::string(), // mime_type | 107 std::string(), // mime_type |
99 "certificate"); // default_name | 108 "certificate"); // default_name |
100 | 109 |
101 ShowCertSelectFileDialog(select_file_dialog_.get(), | 110 ShowCertSelectFileDialog(select_file_dialog_.get(), |
102 ui::SelectFileDialog::SELECT_SAVEAS_FILE, | 111 ui::SelectFileDialog::SELECT_SAVEAS_FILE, |
103 suggested_path, | 112 suggested_path, |
104 parent, | 113 parent, |
105 NULL); | 114 NULL); |
106 } | 115 } |
107 | 116 |
108 Exporter::~Exporter() { | 117 Exporter::~Exporter() { |
109 // There may be pending file dialogs, we need to tell them that we've gone | 118 // There may be pending file dialogs, we need to tell them that we've gone |
110 // away so they don't try and call back to us. | 119 // away so they don't try and call back to us. |
111 if (select_file_dialog_.get()) | 120 if (select_file_dialog_.get()) |
112 select_file_dialog_->ListenerDestroyed(); | 121 select_file_dialog_->ListenerDestroyed(); |
113 | 122 |
114 x509_certificate_model::DestroyCertChain(&cert_chain_list_); | 123 std::for_each(cert_chain_list_.begin(), |
| 124 cert_chain_list_.end(), |
| 125 &net::X509Certificate::FreeOSCertHandle); |
115 } | 126 } |
116 | 127 |
117 void Exporter::FileSelected(const base::FilePath& path, int index, | 128 void Exporter::FileSelected(const base::FilePath& path, int index, |
118 void* params) { | 129 void* params) { |
119 std::string data; | 130 std::string data; |
120 switch (index) { | 131 switch (index) { |
121 case 2: | 132 case 2: |
122 for (size_t i = 0; i < cert_chain_list_.size(); ++i) | 133 for (size_t i = 0; i < cert_chain_list_.size(); ++i) |
123 data += GetBase64String(cert_chain_list_[i]); | 134 data += GetBase64String(cert_chain_list_[i]); |
124 break; | 135 break; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 l10n_util::GetStringUTF16(IDS_CERT_EXPORT_TYPE_PKCS7_CHAIN)); | 187 l10n_util::GetStringUTF16(IDS_CERT_EXPORT_TYPE_PKCS7_CHAIN)); |
177 file_type_info.include_all_files = true; | 188 file_type_info.include_all_files = true; |
178 select_file_dialog->SelectFile( | 189 select_file_dialog->SelectFile( |
179 type, base::string16(), | 190 type, base::string16(), |
180 suggested_path, &file_type_info, | 191 suggested_path, &file_type_info, |
181 1, // 1-based index for |file_type_info.extensions| to specify default. | 192 1, // 1-based index for |file_type_info.extensions| to specify default. |
182 FILE_PATH_LITERAL("crt"), | 193 FILE_PATH_LITERAL("crt"), |
183 parent, params); | 194 parent, params); |
184 } | 195 } |
185 | 196 |
186 void ShowCertExportDialog(WebContents* web_contents, | 197 void ShowCertExportDialog( |
187 gfx::NativeWindow parent, | 198 content::WebContents* web_contents, |
188 net::X509Certificate::OSCertHandle cert) { | 199 gfx::NativeWindow parent, |
189 new Exporter(web_contents, parent, cert); | 200 net::X509Certificate::OSCertHandles::iterator certs_begin, |
| 201 net::X509Certificate::OSCertHandles::iterator certs_end) { |
| 202 new Exporter(web_contents, parent, certs_begin, certs_end); |
190 } | 203 } |
OLD | NEW |