BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_context_menu_controller.h
blob874bdb716eb4976d8dcb1dddb2ac904ec6e916bb
1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
14 #include "ui/base/models/simple_menu_model.h"
15 #include "ui/gfx/native_widget_types.h"
17 class Browser;
18 class Profile;
20 namespace content {
21 class PageNavigator;
24 // An interface implemented by an object that performs actions on the actual
25 // menu for the controller.
26 class BookmarkContextMenuControllerDelegate {
27 public:
28 virtual ~BookmarkContextMenuControllerDelegate() {}
30 // Closes the bookmark context menu.
31 virtual void CloseMenu() = 0;
33 // Sent before any command from the menu is executed.
34 virtual void WillExecuteCommand(
35 int command_id,
36 const std::vector<const bookmarks::BookmarkNode*>& bookmarks) {}
38 // Sent after any command from the menu is executed.
39 virtual void DidExecuteCommand(int command_id) {}
42 // BookmarkContextMenuController creates and manages state for the context menu
43 // shown for any bookmark item.
44 class BookmarkContextMenuController
45 : public bookmarks::BaseBookmarkModelObserver,
46 public ui::SimpleMenuModel::Delegate {
47 public:
48 // Creates the bookmark context menu.
49 // |browser| is used to open the bookmark manager and is NULL in tests.
50 // |profile| is used for opening urls as well as enabling 'open incognito'.
51 // |navigator| is used if |browser| is null, and is provided for testing.
52 // |parent| is the parent for newly created nodes if |selection| is empty.
53 // |selection| is the nodes the context menu operates on and may be empty.
54 BookmarkContextMenuController(
55 gfx::NativeWindow parent_window,
56 BookmarkContextMenuControllerDelegate* delegate,
57 Browser* browser,
58 Profile* profile,
59 content::PageNavigator* navigator,
60 const bookmarks::BookmarkNode* parent,
61 const std::vector<const bookmarks::BookmarkNode*>& selection);
62 ~BookmarkContextMenuController() override;
64 ui::SimpleMenuModel* menu_model() { return menu_model_.get(); }
66 // ui::SimpleMenuModel::Delegate implementation:
67 bool IsCommandIdChecked(int command_id) const override;
68 bool IsCommandIdEnabled(int command_id) const override;
69 bool IsCommandIdVisible(int command_id) const override;
70 bool GetAcceleratorForCommandId(int command_id,
71 ui::Accelerator* accelerator) override;
72 void ExecuteCommand(int command_id, int event_flags) override;
73 bool IsItemForCommandIdDynamic(int command_id) const override;
74 base::string16 GetLabelForCommandId(int command_id) const override;
76 void set_navigator(content::PageNavigator* navigator) {
77 navigator_ = navigator;
80 private:
81 void BuildMenu();
83 // Adds a IDC_* style command to the menu with a localized string.
84 void AddItem(int id, int localization_id);
85 // Adds a separator to the menu.
86 void AddSeparator();
87 // Adds a checkable item to the menu.
88 void AddCheckboxItem(int id, int localization_id);
90 // Overridden from bookmarks::BaseBookmarkModelObserver:
91 // Any change to the model results in closing the menu.
92 void BookmarkModelChanged() override;
94 gfx::NativeWindow parent_window_;
95 BookmarkContextMenuControllerDelegate* delegate_;
96 Browser* browser_;
97 Profile* profile_;
98 content::PageNavigator* navigator_;
99 const bookmarks::BookmarkNode* parent_;
100 std::vector<const bookmarks::BookmarkNode*> selection_;
101 bookmarks::BookmarkModel* model_;
102 scoped_ptr<ui::SimpleMenuModel> menu_model_;
103 // Used to detect deletion of |this| executing a command.
104 base::WeakPtrFactory<BookmarkContextMenuController> weak_factory_;
106 DISALLOW_COPY_AND_ASSIGN(BookmarkContextMenuController);
109 #endif // CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_CONTEXT_MENU_CONTROLLER_H_