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