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