OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_install_ui_default.h" | 5 #include "chrome/browser/extensions/extension_install_ui_default.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | 9 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
10 #include "chrome/browser/api/infobars/infobar_service.h" | 10 #include "chrome/browser/api/infobars/infobar_service.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 using content::WebContents; | 45 using content::WebContents; |
46 using extensions::Extension; | 46 using extensions::Extension; |
47 | 47 |
48 namespace { | 48 namespace { |
49 | 49 |
50 bool disable_failure_ui_for_tests = false; | 50 bool disable_failure_ui_for_tests = false; |
51 | 51 |
52 // Helper class to put up an infobar when installation fails. | 52 // Helper class to put up an infobar when installation fails. |
53 class ErrorInfobarDelegate : public ConfirmInfoBarDelegate { | 53 class ErrorInfobarDelegate : public ConfirmInfoBarDelegate { |
54 public: | 54 public: |
| 55 // Creates an error delegate and adds it to |infobar_service|. |
| 56 static void Create(InfoBarService* infobar_service, |
| 57 Browser* browser, |
| 58 const extensions::CrxInstallerError& error); |
| 59 |
| 60 private: |
55 ErrorInfobarDelegate(InfoBarService* infobar_service, | 61 ErrorInfobarDelegate(InfoBarService* infobar_service, |
56 Browser* browser, | 62 Browser* browser, |
57 const extensions::CrxInstallerError& error) | 63 const extensions::CrxInstallerError& error) |
58 : ConfirmInfoBarDelegate(infobar_service), | 64 : ConfirmInfoBarDelegate(infobar_service), |
59 browser_(browser), | 65 browser_(browser), |
60 error_(error) { | 66 error_(error) { |
61 } | 67 } |
62 | 68 |
63 private: | |
64 virtual string16 GetMessageText() const OVERRIDE { | 69 virtual string16 GetMessageText() const OVERRIDE { |
65 return error_.message(); | 70 return error_.message(); |
66 } | 71 } |
67 | 72 |
68 virtual int GetButtons() const OVERRIDE { | 73 virtual int GetButtons() const OVERRIDE { |
69 return BUTTON_OK; | 74 return BUTTON_OK; |
70 } | 75 } |
71 | 76 |
72 virtual string16 GetLinkText() const OVERRIDE { | 77 virtual string16 GetLinkText() const OVERRIDE { |
73 return error_.type() == extensions::CrxInstallerError::ERROR_OFF_STORE ? | 78 return error_.type() == extensions::CrxInstallerError::ERROR_OFF_STORE ? |
74 l10n_util::GetStringUTF16(IDS_LEARN_MORE) : ASCIIToUTF16(""); | 79 l10n_util::GetStringUTF16(IDS_LEARN_MORE) : ASCIIToUTF16(""); |
75 } | 80 } |
76 | 81 |
77 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE { | 82 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE { |
78 chrome::NavigateParams params( | 83 chrome::NavigateParams params( |
79 browser_, | 84 browser_, |
80 GURL("http://support.google.com/chrome_webstore/?p=crx_warning"), | 85 GURL("http://support.google.com/chrome_webstore/?p=crx_warning"), |
81 content::PAGE_TRANSITION_LINK); | 86 content::PAGE_TRANSITION_LINK); |
82 params.disposition = NEW_FOREGROUND_TAB; | 87 params.disposition = NEW_FOREGROUND_TAB; |
83 chrome::Navigate(¶ms); | 88 chrome::Navigate(¶ms); |
84 return false; | 89 return false; |
85 } | 90 } |
86 | 91 |
87 Browser* browser_; | 92 Browser* browser_; |
88 extensions::CrxInstallerError error_; | 93 extensions::CrxInstallerError error_; |
89 }; | 94 }; |
90 | 95 |
| 96 // static |
| 97 void ErrorInfobarDelegate::Create(InfoBarService* infobar_service, |
| 98 Browser* browser, |
| 99 const extensions::CrxInstallerError& error) { |
| 100 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( |
| 101 new ErrorInfobarDelegate(infobar_service, browser, error))); |
| 102 } |
| 103 |
91 } // namespace | 104 } // namespace |
92 | 105 |
93 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile) | 106 ExtensionInstallUIDefault::ExtensionInstallUIDefault(Profile* profile) |
94 : skip_post_install_ui_(false), | 107 : skip_post_install_ui_(false), |
95 previous_using_native_theme_(false), | 108 previous_using_native_theme_(false), |
96 use_app_installed_bubble_(false) { | 109 use_app_installed_bubble_(false) { |
97 profile_ = profile; | 110 profile_ = profile; |
98 | 111 |
99 // |profile_| can be NULL during tests. | 112 // |profile_| can be NULL during tests. |
100 if (profile_) { | 113 if (profile_) { |
(...skipping 16 matching lines...) Expand all Loading... |
117 return; | 130 return; |
118 | 131 |
119 if (!profile_) { | 132 if (!profile_) { |
120 // TODO(zelidrag): Figure out what exact conditions cause crash | 133 // TODO(zelidrag): Figure out what exact conditions cause crash |
121 // http://crbug.com/159437 and write browser test to cover it. | 134 // http://crbug.com/159437 and write browser test to cover it. |
122 NOTREACHED(); | 135 NOTREACHED(); |
123 return; | 136 return; |
124 } | 137 } |
125 | 138 |
126 if (extension->is_theme()) { | 139 if (extension->is_theme()) { |
127 ShowThemeInfoBar(previous_theme_id_, previous_using_native_theme_, | 140 ThemeInstalledInfoBarDelegate::Create( |
128 extension, profile_); | 141 extension, profile_, previous_theme_id_, previous_using_native_theme_); |
129 return; | 142 return; |
130 } | 143 } |
131 | 144 |
132 // Extensions aren't enabled by default in incognito so we confirm | 145 // Extensions aren't enabled by default in incognito so we confirm |
133 // the install in a normal window. | 146 // the install in a normal window. |
134 Profile* current_profile = profile_->GetOriginalProfile(); | 147 Profile* current_profile = profile_->GetOriginalProfile(); |
135 Browser* browser = | 148 Browser* browser = |
136 chrome::FindOrCreateTabbedBrowser(current_profile, | 149 chrome::FindOrCreateTabbedBrowser(current_profile, |
137 chrome::GetActiveDesktop()); | 150 chrome::GetActiveDesktop()); |
138 if (browser->tab_count() == 0) | 151 if (browser->tab_count() == 0) |
(...skipping 20 matching lines...) Expand all Loading... |
159 const extensions::CrxInstallerError& error) { | 172 const extensions::CrxInstallerError& error) { |
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
161 if (disable_failure_ui_for_tests || skip_post_install_ui_) | 174 if (disable_failure_ui_for_tests || skip_post_install_ui_) |
162 return; | 175 return; |
163 | 176 |
164 Browser* browser = chrome::FindLastActiveWithProfile(profile_, | 177 Browser* browser = chrome::FindLastActiveWithProfile(profile_, |
165 chrome::GetActiveDesktop()); | 178 chrome::GetActiveDesktop()); |
166 WebContents* web_contents = chrome::GetActiveWebContents(browser); | 179 WebContents* web_contents = chrome::GetActiveWebContents(browser); |
167 if (!web_contents) | 180 if (!web_contents) |
168 return; | 181 return; |
169 InfoBarService* infobar_service = | 182 ErrorInfobarDelegate::Create(InfoBarService::FromWebContents(web_contents), |
170 InfoBarService::FromWebContents(web_contents); | 183 browser, error); |
171 infobar_service->AddInfoBar( | |
172 new ErrorInfobarDelegate(infobar_service, browser, error)); | |
173 } | 184 } |
174 | 185 |
175 void ExtensionInstallUIDefault::SetSkipPostInstallUI(bool skip_ui) { | 186 void ExtensionInstallUIDefault::SetSkipPostInstallUI(bool skip_ui) { |
176 skip_post_install_ui_ = skip_ui; | 187 skip_post_install_ui_ = skip_ui; |
177 } | 188 } |
178 | 189 |
179 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) { | 190 void ExtensionInstallUIDefault::SetUseAppInstalledBubble(bool use_bubble) { |
180 use_app_installed_bubble_ = use_bubble; | 191 use_app_installed_bubble_ = use_bubble; |
181 } | 192 } |
182 | 193 |
183 // static | 194 // static |
184 void ExtensionInstallUIDefault::ShowThemeInfoBar( | |
185 const std::string& previous_theme_id, bool previous_using_native_theme, | |
186 const Extension* new_theme, Profile* profile) { | |
187 if (!new_theme->is_theme()) | |
188 return; | |
189 | |
190 // Get last active tabbed browser of profile. | |
191 Browser* browser = browser::FindTabbedBrowser(profile, | |
192 true, | |
193 chrome::GetActiveDesktop()); | |
194 if (!browser) | |
195 return; | |
196 | |
197 WebContents* web_contents = chrome::GetActiveWebContents(browser); | |
198 if (!web_contents) | |
199 return; | |
200 InfoBarService* infobar_service = | |
201 InfoBarService::FromWebContents(web_contents); | |
202 | |
203 // First find any previous theme preview infobars. | |
204 InfoBarDelegate* old_delegate = NULL; | |
205 for (size_t i = 0; i < infobar_service->GetInfoBarCount(); ++i) { | |
206 InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(i); | |
207 ThemeInstalledInfoBarDelegate* theme_infobar = | |
208 delegate->AsThemePreviewInfobarDelegate(); | |
209 if (theme_infobar) { | |
210 // If the user installed the same theme twice, ignore the second install | |
211 // and keep the first install info bar, so that they can easily undo to | |
212 // get back the previous theme. | |
213 if (theme_infobar->MatchesTheme(new_theme)) | |
214 return; | |
215 old_delegate = delegate; | |
216 break; | |
217 } | |
218 } | |
219 | |
220 // Then either replace that old one or add a new one. | |
221 InfoBarDelegate* new_delegate = GetNewThemeInstalledInfoBarDelegate( | |
222 web_contents, new_theme, previous_theme_id, previous_using_native_theme); | |
223 | |
224 if (old_delegate) | |
225 infobar_service->ReplaceInfoBar(old_delegate, new_delegate); | |
226 else | |
227 infobar_service->AddInfoBar(new_delegate); | |
228 } | |
229 | |
230 InfoBarDelegate* ExtensionInstallUIDefault::GetNewThemeInstalledInfoBarDelegate( | |
231 WebContents* web_contents, | |
232 const Extension* new_theme, | |
233 const std::string& previous_theme_id, | |
234 bool previous_using_native_theme) { | |
235 Profile* profile = | |
236 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
237 return new ThemeInstalledInfoBarDelegate( | |
238 InfoBarService::FromWebContents(web_contents), | |
239 profile->GetExtensionService(), | |
240 ThemeServiceFactory::GetForProfile(profile), | |
241 new_theme, | |
242 previous_theme_id, | |
243 previous_using_native_theme); | |
244 } | |
245 | |
246 // static | |
247 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) { | 195 ExtensionInstallUI* ExtensionInstallUI::Create(Profile* profile) { |
248 return new ExtensionInstallUIDefault(profile); | 196 return new ExtensionInstallUIDefault(profile); |
249 } | 197 } |
250 | 198 |
251 // static | 199 // static |
252 void ExtensionInstallUI::OpenAppInstalledUI(Browser* browser, | 200 void ExtensionInstallUI::OpenAppInstalledUI(Browser* browser, |
253 const std::string& app_id) { | 201 const std::string& app_id) { |
254 if (NewTabUI::ShouldShowApps()) { | 202 if (NewTabUI::ShouldShowApps()) { |
255 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( | 203 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( |
256 browser, GURL(chrome::kChromeUINewTabURL))); | 204 browser, GURL(chrome::kChromeUINewTabURL))); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 return new ExtensionInstallPrompt(web_contents); | 236 return new ExtensionInstallPrompt(web_contents); |
289 } | 237 } |
290 | 238 |
291 // static | 239 // static |
292 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithProfile( | 240 ExtensionInstallPrompt* ExtensionInstallUI::CreateInstallPromptWithProfile( |
293 Profile* profile) { | 241 Profile* profile) { |
294 Browser* browser = chrome::FindLastActiveWithProfile(profile, | 242 Browser* browser = chrome::FindLastActiveWithProfile(profile, |
295 chrome::GetActiveDesktop()); | 243 chrome::GetActiveDesktop()); |
296 return CreateInstallPromptWithBrowser(browser); | 244 return CreateInstallPromptWithBrowser(browser); |
297 } | 245 } |
OLD | NEW |