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

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

Powered by Google App Engine
This is Rietveld 408576698