Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / attestation / platform_verification_dialog.cc
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/web_contents_modal_dialog_host.h"
16 #include "components/web_modal/web_contents_modal_dialog_manager.h"
17 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/page_transition_types.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/common/extension.h"
22 #include "ui/aura/window.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/views/border.h"
25 #include "ui/views/controls/styled_label.h"
26 #include "ui/views/layout/fill_layout.h"
27 #include "ui/views/layout/layout_constants.h"
28 #include "ui/views/widget/widget.h"
29
30 namespace chromeos {
31 namespace attestation {
32
33 namespace {
34
35 const int kDialogMaxWidthInPixel = 400;
36
37 }  // namespace
38
39 // static
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();
50
51   PlatformVerificationDialog* dialog = new PlatformVerificationDialog(
52       web_contents,
53       base::UTF8ToUTF16(origin),
54       callback);
55
56   web_modal::WebContentsModalDialogManager* manager =
57       web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
58   const gfx::NativeWindow parent =
59       manager->delegate()->GetWebContentsModalDialogHost()->GetHostView();
60   views::Widget* widget = CreateDialogWidget(dialog, NULL, parent);
61   manager->ShowModalDialog(widget->GetNativeView());
62   widget->Show();
63 }
64
65 PlatformVerificationDialog::~PlatformVerificationDialog() {
66 }
67
68 PlatformVerificationDialog::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
89 bool PlatformVerificationDialog::Cancel() {
90   callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_DENY);
91   return true;
92 }
93
94 bool PlatformVerificationDialog::Accept() {
95   callback_.Run(PlatformVerificationFlow::CONSENT_RESPONSE_ALLOW);
96   return true;
97 }
98
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);
103   return true;
104 }
105
106 base::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
120 ui::ModalType PlatformVerificationDialog::GetModalType() const {
121   return ui::MODAL_TYPE_CHILD;
122 }
123
124 gfx::Size PlatformVerificationDialog::GetPreferredSize() const {
125   return gfx::Size(kDialogMaxWidthInPixel,
126                    GetHeightForWidth(kDialogMaxWidthInPixel));
127 }
128
129 void 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, content::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