summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/toolbar/action_box_button_controller.h
blob: 94820440deb2daad7b7724ab54a5acc1d1355667 (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
// 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.

#ifndef CHROME_BROWSER_UI_TOOLBAR_ACTION_BOX_BUTTON_CONTROLLER_H_
#define CHROME_BROWSER_UI_TOOLBAR_ACTION_BOX_BUTTON_CONTROLLER_H_

#include <map>
#include <string>

#include "base/memory/scoped_ptr.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "ui/base/models/simple_menu_model.h"

class ActionBoxMenuModel;
class Browser;

namespace extensions {
class Extension;
}

namespace ui {
class MenuModel;
}

// Controller for the action box.
//
// This should have the same lifetime as the action box itself -- that is, more
// or less the lifetime of the tab -- unlike ActionBoxMenuModel which is scoped
// to the menu being open.
class ActionBoxButtonController : public ui::SimpleMenuModel::Delegate,
                                  public content::NotificationObserver {
 public:
  class Delegate {
   public:
    // Shows the menu with the given |menu_model|.
    virtual void ShowMenu(scoped_ptr<ActionBoxMenuModel> menu_model);

   protected:
    virtual ~Delegate() {}
  };

  ActionBoxButtonController(Browser* browser, Delegate* delegate);
  virtual ~ActionBoxButtonController();

  // Notifies this that the action box button has been clicked.
  // Methods on the Delegate may be called re-entrantly.
  void OnButtonClicked();

  // Overridden from ui::SimpleMenuModel::Delegate:
  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
  virtual bool GetAcceleratorForCommandId(
      int command_id,
      ui::Accelerator* accelerator) OVERRIDE;
  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;

 private:
  // Gets the command ID for an extension, creating a new one if necessary.
  int GetCommandIdForExtension(const extensions::Extension& extension);

  // Returns the next command ID to be used.
  int GetNextCommandId();

  // content::NotificationObserver implementation.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  Browser* browser_;

  Delegate* delegate_;

  typedef std::map<int, std::string> ExtensionIdCommandMap;
  ExtensionIdCommandMap extension_command_ids_;

  // The command ID to assign to the next dynamic entry that needs one.
  int next_command_id_;

  content::NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(ActionBoxButtonController);
};

#endif  // CHROME_BROWSER_UI_TOOLBAR_ACTION_BOX_BUTTON_CONTROLLER_H_