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/external_install_ui.h" | 5 #include "chrome/browser/extensions/external_install_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_forward.h" |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
14 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/app/chrome_command_ids.h" | 18 #include "chrome/app/chrome_command_ids.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/extensions/extension_install_prompt.h" | 20 #include "chrome/browser/extensions/extension_install_prompt.h" |
19 #include "chrome/browser/extensions/extension_install_ui.h" | 21 #include "chrome/browser/extensions/extension_install_ui.h" |
20 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
21 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 23 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
22 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 virtual string16 GetBubbleViewCancelButtonLabel() OVERRIDE; | 114 virtual string16 GetBubbleViewCancelButtonLabel() OVERRIDE; |
113 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; | 115 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; |
114 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; | 116 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; |
115 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; | 117 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; |
116 | 118 |
117 // content::NotificationObserver implementation. | 119 // content::NotificationObserver implementation. |
118 virtual void Observe(int type, | 120 virtual void Observe(int type, |
119 const content::NotificationSource& source, | 121 const content::NotificationSource& source, |
120 const content::NotificationDetails& details) OVERRIDE; | 122 const content::NotificationDetails& details) OVERRIDE; |
121 | 123 |
| 124 // Callback to notify UI that extension has been removed. |
| 125 void HandleExtensionRemoved(const Extension* extension); |
| 126 |
122 protected: | 127 protected: |
123 ExtensionService* service_; | 128 base::WeakPtr<ExtensionService> service_weak_; |
124 const Extension* extension_; | 129 const Extension* extension_; |
125 content::NotificationRegistrar registrar_; | 130 content::NotificationRegistrar registrar_; |
| 131 base::WeakPtrFactory<ExternalInstallMenuAlert> weak_factory_; |
| 132 base::Callback<void(const Extension*)> on_removed_callback_; |
126 }; | 133 }; |
127 | 134 |
128 // Shows a menu item and a global error bubble, replacing the install dialog. | 135 // Shows a menu item and a global error bubble, replacing the install dialog. |
129 class ExternalInstallGlobalError : public ExternalInstallMenuAlert { | 136 class ExternalInstallGlobalError : public ExternalInstallMenuAlert { |
130 public: | 137 public: |
131 ExternalInstallGlobalError(ExtensionService* service, | 138 ExternalInstallGlobalError(ExtensionService* service, |
132 const Extension* extension, | 139 const Extension* extension, |
133 ExternalInstallDialogDelegate* delegate, | 140 ExternalInstallDialogDelegate* delegate, |
134 const ExtensionInstallPrompt::Prompt& prompt); | 141 const ExtensionInstallPrompt::Prompt& prompt); |
135 virtual ~ExternalInstallGlobalError(); | 142 virtual ~ExternalInstallGlobalError(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 return; | 240 return; |
234 service_weak_->UninstallExtension(extension_id_, false, NULL); | 241 service_weak_->UninstallExtension(extension_id_, false, NULL); |
235 Release(); | 242 Release(); |
236 } | 243 } |
237 | 244 |
238 // ExternalInstallMenuAlert ------------------------------------------------- | 245 // ExternalInstallMenuAlert ------------------------------------------------- |
239 | 246 |
240 ExternalInstallMenuAlert::ExternalInstallMenuAlert( | 247 ExternalInstallMenuAlert::ExternalInstallMenuAlert( |
241 ExtensionService* service, | 248 ExtensionService* service, |
242 const Extension* extension) | 249 const Extension* extension) |
243 : service_(service), | 250 : service_weak_(service->AsWeakPtr()), |
244 extension_(extension) { | 251 extension_(extension), |
| 252 weak_factory_(this) { |
| 253 on_removed_callback_ = base::Bind( |
| 254 &ExternalInstallMenuAlert::HandleExtensionRemoved, |
| 255 weak_factory_.GetWeakPtr()); |
| 256 if (service_weak_.get()) |
| 257 service_weak_->RegisterExtensionRemovedCallback(on_removed_callback_); |
245 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 258 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
246 content::Source<Profile>(service->profile())); | 259 content::Source<Profile>(service->profile())); |
247 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_REMOVED, | |
248 content::Source<Profile>(service->profile())); | |
249 } | 260 } |
250 | 261 |
251 ExternalInstallMenuAlert::~ExternalInstallMenuAlert() { | 262 ExternalInstallMenuAlert::~ExternalInstallMenuAlert() { |
| 263 if (service_weak_.get()) |
| 264 service_weak_->RemoveExtensionRemovedCallback(on_removed_callback_); |
252 } | 265 } |
253 | 266 |
254 GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() { | 267 GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() { |
255 return SEVERITY_LOW; | 268 return SEVERITY_LOW; |
256 } | 269 } |
257 | 270 |
258 bool ExternalInstallMenuAlert::HasMenuItem() { | 271 bool ExternalInstallMenuAlert::HasMenuItem() { |
259 return true; | 272 return true; |
260 } | 273 } |
261 | 274 |
262 int ExternalInstallMenuAlert::MenuItemCommandID() { | 275 int ExternalInstallMenuAlert::MenuItemCommandID() { |
263 return kMenuCommandId; | 276 return kMenuCommandId; |
264 } | 277 } |
265 | 278 |
266 string16 ExternalInstallMenuAlert::MenuItemLabel() { | 279 string16 ExternalInstallMenuAlert::MenuItemLabel() { |
267 int id = -1; | 280 int id = -1; |
268 if (extension_->is_app()) | 281 if (extension_->is_app()) |
269 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP; | 282 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP; |
270 else if (extension_->is_theme()) | 283 else if (extension_->is_theme()) |
271 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME; | 284 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME; |
272 else | 285 else |
273 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION; | 286 id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION; |
274 return l10n_util::GetStringFUTF16(id, UTF8ToUTF16(extension_->name())); | 287 return l10n_util::GetStringFUTF16(id, UTF8ToUTF16(extension_->name())); |
275 } | 288 } |
276 | 289 |
277 void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) { | 290 void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) { |
278 ShowExternalInstallDialog(service_, browser, extension_); | 291 ShowExternalInstallDialog(service_weak_.get(), browser, extension_); |
279 } | 292 } |
280 | 293 |
281 bool ExternalInstallMenuAlert::HasBubbleView() { | 294 bool ExternalInstallMenuAlert::HasBubbleView() { |
282 return false; | 295 return false; |
283 } | 296 } |
284 string16 ExternalInstallMenuAlert::GetBubbleViewTitle() { | 297 string16 ExternalInstallMenuAlert::GetBubbleViewTitle() { |
285 return string16(); | 298 return string16(); |
286 } | 299 } |
287 | 300 |
288 std::vector<string16> ExternalInstallMenuAlert::GetBubbleViewMessages() { | 301 std::vector<string16> ExternalInstallMenuAlert::GetBubbleViewMessages() { |
(...skipping 20 matching lines...) Expand all Loading... |
309 void ExternalInstallMenuAlert::BubbleViewCancelButtonPressed( | 322 void ExternalInstallMenuAlert::BubbleViewCancelButtonPressed( |
310 Browser* browser) { | 323 Browser* browser) { |
311 NOTREACHED(); | 324 NOTREACHED(); |
312 } | 325 } |
313 | 326 |
314 void ExternalInstallMenuAlert::Observe( | 327 void ExternalInstallMenuAlert::Observe( |
315 int type, | 328 int type, |
316 const content::NotificationSource& source, | 329 const content::NotificationSource& source, |
317 const content::NotificationDetails& details) { | 330 const content::NotificationDetails& details) { |
318 // The error is invalidated if the extension has been loaded or removed. | 331 // The error is invalidated if the extension has been loaded or removed. |
319 DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED || | 332 DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED); |
320 type == chrome::NOTIFICATION_EXTENSION_REMOVED); | |
321 const Extension* extension = content::Details<const Extension>(details).ptr(); | 333 const Extension* extension = content::Details<const Extension>(details).ptr(); |
322 if (extension != extension_) | 334 if (extension != extension_) |
323 return; | 335 return; |
324 GlobalErrorService* error_service = | 336 if (service_weak_.get()) { |
325 GlobalErrorServiceFactory::GetForProfile(service_->profile()); | 337 GlobalErrorService* error_service = |
326 error_service->RemoveGlobalError(this); | 338 GlobalErrorServiceFactory::GetForProfile(service_weak_->profile()); |
327 service_->AcknowledgeExternalExtension(extension_->id()); | 339 error_service->RemoveGlobalError(this); |
| 340 service_weak_->AcknowledgeExternalExtension(extension_->id()); |
| 341 } |
328 delete this; | 342 delete this; |
329 } | 343 } |
330 | 344 |
| 345 void ExternalInstallMenuAlert::HandleExtensionRemoved( |
| 346 const Extension* extension) { |
| 347 if (extension != extension_) |
| 348 return; |
| 349 if (service_weak_.get()) { |
| 350 GlobalErrorService* error_service = |
| 351 GlobalErrorServiceFactory::GetForProfile(service_weak_->profile()); |
| 352 error_service->RemoveGlobalError(this); |
| 353 service_weak_->AcknowledgeExternalExtension(extension_->id()); |
| 354 } |
| 355 delete this; |
| 356 } |
| 357 |
331 // ExternalInstallGlobalError ----------------------------------------------- | 358 // ExternalInstallGlobalError ----------------------------------------------- |
332 | 359 |
333 ExternalInstallGlobalError::ExternalInstallGlobalError( | 360 ExternalInstallGlobalError::ExternalInstallGlobalError( |
334 ExtensionService* service, | 361 ExtensionService* service, |
335 const Extension* extension, | 362 const Extension* extension, |
336 ExternalInstallDialogDelegate* delegate, | 363 ExternalInstallDialogDelegate* delegate, |
337 const ExtensionInstallPrompt::Prompt& prompt) | 364 const ExtensionInstallPrompt::Prompt& prompt) |
338 : ExternalInstallMenuAlert(service, extension), | 365 : ExternalInstallMenuAlert(service, extension), |
339 delegate_(delegate), | 366 delegate_(delegate), |
340 prompt_(&prompt) { | 367 prompt_(&prompt) { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 | 480 |
454 bool HasExternalInstallBubble(ExtensionService* service) { | 481 bool HasExternalInstallBubble(ExtensionService* service) { |
455 GlobalErrorService* error_service = | 482 GlobalErrorService* error_service = |
456 GlobalErrorServiceFactory::GetForProfile(service->profile()); | 483 GlobalErrorServiceFactory::GetForProfile(service->profile()); |
457 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID( | 484 GlobalError* error = error_service->GetGlobalErrorByMenuItemCommandID( |
458 kMenuCommandId); | 485 kMenuCommandId); |
459 return error && error->HasBubbleView(); | 486 return error && error->HasBubbleView(); |
460 } | 487 } |
461 | 488 |
462 } // namespace extensions | 489 } // namespace extensions |
OLD | NEW |