Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_install_ui_default.cc
blob41efdffb3e59dc5aa06f3e729e7cc6beee730ce7
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"
7 #include "apps/app_launcher.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_install_prompt.h"
13 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
14 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
15 #include "chrome/browser/infobars/infobar_service.h"
16 #include "chrome/browser/prefs/incognito_mode_prefs.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search/search.h"
19 #include "chrome/browser/themes/theme_service.h"
20 #include "chrome/browser/themes/theme_service_factory.h"
21 #include "chrome/browser/ui/app_list/app_list_service.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/simple_message_box.h"
30 #include "chrome/browser/ui/singleton_tabs.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/extension.h"
34 #include "chrome/common/url_constants.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/web_contents.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"
43 #if defined(USE_ASH)
44 #include "ash/shell.h"
45 #endif
47 using content::BrowserThread;
48 using content::WebContents;
49 using extensions::Extension;
51 namespace {
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))
60 return NULL;
61 Browser* browser =
62 chrome::FindOrCreateTabbedBrowser(profile, chrome::GetActiveDesktop());
63 if (browser->tab_strip_model()->count() == 0)
64 chrome::AddBlankTabAt(browser, -1, true);
65 browser->window()->Show();
66 return browser;
69 void ShowExtensionInstalledBubble(const extensions::Extension* extension,
70 Profile* profile,
71 const SkBitmap& icon) {
72 Browser* browser = FindOrCreateVisibleBrowser(profile);
73 if (browser)
74 chrome::ShowExtensionInstalledBubble(extension, browser, icon);
78 // ErrorInfoBarDelegate -------------------------------------------------------
80 // Helper class to put up an infobar when installation fails.
81 class ErrorInfoBarDelegate : public ConfirmInfoBarDelegate {
82 public:
83 // Creates an error infobar delegate and adds it to |infobar_service|.
84 static void Create(InfoBarService* infobar_service,
85 const extensions::CrxInstallerError& error);
87 private:
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);
103 // static
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),
114 error_(error) {
117 ErrorInfoBarDelegate::~ErrorInfoBarDelegate() {
120 string16 ErrorInfoBarDelegate::GetMessageText() const {
121 return error_.message();
124 int ErrorInfoBarDelegate::GetButtons() const {
125 return BUTTON_OK;
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"),
136 content::Referrer(),
137 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
138 content::PAGE_TRANSITION_LINK, false));
139 return false;
142 } // namespace
145 // ExtensionInstallUI ---------------------------------------------------------
147 // static
148 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) {
149 return new ExtensionInstallUIDefault(profile);
152 // static
153 void ExtensionInstallUI::OpenAppInstalledUI(Profile* profile,
154 const std::string& app_id) {
155 #if defined(OS_CHROMEOS)
156 AppListService::Get()->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));
162 #else
163 Browser* browser = FindOrCreateVisibleBrowser(profile);
164 if (browser) {
165 GURL url(chrome::IsInstantExtendedAPIEnabled() ?
166 chrome::kChromeUIAppsURL : chrome::kChromeUINewTabURL);
167 chrome::NavigateParams params(
168 chrome::GetSingletonTabNavigateParams(browser, url));
169 chrome::Navigate(&params);
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));
176 #endif
179 // static
180 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithBrowser(
181 Browser* browser) {
182 content::WebContents* web_contents = NULL;
183 if (browser)
184 web_contents = browser->tab_strip_model()->GetActiveWebContents();
185 return new ExtensionInstallPrompt(web_contents);
188 // static
189 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithProfile(
190 Profile* profile) {
191 Browser* browser = chrome::FindLastActiveWithProfile(profile,
192 chrome::GetActiveDesktop());
193 return CreateInstallPromptWithBrowser(browser);
197 // ExtensionInstallUIDefault --------------------------------------------------
199 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile)
200 : ExtensionInstallUI(profile),
201 previous_using_native_theme_(false),
202 use_app_installed_bubble_(false) {
203 // |profile| can be NULL during tests.
204 if (profile) {
205 // Remember the current theme in case the user presses undo.
206 const Extension* previous_theme =
207 ThemeServiceFactory::GetThemeForProfile(profile);
208 if (previous_theme)
209 previous_theme_id_ = previous_theme->id();
210 previous_using_native_theme_ =
211 ThemeServiceFactory::GetForProfile(profile)->UsingNativeTheme();
215 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() {}
217 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension,
218 SkBitmap* icon) {
219 if (skip_post_install_ui())
220 return;
222 if (!profile()) {
223 // TODO(zelidrag): Figure out what exact conditions cause crash
224 // http://crbug.com/159437 and write browser test to cover it.
225 NOTREACHED();
226 return;
229 if (extension->is_theme()) {
230 ThemeInstalledInfoBarDelegate::Create(
231 extension, profile(), previous_theme_id_, previous_using_native_theme_);
232 return;
235 // Extensions aren't enabled by default in incognito so we confirm
236 // the install in a normal window.
237 Profile* current_profile = profile()->GetOriginalProfile();
238 if (extension->is_app()) {
239 bool use_bubble = false;
241 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
242 CommandLine* cmdline = CommandLine::ForCurrentProcess();
243 use_bubble = (use_app_installed_bubble_ ||
244 cmdline->HasSwitch(switches::kAppsNewInstallBubble));
245 #endif
247 if (apps::IsAppLauncherEnabled()) {
248 AppListService::Get()->ShowForProfile(current_profile);
250 content::NotificationService::current()->Notify(
251 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
252 content::Source<Profile>(current_profile),
253 content::Details<const std::string>(&extension->id()));
254 return;
257 if (use_bubble) {
258 ShowExtensionInstalledBubble(extension, current_profile, *icon);
259 return;
262 ExtensionInstallUI::OpenAppInstalledUI(current_profile, extension->id());
263 return;
266 ShowExtensionInstalledBubble(extension, current_profile, *icon);
269 void ExtensionInstallUIDefault::OnInstallFailure(
270 const extensions::CrxInstallerError& error) {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
272 if (disable_failure_ui_for_tests() || skip_post_install_ui())
273 return;
275 Browser* browser =
276 chrome::FindLastActiveWithProfile(profile(), chrome::GetActiveDesktop());
277 if (!browser) // Can be NULL in unittests.
278 return;
279 WebContents* web_contents =
280 browser->tab_strip_model()->GetActiveWebContents();
281 if (!web_contents)
282 return;
283 ErrorInfoBarDelegate::Create(InfoBarService::FromWebContents(web_contents),
284 error);
287 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) {
288 use_app_installed_bubble_ = use_bubble;