Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2011 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_handler.h"
     11 #include "chrome/browser/tab_contents/tab_util.h"
     12 #include "chrome/browser/ui/views/window.h"
     13 #include "content/browser/tab_contents/tab_contents.h"
     14 #include "content/browser/tab_contents/tab_contents_view.h"
     15 #include "googleurl/src/gurl.h"
     16 #include "grit/chromium_strings.h"
     17 #include "grit/generated_resources.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 #include "ui/base/message_box_flags.h"
     20 #include "ui/base/text/text_elider.h"
     21 #include "views/controls/message_box_view.h"
     22 #include "views/window/window.h"
     23 
     24 namespace {
     25 
     26 const int kMessageWidth = 400;
     27 
     28 }  // namespace
     29 
     30 ///////////////////////////////////////////////////////////////////////////////
     31 // ExternalProtocolHandler
     32 
     33 // static
     34 void 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 
     45 ExternalProtocolDialog::~ExternalProtocolDialog() {
     46 }
     47 
     48 //////////////////////////////////////////////////////////////////////////////
     49 // ExternalProtocolDialog, views::DialogDelegate implementation:
     50 
     51 int ExternalProtocolDialog::GetDialogButtons() const {
     52   return ui::MessageBoxFlags::DIALOGBUTTON_OK;
     53 }
     54 
     55 std::wstring ExternalProtocolDialog::GetDialogButtonLabel(
     56     ui::MessageBoxFlags::DialogButton button) const {
     57   return UTF16ToWide(
     58       l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT));
     59 }
     60 
     61 std::wstring ExternalProtocolDialog::GetWindowTitle() const {
     62   return UTF16ToWide(l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE));
     63 }
     64 
     65 void ExternalProtocolDialog::DeleteDelegate() {
     66   delete this;
     67 }
     68 
     69 bool 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 
     78 views::View* ExternalProtocolDialog::GetContentsView() {
     79   return message_box_view_;
     80 }
     81 
     82 ///////////////////////////////////////////////////////////////////////////////
     83 // ExternalProtocolDialog, private:
     84 
     85 ExternalProtocolDialog::ExternalProtocolDialog(TabContents* tab_contents,
     86                                                const GURL& url)
     87     : creation_time_(base::TimeTicks::Now()),
     88       scheme_(url.scheme()) {
     89   const int kMaxUrlWithoutSchemeSize = 256;
     90   string16 elided_url_without_scheme;
     91   ui::ElideString(ASCIIToUTF16(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       elided_url_without_scheme) + ASCIIToUTF16("\n\n"));
     98 
     99   message_box_view_ = new views::MessageBoxView(
    100       ui::MessageBoxFlags::kIsConfirmMessageBox,
    101       message_text,
    102       std::wstring(),
    103       kMessageWidth);
    104   message_box_view_->SetCheckBoxLabel(UTF16ToWide(
    105       l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)));
    106 
    107   gfx::NativeWindow parent_window;
    108   if (tab_contents) {
    109     parent_window = tab_contents->view()->GetTopLevelNativeWindow();
    110   } else {
    111     // Dialog is top level if we don't have a tab_contents associated with us.
    112     parent_window = NULL;
    113   }
    114   browser::CreateViewsWindow(parent_window, gfx::Rect(), this)->Show();
    115 }
    116