summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/toolbar/action_box_menu_model.cc
blob: e5c4d641d666a51b3181129cb1b1bf75e6972535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/toolbar/action_box_menu_model.h"

#include "base/logging.h"
#include "base/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_to_mobile_service.h"
#include "chrome/browser/chrome_to_mobile_service_factory.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/api/extension_action/action_info.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"

using extensions::ActionInfo;

////////////////////////////////////////////////////////////////////////////////
// ActionBoxMenuModel

ActionBoxMenuModel::ActionBoxMenuModel(Browser* browser,
                                       ui::SimpleMenuModel::Delegate* delegate)
    : ui::SimpleMenuModel(delegate),
      browser_(browser) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  // TODO(msw): Show the item as disabled for chrome: and file: scheme pages?
  if (ChromeToMobileService::UpdateAndGetCommandState(browser_)) {
    AddItemWithStringId(IDC_CHROME_TO_MOBILE_PAGE,
                        IDS_CHROME_TO_MOBILE_BUBBLE_TOOLTIP);
    SetIcon(GetIndexOfCommandId(IDC_CHROME_TO_MOBILE_PAGE),
            rb.GetNativeImageNamed(IDR_MOBILE));
  }

  // In some unit tests, GetActiveWebContents can return NULL.
  bool starred = browser_->tab_strip_model()->GetActiveWebContents() &&
      BookmarkTabHelper::FromWebContents(browser_->tab_strip_model()->
          GetActiveWebContents())->is_starred();

  AddItemWithStringId(IDC_BOOKMARK_PAGE_FROM_STAR,
                      starred ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR);
  SetIcon(GetIndexOfCommandId(IDC_BOOKMARK_PAGE_FROM_STAR),
          rb.GetNativeImageNamed(starred ? IDR_STAR_LIT : IDR_STAR));

  AddItemWithStringId(IDC_PRINT, IDS_PRINT);
}

ActionBoxMenuModel::~ActionBoxMenuModel() {
}

void ActionBoxMenuModel::AddExtension(const extensions::Extension& extension,
                                      int command_id) {
  if (extension_ids_.empty())
    AddSeparator(ui::NORMAL_SEPARATOR);
  extension_ids_.push_back(extension.id());
  const ActionInfo* page_launcher_info =
      ActionInfo::GetPageLauncherInfo(&extension);
  DCHECK(page_launcher_info);
  AddItem(command_id, UTF8ToUTF16(page_launcher_info->default_title));
}

bool ActionBoxMenuModel::IsItemExtension(int index) {
  // The extensions are always at the end of the model.
  CHECK(index < GetItemCount());
  return index >= GetFirstExtensionIndex();
}

const extensions::Extension* ActionBoxMenuModel::GetExtensionAt(int index) {
  if (!IsItemExtension(index))
    return NULL;

  int index_in_extension_ids = index - GetFirstExtensionIndex();
  CHECK_GE(index_in_extension_ids, 0);
  CHECK_LT(index_in_extension_ids, static_cast<int>(extension_ids_.size()));

  ExtensionService* extension_service =
      extensions::ExtensionSystem::Get(browser_->profile())->
          extension_service();
  return extension_service->extensions()->GetByID(
      extension_ids_[index_in_extension_ids]);
}

void ActionBoxMenuModel::ExecuteCommand(int command_id) {
  delegate()->ExecuteCommand(command_id, 0);
}

int ActionBoxMenuModel::GetFirstExtensionIndex() {
  return GetItemCount() - extension_ids_.size();
}