OLD | NEW |
(Empty) | |
| 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/extensions/extension_install_ui_default.h" |
| 6 |
| 7 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 8 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h" |
| 9 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/themes/theme_service.h" |
| 12 #include "chrome/browser/themes/theme_service_factory.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_dialogs.h" |
| 15 #include "chrome/browser/ui/browser_finder.h" |
| 16 #include "chrome/browser/ui/browser_navigator.h" |
| 17 #include "chrome/browser/ui/browser_window.h" |
| 18 #include "chrome/browser/ui/simple_message_box.h" |
| 19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 20 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 21 #include "chrome/common/chrome_notification_types.h" |
| 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "chrome/common/url_constants.h" |
| 24 #include "content/public/browser/notification_service.h" |
| 25 #include "grit/generated_resources.h" |
| 26 #include "grit/theme_resources.h" |
| 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" |
| 29 |
| 30 using content::WebContents; |
| 31 using extensions::Extension; |
| 32 |
| 33 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile) |
| 34 : ExtensionInstallUI(profile), |
| 35 previous_using_native_theme_(false) { |
| 36 // Remember the current theme in case the user presses undo. |
| 37 if (profile) { |
| 38 const Extension* previous_theme = |
| 39 ThemeServiceFactory::GetThemeForProfile(profile); |
| 40 if (previous_theme) |
| 41 previous_theme_id_ = previous_theme->id(); |
| 42 previous_using_native_theme_ = |
| 43 ThemeServiceFactory::GetForProfile(profile)->UsingNativeTheme(); |
| 44 } |
| 45 } |
| 46 |
| 47 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() { |
| 48 } |
| 49 |
| 50 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension, |
| 51 SkBitmap* icon) { |
| 52 if (extension->is_theme()) { |
| 53 ShowThemeInfoBar(previous_theme_id_, previous_using_native_theme_, |
| 54 extension, profile()); |
| 55 return; |
| 56 } |
| 57 |
| 58 // Extensions aren't enabled by default in incognito so we confirm |
| 59 // the install in a normal window. |
| 60 Profile* current_profile = profile()->GetOriginalProfile(); |
| 61 Browser* browser = browser::FindOrCreateTabbedBrowser(current_profile); |
| 62 if (browser->tab_count() == 0) |
| 63 browser->AddBlankTab(true); |
| 64 browser->window()->Show(); |
| 65 |
| 66 bool use_bubble_for_apps = false; |
| 67 |
| 68 #if defined(TOOLKIT_VIEWS) |
| 69 CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 70 use_bubble_for_apps = (use_app_installed_bubble_ || |
| 71 cmdline->HasSwitch(switches::kAppsNewInstallBubble)); |
| 72 #endif |
| 73 |
| 74 if (extension->is_app() && !use_bubble_for_apps) { |
| 75 ExtensionInstallUI::OpenAppInstalledUI(browser, extension->id()); |
| 76 return; |
| 77 } |
| 78 |
| 79 browser::ShowExtensionInstalledBubble(extension, browser, *icon, |
| 80 current_profile); |
| 81 } |
| 82 |
| 83 void ExtensionInstallUIDefault::OnInstallFailure(const string16& error) { |
| 84 Browser* browser = browser::FindLastActiveWithProfile(profile()); |
| 85 browser::ShowMessageBox(browser ? browser->window()->GetNativeHandle() : NULL, |
| 86 l10n_util::GetStringUTF16(IDS_EXTENSION_INSTALL_FAILURE_TITLE), error, |
| 87 browser::MESSAGE_BOX_TYPE_WARNING); |
| 88 } |
| 89 |
| 90 // static |
| 91 void ExtensionInstallUIDefault::ShowThemeInfoBar( |
| 92 const std::string& previous_theme_id, bool previous_using_native_theme, |
| 93 const Extension* new_theme, Profile* profile) { |
| 94 if (!new_theme->is_theme()) |
| 95 return; |
| 96 |
| 97 // Get last active tabbed browser of profile. |
| 98 Browser* browser = browser::FindTabbedBrowser(profile, true); |
| 99 if (!browser) |
| 100 return; |
| 101 |
| 102 TabContentsWrapper* tab_contents = browser->GetSelectedTabContentsWrapper(); |
| 103 if (!tab_contents) |
| 104 return; |
| 105 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper(); |
| 106 |
| 107 // First find any previous theme preview infobars. |
| 108 InfoBarDelegate* old_delegate = NULL; |
| 109 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) { |
| 110 InfoBarDelegate* delegate = infobar_helper->GetInfoBarDelegateAt(i); |
| 111 ThemeInstalledInfoBarDelegate* theme_infobar = |
| 112 delegate->AsThemePreviewInfobarDelegate(); |
| 113 if (theme_infobar) { |
| 114 // If the user installed the same theme twice, ignore the second install |
| 115 // and keep the first install info bar, so that they can easily undo to |
| 116 // get back the previous theme. |
| 117 if (theme_infobar->MatchesTheme(new_theme)) |
| 118 return; |
| 119 old_delegate = delegate; |
| 120 break; |
| 121 } |
| 122 } |
| 123 |
| 124 // Then either replace that old one or add a new one. |
| 125 InfoBarDelegate* new_delegate = GetNewThemeInstalledInfoBarDelegate( |
| 126 tab_contents, new_theme, previous_theme_id, previous_using_native_theme); |
| 127 |
| 128 if (old_delegate) |
| 129 infobar_helper->ReplaceInfoBar(old_delegate, new_delegate); |
| 130 else |
| 131 infobar_helper->AddInfoBar(new_delegate); |
| 132 } |
| 133 |
| 134 InfoBarDelegate* ExtensionInstallUIDefault::GetNewThemeInstalledInfoBarDelegate( |
| 135 TabContentsWrapper* tab_contents, |
| 136 const Extension* new_theme, |
| 137 const std::string& previous_theme_id, |
| 138 bool previous_using_native_theme) { |
| 139 Profile* profile = tab_contents->profile(); |
| 140 return new ThemeInstalledInfoBarDelegate( |
| 141 tab_contents->infobar_tab_helper(), |
| 142 profile->GetExtensionService(), |
| 143 ThemeServiceFactory::GetForProfile(profile), |
| 144 new_theme, |
| 145 previous_theme_id, |
| 146 previous_using_native_theme); |
| 147 } |
| 148 |
| 149 // static |
| 150 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) { |
| 151 return new ExtensionInstallUIDefault(profile); |
| 152 } |
| 153 |
| 154 // static |
| 155 void ExtensionInstallUI::OpenAppInstalledUI(Browser* browser, |
| 156 const std::string& app_id) { |
| 157 if (NewTabUI::ShouldShowApps()) { |
| 158 browser::NavigateParams params = browser->GetSingletonTabNavigateParams( |
| 159 GURL(chrome::kChromeUINewTabURL)); |
| 160 browser::Navigate(¶ms); |
| 161 |
| 162 content::NotificationService::current()->Notify( |
| 163 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP, |
| 164 content::Source<WebContents>(params.target_contents->web_contents()), |
| 165 content::Details<const std::string>(&app_id)); |
| 166 } else { |
| 167 #if defined(USE_ASH) |
| 168 ash::Shell::GetInstance()->ToggleAppList(); |
| 169 |
| 170 content::NotificationService::current()->Notify( |
| 171 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST, |
| 172 content::Source<Profile>(browser->profile()), |
| 173 content::Details<const std::string>(&app_id)); |
| 174 #else |
| 175 NOTREACHED(); |
| 176 #endif |
| 177 } |
| 178 } |
OLD | NEW |