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