| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_action_context_menu_model.h" | 5 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "chrome/browser/browser_list.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/extensions/extension_tabs_module.h" | 10 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 11 #include "chrome/browser/extensions/extensions_service.h" | 11 #include "chrome/browser/extensions/extensions_service.h" |
| 12 #include "chrome/browser/pref_service.h" | 12 #include "chrome/browser/pref_service.h" |
| 13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
| 15 #include "chrome/common/extensions/extension_action.h" | 15 #include "chrome/common/extensions/extension_action.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 20 | 20 |
| 21 enum MenuEntries { | 21 enum MenuEntries { |
| 22 NAME = 0, | 22 NAME = 0, |
| 23 CONFIGURE, | 23 CONFIGURE, |
| 24 DISABLE, | 24 DISABLE, |
| 25 UNINSTALL, | 25 UNINSTALL, |
| 26 MANAGE, | 26 MANAGE, |
| 27 INSPECT_POPUP | 27 INSPECT_POPUP |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 ExtensionActionContextMenuModel::ExtensionActionContextMenuModel( | 30 ExtensionContextMenuModel::ExtensionContextMenuModel( |
| 31 Extension* extension, ExtensionAction* extension_action, PrefService* prefs, | 31 Extension* extension, |
| 32 MenuDelegate* delegate) | 32 Browser* browser, |
| 33 PopupDelegate* delegate) |
| 33 : ALLOW_THIS_IN_INITIALIZER_LIST(SimpleMenuModel(this)), | 34 : ALLOW_THIS_IN_INITIALIZER_LIST(SimpleMenuModel(this)), |
| 34 extension_(extension), | 35 extension_(extension), |
| 35 extension_action_(extension_action), | 36 browser_(browser), |
| 37 profile_(browser->profile()), |
| 36 delegate_(delegate) { | 38 delegate_(delegate) { |
| 37 AddItem(NAME, UTF8ToUTF16(extension->name())); | 39 extension_action_ = extension->browser_action(); |
| 40 if (!extension_action_) |
| 41 extension_action_ = extension->page_action(); |
| 42 |
| 43 InitCommonCommands(); |
| 44 |
| 45 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
| 46 delegate_) { |
| 47 AddSeparator(); |
| 48 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
| 49 } |
| 50 } |
| 51 |
| 52 ExtensionContextMenuModel::~ExtensionContextMenuModel() { |
| 53 } |
| 54 |
| 55 void ExtensionContextMenuModel::InitCommonCommands() { |
| 56 AddItem(NAME, UTF8ToUTF16(extension_->name())); |
| 38 AddSeparator(); | 57 AddSeparator(); |
| 39 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS); | 58 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS); |
| 40 AddItemWithStringId(DISABLE, IDS_EXTENSIONS_DISABLE); | 59 AddItemWithStringId(DISABLE, IDS_EXTENSIONS_DISABLE); |
| 41 AddItemWithStringId(UNINSTALL, IDS_EXTENSIONS_UNINSTALL); | 60 AddItemWithStringId(UNINSTALL, IDS_EXTENSIONS_UNINSTALL); |
| 42 AddSeparator(); | 61 AddSeparator(); |
| 43 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSIONS); | 62 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSIONS); |
| 44 | |
| 45 if (extension_ && delegate_ && prefs && | |
| 46 prefs->GetBoolean(prefs::kExtensionsUIDeveloperMode)) { | |
| 47 AddSeparator(); | |
| 48 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | |
| 49 } | |
| 50 } | 63 } |
| 51 | 64 |
| 52 ExtensionActionContextMenuModel::~ExtensionActionContextMenuModel() { | 65 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
| 53 } | |
| 54 | |
| 55 bool ExtensionActionContextMenuModel::IsCommandIdChecked(int command_id) const { | |
| 56 return false; | 66 return false; |
| 57 } | 67 } |
| 58 | 68 |
| 59 bool ExtensionActionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 69 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
| 60 if (command_id == CONFIGURE) { | 70 if (command_id == CONFIGURE) { |
| 61 return extension_->options_url().spec().length() > 0; | 71 return extension_->options_url().spec().length() > 0; |
| 62 } else if (command_id == NAME) { | 72 } else if (command_id == NAME) { |
| 63 // The NAME links to the gallery page, which only makes sense if Google is | 73 // The NAME links to the gallery page, which only makes sense if Google is |
| 64 // hosting the extension. For other 3rd party extensions we don't have a | 74 // hosting the extension. For other 3rd party extensions we don't have a |
| 65 // homepage url, so we just disable this menu item on those cases, at least | 75 // homepage url, so we just disable this menu item on those cases, at least |
| 66 // for now. | 76 // for now. |
| 67 return extension_->update_url().DomainIs("google.com"); | 77 return extension_->update_url().DomainIs("google.com"); |
| 68 } else if (command_id == INSPECT_POPUP) { | 78 } else if (command_id == INSPECT_POPUP) { |
| 69 if (!delegate_ || !extension_) | 79 TabContents* contents = browser_->GetSelectedTabContents(); |
| 70 return false; | |
| 71 Browser* browser = BrowserList::GetLastActive(); | |
| 72 if (!browser) | |
| 73 return false; | |
| 74 TabContents* contents = browser->GetSelectedTabContents(); | |
| 75 if (!contents) | 80 if (!contents) |
| 76 return false; | 81 return false; |
| 77 | 82 |
| 78 // Different tabs can have different popups set. We need to make sure we | 83 return extension_action_->HasPopup(ExtensionTabUtil::GetTabId(contents)); |
| 79 // only enable the menu item if the current tab has a popup. | |
| 80 return (extension_action_->HasPopup(ExtensionTabUtil::GetTabId(contents))); | |
| 81 } | 84 } |
| 82 return true; | 85 return true; |
| 83 } | 86 } |
| 84 | 87 |
| 85 bool ExtensionActionContextMenuModel::GetAcceleratorForCommandId( | 88 bool ExtensionContextMenuModel::GetAcceleratorForCommandId( |
| 86 int command_id, menus::Accelerator* accelerator) { | 89 int command_id, menus::Accelerator* accelerator) { |
| 87 return false; | 90 return false; |
| 88 } | 91 } |
| 89 | 92 |
| 90 void ExtensionActionContextMenuModel::ExecuteCommand(int command_id) { | 93 void ExtensionContextMenuModel::ExecuteCommand(int command_id) { |
| 91 // TODO(finnur): GetLastActive returns NULL in unit tests. | |
| 92 Browser* browser = BrowserList::GetLastActive(); | |
| 93 Profile* profile = browser->profile(); | |
| 94 | |
| 95 switch (command_id) { | 94 switch (command_id) { |
| 96 case NAME: { | 95 case NAME: { |
| 97 GURL url(std::string(extension_urls::kGalleryBrowsePrefix) + | 96 GURL url(std::string(extension_urls::kGalleryBrowsePrefix) + |
| 98 std::string("/detail/") + extension_->id()); | 97 std::string("/detail/") + extension_->id()); |
| 99 browser->OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); | 98 browser_->OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK); |
| 100 break; | 99 break; |
| 101 } | 100 } |
| 102 case CONFIGURE: | 101 case CONFIGURE: |
| 103 DCHECK(!extension_->options_url().is_empty()); | 102 DCHECK(!extension_->options_url().is_empty()); |
| 104 profile->GetExtensionProcessManager()->OpenOptionsPage(extension_, | 103 profile_->GetExtensionProcessManager()->OpenOptionsPage(extension_, |
| 105 browser); | 104 browser_); |
| 106 break; | 105 break; |
| 107 case DISABLE: { | 106 case DISABLE: { |
| 108 ExtensionsService* extension_service = profile->GetExtensionsService(); | 107 ExtensionsService* extension_service = profile_->GetExtensionsService(); |
| 109 extension_service->DisableExtension(extension_->id()); | 108 extension_service->DisableExtension(extension_->id()); |
| 110 break; | 109 break; |
| 111 } | 110 } |
| 112 case UNINSTALL: { | 111 case UNINSTALL: { |
| 113 scoped_ptr<SkBitmap> uninstall_icon; | 112 scoped_ptr<SkBitmap> uninstall_icon; |
| 114 Extension::DecodeIcon(extension_, Extension::EXTENSION_ICON_LARGE, | 113 Extension::DecodeIcon(extension_, Extension::EXTENSION_ICON_LARGE, |
| 115 &uninstall_icon); | 114 &uninstall_icon); |
| 116 | 115 |
| 117 ExtensionInstallUI client(profile); | 116 ExtensionInstallUI client(profile_); |
| 118 client.ConfirmUninstall(this, extension_, uninstall_icon.get()); | 117 client.ConfirmUninstall(this, extension_, uninstall_icon.get()); |
| 119 break; | 118 break; |
| 120 } | 119 } |
| 121 case MANAGE: { | 120 case MANAGE: { |
| 122 browser->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(), | 121 browser_->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(), |
| 123 SINGLETON_TAB, PageTransition::LINK); | 122 SINGLETON_TAB, PageTransition::LINK); |
| 124 break; | 123 break; |
| 125 } | 124 } |
| 126 case INSPECT_POPUP: { | 125 case INSPECT_POPUP: { |
| 127 if (delegate_) | 126 delegate_->InspectPopup(extension_action_); |
| 128 delegate_->ShowPopupForDevToolsWindow(extension_, extension_action_); | |
| 129 break; | 127 break; |
| 130 } | 128 } |
| 131 default: | 129 default: |
| 132 NOTREACHED() << "Unknown option"; | 130 NOTREACHED() << "Unknown option"; |
| 133 break; | 131 break; |
| 134 } | 132 } |
| 135 } | 133 } |
| 136 | 134 |
| 137 void ExtensionActionContextMenuModel::InstallUIProceed(bool create_app) { | 135 void ExtensionContextMenuModel::InstallUIProceed(bool create_app) { |
| 138 DCHECK(!create_app); | 136 DCHECK(!create_app); |
| 139 | 137 |
| 140 // TODO(finnur): GetLastActive returns NULL in unit tests. | |
| 141 Browser* browser = BrowserList::GetLastActive(); | |
| 142 std::string id = extension_->id(); | 138 std::string id = extension_->id(); |
| 143 browser->profile()->GetExtensionsService()->UninstallExtension(id, false); | 139 profile_->GetExtensionsService()->UninstallExtension(id, false); |
| 144 } | 140 } |
| OLD | NEW |