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#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_
6#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_
7#pragma once
8
9#include <vector>
10
11#include "base/basictypes.h"
12#include "chrome/browser/bookmarks/base_bookmark_model_observer.h"
13#include "ui/base/models/simple_menu_model.h"
14#include "ui/gfx/native_widget_types.h"
15
16class PageNavigator;
17class Profile;
18
19// An interface implemented by an object that performs actions on the actual
20// menu for the controller.
21class BookmarkContextMenuControllerDelegate {
22 public:
23  virtual ~BookmarkContextMenuControllerDelegate() {}
24
25  // Closes the bookmark context menu.
26  virtual void CloseMenu() = 0;
27
28  // Sent before any command from the menu is executed.
29  virtual void WillExecuteCommand() {}
30
31  // Sent after any command from the menu is executed.
32  virtual void DidExecuteCommand() {}
33};
34
35// BookmarkContextMenuController creates and manages state for the context menu
36// shown for any bookmark item.
37class BookmarkContextMenuController : public BaseBookmarkModelObserver,
38                                      public ui::SimpleMenuModel::Delegate {
39 public:
40  // Creates the bookmark context menu.
41  // |profile| is used for opening urls as well as enabling 'open incognito'.
42  // |browser| is used to determine the PageNavigator and may be null.
43  // |navigator| is used if |browser| is null, and is provided for testing.
44  // |parent| is the parent for newly created nodes if |selection| is empty.
45  // |selection| is the nodes the context menu operates on and may be empty.
46  BookmarkContextMenuController(
47      gfx::NativeWindow parent_window,
48      BookmarkContextMenuControllerDelegate* delegate,
49      Profile* profile,
50      PageNavigator* navigator,
51      const BookmarkNode* parent,
52      const std::vector<const BookmarkNode*>& selection);
53  virtual ~BookmarkContextMenuController();
54
55  void BuildMenu();
56
57  ui::SimpleMenuModel* menu_model() const { return menu_model_.get(); }
58
59  // ui::SimpleMenuModel::Delegate implementation:
60  virtual bool IsCommandIdChecked(int command_id) const;
61  virtual bool IsCommandIdEnabled(int command_id) const;
62  virtual bool GetAcceleratorForCommandId(int command_id,
63                                          ui::Accelerator* accelerator);
64  virtual void ExecuteCommand(int command_id);
65
66  // Accessors:
67  Profile* profile() const { return profile_; }
68  PageNavigator* navigator() const { return navigator_; }
69
70 private:
71  // Adds a IDC_* style command to the menu with a localized string.
72  void AddItem(int id, int localization_id);
73  // Adds a separator to the menu.
74  void AddSeparator();
75  // Adds a checkable item to the menu.
76  void AddCheckboxItem(int id, int localization_id);
77
78  // Overridden from BaseBookmarkModelObserver:
79  // Any change to the model results in closing the menu.
80  virtual void BookmarkModelChanged();
81
82  // Returns true if selection_ has at least one bookmark of type url.
83  bool HasURLs() const;
84
85  gfx::NativeWindow parent_window_;
86  BookmarkContextMenuControllerDelegate* delegate_;
87  Profile* profile_;
88  PageNavigator* navigator_;
89  const BookmarkNode* parent_;
90  std::vector<const BookmarkNode*> selection_;
91  BookmarkModel* model_;
92  scoped_ptr<ui::SimpleMenuModel> menu_model_;
93
94  DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuController);
95};
96
97#endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_
98