extension_context_menu_model.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
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
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/extension_context_menu_model.h"
6
7#include "base/utf_string_conversions.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/extensions/extensions_service.h"
10#include "chrome/browser/extensions/extension_tabs_module.h"
11#include "chrome/browser/prefs/pref_service.h"
12#include "chrome/browser/profile.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/common/extensions/extension_action.h"
15#include "chrome/common/extensions/extension_constants.h"
16#include "chrome/common/extensions/extension.h"
17#include "chrome/common/pref_names.h"
18#include "chrome/common/url_constants.h"
19#include "grit/generated_resources.h"
20
21enum MenuEntries {
22  NAME = 0,
23  CONFIGURE,
24  DISABLE,
25  UNINSTALL,
26  MANAGE,
27  INSPECT_POPUP
28};
29
30ExtensionContextMenuModel::ExtensionContextMenuModel(
31    const Extension* extension,
32    Browser* browser,
33    PopupDelegate* delegate)
34    : ALLOW_THIS_IN_INITIALIZER_LIST(SimpleMenuModel(this)),
35      extension_id_(extension->id()),
36      browser_(browser),
37      profile_(browser->profile()),
38      delegate_(delegate) {
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
52ExtensionContextMenuModel::~ExtensionContextMenuModel() {
53}
54
55void ExtensionContextMenuModel::InitCommonCommands() {
56  const Extension* extension = GetExtension();
57
58  // The extension pointer should only be null if the extension was uninstalled,
59  // and since the menu just opened, it should still be installed.
60  DCHECK(extension);
61
62  AddItem(NAME, UTF8ToUTF16(extension->name()));
63  AddSeparator();
64  AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS);
65  AddItemWithStringId(DISABLE, IDS_EXTENSIONS_DISABLE);
66  AddItemWithStringId(UNINSTALL, IDS_EXTENSIONS_UNINSTALL);
67  AddSeparator();
68  AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSIONS);
69}
70
71bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
72  return false;
73}
74
75bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
76  const Extension* extension = this->GetExtension();
77  if (!extension)
78    return false;
79
80  if (command_id == CONFIGURE) {
81    return extension->options_url().spec().length() > 0;
82  } else if (command_id == NAME) {
83    // The NAME links to the Homepage URL. If the extension doesn't have a
84    // homepage, we just disable this menu item.
85    return extension->GetHomepageURL().is_valid();
86  } else if (command_id == INSPECT_POPUP) {
87    TabContents* contents = browser_->GetSelectedTabContents();
88    if (!contents)
89      return false;
90
91    return extension_action_->HasPopup(ExtensionTabUtil::GetTabId(contents));
92  }
93  return true;
94}
95
96bool ExtensionContextMenuModel::GetAcceleratorForCommandId(
97    int command_id, menus::Accelerator* accelerator) {
98  return false;
99}
100
101void ExtensionContextMenuModel::ExecuteCommand(int command_id) {
102  const Extension* extension = GetExtension();
103  if (!extension)
104    return;
105
106  switch (command_id) {
107    case NAME: {
108      browser_->OpenURL(extension->GetHomepageURL(), GURL(),
109                        NEW_FOREGROUND_TAB, PageTransition::LINK);
110      break;
111    }
112    case CONFIGURE:
113      DCHECK(!extension->options_url().is_empty());
114      profile_->GetExtensionProcessManager()->OpenOptionsPage(extension,
115                                                              browser_);
116      break;
117    case DISABLE: {
118      ExtensionsService* extension_service = profile_->GetExtensionsService();
119      extension_service->DisableExtension(extension_id_);
120      break;
121    }
122    case UNINSTALL: {
123      AddRef();  // Balanced in InstallUIProceed and InstallUIAbort.
124      install_ui_.reset(new ExtensionInstallUI(profile_));
125      install_ui_->ConfirmUninstall(this, extension);
126      break;
127    }
128    case MANAGE: {
129      browser_->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(),
130                        SINGLETON_TAB, PageTransition::LINK);
131      break;
132    }
133    case INSPECT_POPUP: {
134      delegate_->InspectPopup(extension_action_);
135      break;
136    }
137    default:
138     NOTREACHED() << "Unknown option";
139     break;
140  }
141}
142
143void ExtensionContextMenuModel::InstallUIProceed() {
144  if (GetExtension())
145    profile_->GetExtensionsService()->UninstallExtension(extension_id_, false);
146
147  Release();
148}
149
150void ExtensionContextMenuModel::InstallUIAbort() {
151  Release();
152}
153
154const Extension* ExtensionContextMenuModel::GetExtension() const {
155  ExtensionsService* extension_service = profile_->GetExtensionsService();
156  return extension_service->GetExtensionById(extension_id_, false);
157}
158