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/extensions/theme_installed_infobar_delegate.h" 6 7 #include <string> 8 9 #include "base/utf_string_conversions.h" 10 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/themes/theme_service.h" 13 #include "chrome/browser/themes/theme_service_factory.h" 14 #include "chrome/common/extensions/extension.h" 15 #include "content/browser/tab_contents/tab_contents.h" 16 #include "content/common/notification_service.h" 17 #include "grit/generated_resources.h" 18 #include "grit/theme_resources.h" 19 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/resource/resource_bundle.h" 21 22 ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate( 23 TabContents* tab_contents, 24 const Extension* new_theme, 25 const std::string& previous_theme_id) 26 : ConfirmInfoBarDelegate(tab_contents), 27 profile_(tab_contents->profile()), 28 theme_service_(ThemeServiceFactory::GetForProfile(profile_)), 29 name_(new_theme->name()), 30 theme_id_(new_theme->id()), 31 previous_theme_id_(previous_theme_id), 32 tab_contents_(tab_contents) { 33 theme_service_->OnInfobarDisplayed(); 34 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, 35 NotificationService::AllSources()); 36 } 37 38 bool ThemeInstalledInfoBarDelegate::MatchesTheme(const Extension* theme) const { 39 return theme && (theme->id() == theme_id_); 40 } 41 42 ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() { 43 // We don't want any notifications while we're running our destructor. 44 registrar_.RemoveAll(); 45 46 theme_service_->OnInfobarDestroyed(); 47 } 48 49 bool ThemeInstalledInfoBarDelegate::Cancel() { 50 if (!previous_theme_id_.empty()) { 51 ExtensionService* service = profile_->GetExtensionService(); 52 if (service) { 53 const Extension* previous_theme = 54 service->GetExtensionById(previous_theme_id_, true); 55 if (previous_theme) { 56 theme_service_->SetTheme(previous_theme); 57 return true; 58 } 59 } 60 } 61 62 theme_service_->UseDefaultTheme(); 63 return true; 64 } 65 66 void ThemeInstalledInfoBarDelegate::InfoBarClosed() { 67 delete this; 68 } 69 70 SkBitmap* ThemeInstalledInfoBarDelegate::GetIcon() const { 71 // TODO(aa): Reply with the theme's icon, but this requires reading it 72 // asynchronously from disk. 73 return ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_INFOBAR_THEME); 74 } 75 76 ThemeInstalledInfoBarDelegate* 77 ThemeInstalledInfoBarDelegate::AsThemePreviewInfobarDelegate() { 78 return this; 79 } 80 81 string16 ThemeInstalledInfoBarDelegate::GetMessageText() const { 82 return l10n_util::GetStringFUTF16(IDS_THEME_INSTALL_INFOBAR_LABEL, 83 UTF8ToUTF16(name_)); 84 } 85 86 int ThemeInstalledInfoBarDelegate::GetButtons() const { 87 return BUTTON_CANCEL; 88 } 89 90 string16 ThemeInstalledInfoBarDelegate::GetButtonLabel( 91 InfoBarButton button) const { 92 DCHECK_EQ(BUTTON_CANCEL, button); 93 return l10n_util::GetStringUTF16(IDS_THEME_INSTALL_INFOBAR_UNDO_BUTTON); 94 } 95 96 void ThemeInstalledInfoBarDelegate::Observe( 97 NotificationType type, 98 const NotificationSource& source, 99 const NotificationDetails& details) { 100 DCHECK_EQ(NotificationType::BROWSER_THEME_CHANGED, type.value); 101 // If the new theme is different from what this info bar is associated 102 // with, close this info bar since it is no longer relevant. 103 if (theme_id_ != theme_service_->GetThemeID()) { 104 if (tab_contents_ && !tab_contents_->is_being_destroyed()) { 105 tab_contents_->RemoveInfoBar(this); 106 // The infobar is gone so there is no reason for this delegate to keep 107 // a pointer to the TabContents (the TabContents has deleted its 108 // reference to this delegate and a new delegate will be created if 109 // a new infobar is created). 110 tab_contents_ = NULL; 111 // Although it's not being used anymore, this delegate is never deleted. 112 // It can not be deleted now because it is still needed if we 113 // "undo" the theme change that triggered this notification 114 // (when InfoBar::OnBackgroundExpose() is called). This will likely 115 // be fixed when infobar delegate deletion is cleaned up for 116 // http://crbug.com/62154. 117 } 118 } 119 } 120