Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / extensions / extension_install_ui_default.cc
blobdd8ca0a2a5d74221ff523234e8c5b994fd9763ad
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 "base/bind.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/themes/theme_service.h"
16 #include "chrome/browser/themes/theme_service_factory.h"
17 #include "chrome/browser/ui/app_list/app_list_service.h"
18 #include "chrome/browser/ui/app_list/app_list_util.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_dialogs.h"
21 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/browser_navigator.h"
23 #include "chrome/browser/ui/browser_tabstrip.h"
24 #include "chrome/browser/ui/browser_window.h"
25 #include "chrome/browser/ui/host_desktop.h"
26 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
27 #include "chrome/browser/ui/simple_message_box.h"
28 #include "chrome/browser/ui/singleton_tabs.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
30 #include "chrome/common/url_constants.h"
31 #include "chrome/grit/generated_resources.h"
32 #include "components/infobars/core/confirm_infobar_delegate.h"
33 #include "components/infobars/core/infobar.h"
34 #include "components/search/search.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 "extensions/common/extension.h"
39 #include "grit/components_strings.h"
40 #include "ui/base/l10n/l10n_util.h"
42 #if defined(USE_ASH)
43 #include "ash/shell.h"
44 #endif
46 using content::BrowserThread;
47 using content::WebContents;
48 using extensions::Extension;
50 namespace {
52 // Helpers --------------------------------------------------------------------
54 Browser* FindOrCreateVisibleBrowser(Profile* profile) {
55 // TODO(mpcomplete): remove this workaround for http://crbug.com/244246
56 // after fixing http://crbug.com/38676.
57 if (!IncognitoModePrefs::CanOpenBrowser(profile))
58 return NULL;
59 chrome::ScopedTabbedBrowserDisplayer displayer(
60 profile, chrome::GetActiveDesktop());
61 Browser* browser = displayer.browser();
62 if (browser->tab_strip_model()->count() == 0)
63 chrome::AddTabAt(browser, GURL(), -1, true);
64 return browser;
67 void ShowExtensionInstalledBubble(const extensions::Extension* extension,
68 Profile* profile,
69 const SkBitmap& icon) {
70 Browser* browser = FindOrCreateVisibleBrowser(profile);
71 if (browser)
72 chrome::ShowExtensionInstalledBubble(extension, browser, icon);
76 // ErrorInfoBarDelegate -------------------------------------------------------
78 // Helper class to put up an infobar when installation fails.
79 class ErrorInfoBarDelegate : public ConfirmInfoBarDelegate {
80 public:
81 // Creates an error infobar and delegate and adds the infobar to
82 // |infobar_service|.
83 static void Create(InfoBarService* infobar_service,
84 const extensions::CrxInstallerError& error);
86 private:
87 explicit ErrorInfoBarDelegate(const extensions::CrxInstallerError& error);
88 virtual ~ErrorInfoBarDelegate();
90 // ConfirmInfoBarDelegate:
91 virtual base::string16 GetMessageText() const override;
92 virtual int GetButtons() const override;
93 virtual base::string16 GetLinkText() const override;
94 virtual bool LinkClicked(WindowOpenDisposition disposition) override;
96 extensions::CrxInstallerError error_;
98 DISALLOW_COPY_AND_ASSIGN(ErrorInfoBarDelegate);
101 // static
102 void ErrorInfoBarDelegate::Create(InfoBarService* infobar_service,
103 const extensions::CrxInstallerError& error) {
104 infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
105 scoped_ptr<ConfirmInfoBarDelegate>(new ErrorInfoBarDelegate(error))));
108 ErrorInfoBarDelegate::ErrorInfoBarDelegate(
109 const extensions::CrxInstallerError& error)
110 : ConfirmInfoBarDelegate(),
111 error_(error) {
114 ErrorInfoBarDelegate::~ErrorInfoBarDelegate() {
117 base::string16 ErrorInfoBarDelegate::GetMessageText() const {
118 return error_.message();
121 int ErrorInfoBarDelegate::GetButtons() const {
122 return BUTTON_OK;
125 base::string16 ErrorInfoBarDelegate::GetLinkText() const {
126 return (error_.type() == extensions::CrxInstallerError::ERROR_OFF_STORE) ?
127 l10n_util::GetStringUTF16(IDS_LEARN_MORE) : base::string16();
130 bool ErrorInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
131 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
132 content::OpenURLParams(
133 GURL("http://support.google.com/chrome_webstore/?p=crx_warning"),
134 content::Referrer(),
135 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
136 ui::PAGE_TRANSITION_LINK, false));
137 return false;
140 } // namespace
143 // ExtensionInstallUI ---------------------------------------------------------
145 // static
146 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) {
147 return new ExtensionInstallUIDefault(profile);
150 // static
151 void ExtensionInstallUI::OpenAppInstalledUI(Profile* profile,
152 const std::string& app_id) {
153 #if defined(OS_CHROMEOS)
154 // App Launcher always enabled on ChromeOS, so always handled in
155 // OnInstallSuccess.
156 NOTREACHED();
157 #else
158 Browser* browser = FindOrCreateVisibleBrowser(profile);
159 if (browser) {
160 GURL url(chrome::IsInstantExtendedAPIEnabled() ?
161 chrome::kChromeUIAppsURL : chrome::kChromeUINewTabURL);
162 chrome::NavigateParams params(
163 chrome::GetSingletonTabNavigateParams(browser, url));
164 chrome::Navigate(&params);
166 content::NotificationService::current()->Notify(
167 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
168 content::Source<WebContents>(params.target_contents),
169 content::Details<const std::string>(&app_id));
171 #endif
174 // static
175 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithBrowser(
176 Browser* browser) {
177 content::WebContents* web_contents = NULL;
178 if (browser)
179 web_contents = browser->tab_strip_model()->GetActiveWebContents();
180 return new ExtensionInstallPrompt(web_contents);
183 // static
184 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithProfile(
185 Profile* profile) {
186 Browser* browser = chrome::FindLastActiveWithProfile(profile,
187 chrome::GetActiveDesktop());
188 if (browser)
189 return CreateInstallPromptWithBrowser(browser);
190 // No browser window is open yet. Create a free-standing dialog associated
191 // with |profile|.
192 return new ExtensionInstallPrompt(profile, NULL, NULL);
196 // ExtensionInstallUIDefault --------------------------------------------------
198 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile)
199 : ExtensionInstallUI(profile),
200 previous_using_system_theme_(false),
201 use_app_installed_bubble_(false) {
202 // |profile| can be NULL during tests.
203 if (profile) {
204 // Remember the current theme in case the user presses undo.
205 const Extension* previous_theme =
206 ThemeServiceFactory::GetThemeForProfile(profile);
207 if (previous_theme)
208 previous_theme_id_ = previous_theme->id();
209 previous_using_system_theme_ =
210 ThemeServiceFactory::GetForProfile(profile)->UsingSystemTheme();
214 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() {}
216 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension,
217 const SkBitmap* icon) {
218 if (skip_post_install_ui())
219 return;
221 if (!profile()) {
222 // TODO(zelidrag): Figure out what exact conditions cause crash
223 // http://crbug.com/159437 and write browser test to cover it.
224 NOTREACHED();
225 return;
228 if (extension->is_theme()) {
229 ThemeInstalledInfoBarDelegate::Create(
230 extension, profile(), previous_theme_id_, previous_using_system_theme_);
231 return;
234 // Extensions aren't enabled by default in incognito so we confirm
235 // the install in a normal window.
236 Profile* current_profile = profile()->GetOriginalProfile();
237 if (extension->is_app()) {
238 bool use_bubble = false;
240 #if defined(TOOLKIT_VIEWS) || defined(OS_MACOSX)
241 use_bubble = use_app_installed_bubble_;
242 #endif
244 if (IsAppLauncherEnabled()) {
245 // TODO(tapted): ExtensionInstallUI should retain the desktop type from
246 // the browser used to initiate the flow. http://crbug.com/308360.
247 AppListService::Get(chrome::GetActiveDesktop())
248 ->ShowForAppInstall(current_profile, extension->id(), false);
249 return;
252 if (use_bubble) {
253 ShowExtensionInstalledBubble(extension, current_profile, *icon);
254 return;
257 ExtensionInstallUI::OpenAppInstalledUI(current_profile, extension->id());
258 return;
261 ShowExtensionInstalledBubble(extension, current_profile, *icon);
264 void ExtensionInstallUIDefault::OnInstallFailure(
265 const extensions::CrxInstallerError& error) {
266 DCHECK_CURRENTLY_ON(BrowserThread::UI);
267 if (disable_failure_ui_for_tests() || skip_post_install_ui())
268 return;
270 Browser* browser =
271 chrome::FindLastActiveWithProfile(profile(), chrome::GetActiveDesktop());
272 if (!browser) // Can be NULL in unittests.
273 return;
274 WebContents* web_contents =
275 browser->tab_strip_model()->GetActiveWebContents();
276 if (!web_contents)
277 return;
278 ErrorInfoBarDelegate::Create(InfoBarService::FromWebContents(web_contents),
279 error);
282 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) {
283 use_app_installed_bubble_ = use_bubble;