external_protocol_dialog.cc revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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 "app/l10n_util.h"
8#include "app/message_box_flags.h"
9#include "app/text_elider.h"
10#include "base/metrics/histogram.h"
11#include "base/string_util.h"
12#include "base/utf_string_conversions.h"
13#include "chrome/browser/external_protocol_handler.h"
14#include "chrome/browser/tab_contents/tab_contents.h"
15#include "chrome/browser/tab_contents/tab_contents_view.h"
16#include "chrome/browser/tab_contents/tab_util.h"
17#include "chrome/browser/views/window.h"
18#include "googleurl/src/gurl.h"
19#include "grit/chromium_strings.h"
20#include "grit/generated_resources.h"
21#include "views/controls/message_box_view.h"
22#include "views/window/window.h"
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  TabContents* tab_contents = tab_util::GetTabContentsByID(
37      render_process_host_id, routing_id);
38  DCHECK(tab_contents);
39  new ExternalProtocolDialog(tab_contents, url);
40}
41
42///////////////////////////////////////////////////////////////////////////////
43// ExternalProtocolDialog
44
45ExternalProtocolDialog::~ExternalProtocolDialog() {
46}
47
48//////////////////////////////////////////////////////////////////////////////
49// ExternalProtocolDialog, views::DialogDelegate implementation:
50
51int ExternalProtocolDialog::GetDialogButtons() const {
52  return MessageBoxFlags::DIALOGBUTTON_OK;
53}
54
55std::wstring ExternalProtocolDialog::GetDialogButtonLabel(
56    MessageBoxFlags::DialogButton button) const {
57  return UTF16ToWide(
58      l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT));
59}
60
61std::wstring ExternalProtocolDialog::GetWindowTitle() const {
62  return UTF16ToWide(l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE));
63}
64
65void ExternalProtocolDialog::DeleteDelegate() {
66  delete this;
67}
68
69bool ExternalProtocolDialog::Accept() {
70  if (message_box_view_->IsCheckBoxSelected()) {
71    ExternalProtocolHandler::SetBlockState(
72        scheme_, ExternalProtocolHandler::DONT_BLOCK);
73  }
74  // Returning true closes the dialog.
75  return true;
76}
77
78views::View* ExternalProtocolDialog::GetContentsView() {
79  return message_box_view_;
80}
81
82///////////////////////////////////////////////////////////////////////////////
83// ExternalProtocolDialog, private:
84
85ExternalProtocolDialog::ExternalProtocolDialog(TabContents* tab_contents,
86                                               const GURL& url)
87    : creation_time_(base::TimeTicks::Now()),
88      scheme_(url.scheme()) {
89  const int kMaxUrlWithoutSchemeSize = 256;
90  std::wstring elided_url_without_scheme;
91  gfx::ElideString(ASCIIToWide(url.possibly_invalid_spec()),
92      kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
93
94  std::wstring message_text = UTF16ToWide(l10n_util::GetStringFUTF16(
95      IDS_EXTERNAL_PROTOCOL_INFORMATION,
96      ASCIIToUTF16(url.scheme() + ":"),
97      WideToUTF16(elided_url_without_scheme)) + ASCIIToUTF16("\n\n"));
98
99  message_box_view_ = new MessageBoxView(MessageBoxFlags::kIsConfirmMessageBox,
100                                         message_text,
101                                         std::wstring(),
102                                         kMessageWidth);
103  message_box_view_->SetCheckBoxLabel(UTF16ToWide(
104      l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)));
105
106  gfx::NativeWindow parent_window;
107  if (tab_contents) {
108    parent_window = tab_contents->view()->GetTopLevelNativeWindow();
109  } else {
110    // Dialog is top level if we don't have a tab_contents associated with us.
111    parent_window = NULL;
112  }
113  browser::CreateViewsWindow(parent_window, gfx::Rect(), this)->Show();
114}
115