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/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/extension_install_prompt.h"
11 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/prefs/incognito_mode_prefs.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/themes/theme_service.h"
17 #include "chrome/browser/themes/theme_service_factory.h"
18 #include "chrome/browser/ui/app_list/app_list_service.h"
19 #include "chrome/browser/ui/app_list/app_list_util.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_dialogs.h"
22 #include "chrome/browser/ui/browser_finder.h"
23 #include "chrome/browser/ui/browser_navigator.h"
24 #include "chrome/browser/ui/browser_tabstrip.h"
25 #include "chrome/browser/ui/browser_window.h"
26 #include "chrome/browser/ui/host_desktop.h"
27 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
28 #include "chrome/browser/ui/simple_message_box.h"
29 #include "chrome/browser/ui/singleton_tabs.h"
30 #include "chrome/browser/ui/tabs/tab_strip_model.h"
31 #include "chrome/common/url_constants.h"
32 #include "components/infobars/core/confirm_infobar_delegate.h"
33 #include "components/infobars/core/infobar.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/web_contents.h"
37 #include "extensions/common/extension.h"
38 #include "grit/generated_resources.h"
39 #include "grit/theme_resources.h"
40 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/base/resource/resource_bundle.h"
44 #include "ash/shell.h"
47 using content::BrowserThread
;
48 using content::WebContents
;
49 using extensions::Extension
;
53 // Helpers --------------------------------------------------------------------
55 Browser
* FindOrCreateVisibleBrowser(Profile
* profile
) {
56 // TODO(mpcomplete): remove this workaround for http://crbug.com/244246
57 // after fixing http://crbug.com/38676.
58 if (!IncognitoModePrefs::CanOpenBrowser(profile
))
60 chrome::ScopedTabbedBrowserDisplayer
displayer(
61 profile
, chrome::GetActiveDesktop());
62 Browser
* browser
= displayer
.browser();
63 if (browser
->tab_strip_model()->count() == 0)
64 chrome::AddTabAt(browser
, GURL(), -1, true);
68 void ShowExtensionInstalledBubble(const extensions::Extension
* extension
,
70 const SkBitmap
& icon
) {
71 Browser
* browser
= FindOrCreateVisibleBrowser(profile
);
73 chrome::ShowExtensionInstalledBubble(extension
, browser
, icon
);
77 // ErrorInfoBarDelegate -------------------------------------------------------
79 // Helper class to put up an infobar when installation fails.
80 class ErrorInfoBarDelegate
: public ConfirmInfoBarDelegate
{
82 // Creates an error infobar and delegate and adds the infobar to
84 static void Create(InfoBarService
* infobar_service
,
85 const extensions::CrxInstallerError
& error
);
88 explicit ErrorInfoBarDelegate(const extensions::CrxInstallerError
& error
);
89 virtual ~ErrorInfoBarDelegate();
91 // ConfirmInfoBarDelegate:
92 virtual base::string16
GetMessageText() const OVERRIDE
;
93 virtual int GetButtons() const OVERRIDE
;
94 virtual base::string16
GetLinkText() const OVERRIDE
;
95 virtual bool LinkClicked(WindowOpenDisposition disposition
) OVERRIDE
;
97 extensions::CrxInstallerError error_
;
99 DISALLOW_COPY_AND_ASSIGN(ErrorInfoBarDelegate
);
103 void ErrorInfoBarDelegate::Create(InfoBarService
* infobar_service
,
104 const extensions::CrxInstallerError
& error
) {
105 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
106 scoped_ptr
<ConfirmInfoBarDelegate
>(new ErrorInfoBarDelegate(error
))));
109 ErrorInfoBarDelegate::ErrorInfoBarDelegate(
110 const extensions::CrxInstallerError
& error
)
111 : ConfirmInfoBarDelegate(),
115 ErrorInfoBarDelegate::~ErrorInfoBarDelegate() {
118 base::string16
ErrorInfoBarDelegate::GetMessageText() const {
119 return error_
.message();
122 int ErrorInfoBarDelegate::GetButtons() const {
126 base::string16
ErrorInfoBarDelegate::GetLinkText() const {
127 return (error_
.type() == extensions::CrxInstallerError::ERROR_OFF_STORE
) ?
128 l10n_util::GetStringUTF16(IDS_LEARN_MORE
) : base::string16();
131 bool ErrorInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition
) {
132 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
133 content::OpenURLParams(
134 GURL("http://support.google.com/chrome_webstore/?p=crx_warning"),
136 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
137 content::PAGE_TRANSITION_LINK
, false));
144 // ExtensionInstallUI ---------------------------------------------------------
147 ExtensionInstallUI
* ExtensionInstallUI::Create(Profile
* profile
) {
148 return new ExtensionInstallUIDefault(profile
);
152 void ExtensionInstallUI::OpenAppInstalledUI(Profile
* profile
,
153 const std::string
& app_id
) {
154 #if defined(OS_CHROMEOS)
155 AppListService::Get(chrome::HOST_DESKTOP_TYPE_ASH
)->
156 ShowForProfile(profile
);
158 content::NotificationService::current()->Notify(
159 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST
,
160 content::Source
<Profile
>(profile
),
161 content::Details
<const std::string
>(&app_id
));
163 Browser
* browser
= FindOrCreateVisibleBrowser(profile
);
165 GURL
url(chrome::IsInstantExtendedAPIEnabled() ?
166 chrome::kChromeUIAppsURL
: chrome::kChromeUINewTabURL
);
167 chrome::NavigateParams
params(
168 chrome::GetSingletonTabNavigateParams(browser
, url
));
169 chrome::Navigate(¶ms
);
171 content::NotificationService::current()->Notify(
172 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP
,
173 content::Source
<WebContents
>(params
.target_contents
),
174 content::Details
<const std::string
>(&app_id
));
180 ExtensionInstallPrompt
* ExtensionInstallUI::CreateInstallPromptWithBrowser(
182 content::WebContents
* web_contents
= NULL
;
184 web_contents
= browser
->tab_strip_model()->GetActiveWebContents();
185 return new ExtensionInstallPrompt(web_contents
);
189 ExtensionInstallPrompt
* ExtensionInstallUI::CreateInstallPromptWithProfile(
191 Browser
* browser
= chrome::FindLastActiveWithProfile(profile
,
192 chrome::GetActiveDesktop());
194 return CreateInstallPromptWithBrowser(browser
);
195 // No browser window is open yet. Create a free-standing dialog associated
197 return new ExtensionInstallPrompt(profile
, NULL
, NULL
);
201 // ExtensionInstallUIDefault --------------------------------------------------
203 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile
* profile
)
204 : ExtensionInstallUI(profile
),
205 previous_using_system_theme_(false),
206 use_app_installed_bubble_(false) {
207 // |profile| can be NULL during tests.
209 // Remember the current theme in case the user presses undo.
210 const Extension
* previous_theme
=
211 ThemeServiceFactory::GetThemeForProfile(profile
);
213 previous_theme_id_
= previous_theme
->id();
214 previous_using_system_theme_
=
215 ThemeServiceFactory::GetForProfile(profile
)->UsingSystemTheme();
219 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() {}
221 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension
* extension
,
223 if (skip_post_install_ui())
227 // TODO(zelidrag): Figure out what exact conditions cause crash
228 // http://crbug.com/159437 and write browser test to cover it.
233 if (extension
->is_theme()) {
234 ThemeInstalledInfoBarDelegate::Create(
235 extension
, profile(), previous_theme_id_
, previous_using_system_theme_
);
239 // Extensions aren't enabled by default in incognito so we confirm
240 // the install in a normal window.
241 Profile
* current_profile
= profile()->GetOriginalProfile();
242 if (extension
->is_app()) {
243 bool use_bubble
= false;
245 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
246 use_bubble
= use_app_installed_bubble_
;
249 if (IsAppLauncherEnabled()) {
250 // TODO(tapted): ExtensionInstallUI should retain the desktop type from
251 // the browser used to initiate the flow. http://crbug.com/308360.
252 AppListService::Get(chrome::GetActiveDesktop())->
253 ShowForProfile(current_profile
);
255 content::NotificationService::current()->Notify(
256 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST
,
257 content::Source
<Profile
>(current_profile
),
258 content::Details
<const std::string
>(&extension
->id()));
263 ShowExtensionInstalledBubble(extension
, current_profile
, *icon
);
267 ExtensionInstallUI::OpenAppInstalledUI(current_profile
, extension
->id());
271 ShowExtensionInstalledBubble(extension
, current_profile
, *icon
);
274 void ExtensionInstallUIDefault::OnInstallFailure(
275 const extensions::CrxInstallerError
& error
) {
276 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
277 if (disable_failure_ui_for_tests() || skip_post_install_ui())
281 chrome::FindLastActiveWithProfile(profile(), chrome::GetActiveDesktop());
282 if (!browser
) // Can be NULL in unittests.
284 WebContents
* web_contents
=
285 browser
->tab_strip_model()->GetActiveWebContents();
288 ErrorInfoBarDelegate::Create(InfoBarService::FromWebContents(web_contents
),
292 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble
) {
293 use_app_installed_bubble_
= use_bubble
;