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