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.
5 #include "chrome/browser/ui/extensions/extension_install_ui_default.h"
8 #include "base/command_line.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/extension_install_prompt.h"
12 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
13 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
14 #include "chrome/browser/infobars/infobar_service.h"
15 #include "chrome/browser/prefs/incognito_mode_prefs.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/themes/theme_service.h"
19 #include "chrome/browser/themes/theme_service_factory.h"
20 #include "chrome/browser/ui/app_list/app_list_service.h"
21 #include "chrome/browser/ui/app_list/app_list_util.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_dialogs.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_navigator.h"
26 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/host_desktop.h"
29 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
30 #include "chrome/browser/ui/simple_message_box.h"
31 #include "chrome/browser/ui/singleton_tabs.h"
32 #include "chrome/browser/ui/tabs/tab_strip_model.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/extensions/extension.h"
35 #include "chrome/common/url_constants.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/web_contents.h"
39 #include "grit/generated_resources.h"
40 #include "grit/theme_resources.h"
41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/base/resource/resource_bundle.h"
45 #include "ash/shell.h"
48 using content::BrowserThread;
49 using content::WebContents;
50 using extensions::Extension;
54 // Helpers --------------------------------------------------------------------
56 Browser* FindOrCreateVisibleBrowser(Profile* profile) {
57 // TODO(mpcomplete): remove this workaround for http://crbug.com/244246
58 // after fixing http://crbug.com/38676.
59 if (!IncognitoModePrefs::CanOpenBrowser(profile))
61 chrome::ScopedTabbedBrowserDisplayer displayer(
62 profile, chrome::GetActiveDesktop());
63 Browser* browser = displayer.browser();
64 if (browser->tab_strip_model()->count() == 0)
65 chrome::AddBlankTabAt(browser, -1, true);
69 void ShowExtensionInstalledBubble(const extensions::Extension* extension,
71 const SkBitmap& icon) {
72 Browser* browser = FindOrCreateVisibleBrowser(profile);
74 chrome::ShowExtensionInstalledBubble(extension, browser, icon);
78 // ErrorInfoBarDelegate -------------------------------------------------------
80 // Helper class to put up an infobar when installation fails.
81 class ErrorInfoBarDelegate : public ConfirmInfoBarDelegate {
83 // Creates an error infobar delegate and adds it to |infobar_service|.
84 static void Create(InfoBarService* infobar_service,
85 const extensions::CrxInstallerError& error);
88 ErrorInfoBarDelegate(InfoBarService* infobar_service,
89 const extensions::CrxInstallerError& error);
90 virtual ~ErrorInfoBarDelegate();
92 // ConfirmInfoBarDelegate:
93 virtual string16 GetMessageText() const OVERRIDE;
94 virtual int GetButtons() const OVERRIDE;
95 virtual string16 GetLinkText() const OVERRIDE;
96 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE;
98 extensions::CrxInstallerError error_;
100 DISALLOW_COPY_AND_ASSIGN(ErrorInfoBarDelegate);
104 void ErrorInfoBarDelegate::Create(InfoBarService* infobar_service,
105 const extensions::CrxInstallerError& error) {
106 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>(
107 new ErrorInfoBarDelegate(infobar_service, error)));
110 ErrorInfoBarDelegate::ErrorInfoBarDelegate(
111 InfoBarService* infobar_service,
112 const extensions::CrxInstallerError& error)
113 : ConfirmInfoBarDelegate(infobar_service),
117 ErrorInfoBarDelegate::~ErrorInfoBarDelegate() {
120 string16 ErrorInfoBarDelegate::GetMessageText() const {
121 return error_.message();
124 int ErrorInfoBarDelegate::GetButtons() const {
128 string16 ErrorInfoBarDelegate::GetLinkText() const {
129 return (error_.type() == extensions::CrxInstallerError::ERROR_OFF_STORE) ?
130 l10n_util::GetStringUTF16(IDS_LEARN_MORE) : string16();
133 bool ErrorInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
134 web_contents()->OpenURL(content::OpenURLParams(
135 GURL("http://support.google.com/chrome_webstore/?p=crx_warning"),
137 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
138 content::PAGE_TRANSITION_LINK, false));
145 // ExtensionInstallUI ---------------------------------------------------------
148 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) {
149 return new ExtensionInstallUIDefault(profile);
153 void ExtensionInstallUI::OpenAppInstalledUI(Profile* profile,
154 const std::string& app_id) {
155 #if defined(OS_CHROMEOS)
156 AppListService::Get(chrome::HOST_DESKTOP_TYPE_ASH)->
157 ShowForProfile(profile);
159 content::NotificationService::current()->Notify(
160 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
161 content::Source<Profile>(profile),
162 content::Details<const std::string>(&app_id));
164 Browser* browser = FindOrCreateVisibleBrowser(profile);
166 GURL url(chrome::IsInstantExtendedAPIEnabled() ?
167 chrome::kChromeUIAppsURL : chrome::kChromeUINewTabURL);
168 chrome::NavigateParams params(
169 chrome::GetSingletonTabNavigateParams(browser, url));
170 chrome::Navigate(¶ms);
172 content::NotificationService::current()->Notify(
173 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
174 content::Source<WebContents>(params.target_contents),
175 content::Details<const std::string>(&app_id));
181 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithBrowser(
183 content::WebContents* web_contents = NULL;
185 web_contents = browser->tab_strip_model()->GetActiveWebContents();
186 return new ExtensionInstallPrompt(web_contents);
190 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithProfile(
192 Browser* browser = chrome::FindLastActiveWithProfile(profile,
193 chrome::GetActiveDesktop());
194 return CreateInstallPromptWithBrowser(browser);
198 // ExtensionInstallUIDefault --------------------------------------------------
200 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile)
201 : ExtensionInstallUI(profile),
202 previous_using_native_theme_(false),
203 use_app_installed_bubble_(false) {
204 // |profile| can be NULL during tests.
206 // Remember the current theme in case the user presses undo.
207 const Extension* previous_theme =
208 ThemeServiceFactory::GetThemeForProfile(profile);
210 previous_theme_id_ = previous_theme->id();
211 previous_using_native_theme_ =
212 ThemeServiceFactory::GetForProfile(profile)->UsingNativeTheme();
216 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() {}
218 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension,
220 if (skip_post_install_ui())
224 // TODO(zelidrag): Figure out what exact conditions cause crash
225 // http://crbug.com/159437 and write browser test to cover it.
230 if (extension->is_theme()) {
231 ThemeInstalledInfoBarDelegate::Create(
232 extension, profile(), previous_theme_id_, previous_using_native_theme_);
236 // Extensions aren't enabled by default in incognito so we confirm
237 // the install in a normal window.
238 Profile* current_profile = profile()->GetOriginalProfile();
239 if (extension->is_app()) {
240 bool use_bubble = false;
242 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
243 CommandLine* cmdline = CommandLine::ForCurrentProcess();
244 use_bubble = (use_app_installed_bubble_ ||
245 cmdline->HasSwitch(switches::kAppsNewInstallBubble));
248 if (IsAppLauncherEnabled()) {
249 // TODO(tapted): ExtensionInstallUI should retain the desktop type from
250 // the browser used to initiate the flow. http://crbug.com/308360.
251 AppListService::Get(chrome::GetActiveDesktop())->
252 ShowForProfile(current_profile);
254 content::NotificationService::current()->Notify(
255 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
256 content::Source<Profile>(current_profile),
257 content::Details<const std::string>(&extension->id()));
262 ShowExtensionInstalledBubble(extension, current_profile, *icon);
266 ExtensionInstallUI::OpenAppInstalledUI(current_profile, extension->id());
270 ShowExtensionInstalledBubble(extension, current_profile, *icon);
273 void ExtensionInstallUIDefault::OnInstallFailure(
274 const extensions::CrxInstallerError& error) {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
276 if (disable_failure_ui_for_tests() || skip_post_install_ui())
280 chrome::FindLastActiveWithProfile(profile(), chrome::GetActiveDesktop());
281 if (!browser) // Can be NULL in unittests.
283 WebContents* web_contents =
284 browser->tab_strip_model()->GetActiveWebContents();
287 ErrorInfoBarDelegate::Create(InfoBarService::FromWebContents(web_contents),
291 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) {
292 use_app_installed_bubble_ = use_bubble;