Home | History | Annotate | Download | only in ui
      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/ui/tab_modal_confirm_dialog_delegate.h"
      6 
      7 #include "chrome/browser/chrome_notification_types.h"
      8 #include "chrome/grit/generated_resources.h"
      9 #include "content/public/browser/navigation_controller.h"
     10 #include "content/public/browser/notification_source.h"
     11 #include "content/public/browser/web_contents.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 
     14 using content::NavigationController;
     15 using content::WebContents;
     16 
     17 TabModalConfirmDialogDelegate::TabModalConfirmDialogDelegate(
     18     WebContents* web_contents)
     19     : close_delegate_(NULL),
     20       closing_(false) {
     21   NavigationController* controller = &web_contents->GetController();
     22   registrar_.Add(this, content::NOTIFICATION_LOAD_START,
     23                  content::Source<NavigationController>(controller));
     24 }
     25 
     26 TabModalConfirmDialogDelegate::~TabModalConfirmDialogDelegate() {
     27   // If we end up here, the window has been closed, so make sure we don't close
     28   // it again.
     29   close_delegate_ = NULL;
     30   // Make sure everything is cleaned up.
     31   Cancel();
     32 }
     33 
     34 void TabModalConfirmDialogDelegate::Cancel() {
     35   if (closing_)
     36     return;
     37   // Make sure we won't do anything when another action occurs.
     38   closing_ = true;
     39   OnCanceled();
     40   CloseDialog();
     41 }
     42 
     43 void TabModalConfirmDialogDelegate::Accept() {
     44   if (closing_)
     45     return;
     46   // Make sure we won't do anything when another action occurs.
     47   closing_ = true;
     48   OnAccepted();
     49   CloseDialog();
     50 }
     51 
     52 void TabModalConfirmDialogDelegate::Observe(
     53     int type,
     54     const content::NotificationSource& source,
     55     const content::NotificationDetails& details) {
     56   // Close the dialog if we load a page (because the action might not apply to
     57   // the same page anymore).
     58   DCHECK_EQ(content::NOTIFICATION_LOAD_START, type);
     59 
     60   Close();
     61 }
     62 
     63 void TabModalConfirmDialogDelegate::Close() {
     64   if (closing_)
     65     return;
     66   // Make sure we won't do anything when another action occurs.
     67   closing_ = true;
     68   OnClosed();
     69   CloseDialog();
     70 }
     71 
     72 void TabModalConfirmDialogDelegate::LinkClicked(
     73     WindowOpenDisposition disposition) {
     74   if (closing_)
     75     return;
     76   OnLinkClicked(disposition);
     77 }
     78 
     79 gfx::Image* TabModalConfirmDialogDelegate::GetIcon() {
     80   return NULL;
     81 }
     82 
     83 base::string16 TabModalConfirmDialogDelegate::GetAcceptButtonTitle() {
     84   return l10n_util::GetStringUTF16(IDS_OK);
     85 }
     86 
     87 base::string16 TabModalConfirmDialogDelegate::GetCancelButtonTitle() {
     88   return l10n_util::GetStringUTF16(IDS_CANCEL);
     89 }
     90 
     91 base::string16 TabModalConfirmDialogDelegate::GetLinkText() const {
     92   return base::string16();
     93 }
     94 
     95 const char* TabModalConfirmDialogDelegate::GetAcceptButtonIcon() {
     96   return NULL;
     97 }
     98 
     99 const char* TabModalConfirmDialogDelegate::GetCancelButtonIcon() {
    100   return NULL;
    101 }
    102 
    103 void TabModalConfirmDialogDelegate::OnAccepted() {
    104 }
    105 
    106 void TabModalConfirmDialogDelegate::OnCanceled() {
    107 }
    108 
    109 void TabModalConfirmDialogDelegate::OnLinkClicked(
    110     WindowOpenDisposition disposition) {
    111 }
    112 
    113 void TabModalConfirmDialogDelegate::OnClosed() {
    114 }
    115 
    116 void TabModalConfirmDialogDelegate::CloseDialog() {
    117   if (close_delegate_)
    118     close_delegate_->CloseDialog();
    119 }
    120