OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h" | 5 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/api/infobars/infobar_service.h" | 10 #include "chrome/browser/api/infobars/infobar_service.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/themes/theme_service.h" | 13 #include "chrome/browser/themes/theme_service.h" |
14 #include "chrome/browser/themes/theme_service_factory.h" | 14 #include "chrome/browser/themes/theme_service_factory.h" |
| 15 #include "chrome/browser/ui/browser_finder.h" |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" |
15 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
17 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
18 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
20 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
22 | 24 |
| 25 |
| 26 // static |
| 27 void ThemeInstalledInfoBarDelegate::Create( |
| 28 const extensions::Extension* new_theme, |
| 29 Profile* profile, |
| 30 const std::string& previous_theme_id, |
| 31 bool previous_using_native_theme) { |
| 32 if (!new_theme->is_theme()) |
| 33 return; |
| 34 |
| 35 // Get last active tabbed browser of profile. |
| 36 Browser* browser = browser::FindTabbedBrowser(profile, |
| 37 true, |
| 38 chrome::GetActiveDesktop()); |
| 39 if (!browser) |
| 40 return; |
| 41 |
| 42 content::WebContents* web_contents = chrome::GetActiveWebContents(browser); |
| 43 if (!web_contents) |
| 44 return; |
| 45 InfoBarService* infobar_service = |
| 46 InfoBarService::FromWebContents(web_contents); |
| 47 |
| 48 // First find any previous theme preview infobars. |
| 49 InfoBarDelegate* old_delegate = NULL; |
| 50 for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { |
| 51 InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(i); |
| 52 ThemeInstalledInfoBarDelegate* theme_infobar = |
| 53 delegate->AsThemePreviewInfobarDelegate(); |
| 54 if (theme_infobar) { |
| 55 // If the user installed the same theme twice, ignore the second install |
| 56 // and keep the first install info bar, so that they can easily undo to |
| 57 // get back the previous theme. |
| 58 if (theme_infobar->theme_id_ == new_theme->id()) |
| 59 return; |
| 60 old_delegate = delegate; |
| 61 break; |
| 62 } |
| 63 } |
| 64 |
| 65 // Then either replace that old one or add a new one. |
| 66 scoped_ptr<InfoBarDelegate> new_delegate( |
| 67 new ThemeInstalledInfoBarDelegate( |
| 68 infobar_service, |
| 69 profile->GetExtensionService(), |
| 70 ThemeServiceFactory::GetForProfile(profile), |
| 71 new_theme, |
| 72 previous_theme_id, |
| 73 previous_using_native_theme)); |
| 74 |
| 75 if (old_delegate) |
| 76 infobar_service->ReplaceInfoBar(old_delegate, new_delegate.Pass()); |
| 77 else |
| 78 infobar_service->AddInfoBar(new_delegate.Pass()); |
| 79 } |
| 80 |
| 81 bool ThemeInstalledInfoBarDelegate::Cancel() { |
| 82 if (!previous_theme_id_.empty()) { |
| 83 const extensions::Extension* previous_theme = |
| 84 extension_service_->GetExtensionById(previous_theme_id_, true); |
| 85 if (previous_theme) { |
| 86 theme_service_->SetTheme(previous_theme); |
| 87 return false; // The theme change will close us. |
| 88 } |
| 89 } |
| 90 |
| 91 if (previous_using_native_theme_) |
| 92 theme_service_->SetNativeTheme(); |
| 93 else |
| 94 theme_service_->UseDefaultTheme(); |
| 95 return false; // The theme change will close us. |
| 96 } |
| 97 |
23 ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate( | 98 ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate( |
24 InfoBarService* infobar_service, | 99 InfoBarService* infobar_service, |
25 ExtensionService* extension_service, | 100 ExtensionService* extension_service, |
26 ThemeService* theme_service, | 101 ThemeService* theme_service, |
27 const extensions::Extension* new_theme, | 102 const extensions::Extension* new_theme, |
28 const std::string& previous_theme_id, | 103 const std::string& previous_theme_id, |
29 bool previous_using_native_theme) | 104 bool previous_using_native_theme) |
30 : ConfirmInfoBarDelegate(infobar_service), | 105 : ConfirmInfoBarDelegate(infobar_service), |
31 extension_service_(extension_service), | 106 extension_service_(extension_service), |
32 theme_service_(theme_service), | 107 theme_service_(theme_service), |
33 name_(new_theme->name()), | 108 name_(new_theme->name()), |
34 theme_id_(new_theme->id()), | 109 theme_id_(new_theme->id()), |
35 previous_theme_id_(previous_theme_id), | 110 previous_theme_id_(previous_theme_id), |
36 previous_using_native_theme_(previous_using_native_theme) { | 111 previous_using_native_theme_(previous_using_native_theme) { |
37 theme_service_->OnInfobarDisplayed(); | 112 theme_service_->OnInfobarDisplayed(); |
38 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 113 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
39 content::Source<ThemeService>(theme_service_)); | 114 content::Source<ThemeService>(theme_service_)); |
40 } | 115 } |
41 | 116 |
42 bool ThemeInstalledInfoBarDelegate::MatchesTheme( | |
43 const extensions::Extension* theme) const { | |
44 return theme && (theme->id() == theme_id_); | |
45 } | |
46 | |
47 ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() { | 117 ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() { |
48 // We don't want any notifications while we're running our destructor. | 118 // We don't want any notifications while we're running our destructor. |
49 registrar_.RemoveAll(); | 119 registrar_.RemoveAll(); |
50 | 120 |
51 theme_service_->OnInfobarDestroyed(); | 121 theme_service_->OnInfobarDestroyed(); |
52 } | 122 } |
53 | 123 |
54 bool ThemeInstalledInfoBarDelegate::Cancel() { | |
55 if (!previous_theme_id_.empty()) { | |
56 const extensions::Extension* previous_theme = | |
57 extension_service_->GetExtensionById(previous_theme_id_, true); | |
58 if (previous_theme) { | |
59 theme_service_->SetTheme(previous_theme); | |
60 return false; // The theme change will close us. | |
61 } | |
62 } | |
63 | |
64 if (previous_using_native_theme_) | |
65 theme_service_->SetNativeTheme(); | |
66 else | |
67 theme_service_->UseDefaultTheme(); | |
68 return false; // The theme change will close us. | |
69 } | |
70 | |
71 gfx::Image* ThemeInstalledInfoBarDelegate::GetIcon() const { | 124 gfx::Image* ThemeInstalledInfoBarDelegate::GetIcon() const { |
72 // TODO(aa): Reply with the theme's icon, but this requires reading it | 125 // TODO(aa): Reply with the theme's icon, but this requires reading it |
73 // asynchronously from disk. | 126 // asynchronously from disk. |
74 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 127 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
75 IDR_INFOBAR_THEME); | 128 IDR_INFOBAR_THEME); |
76 } | 129 } |
77 | 130 |
78 InfoBarDelegate::Type ThemeInstalledInfoBarDelegate::GetInfoBarType() const { | 131 InfoBarDelegate::Type ThemeInstalledInfoBarDelegate::GetInfoBarType() const { |
79 return PAGE_ACTION_TYPE; | 132 return PAGE_ACTION_TYPE; |
80 } | 133 } |
(...skipping 21 matching lines...) Expand all Loading... |
102 void ThemeInstalledInfoBarDelegate::Observe( | 155 void ThemeInstalledInfoBarDelegate::Observe( |
103 int type, | 156 int type, |
104 const content::NotificationSource& source, | 157 const content::NotificationSource& source, |
105 const content::NotificationDetails& details) { | 158 const content::NotificationDetails& details) { |
106 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); | 159 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
107 // If the new theme is different from what this info bar is associated with, | 160 // If the new theme is different from what this info bar is associated with, |
108 // close this info bar since it is no longer relevant. | 161 // close this info bar since it is no longer relevant. |
109 if (theme_id_ != theme_service_->GetThemeID()) | 162 if (theme_id_ != theme_service_->GetThemeID()) |
110 RemoveSelf(); | 163 RemoveSelf(); |
111 } | 164 } |
OLD | NEW |