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 "chrome/grit/generated_resources.h"
15 #include "components/web_modal/popup_manager.h"
16 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/extension.h"
19 #include "grit/components_strings.h"
20 #include "ui/aura/window.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/page_transition_types.h"
23 #include "ui/views/border.h"
24 #include "ui/views/controls/styled_label.h"
25 #include "ui/views/layout/fill_layout.h"
26 #include "ui/views/layout/layout_constants.h"
27 #include "ui/views/widget/widget.h"
28 #include "ui/views/window/dialog_delegate.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 to be shown.
57 web_modal::PopupManager
* popup_manager
=
58 web_modal::PopupManager::FromWebContents(web_contents
);
59 DCHECK(popup_manager
);
60 views::Widget
* widget
= views::DialogDelegate::CreateDialogWidget(
61 dialog
, NULL
, popup_manager
->GetHostView());
62 popup_manager
->ShowModalDialog(widget
->GetNativeView(), web_contents
);
65 PlatformVerificationDialog::~PlatformVerificationDialog() {
68 PlatformVerificationDialog::PlatformVerificationDialog(
69 content::WebContents
* web_contents
,
70 const base::string16
& domain
,
71 const PlatformVerificationFlow::Delegate::ConsentCallback
& callback
)
72 : web_contents_(web_contents
),
75 SetLayoutManager(new views::FillLayout());
76 SetBorder(views::Border::CreateEmptyBorder(
77 0, views::kButtonHEdgeMarginNew
, 0, views::kButtonHEdgeMarginNew
));
78 const base::string16 learn_more
= l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
79 std::vector
<size_t> offsets
;
80 base::string16 headline
= l10n_util::GetStringFUTF16(
81 IDS_PLATFORM_VERIFICATION_DIALOG_HEADLINE
, domain_
, learn_more
, &offsets
);
82 views::StyledLabel
* headline_label
= new views::StyledLabel(headline
, this);
83 headline_label
->AddStyleRange(
84 gfx::Range(offsets
[1], offsets
[1] + learn_more
.size()),
85 views::StyledLabel::RangeStyleInfo::CreateForLink());
86 AddChildView(headline_label
);
89 bool PlatformVerificationDialog::Cancel() {
90 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY
);
94 bool PlatformVerificationDialog::Accept() {
95 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW
);
99 bool PlatformVerificationDialog::Close() {
100 // This method is called when the tab is closed and in that case the decision
101 // hasn't been made yet.
102 callback_
.Run(PlatformVerificationFlow::CONSENT_RESPONSE_NONE
);
106 base::string16
PlatformVerificationDialog::GetDialogButtonLabel(
107 ui::DialogButton button
) const {
109 case ui::DIALOG_BUTTON_OK
:
110 return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW
);
111 case ui::DIALOG_BUTTON_CANCEL
:
112 return l10n_util::GetStringFUTF16(
113 IDS_PLATFORM_VERIFICATION_DIALOG_DENY
, domain_
);
117 return base::string16();
120 ui::ModalType
PlatformVerificationDialog::GetModalType() const {
121 return ui::MODAL_TYPE_CHILD
;
124 gfx::Size
PlatformVerificationDialog::GetPreferredSize() const {
125 return gfx::Size(kDialogMaxWidthInPixel
,
126 GetHeightForWidth(kDialogMaxWidthInPixel
));
129 void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range
& range
,
131 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
132 const GURL
learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL
);
134 // |web_contents_| might not be in a browser in case of v2 apps. In that case,
135 // open a new tab in the usual way.
137 Profile
* profile
= Profile::FromBrowserContext(
138 web_contents_
->GetBrowserContext());
139 chrome::NavigateParams
params(
140 profile
, learn_more_url
, ui::PAGE_TRANSITION_LINK
);
141 params
.disposition
= SINGLETON_TAB
;
142 chrome::Navigate(¶ms
);
144 chrome::ShowSingletonTab(browser
, learn_more_url
);
148 } // namespace attestation
149 } // namespace chromeos