| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_disabled_infobar_delegate.h" | 5 #include "chrome/browser/extensions/extension_disabled_infobar_delegate.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/extension_install_ui.h" | 11 #include "chrome/browser/extensions/extension_install_ui.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/tab_contents/infobar_delegate.h" | 13 #include "chrome/browser/tab_contents/infobar_delegate.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | 14 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/browser_list.h" | 15 #include "chrome/browser/browser_list.h" |
| 16 #include "chrome/common/extensions/extension_file_util.h" | 16 #include "chrome/common/extensions/extension_file_util.h" |
| 17 #include "chrome/common/extensions/extension_resource.h" | 17 #include "chrome/common/extensions/extension_resource.h" |
| 18 #include "chrome/common/notification_details.h" | 18 #include "chrome/common/notification_details.h" |
| 19 #include "chrome/common/notification_registrar.h" | 19 #include "chrome/common/notification_registrar.h" |
| 20 #include "chrome/common/notification_source.h" | 20 #include "chrome/common/notification_source.h" |
| 21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 | 22 |
| 23 // ExtensionDisabledDialogDelegate -------------------------------------------- |
| 24 |
| 23 class ExtensionDisabledDialogDelegate | 25 class ExtensionDisabledDialogDelegate |
| 24 : public ExtensionInstallUI::Delegate, | 26 : public ExtensionInstallUI::Delegate, |
| 25 public base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate> { | 27 public base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate> { |
| 26 public: | 28 public: |
| 27 ExtensionDisabledDialogDelegate(Profile* profile, | 29 ExtensionDisabledDialogDelegate(Profile* profile, |
| 28 ExtensionService* service, | 30 ExtensionService* service, |
| 29 const Extension* extension) | 31 const Extension* extension); |
| 30 : service_(service), extension_(extension) { | |
| 31 AddRef(); // Balanced in Proceed or Abort. | |
| 32 | |
| 33 install_ui_.reset(new ExtensionInstallUI(profile)); | |
| 34 install_ui_->ConfirmInstall(this, extension_); | |
| 35 } | |
| 36 | |
| 37 // Overridden from ExtensionInstallUI::Delegate: | |
| 38 virtual void InstallUIProceed() { | |
| 39 service_->GrantPermissionsAndEnableExtension(extension_); | |
| 40 Release(); | |
| 41 } | |
| 42 virtual void InstallUIAbort() { | |
| 43 // Do nothing. The extension will remain disabled. | |
| 44 Release(); | |
| 45 } | |
| 46 | 32 |
| 47 private: | 33 private: |
| 48 friend class base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate>; | 34 friend class base::RefCountedThreadSafe<ExtensionDisabledDialogDelegate>; |
| 49 | 35 |
| 50 virtual ~ExtensionDisabledDialogDelegate() {} | 36 virtual ~ExtensionDisabledDialogDelegate(); |
| 37 |
| 38 // ExtensionInstallUI::Delegate: |
| 39 virtual void InstallUIProceed(); |
| 40 virtual void InstallUIAbort(); |
| 51 | 41 |
| 52 // The UI for showing the install dialog when enabling. | 42 // The UI for showing the install dialog when enabling. |
| 53 scoped_ptr<ExtensionInstallUI> install_ui_; | 43 scoped_ptr<ExtensionInstallUI> install_ui_; |
| 54 | 44 |
| 55 ExtensionService* service_; | 45 ExtensionService* service_; |
| 56 const Extension* extension_; | 46 const Extension* extension_; |
| 57 }; | 47 }; |
| 58 | 48 |
| 59 class ExtensionDisabledInfobarDelegate | 49 ExtensionDisabledDialogDelegate::ExtensionDisabledDialogDelegate( |
| 60 : public ConfirmInfoBarDelegate, | 50 Profile* profile, |
| 61 public NotificationObserver { | 51 ExtensionService* service, |
| 52 const Extension* extension) |
| 53 : service_(service), extension_(extension) { |
| 54 AddRef(); // Balanced in Proceed or Abort. |
| 55 |
| 56 install_ui_.reset(new ExtensionInstallUI(profile)); |
| 57 install_ui_->ConfirmInstall(this, extension_); |
| 58 } |
| 59 |
| 60 ExtensionDisabledDialogDelegate::~ExtensionDisabledDialogDelegate() { |
| 61 } |
| 62 |
| 63 void ExtensionDisabledDialogDelegate::InstallUIProceed() { |
| 64 service_->GrantPermissionsAndEnableExtension(extension_); |
| 65 Release(); |
| 66 } |
| 67 |
| 68 void ExtensionDisabledDialogDelegate::InstallUIAbort() { |
| 69 // Do nothing. The extension will remain disabled. |
| 70 Release(); |
| 71 } |
| 72 |
| 73 |
| 74 // ExtensionDisabledInfobarDelegate ------------------------------------------- |
| 75 |
| 76 class ExtensionDisabledInfobarDelegate : public ConfirmInfoBarDelegate, |
| 77 public NotificationObserver { |
| 62 public: | 78 public: |
| 63 ExtensionDisabledInfobarDelegate(TabContents* tab_contents, | 79 ExtensionDisabledInfobarDelegate(TabContents* tab_contents, |
| 64 ExtensionService* service, | 80 ExtensionService* service, |
| 65 const Extension* extension) | 81 const Extension* extension); |
| 66 : ConfirmInfoBarDelegate(tab_contents), | |
| 67 tab_contents_(tab_contents), | |
| 68 service_(service), | |
| 69 extension_(extension) { | |
| 70 // The user might re-enable the extension in other ways, so watch for that. | |
| 71 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | |
| 72 Source<Profile>(service->profile())); | |
| 73 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | |
| 74 Source<Profile>(service->profile())); | |
| 75 } | |
| 76 virtual ~ExtensionDisabledInfobarDelegate() { | |
| 77 } | |
| 78 virtual string16 GetMessageText() const { | |
| 79 return l10n_util::GetStringFUTF16(extension_->is_app() ? | |
| 80 IDS_APP_DISABLED_INFOBAR_LABEL : IDS_EXTENSION_DISABLED_INFOBAR_LABEL, | |
| 81 UTF8ToUTF16(extension_->name())); | |
| 82 } | |
| 83 virtual SkBitmap* GetIcon() const { | |
| 84 return NULL; | |
| 85 } | |
| 86 virtual int GetButtons() const { | |
| 87 return BUTTON_OK; | |
| 88 } | |
| 89 virtual string16 GetButtonLabel( | |
| 90 ConfirmInfoBarDelegate::InfoBarButton button) const { | |
| 91 return l10n_util::GetStringUTF16( | |
| 92 IDS_EXTENSION_DISABLED_INFOBAR_ENABLE_BUTTON); | |
| 93 } | |
| 94 virtual bool Accept() { | |
| 95 // This object manages its own lifetime. | |
| 96 new ExtensionDisabledDialogDelegate(tab_contents_->profile(), | |
| 97 service_, extension_); | |
| 98 return true; | |
| 99 } | |
| 100 virtual void InfoBarClosed() { | |
| 101 delete this; | |
| 102 } | |
| 103 | 82 |
| 83 private: |
| 84 virtual ~ExtensionDisabledInfobarDelegate(); |
| 85 |
| 86 // ConfirmInfoBarDelegate: |
| 87 virtual void InfoBarClosed(); |
| 88 virtual string16 GetMessageText() const; |
| 89 virtual int GetButtons() const; |
| 90 virtual string16 GetButtonLabel(InfoBarButton button) const; |
| 91 virtual bool Accept(); |
| 92 |
| 93 // NotificationObserver: |
| 104 virtual void Observe(NotificationType type, | 94 virtual void Observe(NotificationType type, |
| 105 const NotificationSource& source, | 95 const NotificationSource& source, |
| 106 const NotificationDetails& details) { | 96 const NotificationDetails& details); |
| 107 // TODO(mpcomplete): RemoveInfoBar doesn't seem to always result in us | |
| 108 // getting deleted. | |
| 109 const Extension* extension = NULL; | |
| 110 switch (type.value) { | |
| 111 case NotificationType::EXTENSION_LOADED: | |
| 112 extension = Details<const Extension>(details).ptr(); | |
| 113 break; | |
| 114 case NotificationType::EXTENSION_UNLOADED: { | |
| 115 UnloadedExtensionInfo* info = | |
| 116 Details<UnloadedExtensionInfo>(details).ptr(); | |
| 117 if (info->reason == UnloadedExtensionInfo::DISABLE) | |
| 118 extension = info->extension; | |
| 119 break; | |
| 120 } | |
| 121 default: | |
| 122 NOTREACHED(); | |
| 123 return; | |
| 124 } | |
| 125 if (extension == extension_) | |
| 126 tab_contents_->RemoveInfoBar(this); | |
| 127 } | |
| 128 | 97 |
| 129 private: | |
| 130 NotificationRegistrar registrar_; | 98 NotificationRegistrar registrar_; |
| 131 TabContents* tab_contents_; | 99 TabContents* tab_contents_; |
| 132 ExtensionService* service_; | 100 ExtensionService* service_; |
| 133 const Extension* extension_; | 101 const Extension* extension_; |
| 134 }; | 102 }; |
| 135 | 103 |
| 104 ExtensionDisabledInfobarDelegate::ExtensionDisabledInfobarDelegate( |
| 105 TabContents* tab_contents, |
| 106 ExtensionService* service, |
| 107 const Extension* extension) |
| 108 : ConfirmInfoBarDelegate(tab_contents), |
| 109 tab_contents_(tab_contents), |
| 110 service_(service), |
| 111 extension_(extension) { |
| 112 // The user might re-enable the extension in other ways, so watch for that. |
| 113 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
| 114 Source<Profile>(service->profile())); |
| 115 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
| 116 Source<Profile>(service->profile())); |
| 117 } |
| 118 |
| 119 ExtensionDisabledInfobarDelegate::~ExtensionDisabledInfobarDelegate() { |
| 120 } |
| 121 |
| 122 void ExtensionDisabledInfobarDelegate::InfoBarClosed() { |
| 123 delete this; |
| 124 } |
| 125 |
| 126 string16 ExtensionDisabledInfobarDelegate::GetMessageText() const { |
| 127 return l10n_util::GetStringFUTF16(extension_->is_app() ? |
| 128 IDS_APP_DISABLED_INFOBAR_LABEL : IDS_EXTENSION_DISABLED_INFOBAR_LABEL, |
| 129 UTF8ToUTF16(extension_->name())); |
| 130 } |
| 131 |
| 132 int ExtensionDisabledInfobarDelegate::GetButtons() const { |
| 133 return BUTTON_OK; |
| 134 } |
| 135 |
| 136 string16 ExtensionDisabledInfobarDelegate::GetButtonLabel( |
| 137 InfoBarButton button) const { |
| 138 DCHECK_EQ(BUTTON_OK, button); |
| 139 return l10n_util::GetStringUTF16( |
| 140 IDS_EXTENSION_DISABLED_INFOBAR_ENABLE_BUTTON); |
| 141 } |
| 142 |
| 143 bool ExtensionDisabledInfobarDelegate::Accept() { |
| 144 // This object manages its own lifetime. |
| 145 new ExtensionDisabledDialogDelegate(tab_contents_->profile(), service_, |
| 146 extension_); |
| 147 return true; |
| 148 } |
| 149 |
| 150 void ExtensionDisabledInfobarDelegate::Observe( |
| 151 NotificationType type, |
| 152 const NotificationSource& source, |
| 153 const NotificationDetails& details) { |
| 154 // TODO(mpcomplete): RemoveInfoBar doesn't seem to always result in us getting |
| 155 // deleted. |
| 156 const Extension* extension; |
| 157 if (type.value == NotificationType::EXTENSION_LOADED) { |
| 158 extension = Details<const Extension>(details).ptr(); |
| 159 } else { |
| 160 DCHECK_EQ(NotificationType::EXTENSION_UNLOADED, type.value); |
| 161 UnloadedExtensionInfo* info = Details<UnloadedExtensionInfo>(details).ptr(); |
| 162 extension = (info->reason == UnloadedExtensionInfo::DISABLE) ? |
| 163 info->extension : NULL; |
| 164 } |
| 165 if (extension == extension_) |
| 166 tab_contents_->RemoveInfoBar(this); |
| 167 } |
| 168 |
| 169 |
| 170 // Globals -------------------------------------------------------------------- |
| 171 |
| 136 void ShowExtensionDisabledUI(ExtensionService* service, Profile* profile, | 172 void ShowExtensionDisabledUI(ExtensionService* service, Profile* profile, |
| 137 const Extension* extension) { | 173 const Extension* extension) { |
| 138 Browser* browser = BrowserList::GetLastActiveWithProfile(profile); | 174 Browser* browser = BrowserList::GetLastActiveWithProfile(profile); |
| 139 if (!browser) | 175 if (!browser) |
| 140 return; | 176 return; |
| 141 | 177 |
| 142 TabContents* tab_contents = browser->GetSelectedTabContents(); | 178 TabContents* tab_contents = browser->GetSelectedTabContents(); |
| 143 if (!tab_contents) | 179 if (!tab_contents) |
| 144 return; | 180 return; |
| 145 | 181 |
| 146 tab_contents->AddInfoBar(new ExtensionDisabledInfobarDelegate( | 182 tab_contents->AddInfoBar(new ExtensionDisabledInfobarDelegate( |
| 147 tab_contents, service, extension)); | 183 tab_contents, service, extension)); |
| 148 } | 184 } |
| 149 | 185 |
| 150 void ShowExtensionDisabledDialog(ExtensionService* service, Profile* profile, | 186 void ShowExtensionDisabledDialog(ExtensionService* service, Profile* profile, |
| 151 const Extension* extension) { | 187 const Extension* extension) { |
| 152 // This object manages its own lifetime. | 188 // This object manages its own lifetime. |
| 153 new ExtensionDisabledDialogDelegate(profile, service, extension); | 189 new ExtensionDisabledDialogDelegate(profile, service, extension); |
| 154 } | 190 } |
| OLD | NEW |