extension_context_menu_model.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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/extensions/extension_service.h"
9#include "chrome/browser/extensions/extension_tabs_module.h"
10#include "chrome/browser/prefs/pref_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/common/extensions/extension_action.h"
14#include "chrome/common/extensions/extension_constants.h"
15#include "chrome/common/extensions/extension.h"
16#include "chrome/common/pref_names.h"
17#include "chrome/common/url_constants.h"
18#include "grit/generated_resources.h"
19
20enum MenuEntries {
21  NAME = 0,
22  CONFIGURE,
23  HIDE,
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  if (extension->browser_action())
68    AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
69  AddSeparator();
70  AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSIONS);
71}
72
73bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
74  return false;
75}
76
77bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
78  const Extension* extension = this->GetExtension();
79  if (!extension)
80    return false;
81
82  if (command_id == CONFIGURE) {
83    return extension->options_url().spec().length() > 0;
84  } else if (command_id == NAME) {
85    // The NAME links to the Homepage URL. If the extension doesn't have a
86    // homepage, we just disable this menu item.
87    return extension->GetHomepageURL().is_valid();
88  } else if (command_id == INSPECT_POPUP) {
89    TabContents* contents = browser_->GetSelectedTabContents();
90    if (!contents)
91      return false;
92
93    return extension_action_->HasPopup(ExtensionTabUtil::GetTabId(contents));
94  }
95  return true;
96}
97
98bool ExtensionContextMenuModel::GetAcceleratorForCommandId(
99    int command_id, ui::Accelerator* accelerator) {
100  return false;
101}
102
103void ExtensionContextMenuModel::ExecuteCommand(int command_id) {
104  const Extension* extension = GetExtension();
105  if (!extension)
106    return;
107
108  switch (command_id) {
109    case NAME: {
110      browser_->OpenURL(extension->GetHomepageURL(), GURL(),
111                        NEW_FOREGROUND_TAB, PageTransition::LINK);
112      break;
113    }
114    case CONFIGURE:
115      DCHECK(!extension->options_url().is_empty());
116      profile_->GetExtensionProcessManager()->OpenOptionsPage(extension,
117                                                              browser_);
118      break;
119    case HIDE: {
120      ExtensionService* extension_service = profile_->GetExtensionService();
121      extension_service->SetBrowserActionVisibility(extension, false);
122      break;
123    }
124    case DISABLE: {
125      ExtensionService* extension_service = profile_->GetExtensionService();
126      extension_service->DisableExtension(extension_id_);
127      break;
128    }
129    case UNINSTALL: {
130      AddRef();  // Balanced in InstallUIProceed and InstallUIAbort.
131      install_ui_.reset(new ExtensionInstallUI(profile_));
132      install_ui_->ConfirmUninstall(this, extension);
133      break;
134    }
135    case MANAGE: {
136      browser_->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(),
137                        SINGLETON_TAB, PageTransition::LINK);
138      break;
139    }
140    case INSPECT_POPUP: {
141      delegate_->InspectPopup(extension_action_);
142      break;
143    }
144    default:
145     NOTREACHED() << "Unknown option";
146     break;
147  }
148}
149
150void ExtensionContextMenuModel::InstallUIProceed() {
151  if (GetExtension())
152    profile_->GetExtensionService()->UninstallExtension(extension_id_, false);
153
154  Release();
155}
156
157void ExtensionContextMenuModel::InstallUIAbort() {
158  Release();
159}
160
161const Extension* ExtensionContextMenuModel::GetExtension() const {
162  ExtensionService* extension_service = profile_->GetExtensionService();
163  return extension_service->GetExtensionById(extension_id_, false);
164}
165