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