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 "content/public/common/page_transition_types.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "ui/aura/window.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/views/border.h"
23 #include "ui/views/controls/styled_label.h"
24 #include "ui/views/layout/fill_layout.h"
25 #include "ui/views/layout/layout_constants.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/window/dialog_delegate.h"
30 namespace attestation {
34 const int kDialogMaxWidthInPixel = 400;
39 void PlatformVerificationDialog::ShowDialog(
40 content::WebContents* web_contents,
41 const PlatformVerificationFlow::Delegate::ConsentCallback& callback) {
42 GURL url = web_contents->GetLastCommittedURL();
43 // In the case of an extension or hosted app, the origin of the request is
44 // best described by the extension / app name.
45 const extensions::Extension* extension =
46 extensions::ExtensionRegistry::Get(web_contents->GetBrowserContext())->
47 enabled_extensions().GetExtensionOrAppByURL(url);
48 std::string origin = extension ? extension->name() : url.GetOrigin().spec();
50 PlatformVerificationDialog* dialog = new PlatformVerificationDialog(
52 base::UTF8ToUTF16(origin),
55 // Sets up the dialog widget to be shown.
56 web_modal::PopupManager* popup_manager =
57 web_modal::PopupManager::FromWebContents(web_contents);
58 DCHECK(popup_manager);
59 views::Widget* widget = views::DialogDelegate::CreateDialogWidget(
60 dialog, NULL, popup_manager->GetHostView());
61 popup_manager->ShowModalDialog(widget->GetNativeView(), web_contents);
64 PlatformVerificationDialog::~PlatformVerificationDialog() {
67 PlatformVerificationDialog::PlatformVerificationDialog(
68 content::WebContents* web_contents,
69 const base::string16& domain,
70 const PlatformVerificationFlow::Delegate::ConsentCallback& callback)
71 : web_contents_(web_contents),
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);
88 bool PlatformVerificationDialog::Cancel() {
89 callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
93 bool PlatformVerificationDialog::Accept() {
94 callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW);
98 bool PlatformVerificationDialog::Close() {
99 // This method is called when the tab is closed and in that case the decision
100 // hasn't been made yet.
101 callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_NONE);
105 base::string16 PlatformVerificationDialog::GetDialogButtonLabel(
106 ui::DialogButton button) const {
108 case ui::DIALOG_BUTTON_OK:
109 return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW);
110 case ui::DIALOG_BUTTON_CANCEL:
111 return l10n_util::GetStringFUTF16(
112 IDS_PLATFORM_VERIFICATION_DIALOG_DENY, domain_);
116 return base::string16();
119 ui::ModalType PlatformVerificationDialog::GetModalType() const {
120 return ui::MODAL_TYPE_CHILD;
123 gfx::Size PlatformVerificationDialog::GetPreferredSize() const {
124 return gfx::Size(kDialogMaxWidthInPixel,
125 GetHeightForWidth(kDialogMaxWidthInPixel));
128 void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range& range,
130 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
131 const GURL learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL);
133 // |web_contents_| might not be in a browser in case of v2 apps. In that case,
134 // open a new tab in the usual way.
136 Profile* profile = Profile::FromBrowserContext(
137 web_contents_->GetBrowserContext());
138 chrome::NavigateParams params(
139 profile, learn_more_url, content::PAGE_TRANSITION_LINK);
140 params.disposition = SINGLETON_TAB;
141 chrome::Navigate(¶ms);
143 chrome::ShowSingletonTab(browser, learn_more_url);
147 } // namespace attestation
148 } // namespace chromeos