Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / attestation / platform_verification_dialog.cc
blob997982541270c3a66c6ccb67ff9a65d98c0ddb14
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/constrained_window/constrained_window_views.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 return constrained_window::ShowWebModalDialogViews(dialog, web_contents);
63 PlatformVerificationDialog::~PlatformVerificationDialog() {
66 PlatformVerificationDialog::PlatformVerificationDialog(
67 content::WebContents* web_contents,
68 const base::string16& domain,
69 const ConsentCallback& callback)
70 : content::WebContentsObserver(web_contents),
71 domain_(domain),
72 callback_(callback) {
73 SetLayoutManager(new views::FillLayout());
74 SetBorder(views::Border::CreateEmptyBorder(
75 0, views::kButtonHEdgeMarginNew, 0, views::kButtonHEdgeMarginNew));
76 const base::string16 learn_more = l10n_util::GetStringUTF16(IDS_LEARN_MORE);
77 std::vector<size_t> offsets;
78 base::string16 headline = l10n_util::GetStringFUTF16(
79 IDS_PLATFORM_VERIFICATION_DIALOG_HEADLINE, domain_, learn_more, &offsets);
80 views::StyledLabel* headline_label = new views::StyledLabel(headline, this);
81 headline_label->AddStyleRange(
82 gfx::Range(offsets[1], offsets[1] + learn_more.size()),
83 views::StyledLabel::RangeStyleInfo::CreateForLink());
84 AddChildView(headline_label);
87 bool PlatformVerificationDialog::Cancel() {
88 // This method is called when user clicked "Disable on <origin>" button or
89 // when user pressed the "Esc" key. See http://crbug.com/467155
90 callback_.Run(CONSENT_RESPONSE_DENY);
91 return true;
94 bool PlatformVerificationDialog::Accept() {
95 // This method is called when user clicked "OK, I got it" button.
96 callback_.Run(CONSENT_RESPONSE_ALLOW);
97 return true;
100 bool PlatformVerificationDialog::Close() {
101 // This method is called when user clicked "x" to dismiss the dialog, the
102 // permission request is canceled, or when the tab containing this dialog is
103 // closed.
104 callback_.Run(CONSENT_RESPONSE_NONE);
105 return true;
108 base::string16 PlatformVerificationDialog::GetDialogButtonLabel(
109 ui::DialogButton button) const {
110 switch (button) {
111 case ui::DIALOG_BUTTON_OK:
112 return l10n_util::GetStringUTF16(IDS_PLATFORM_VERIFICATION_DIALOG_ALLOW);
113 case ui::DIALOG_BUTTON_CANCEL:
114 return l10n_util::GetStringFUTF16(
115 IDS_PLATFORM_VERIFICATION_DIALOG_DENY, domain_);
116 default:
117 NOTREACHED();
119 return base::string16();
122 ui::ModalType PlatformVerificationDialog::GetModalType() const {
123 return ui::MODAL_TYPE_CHILD;
126 gfx::Size PlatformVerificationDialog::GetPreferredSize() const {
127 return gfx::Size(kDialogMaxWidthInPixel,
128 GetHeightForWidth(kDialogMaxWidthInPixel));
131 void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range& range,
132 int event_flags) {
133 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
134 const GURL learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL);
136 // |web_contents()| might not be in a browser in case of v2 apps. In that
137 // case, open a new tab in the usual way.
138 if (!browser) {
139 Profile* profile =
140 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
141 chrome::NavigateParams params(
142 profile, learn_more_url, ui::PAGE_TRANSITION_LINK);
143 params.disposition = SINGLETON_TAB;
144 chrome::Navigate(&params);
145 } else {
146 chrome::ShowSingletonTab(browser, learn_more_url);
150 void PlatformVerificationDialog::DidStartNavigationToPendingEntry(
151 const GURL& url,
152 content::NavigationController::ReloadType reload_type) {
153 views::Widget* widget = GetWidget();
154 if (widget)
155 widget->Close();
158 } // namespace attestation
159 } // namespace chromeos