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