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.
5 #include "chrome/browser/chromeos/attestation/platform_verification_dialog.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser_finder.h"
10 #include "chrome/browser/ui/browser_navigator.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/singleton_tabs.h"
13 #include "chrome/common/url_constants.h"
14 #include "components/web_modal/web_contents_modal_dialog_host.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h"
21 #include "grit/generated_resources.h"
22 #include "ui/aura/window.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/views/border.h"
25 #include "ui/views/controls/styled_label.h"
26 #include "ui/views/layout/fill_layout.h"
27 #include "ui/views/layout/layout_constants.h"
28 #include "ui/views/widget/widget.h"
31 namespace attestation
{
35 const int kDialogMaxWidthInPixel
= 400;
40 void PlatformVerificationDialog::ShowDialog(
41 content::WebContents
* web_contents
,
42 const PlatformVerificationFlow::Delegate::ConsentCallback
& callback
) {
43 GURL url
= web_contents
->GetLastCommittedURL();
44 // In the case of an extension or hosted app, the origin of the request is
45 // best described by the extension / app name.
46 const extensions::Extension
* extension
=
47 extensions::ExtensionRegistry::Get(web_contents
->GetBrowserContext())->
48 enabled_extensions().GetExtensionOrAppByURL(url
);
49 std::string origin
= extension
? extension
->name() : url
.GetOrigin().spec();
51 PlatformVerificationDialog
* dialog
= new PlatformVerificationDialog(
53 base::UTF8ToUTF16(origin
),
56 // Sets up the dialog widget and shows it.
57 web_modal::WebContentsModalDialogManager
* web_contents_modal_dialog_manager
=
58 web_modal::WebContentsModalDialogManager::FromWebContents(web_contents
);
59 web_modal::WebContentsModalDialogManagerDelegate
* modal_delegate
=
60 web_contents_modal_dialog_manager
->delegate();
61 views::Widget
* widget
= views::Widget::CreateWindowAsFramelessChild(
62 dialog
, modal_delegate
->GetWebContentsModalDialogHost()->GetHostView());
63 web_contents_modal_dialog_manager
->ShowModalDialog(
64 widget
->GetNativeView());
68 PlatformVerificationDialog::~PlatformVerificationDialog() {
71 PlatformVerificationDialog::PlatformVerificationDialog(
72 content::WebContents
* web_contents
,
73 const base::string16
& domain
,
74 const PlatformVerificationFlow::Delegate::ConsentCallback
& callback
)
75 : web_contents_(web_contents
),
78 SetLayoutManager(new views::FillLayout());
79 SetBorder(views::Border::CreateEmptyBorder(
80 0, views::kButtonHEdgeMarginNew
, 0, views::kButtonHEdgeMarginNew
));
81 const base::string16 learn_more
= l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
82 std::vector
<size_t> offsets
;
83 base::string16 headline
= l10n_util::GetStringFUTF16(
84 IDS_PLATFORM_VERIFICATION_DIALOG_HEADLINE
, domain_
, learn_more
, &offsets
);
85 views::StyledLabel
* headline_label
= new views::StyledLabel(headline
, this);
86 headline_label
->AddStyleRange(
87 gfx::Range(offsets
[1], offsets
[1] + learn_more
.size()),
88 views::StyledLabel::RangeStyleInfo::CreateForLink());
89 AddChildView(headline_label
);
92 bool PlatformVerificationDialog::Cancel() {
93 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY
);
97 bool PlatformVerificationDialog::Accept() {
98 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW
);
102 bool PlatformVerificationDialog::Close() {
103 // This method is called when the tab is closed and in that case the decision
104 // hasn't been made yet.
105 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_NONE
);
109 base::string16
PlatformVerificationDialog::GetDialogButtonLabel(
110 ui::DialogButton button
) const {
112 case ui::DIALOG_BUTTON_OK
:
113 return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW
);
114 case ui::DIALOG_BUTTON_CANCEL
:
115 return l10n_util::GetStringFUTF16(
116 IDS_PLATFORM_VERIFICATION_DIALOG_DENY
, domain_
);
120 return base::string16();
123 ui::ModalType
PlatformVerificationDialog::GetModalType() const {
124 return ui::MODAL_TYPE_CHILD
;
127 gfx::Size
PlatformVerificationDialog::GetPreferredSize() {
128 return gfx::Size(kDialogMaxWidthInPixel
,
129 GetHeightForWidth(kDialogMaxWidthInPixel
));
132 void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range
& range
,
134 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
135 const GURL
learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL
);
137 // |web_contents_| might not be in a browser in case of v2 apps. In that case,
138 // open a new tab in the usual way.
140 Profile
* profile
= Profile::FromBrowserContext(
141 web_contents_
->GetBrowserContext());
142 chrome::NavigateParams
params(
143 profile
, learn_more_url
, content::PAGE_TRANSITION_LINK
);
144 params
.disposition
= SINGLETON_TAB
;
145 chrome::Navigate(¶ms
);
147 chrome::ShowSingletonTab(browser
, learn_more_url
);
151 } // namespace attestation
152 } // namespace chromeos