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.
4
5#include "chrome/browser/chromeos/attestation/platform_verification_dialog.h"
6
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"
29
30namespace chromeos {
31namespace attestation {
32
33namespace {
34
35const int kDialogMaxWidthInPixel = 400;
36
37}  // namespace
38
39// static
40void 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();
50
51  PlatformVerificationDialog* dialog = new PlatformVerificationDialog(
52      web_contents,
53      base::UTF8ToUTF16(origin),
54      callback);
55
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);
63}
64
65PlatformVerificationDialog::~PlatformVerificationDialog() {
66}
67
68PlatformVerificationDialog::PlatformVerificationDialog(
69    content::WebContents* web_contents,
70    const base::string16& domain,
71    const PlatformVerificationFlow::Delegate::ConsentCallback& callback)
72    : web_contents_(web_contents),
73      domain_(domain),
74      callback_(callback) {
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);
87}
88
89bool PlatformVerificationDialog::Cancel() {
90  callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
91  return true;
92}
93
94bool PlatformVerificationDialog::Accept() {
95  callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW);
96  return true;
97}
98
99bool 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);
103  return true;
104}
105
106base::string16 PlatformVerificationDialog::GetDialogButtonLabel(
107    ui::DialogButton button) const {
108  switch (button) {
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_);
114    default:
115      NOTREACHED();
116  }
117  return base::string16();
118}
119
120ui::ModalType PlatformVerificationDialog::GetModalType() const {
121  return ui::MODAL_TYPE_CHILD;
122}
123
124gfx::Size PlatformVerificationDialog::GetPreferredSize() const {
125  return gfx::Size(kDialogMaxWidthInPixel,
126                   GetHeightForWidth(kDialogMaxWidthInPixel));
127}
128
129void PlatformVerificationDialog::StyledLabelLinkClicked(const gfx::Range& range,
130                                                        int event_flags) {
131  Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
132  const GURL learn_more_url(chrome::kEnhancedPlaybackNotificationLearnMoreURL);
133
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.
136  if (!browser) {
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(&params);
143  } else {
144    chrome::ShowSingletonTab(browser, learn_more_url);
145  }
146}
147
148}  // namespace attestation
149}  // namespace chromeos
150