Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: chrome/browser/extensions/extension_install_ui_default.cc

Issue 10388252: Refactoring ExtenionInstallUI to abstract the Browser references. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Synced + mac fix Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/command_line.h"
8 #include "chrome/browser/extensions/extension_install_prompt.h"
9 #include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
10 #include "chrome/browser/infobars/infobar_tab_helper.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/themes/theme_service.h"
13 #include "chrome/browser/themes/theme_service_factory.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_navigator.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/simple_message_box.h"
20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
21 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
22 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/extensions/extension.h"
25 #include "chrome/common/url_constants.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/notification_service.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32
33 #if defined(USE_ASH)
34 #include "ash/shell.h"
35 #endif
36
37 using content::BrowserThread;
38 using content::WebContents;
39 using extensions::Extension;
40
41 namespace {
42
43 bool disable_failure_ui_for_tests = false;
44
45 } // namespace
46
47 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile)
48 : profile_(profile),
49 skip_post_install_ui_(false),
50 previous_using_native_theme_(false),
51 use_app_installed_bubble_(false) {
52 // Remember the current theme in case the user presses undo.
53 if (profile) {
54 const Extension* previous_theme =
55 ThemeServiceFactory::GetThemeForProfile(profile);
56 if (previous_theme)
57 previous_theme_id_ = previous_theme->id();
58 previous_using_native_theme_ =
59 ThemeServiceFactory::GetForProfile(profile)->UsingNativeTheme();
60 }
61 }
62
63 ExtensionInstallUIDefault::~ExtensionInstallUIDefault() {
64 }
65
66 void ExtensionInstallUIDefault::OnInstallSuccess(const Extension* extension,
67 SkBitmap* icon) {
68 if (skip_post_install_ui_)
69 return;
70
71 if (extension->is_theme()) {
72 ShowThemeInfoBar(previous_theme_id_, previous_using_native_theme_,
73 extension, profile_);
74 return;
75 }
76
77 // Extensions aren't enabled by default in incognito so we confirm
78 // the install in a normal window.
79 Profile* current_profile = profile_->GetOriginalProfile();
80 Browser* browser = browser::FindOrCreateTabbedBrowser(current_profile);
81 if (browser->tab_count() == 0)
82 browser->AddBlankTab(true);
83 browser->window()->Show();
84
85 bool use_bubble_for_apps = false;
86
87 #if defined(TOOLKIT_VIEWS)
88 CommandLine* cmdline = CommandLine::ForCurrentProcess();
89 use_bubble_for_apps = (use_app_installed_bubble_ ||
90 cmdline->HasSwitch(switches::kAppsNewInstallBubble));
91 #endif
92
93 if (extension->is_app() && !use_bubble_for_apps) {
94 ExtensionInstallUI::OpenAppInstalledUI(browser, extension->id());
95 return;
96 }
97
98 browser::ShowExtensionInstalledBubble(extension, browser, *icon,
99 current_profile);
100 }
101
102 void ExtensionInstallUIDefault::OnInstallFailure(const string16& error) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 if (disable_failure_ui_for_tests || skip_post_install_ui_)
105 return;
106
107 Browser* browser = browser::FindLastActiveWithProfile(profile_);
108 browser::ShowMessageBox(browser ? browser->window()->GetNativeWindow() : NULL,
109 l10n_util::GetStringUTF16(IDS_EXTENSION_INSTALL_FAILURE_TITLE), error,
110 browser::MESSAGE_BOX_TYPE_WARNING);
111 }
112
113 void ExtensionInstallUIDefault::SetSkipPostInstallUI(bool skip_ui) {
114 skip_post_install_ui_ = skip_ui;
115 }
116
117 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) {
118 use_app_installed_bubble_ = use_bubble;
119 }
120
121 // static
122 void ExtensionInstallUIDefault::ShowThemeInfoBar(
123 const std::string& previous_theme_id, bool previous_using_native_theme,
124 const Extension* new_theme, Profile* profile) {
125 if (!new_theme->is_theme())
126 return;
127
128 // Get last active tabbed browser of profile.
129 Browser* browser = browser::FindTabbedBrowser(profile, true);
130 if (!browser)
131 return;
132
133 TabContentsWrapper* tab_contents = browser->GetSelectedTabContentsWrapper();
134 if (!tab_contents)
135 return;
136 InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper();
137
138 // First find any previous theme preview infobars.
139 InfoBarDelegate* old_delegate = NULL;
140 for (size_t i = 0; i < infobar_helper->infobar_count(); ++i) {
141 InfoBarDelegate* delegate = infobar_helper->GetInfoBarDelegateAt(i);
142 ThemeInstalledInfoBarDelegate* theme_infobar =
143 delegate->AsThemePreviewInfobarDelegate();
144 if (theme_infobar) {
145 // If the user installed the same theme twice, ignore the second install
146 // and keep the first install info bar, so that they can easily undo to
147 // get back the previous theme.
148 if (theme_infobar->MatchesTheme(new_theme))
149 return;
150 old_delegate = delegate;
151 break;
152 }
153 }
154
155 // Then either replace that old one or add a new one.
156 InfoBarDelegate* new_delegate = GetNewThemeInstalledInfoBarDelegate(
157 tab_contents, new_theme, previous_theme_id, previous_using_native_theme);
158
159 if (old_delegate)
160 infobar_helper->ReplaceInfoBar(old_delegate, new_delegate);
161 else
162 infobar_helper->AddInfoBar(new_delegate);
163 }
164
165 InfoBarDelegate* ExtensionInstallUIDefault::GetNewThemeInstalledInfoBarDelegate(
166 TabContentsWrapper* tab_contents,
167 const Extension* new_theme,
168 const std::string& previous_theme_id,
169 bool previous_using_native_theme) {
170 Profile* profile = tab_contents->profile();
171 return new ThemeInstalledInfoBarDelegate(
172 tab_contents->infobar_tab_helper(),
173 profile->GetExtensionService(),
174 ThemeServiceFactory::GetForProfile(profile),
175 new_theme,
176 previous_theme_id,
177 previous_using_native_theme);
178 }
179
180 // static
181 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) {
182 return new ExtensionInstallUIDefault(profile);
183 }
184
185 // static
186 void ExtensionInstallUI::OpenAppInstalledUI(Browser* browser,
187 const std::string& app_id) {
188 if (NewTabUI::ShouldShowApps()) {
189 browser::NavigateParams params = browser->GetSingletonTabNavigateParams(
190 GURL(chrome::kChromeUINewTabURL));
191 browser::Navigate(&params);
192
193 content::NotificationService::current()->Notify(
194 chrome::NOTIFICATION_APP_INSTALLED_TO_NTP,
195 content::Source<WebContents>(params.target_contents->web_contents()),
196 content::Details<const std::string>(&app_id));
197 } else {
198 #if defined(USE_ASH)
199 ash::Shell::GetInstance()->ToggleAppList();
200
201 content::NotificationService::current()->Notify(
202 chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
203 content::Source<Profile>(browser->profile()),
204 content::Details<const std::string>(&app_id));
205 #else
206 NOTREACHED();
207 #endif
208 }
209 }
210
211 // static
212 void ExtensionInstallUI::DisableFailureUIForTests() {
213 disable_failure_ui_for_tests = true;
214 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_install_ui_default.h ('k') | chrome/browser/extensions/extension_management_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698