[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / signed_certificate_timestamps_views.cc
blob89c55986b92897842064da3aebc52589124dc18e
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.
5 #include "chrome/browser/ui/views/signed_certificate_timestamps_views.h"
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/constrained_window_views.h"
12 #include "chrome/browser/ui/views/signed_certificate_timestamp_info_view.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #include "components/web_modal/web_contents_modal_dialog_manager.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
16 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/signed_certificate_timestamp_store.h"
18 #include "content/public/common/signed_certificate_timestamp_id_and_status.h"
19 #include "grit/generated_resources.h"
20 #include "net/cert/signed_certificate_timestamp.h"
21 #include "net/ssl/signed_certificate_timestamp_and_status.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/models/combobox_model.h"
24 #include "ui/views/controls/combobox/combobox.h"
25 #include "ui/views/layout/grid_layout.h"
26 #include "ui/views/layout/layout_constants.h"
27 #include "ui/views/widget/widget.h"
29 using web_modal::WebContentsModalDialogManager;
30 using web_modal::WebContentsModalDialogManagerDelegate;
31 using views::GridLayout;
33 namespace {
35 void SignedCertificateTimestampIDsToList(
36 const content::SignedCertificateTimestampIDStatusList& sct_ids_list,
37 net::SignedCertificateTimestampAndStatusList* sct_list) {
38 for (content::SignedCertificateTimestampIDStatusList::const_iterator it =
39 sct_ids_list.begin();
40 it != sct_ids_list.end();
41 ++it) {
42 scoped_refptr<net::ct::SignedCertificateTimestamp> sct;
43 content::SignedCertificateTimestampStore::GetInstance()->Retrieve(it->id,
44 &sct);
45 sct_list->push_back(
46 net::SignedCertificateTimestampAndStatus(sct, it->status));
50 } // namespace
52 namespace chrome {
54 void ShowSignedCertificateTimestampsViewer(
55 content::WebContents* web_contents,
56 const content::SignedCertificateTimestampIDStatusList& sct_ids_list) {
57 net::SignedCertificateTimestampAndStatusList sct_list;
58 SignedCertificateTimestampIDsToList(sct_ids_list, &sct_list);
59 new SignedCertificateTimestampsViews(web_contents, sct_list);
62 } // namespace chrome
64 class SCTListModel : public ui::ComboboxModel {
65 public:
66 explicit SCTListModel(
67 const net::SignedCertificateTimestampAndStatusList& sct_list);
68 virtual ~SCTListModel();
70 // Overridden from ui::ComboboxModel:
71 virtual int GetItemCount() const OVERRIDE;
72 virtual base::string16 GetItemAt(int index) OVERRIDE;
74 private:
75 net::SignedCertificateTimestampAndStatusList sct_list_;
77 DISALLOW_COPY_AND_ASSIGN(SCTListModel);
80 SCTListModel::SCTListModel(
81 const net::SignedCertificateTimestampAndStatusList& sct_list)
82 : sct_list_(sct_list) {}
84 SCTListModel::~SCTListModel() {}
86 int SCTListModel::GetItemCount() const { return sct_list_.size(); }
88 base::string16 SCTListModel::GetItemAt(int index) {
89 DCHECK_LT(static_cast<size_t>(index), sct_list_.size());
90 std::string origin = l10n_util::GetStringUTF8(
91 chrome::ct::SCTOriginToResourceID(*(sct_list_[index].sct)));
93 std::string status = l10n_util::GetStringUTF8(
94 chrome::ct::StatusToResourceID(sct_list_[index].status));
96 // This formatting string may be internationalized for RTL, etc.
97 return l10n_util::GetStringFUTF16(IDS_SCT_CHOOSER_FORMAT,
98 base::IntToString16(index + 1),
99 base::UTF8ToUTF16(origin),
100 base::UTF8ToUTF16(status));
103 SignedCertificateTimestampsViews::SignedCertificateTimestampsViews(
104 content::WebContents* web_contents,
105 const net::SignedCertificateTimestampAndStatusList& sct_list)
106 : sct_info_view_(NULL),
107 sct_list_(sct_list) {
108 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
109 WebContentsModalDialogManager::FromWebContents(web_contents);
110 WebContentsModalDialogManagerDelegate* modal_delegate =
111 web_contents_modal_dialog_manager->delegate();
112 DCHECK(modal_delegate);
113 views::Widget* window = views::Widget::CreateWindowAsFramelessChild(
114 this, modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
115 web_contents_modal_dialog_manager->ShowModalDialog(
116 window->GetNativeView());
119 SignedCertificateTimestampsViews::~SignedCertificateTimestampsViews() {}
121 base::string16 SignedCertificateTimestampsViews::GetWindowTitle() const {
122 return l10n_util::GetStringUTF16(IDS_SCT_VIEWER_TITLE);
125 int SignedCertificateTimestampsViews::GetDialogButtons() const {
126 return ui::DIALOG_BUTTON_CANCEL;
129 ui::ModalType SignedCertificateTimestampsViews::GetModalType() const {
130 #if defined(USE_ASH)
131 return ui::MODAL_TYPE_CHILD;
132 #else
133 return views::WidgetDelegate::GetModalType();
134 #endif
137 void SignedCertificateTimestampsViews::OnPerformAction(
138 views::Combobox* combobox) {
139 DCHECK_EQ(combobox, sct_selector_box_.get());
140 DCHECK_LT(combobox->selected_index(), sct_list_model_->GetItemCount());
141 ShowSCTInfo(combobox->selected_index());
144 void SignedCertificateTimestampsViews::ViewHierarchyChanged(
145 const ViewHierarchyChangedDetails& details) {
146 if (details.is_add && details.child == this)
147 Init();
150 void SignedCertificateTimestampsViews::Init() {
151 GridLayout* layout = GridLayout::CreatePanel(this);
152 SetLayoutManager(layout);
154 const int kSelectorBoxLayoutId = 0;
155 views::ColumnSet* column_set = layout->AddColumnSet(kSelectorBoxLayoutId);
156 column_set->AddColumn(
157 GridLayout::FILL, GridLayout::FILL, 1, GridLayout::USE_PREF, 0, 0);
159 layout->StartRow(0, kSelectorBoxLayoutId);
160 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
162 // Add SCT selector drop-down list.
163 layout->StartRow(0, kSelectorBoxLayoutId);
164 sct_list_model_.reset(new SCTListModel(sct_list_));
165 sct_selector_box_.reset(new views::Combobox(sct_list_model_.get()));
166 sct_selector_box_->set_listener(this);
167 sct_selector_box_->set_owned_by_client();
168 layout->AddView(sct_selector_box_.get());
169 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
171 // Add the SCT info view, displaying information about a specific SCT.
172 layout->StartRow(0, kSelectorBoxLayoutId);
173 sct_info_view_ = new SignedCertificateTimestampInfoView();
174 layout->AddView(sct_info_view_);
176 sct_info_view_->SetSignedCertificateTimestamp(*(sct_list_[0].sct),
177 sct_list_[0].status);
180 void SignedCertificateTimestampsViews::ShowSCTInfo(int sct_index) {
181 if ((sct_index < 0) || (static_cast<size_t>(sct_index) > sct_list_.size()))
182 return;
184 sct_info_view_->SetSignedCertificateTimestamp(*(sct_list_[sct_index].sct),
185 sct_list_[sct_index].status);
188 void SignedCertificateTimestampsViews::Observe(
189 int type,
190 const content::NotificationSource& source,
191 const content::NotificationDetails& details) {
192 GetWidget()->Close();