platform_verification_dialog.cc revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright 2013 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/chromeos/attestation/platform_verification_dialog.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/ui/browser_finder.h"
9#include "chrome/browser/ui/browser_window.h"
10#include "chrome/browser/ui/singleton_tabs.h"
11#include "chrome/common/url_constants.h"
12#include "components/web_modal/web_contents_modal_dialog_host.h"
13#include "components/web_modal/web_contents_modal_dialog_manager.h"
14#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
15#include "content/public/browser/web_contents.h"
16#include "grit/generated_resources.h"
17#include "ui/aura/window.h"
18#include "ui/base/l10n/l10n_util.h"
19#include "ui/views/border.h"
20#include "ui/views/controls/styled_label.h"
21#include "ui/views/layout/fill_layout.h"
22#include "ui/views/layout/layout_constants.h"
23#include "ui/views/widget/widget.h"
24
25namespace chromeos {
26namespace attestation {
27
28namespace {
29
30const int kDialogMaxWidthInPixel = 400;
31
32}  // namespace
33
34// static
35void PlatformVerificationDialog::ShowDialog(
36    content::WebContents* web_contents,
37    const PlatformVerificationFlow::Delegate::ConsentCallback& callback) {
38  std::string origin = web_contents->GetLastCommittedURL().GetOrigin().spec();
39
40  PlatformVerificationDialog* dialog = new PlatformVerificationDialog(
41      chrome::FindBrowserWithWebContents(web_contents),
42      UTF8ToUTF16(origin),
43      callback);
44
45  // Sets up the dialog widget and shows it.
46  web_modal::WebContentsModalDialogManager* web_contents_modal_dialog_manager =
47      web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
48  web_modal::WebContentsModalDialogManagerDelegate* modal_delegate =
49      web_contents_modal_dialog_manager->delegate();
50  views::Widget* widget = views::Widget::CreateWindowAsFramelessChild(
51      dialog, modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
52  web_contents_modal_dialog_manager->ShowDialog(widget->GetNativeView());
53  widget->Show();
54}
55
56PlatformVerificationDialog::~PlatformVerificationDialog() {
57}
58
59PlatformVerificationDialog::PlatformVerificationDialog(
60    Browser* browser,
61    const base::string16& domain,
62    const PlatformVerificationFlow::Delegate::ConsentCallback& callback)
63    : browser_(browser),
64      domain_(domain),
65      callback_(callback) {
66  SetLayoutManager(new views::FillLayout());
67  set_border(views::Border::CreateEmptyBorder(
68      0, views::kButtonHEdgeMarginNew, 0, views::kButtonHEdgeMarginNew));
69  const base::string16 learn_more = l10n_util::GetStringUTF16(IDS_LEARN_MORE);
70  std::vector<size_t> offsets;
71  base::string16 headline = l10n_util::GetStringFUTF16(
72      IDS_PLATFORM_VERIFICATION_DIALOG_HEADLINE, domain_, learn_more, &offsets);
73  views::StyledLabel* headline_label = new views::StyledLabel(headline, this);
74  headline_label->AddStyleRange(
75      gfx::Range(offsets[1], offsets[1] + learn_more.size()),
76      views::StyledLabel::RangeStyleInfo::CreateForLink());
77  AddChildView(headline_label);
78}
79
80bool PlatformVerificationDialog::Cancel() {
81  callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
82  return true;
83}
84
85bool PlatformVerificationDialog::Accept() {
86  callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW);
87  return true;
88}
89
90base::string16 PlatformVerificationDialog::GetDialogButtonLabel(
91    ui::DialogButton button) const {
92  switch (button) {
93    case ui::DIALOG_BUTTON_OK:
94      return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW);
95    case ui::DIALOG_BUTTON_CANCEL:
96      return l10n_util::GetStringFUTF16(
97          IDS_PLATFORM_VERIFICATION_DIALOG_DENY, domain_);
98    default:
99      NOTREACHED();
100  }
101  return base::string16();
102}
103
104ui::ModalType PlatformVerificationDialog::GetModalType() const {
105  return ui::MODAL_TYPE_CHILD;
106}
107
108gfx::Size PlatformVerificationDialog::GetPreferredSize() {
109  return gfx::Size(kDialogMaxWidthInPixel,
110                   GetHeightForWidth(kDialogMaxWidthInPixel));
111}
112
113void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range& range,
114                                                        int event_flags) {
115  chrome::ShowSingletonTab(browser_, GURL(
116      chrome::kAttestationForContentProtectionLearnMoreURL));
117}
118
119}  // namespace attestation
120}  // namespace chromeos
121