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