| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "chrome/browser/ui/views/signed_certificate_timestamps_views.h" | |
| 6 | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "base/strings/stringprintf.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/ui/views/signed_certificate_timestamp_info_view.h" | |
| 12 #include "chrome/grit/generated_resources.h" | |
| 13 #include "components/constrained_window/constrained_window_views.h" | |
| 14 #include "content/public/browser/notification_source.h" | |
| 15 #include "content/public/browser/signed_certificate_timestamp_store.h" | |
| 16 #include "content/public/common/signed_certificate_timestamp_id_and_status.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 #include "ui/base/models/combobox_model.h" | |
| 19 #include "ui/views/controls/combobox/combobox.h" | |
| 20 #include "ui/views/layout/grid_layout.h" | |
| 21 #include "ui/views/layout/layout_constants.h" | |
| 22 #include "ui/views/widget/widget.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void SignedCertificateTimestampIDsToList( | |
| 27 const content::SignedCertificateTimestampIDStatusList& sct_ids_list, | |
| 28 net::SignedCertificateTimestampAndStatusList* sct_list) { | |
| 29 for (content::SignedCertificateTimestampIDStatusList::const_iterator it = | |
| 30 sct_ids_list.begin(); | |
| 31 it != sct_ids_list.end(); | |
| 32 ++it) { | |
| 33 scoped_refptr<net::ct::SignedCertificateTimestamp> sct; | |
| 34 content::SignedCertificateTimestampStore::GetInstance()->Retrieve(it->id, | |
| 35 &sct); | |
| 36 sct_list->push_back( | |
| 37 net::SignedCertificateTimestampAndStatus(sct, it->status)); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 namespace chrome { | |
| 44 | |
| 45 void ShowSignedCertificateTimestampsViewer( | |
| 46 content::WebContents* web_contents, | |
| 47 const content::SignedCertificateTimestampIDStatusList& sct_ids_list) { | |
| 48 net::SignedCertificateTimestampAndStatusList sct_list; | |
| 49 SignedCertificateTimestampIDsToList(sct_ids_list, &sct_list); | |
| 50 new SignedCertificateTimestampsViews(web_contents, sct_list); | |
| 51 } | |
| 52 | |
| 53 } // namespace chrome | |
| 54 | |
| 55 class SCTListModel : public ui::ComboboxModel { | |
| 56 public: | |
| 57 explicit SCTListModel( | |
| 58 const net::SignedCertificateTimestampAndStatusList& sct_list); | |
| 59 ~SCTListModel() override; | |
| 60 | |
| 61 // Overridden from ui::ComboboxModel: | |
| 62 int GetItemCount() const override; | |
| 63 base::string16 GetItemAt(int index) override; | |
| 64 | |
| 65 private: | |
| 66 net::SignedCertificateTimestampAndStatusList sct_list_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(SCTListModel); | |
| 69 }; | |
| 70 | |
| 71 SCTListModel::SCTListModel( | |
| 72 const net::SignedCertificateTimestampAndStatusList& sct_list) | |
| 73 : sct_list_(sct_list) {} | |
| 74 | |
| 75 SCTListModel::~SCTListModel() {} | |
| 76 | |
| 77 int SCTListModel::GetItemCount() const { return sct_list_.size(); } | |
| 78 | |
| 79 base::string16 SCTListModel::GetItemAt(int index) { | |
| 80 DCHECK_LT(static_cast<size_t>(index), sct_list_.size()); | |
| 81 std::string origin = l10n_util::GetStringUTF8( | |
| 82 chrome::ct::SCTOriginToResourceID(*(sct_list_[index].sct.get()))); | |
| 83 | |
| 84 std::string status = l10n_util::GetStringUTF8( | |
| 85 chrome::ct::StatusToResourceID(sct_list_[index].status)); | |
| 86 | |
| 87 // This formatting string may be internationalized for RTL, etc. | |
| 88 return l10n_util::GetStringFUTF16(IDS_SCT_CHOOSER_FORMAT, | |
| 89 base::IntToString16(index + 1), | |
| 90 base::UTF8ToUTF16(origin), | |
| 91 base::UTF8ToUTF16(status)); | |
| 92 } | |
| 93 | |
| 94 SignedCertificateTimestampsViews::SignedCertificateTimestampsViews( | |
| 95 content::WebContents* web_contents, | |
| 96 const net::SignedCertificateTimestampAndStatusList& sct_list) | |
| 97 : sct_info_view_(NULL), | |
| 98 sct_list_(sct_list) { | |
| 99 constrained_window::ShowWebModalDialogViews(this, web_contents); | |
| 100 } | |
| 101 | |
| 102 SignedCertificateTimestampsViews::~SignedCertificateTimestampsViews() {} | |
| 103 | |
| 104 base::string16 SignedCertificateTimestampsViews::GetWindowTitle() const { | |
| 105 return l10n_util::GetStringUTF16(IDS_SCT_VIEWER_TITLE); | |
| 106 } | |
| 107 | |
| 108 int SignedCertificateTimestampsViews::GetDialogButtons() const { | |
| 109 return ui::DIALOG_BUTTON_CANCEL; | |
| 110 } | |
| 111 | |
| 112 ui::ModalType SignedCertificateTimestampsViews::GetModalType() const { | |
| 113 return ui::MODAL_TYPE_CHILD; | |
| 114 } | |
| 115 | |
| 116 void SignedCertificateTimestampsViews::OnPerformAction( | |
| 117 views::Combobox* combobox) { | |
| 118 DCHECK_EQ(combobox, sct_selector_box_.get()); | |
| 119 DCHECK_LT(combobox->selected_index(), sct_list_model_->GetItemCount()); | |
| 120 ShowSCTInfo(combobox->selected_index()); | |
| 121 } | |
| 122 | |
| 123 void SignedCertificateTimestampsViews::ViewHierarchyChanged( | |
| 124 const ViewHierarchyChangedDetails& details) { | |
| 125 views::DialogDelegateView::ViewHierarchyChanged(details); | |
| 126 if (details.is_add && details.child == this) | |
| 127 Init(); | |
| 128 } | |
| 129 | |
| 130 void SignedCertificateTimestampsViews::Init() { | |
| 131 views::GridLayout* layout = views::GridLayout::CreatePanel(this); | |
| 132 SetLayoutManager(layout); | |
| 133 | |
| 134 const int kSelectorBoxLayoutId = 0; | |
| 135 views::ColumnSet* column_set = layout->AddColumnSet(kSelectorBoxLayoutId); | |
| 136 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 137 views::GridLayout::USE_PREF, 0, 0); | |
| 138 | |
| 139 layout->StartRow(0, kSelectorBoxLayoutId); | |
| 140 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 141 | |
| 142 // Add SCT selector drop-down list. | |
| 143 layout->StartRow(0, kSelectorBoxLayoutId); | |
| 144 sct_list_model_.reset(new SCTListModel(sct_list_)); | |
| 145 sct_selector_box_.reset(new views::Combobox(sct_list_model_.get())); | |
| 146 sct_selector_box_->set_listener(this); | |
| 147 sct_selector_box_->set_owned_by_client(); | |
| 148 layout->AddView(sct_selector_box_.get()); | |
| 149 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); | |
| 150 | |
| 151 // Add the SCT info view, displaying information about a specific SCT. | |
| 152 layout->StartRow(0, kSelectorBoxLayoutId); | |
| 153 sct_info_view_ = new SignedCertificateTimestampInfoView(); | |
| 154 layout->AddView(sct_info_view_); | |
| 155 | |
| 156 sct_info_view_->SetSignedCertificateTimestamp(*(sct_list_[0].sct.get()), | |
| 157 sct_list_[0].status); | |
| 158 } | |
| 159 | |
| 160 void SignedCertificateTimestampsViews::ShowSCTInfo(int sct_index) { | |
| 161 if ((sct_index < 0) || (static_cast<size_t>(sct_index) > sct_list_.size())) | |
| 162 return; | |
| 163 | |
| 164 sct_info_view_->SetSignedCertificateTimestamp( | |
| 165 *(sct_list_[sct_index].sct.get()), sct_list_[sct_index].status); | |
| 166 } | |
| 167 | |
| 168 void SignedCertificateTimestampsViews::Observe( | |
| 169 int type, | |
| 170 const content::NotificationSource& source, | |
| 171 const content::NotificationDetails& details) { | |
| 172 GetWidget()->Close(); | |
| 173 } | |
| OLD | NEW |