OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/gtk/certificate_manager.h" | 5 #include "chrome/browser/gtk/certificate_manager.h" |
6 | 6 |
7 #include <cert.h> | 7 #include <cert.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #include <pk11pub.h> | 9 #include <pk11pub.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "app/gtk_signal.h" | 14 #include "app/gtk_signal.h" |
15 #include "app/l10n_util.h" | 15 #include "app/l10n_util.h" |
16 #include "app/l10n_util_collator.h" | 16 #include "app/l10n_util_collator.h" |
17 #include "base/i18n/time_formatting.h" | 17 #include "base/i18n/time_formatting.h" |
18 #include "base/nss_util.h" | 18 #include "base/nss_util.h" |
19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
20 #include "chrome/browser/gtk/certificate_viewer.h" | 20 #include "chrome/browser/gtk/certificate_viewer.h" |
21 #include "chrome/browser/gtk/gtk_util.h" | 21 #include "chrome/browser/gtk/gtk_util.h" |
| 22 #include "chrome/browser/pref_member.h" |
| 23 #include "chrome/browser/pref_service.h" |
| 24 #include "chrome/browser/profile.h" |
| 25 #include "chrome/common/pref_names.h" |
22 #include "chrome/third_party/mozilla_security_manager/nsNSSCertHelper.h" | 26 #include "chrome/third_party/mozilla_security_manager/nsNSSCertHelper.h" |
23 #include "chrome/third_party/mozilla_security_manager/nsNSSCertificate.h" | 27 #include "chrome/third_party/mozilla_security_manager/nsNSSCertificate.h" |
24 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
25 | 29 |
26 // PSM = Mozilla's Personal Security Manager. | 30 // PSM = Mozilla's Personal Security Manager. |
27 namespace psm = mozilla_security_manager; | 31 namespace psm = mozilla_security_manager; |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
31 // Convert a char* return value from NSS into a std::string and free the NSS | 35 // Convert a char* return value from NSS into a std::string and free the NSS |
(...skipping 10 matching lines...) Expand all Loading... |
42 //////////////////////////////////////////////////////////////////////////////// | 46 //////////////////////////////////////////////////////////////////////////////// |
43 // CertificatePage class definition. | 47 // CertificatePage class definition. |
44 | 48 |
45 class CertificatePage { | 49 class CertificatePage { |
46 public: | 50 public: |
47 explicit CertificatePage(psm::CertType type); | 51 explicit CertificatePage(psm::CertType type); |
48 | 52 |
49 void PopulateTree(CERTCertList* cert_list); | 53 void PopulateTree(CERTCertList* cert_list); |
50 | 54 |
51 // Get the top-level widget of this page. | 55 // Get the top-level widget of this page. |
52 GtkWidget* widget() { | 56 GtkWidget* widget() { return vbox_; } |
53 return vbox_; | |
54 } | |
55 | 57 |
56 private: | 58 private: |
57 // Columns of the tree store. | 59 // Columns of the tree store. |
58 enum { | 60 enum { |
59 CERT_NAME, | 61 CERT_NAME, |
60 CERT_SECURITY_DEVICE, | 62 CERT_SECURITY_DEVICE, |
61 CERT_SERIAL_NUMBER, | 63 CERT_SERIAL_NUMBER, |
62 CERT_EXPIRES_ON, | 64 CERT_EXPIRES_ON, |
63 CERT_EXPIRES_ON_INT, | 65 CERT_EXPIRES_ON_INT, |
64 CERT_ADDRESS, | 66 CERT_ADDRESS, |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 gtk_tree_model_get(model, &iter, CERT_POINTER, &cert, -1); | 339 gtk_tree_model_get(model, &iter, CERT_POINTER, &cert, -1); |
338 if (cert) | 340 if (cert) |
339 ShowCertificateViewer(GTK_WINDOW(gtk_widget_get_toplevel(widget())), cert); | 341 ShowCertificateViewer(GTK_WINDOW(gtk_widget_get_toplevel(widget())), cert); |
340 } | 342 } |
341 | 343 |
342 //////////////////////////////////////////////////////////////////////////////// | 344 //////////////////////////////////////////////////////////////////////////////// |
343 // CertificateManager class definition. | 345 // CertificateManager class definition. |
344 | 346 |
345 class CertificateManager { | 347 class CertificateManager { |
346 public: | 348 public: |
347 explicit CertificateManager(gfx::NativeWindow parent); | 349 explicit CertificateManager(gfx::NativeWindow parent, Profile* profile); |
348 ~CertificateManager(); | 350 ~CertificateManager(); |
349 | 351 |
350 void Show(); | 352 // Shows the Tab corresponding to the specified |page|. |
| 353 void ShowCertificatePage(CertificateManagerPage page); |
351 | 354 |
352 private: | 355 private: |
| 356 CHROMEGTK_CALLBACK_2(CertificateManager, void, OnSwitchPage, |
| 357 GtkNotebookPage*, guint); |
| 358 |
353 CERTCertList* cert_list_; | 359 CERTCertList* cert_list_; |
354 | 360 |
355 CertificatePage user_page_; | 361 CertificatePage user_page_; |
356 CertificatePage email_page_; | 362 CertificatePage email_page_; |
357 CertificatePage server_page_; | 363 CertificatePage server_page_; |
358 CertificatePage ca_page_; | 364 CertificatePage ca_page_; |
359 CertificatePage unknown_page_; | 365 CertificatePage unknown_page_; |
360 | 366 |
361 GtkWidget* dialog_; | 367 GtkWidget* dialog_; |
| 368 |
| 369 GtkWidget* notebook_; |
| 370 |
| 371 // The last page the user was on when they opened the CertificateManager |
| 372 // window. |
| 373 IntegerPrefMember last_selected_page_; |
| 374 |
| 375 DISALLOW_COPY_AND_ASSIGN(CertificateManager); |
362 }; | 376 }; |
363 | 377 |
364 //////////////////////////////////////////////////////////////////////////////// | 378 //////////////////////////////////////////////////////////////////////////////// |
365 // CertificateManager implementation. | 379 // CertificateManager implementation. |
366 | 380 |
367 void OnDestroy(GtkDialog* dialog, CertificateManager* cert_manager) { | 381 void OnDestroy(GtkDialog* dialog, CertificateManager* cert_manager) { |
368 delete cert_manager; | 382 delete cert_manager; |
369 } | 383 } |
370 | 384 |
371 CertificateManager::CertificateManager(gfx::NativeWindow parent) | 385 CertificateManager::CertificateManager(gfx::NativeWindow parent, |
| 386 Profile* profile) |
372 : user_page_(psm::USER_CERT), | 387 : user_page_(psm::USER_CERT), |
373 email_page_(psm::EMAIL_CERT), | 388 email_page_(psm::EMAIL_CERT), |
374 server_page_(psm::SERVER_CERT), | 389 server_page_(psm::SERVER_CERT), |
375 ca_page_(psm::CA_CERT), | 390 ca_page_(psm::CA_CERT), |
376 unknown_page_(psm::UNKNOWN_CERT) { | 391 unknown_page_(psm::UNKNOWN_CERT) { |
| 392 // We don't need to observe changes in this value. |
| 393 last_selected_page_.Init(prefs::kCertificateManagerWindowLastTabIndex, |
| 394 profile->GetPrefs(), NULL); |
| 395 |
377 dialog_ = gtk_dialog_new_with_buttons( | 396 dialog_ = gtk_dialog_new_with_buttons( |
378 l10n_util::GetStringUTF8(IDS_CERTIFICATE_MANAGER_TITLE).c_str(), | 397 l10n_util::GetStringUTF8(IDS_CERTIFICATE_MANAGER_TITLE).c_str(), |
379 parent, | 398 parent, |
380 // Non-modal. | 399 // Non-modal. |
381 GTK_DIALOG_NO_SEPARATOR, | 400 GTK_DIALOG_NO_SEPARATOR, |
382 GTK_STOCK_CLOSE, | 401 GTK_STOCK_CLOSE, |
383 GTK_RESPONSE_CLOSE, | 402 GTK_RESPONSE_CLOSE, |
384 NULL); | 403 NULL); |
385 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), | 404 gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog_)->vbox), |
386 gtk_util::kContentAreaSpacing); | 405 gtk_util::kContentAreaSpacing); |
387 gtk_window_set_default_size(GTK_WINDOW(dialog_), 600, 440); | 406 gtk_window_set_default_size(GTK_WINDOW(dialog_), 600, 440); |
388 | 407 |
389 GtkWidget* notebook = gtk_notebook_new(); | 408 notebook_ = gtk_notebook_new(); |
390 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook); | 409 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog_)->vbox), notebook_); |
391 | 410 |
392 // TODO(mattm): Remember which page user viewed last. | |
393 gtk_notebook_append_page( | 411 gtk_notebook_append_page( |
394 GTK_NOTEBOOK(notebook), | 412 GTK_NOTEBOOK(notebook_), |
395 user_page_.widget(), | 413 user_page_.widget(), |
396 gtk_label_new_with_mnemonic( | 414 gtk_label_new_with_mnemonic( |
397 l10n_util::GetStringUTF8( | 415 l10n_util::GetStringUTF8( |
398 IDS_CERT_MANAGER_PERSONAL_CERTS_TAB_LABEL).c_str())); | 416 IDS_CERT_MANAGER_PERSONAL_CERTS_TAB_LABEL).c_str())); |
399 | 417 |
400 gtk_notebook_append_page( | 418 gtk_notebook_append_page( |
401 GTK_NOTEBOOK(notebook), | 419 GTK_NOTEBOOK(notebook_), |
402 email_page_.widget(), | 420 email_page_.widget(), |
403 gtk_label_new_with_mnemonic( | 421 gtk_label_new_with_mnemonic( |
404 l10n_util::GetStringUTF8( | 422 l10n_util::GetStringUTF8( |
405 IDS_CERT_MANAGER_OTHER_PEOPLES_CERTS_TAB_LABEL).c_str())); | 423 IDS_CERT_MANAGER_OTHER_PEOPLES_CERTS_TAB_LABEL).c_str())); |
406 | 424 |
407 gtk_notebook_append_page( | 425 gtk_notebook_append_page( |
408 GTK_NOTEBOOK(notebook), | 426 GTK_NOTEBOOK(notebook_), |
409 server_page_.widget(), | 427 server_page_.widget(), |
410 gtk_label_new_with_mnemonic( | 428 gtk_label_new_with_mnemonic( |
411 l10n_util::GetStringUTF8( | 429 l10n_util::GetStringUTF8( |
412 IDS_CERT_MANAGER_SERVER_CERTS_TAB_LABEL).c_str())); | 430 IDS_CERT_MANAGER_SERVER_CERTS_TAB_LABEL).c_str())); |
413 | 431 |
414 gtk_notebook_append_page( | 432 gtk_notebook_append_page( |
415 GTK_NOTEBOOK(notebook), | 433 GTK_NOTEBOOK(notebook_), |
416 ca_page_.widget(), | 434 ca_page_.widget(), |
417 gtk_label_new_with_mnemonic( | 435 gtk_label_new_with_mnemonic( |
418 l10n_util::GetStringUTF8( | 436 l10n_util::GetStringUTF8( |
419 IDS_CERT_MANAGER_CERT_AUTHORITIES_TAB_LABEL).c_str())); | 437 IDS_CERT_MANAGER_CERT_AUTHORITIES_TAB_LABEL).c_str())); |
420 | 438 |
421 gtk_notebook_append_page( | 439 gtk_notebook_append_page( |
422 GTK_NOTEBOOK(notebook), | 440 GTK_NOTEBOOK(notebook_), |
423 unknown_page_.widget(), | 441 unknown_page_.widget(), |
424 gtk_label_new_with_mnemonic( | 442 gtk_label_new_with_mnemonic( |
425 l10n_util::GetStringUTF8( | 443 l10n_util::GetStringUTF8( |
426 IDS_CERT_MANAGER_UNKNOWN_TAB_LABEL).c_str())); | 444 IDS_CERT_MANAGER_UNKNOWN_TAB_LABEL).c_str())); |
427 | 445 |
428 cert_list_ = PK11_ListCerts(PK11CertListUnique, NULL); | 446 cert_list_ = PK11_ListCerts(PK11CertListUnique, NULL); |
429 user_page_.PopulateTree(cert_list_); | 447 user_page_.PopulateTree(cert_list_); |
430 email_page_.PopulateTree(cert_list_); | 448 email_page_.PopulateTree(cert_list_); |
431 server_page_.PopulateTree(cert_list_); | 449 server_page_.PopulateTree(cert_list_); |
432 ca_page_.PopulateTree(cert_list_); | 450 ca_page_.PopulateTree(cert_list_); |
433 unknown_page_.PopulateTree(cert_list_); | 451 unknown_page_.PopulateTree(cert_list_); |
434 | 452 |
| 453 // Need to show the notebook before connecting switch-page signal, otherwise |
| 454 // we'll immediately get a signal switching to page 0 and overwrite our |
| 455 // last_selected_page_ value. |
| 456 gtk_util::ShowDialogWithLocalizedSize(dialog_, -1, -1, true); |
| 457 |
| 458 g_signal_connect(notebook_, "switch-page", |
| 459 G_CALLBACK(OnSwitchPageThunk), this); |
| 460 |
435 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); | 461 g_signal_connect(dialog_, "response", G_CALLBACK(gtk_widget_destroy), NULL); |
436 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnDestroy), this); | 462 g_signal_connect(dialog_, "destroy", G_CALLBACK(OnDestroy), this); |
437 } | 463 } |
438 | 464 |
439 CertificateManager::~CertificateManager() { | 465 CertificateManager::~CertificateManager() { |
440 CERT_DestroyCertList(cert_list_); | 466 CERT_DestroyCertList(cert_list_); |
441 } | 467 } |
442 | 468 |
443 void CertificateManager::Show() { | 469 void CertificateManager::OnSwitchPage(GtkWidget* notebook, |
444 gtk_util::ShowDialog(dialog_); | 470 GtkNotebookPage* page, |
| 471 guint page_num) { |
| 472 int index = static_cast<int>(page_num); |
| 473 DCHECK(index > PAGE_DEFAULT && index < PAGE_COUNT); |
| 474 last_selected_page_.SetValue(index); |
445 } | 475 } |
446 | 476 |
447 } // namespace | 477 void CertificateManager::ShowCertificatePage(CertificateManagerPage page) { |
| 478 // Bring options window to front if it already existed and isn't already |
| 479 // in front |
| 480 gtk_window_present_with_time(GTK_WINDOW(dialog_), |
| 481 gtk_get_current_event_time()); |
448 | 482 |
449 void ShowCertificateManager(gfx::NativeWindow parent) { | 483 if (page == PAGE_DEFAULT) { |
| 484 // Remember the last visited page from local state. |
| 485 page = static_cast<CertificateManagerPage>(last_selected_page_.GetValue()); |
| 486 if (page == PAGE_DEFAULT) |
| 487 page = PAGE_USER; |
| 488 } |
| 489 // If the page number is out of bounds, reset to the first tab. |
| 490 if (page < 0 || page >= gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook_))) |
| 491 page = PAGE_USER; |
| 492 |
| 493 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook_), page); |
| 494 } |
| 495 |
| 496 } // namespace |
| 497 |
| 498 namespace certificate_manager_util { |
| 499 |
| 500 void RegisterUserPrefs(PrefService* prefs) { |
| 501 prefs->RegisterIntegerPref(prefs::kCertificateManagerWindowLastTabIndex, 0); |
| 502 } |
| 503 |
| 504 } // namespace certificate_manager_util |
| 505 |
| 506 void ShowCertificateManager(gfx::NativeWindow parent, Profile* profile, |
| 507 CertificateManagerPage page) { |
450 base::EnsureNSSInit(); | 508 base::EnsureNSSInit(); |
451 (new CertificateManager(parent))->Show(); | 509 CertificateManager* manager = new CertificateManager(parent, profile); |
| 510 manager->ShowCertificatePage(page); |
452 } | 511 } |
OLD | NEW |