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