external_protocol_dialog.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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/external_protocol_dialog.h"
6
7#include "base/metrics/histogram.h"
8#include "base/strings/string_util.h"
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/external_protocol/external_protocol_handler.h"
11#include "chrome/browser/tab_contents/tab_util.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/browser/web_contents_view.h"
14#include "grit/chromium_strings.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17#include "ui/gfx/text_elider.h"
18#include "ui/views/controls/message_box_view.h"
19#include "ui/views/widget/widget.h"
20#include "url/gurl.h"
21
22using content::WebContents;
23
24namespace {
25
26const int kMessageWidth = 400;
27
28}  // namespace
29
30///////////////////////////////////////////////////////////////////////////////
31// ExternalProtocolHandler
32
33// static
34void ExternalProtocolHandler::RunExternalProtocolDialog(
35    const GURL& url, int render_process_host_id, int routing_id) {
36  WebContents* web_contents = tab_util::GetWebContentsByID(
37      render_process_host_id, routing_id);
38  new ExternalProtocolDialog(web_contents, url);
39}
40
41///////////////////////////////////////////////////////////////////////////////
42// ExternalProtocolDialog
43
44ExternalProtocolDialog::~ExternalProtocolDialog() {
45}
46
47//////////////////////////////////////////////////////////////////////////////
48// ExternalProtocolDialog, views::DialogDelegate implementation:
49
50int ExternalProtocolDialog::GetDialogButtons() const {
51  return ui::DIALOG_BUTTON_OK;
52}
53
54base::string16 ExternalProtocolDialog::GetDialogButtonLabel(
55    ui::DialogButton button) const {
56  return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT);
57}
58
59base::string16 ExternalProtocolDialog::GetWindowTitle() const {
60  return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE);
61}
62
63void ExternalProtocolDialog::DeleteDelegate() {
64  delete this;
65}
66
67bool ExternalProtocolDialog::Accept() {
68  if (message_box_view_->IsCheckBoxSelected()) {
69    ExternalProtocolHandler::SetBlockState(
70        scheme_, ExternalProtocolHandler::DONT_BLOCK);
71  }
72  // Returning true closes the dialog.
73  return true;
74}
75
76views::View* ExternalProtocolDialog::GetContentsView() {
77  return message_box_view_;
78}
79
80const views::Widget* ExternalProtocolDialog::GetWidget() const {
81  return message_box_view_->GetWidget();
82}
83
84views::Widget* ExternalProtocolDialog::GetWidget() {
85  return message_box_view_->GetWidget();
86}
87
88///////////////////////////////////////////////////////////////////////////////
89// ExternalProtocolDialog, private:
90
91ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents,
92                                               const GURL& url)
93    : creation_time_(base::TimeTicks::Now()),
94      scheme_(url.scheme()) {
95  const int kMaxUrlWithoutSchemeSize = 256;
96  base::string16 elided_url_without_scheme;
97  gfx::ElideString(base::ASCIIToUTF16(url.possibly_invalid_spec()),
98      kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
99
100  views::MessageBoxView::InitParams params(
101      l10n_util::GetStringFUTF16(IDS_EXTERNAL_PROTOCOL_INFORMATION,
102      base::ASCIIToUTF16(url.scheme() + ":"),
103      elided_url_without_scheme) + base::ASCIIToUTF16("\n\n"));
104  params.message_width = kMessageWidth;
105  message_box_view_ = new views::MessageBoxView(params);
106  message_box_view_->SetCheckBoxLabel(
107      l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT));
108
109  gfx::NativeWindow parent_window;
110  if (web_contents) {
111    parent_window = web_contents->GetView()->GetTopLevelNativeWindow();
112  } else {
113    // Dialog is top level if we don't have a web_contents associated with us.
114    parent_window = NULL;
115  }
116  views::DialogDelegate::CreateDialogWidget(this, NULL, parent_window)->Show();
117}
118