Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / attestation / platform_verification_dialog.cc
blobb4a84a25802b78b39a20241f74224d8bb9de61f5
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"
30 namespace chromeos {
31 namespace attestation {
33 namespace {
35 const int kDialogMaxWidthInPixel = 400;
37 } // namespace
39 // static
40 views::Widget* PlatformVerificationDialog::ShowDialog(
41 content::WebContents* web_contents,
42 const GURL& requesting_origin,
43 const ConsentCallback& callback) {
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()
49 .GetExtensionOrAppByURL(web_contents->GetLastCommittedURL());
51 // TODO(xhwang): We should only show the name if the request if from the
52 // extension's true frame. See http://crbug.com/455821
53 std::string origin = extension ? extension->name() : requesting_origin.spec();
55 PlatformVerificationDialog* dialog = new PlatformVerificationDialog(
56 web_contents,
57 base::UTF8ToUTF16(origin),
58 callback);
60 // Sets up the dialog widget to be shown.
61 web_modal::PopupManager* popup_manager =
62 web_modal::PopupManager::FromWebContents(web_contents);
63 DCHECK(popup_manager);
64 views::Widget* widget = views::DialogDelegate::CreateDialogWidget(
65 dialog, NULL, popup_manager->GetHostView());
66 popup_manager->ShowModalDialog(widget->GetNativeView(), web_contents);
68 return widget;
71 PlatformVerificationDialog::~PlatformVerificationDialog() {
74 PlatformVerificationDialog::PlatformVerificationDialog(
75 content::WebContents* web_contents,
76 const base::string16& domain,
77 const ConsentCallback& callback)
78 : content::WebContentsObserver(web_contents),
79 domain_(domain),
80 callback_(callback) {
81 SetLayoutManager(new views::FillLayout());
82 SetBorder(views::Border::CreateEmptyBorder(
83 0, views::kButtonHEdgeMarginNew, 0, views::kButtonHEdgeMarginNew));
84 const base::string16 learn_more = l10n_util::GetStringUTF16(IDS_LEARN_MORE);
85 std::vector<size_t> offsets;
86 base::string16 headline = l10n_util::GetStringFUTF16(
87 IDS_PLATFORM_VERIFICATION_DIALOG_HEADLINE, domain_, learn_more, &offsets);
88 views::StyledLabel* headline_label = new views::StyledLabel(headline, this);
89 headline_label->AddStyleRange(
90 gfx::Range(offsets[1], offsets[1] + learn_more.size()),
91 views::StyledLabel::RangeStyleInfo::CreateForLink());
92 AddChildView(headline_label);
95 bool PlatformVerificationDialog::Cancel() {
96 // This method is called when user clicked "Disable on <origin>" button or
97 // when user pressed the "Esc" key. See http://crbug.com/467155
98 callback_.Run(CONSENT_RESPONSE_DENY);
99 return true;
102 bool PlatformVerificationDialog::Accept() {
103 // This method is called when user clicked "OK, I got it" button.
104 callback_.Run(CONSENT_RESPONSE_ALLOW);
105 return true;
108 bool PlatformVerificationDialog::Close() {
109 // This method is called when user clicked "x" to dismiss the dialog, the
110 // permission request is canceled, or when the tab containing this dialog is
111 // closed.
112 callback_.Run(CONSENT_RESPONSE_NONE);
113 return true;
116 base::string16 PlatformVerificationDialog::GetDialogButtonLabel(
117 ui::DialogButton button) const {
118 switch (button) {
119 case ui::DIALOG_BUTTON_OK:
120 return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW);
121 case ui::DIALOG_BUTTON_CANCEL:
122 return l10n_util::GetStringFUTF16(
123 IDS_PLATFORM_VERIFICATION_DIALOG_DENY, domain_);
124 default:
125 NOTREACHED();
127 return base::string16();
130 ui::ModalType PlatformVerificationDialog::GetModalType() const {
131 return ui::MODAL_TYPE_CHILD;
134 gfx::Size PlatformVerificationDialog::GetPreferredSize() const {
135 return gfx::Size(kDialogMaxWidthInPixel,
136 GetHeightForWidth(kDialogMaxWidthInPixel));
139 void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range& range,
140 int event_flags) {
141 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
142 const GURL learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL);
144 // |web_contents()| might not be in a browser in case of v2 apps. In that
145 // case, open a new tab in the usual way.
146 if (!browser) {
147 Profile* profile =
148 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
149 chrome::NavigateParams params(
150 profile, learn_more_url, ui::PAGE_TRANSITION_LINK);
151 params.disposition = SINGLETON_TAB;
152 chrome::Navigate(&params);
153 } else {
154 chrome::ShowSingletonTab(browser, learn_more_url);
158 void PlatformVerificationDialog::DidStartNavigationToPendingEntry(
159 const GURL& url,
160 content::NavigationController::ReloadType reload_type) {
161 views::Widget* widget = GetWidget();
162 if (widget)
163 widget->Close();
166 } // namespace attestation
167 } // namespace chromeos